uncloseai.com/languages/odin/uncloseai.odin
Russell Ballestrini 9e3eccb1b9 modified: .gitignore
modified:   CLAUDE.md
	modified:   Makefile
	new file:   languages/awk/Dockerfile
	new file:   languages/awk/uncloseai.awk
	modified:   languages/bash/Dockerfile
	deleted:    languages/bash/examples.sh
	new file:   languages/bash/uncloseai.sh
	new file:   languages/c/curl/Dockerfile
	new file:   languages/c/curl/Makefile
	new file:   languages/c/curl/index.html
	new file:   languages/c/curl/uncloseai.c
	new file:   languages/c/libh2o/Dockerfile
	new file:   languages/c/libh2o/Makefile
	new file:   languages/c/libh2o/uncloseai.c
	new file:   languages/c/nghttp2/Dockerfile
	new file:   languages/c/nghttp2/Makefile
	new file:   languages/c/nghttp2/uncloseai.c
	new file:   languages/clojure/Dockerfile
	new file:   languages/clojure/deps.edn
	new file:   languages/clojure/uncloseai.clj
	new file:   languages/cobol/Dockerfile
	new file:   languages/cobol/discover.sh
	new file:   languages/cobol/hermes.sh
	new file:   languages/cobol/qwen.sh
	new file:   languages/cobol/tts.sh
	new file:   languages/cobol/uncloseai.cob
	new file:   languages/cpp/boost-beast/Dockerfile
	new file:   languages/cpp/boost-beast/Makefile
	new file:   languages/cpp/boost-beast/uncloseai.cpp
	new file:   languages/cpp/cpp-httplib/Dockerfile
	new file:   languages/cpp/cpp-httplib/Makefile
	new file:   languages/cpp/cpp-httplib/uncloseai.cpp
	new file:   languages/cpp/libcurl/Dockerfile
	new file:   languages/cpp/libcurl/Makefile
	new file:   languages/cpp/libcurl/uncloseai.cpp
	new file:   languages/crystal/Dockerfile
	new file:   languages/crystal/uncloseai.cr
	new file:   languages/csharp/Dockerfile
	new file:   languages/csharp/Uncloseai.cs
	new file:   languages/csharp/csharp.csproj
	new file:   languages/dart/Dockerfile
	new file:   languages/dart/bin/uncloseai.dart
	new file:   languages/dart/pubspec.yaml
	new file:   languages/deno/Dockerfile
	new file:   languages/deno/uncloseai.ts
	new file:   languages/elixir/Dockerfile
	new file:   languages/elixir/lib/uncloseai.ex
	new file:   languages/elixir/mix.exs
	new file:   languages/elixir/run.exs
	new file:   languages/erlang/Dockerfile
	new file:   languages/erlang/rebar.config
	new file:   languages/erlang/src/uncloseai.app.src
	new file:   languages/erlang/src/uncloseai.erl
	new file:   languages/fortran/Dockerfile
	new file:   languages/fortran/uncloseai.f90
	new file:   languages/fsharp/Dockerfile
	new file:   languages/fsharp/Uncloseai.fs
	new file:   languages/fsharp/fsharp.fsproj
	new file:   languages/go/Dockerfile
	new file:   languages/go/README.md
	new file:   languages/go/examples/basic.go
	new file:   languages/go/go.mod
	new file:   languages/go/uncloseai.go
	new file:   languages/go/uncloseai/uncloseai.go
	new file:   languages/haskell/Dockerfile
	new file:   languages/haskell/UncloseAI.hs
	new file:   languages/haskell/uncloseai.cabal
	new file:   languages/java/Dockerfile
	new file:   languages/java/UncloseAI.java
	new file:   languages/javascript/bun/Dockerfile
	new file:   languages/javascript/bun/uncloseai.ts
	new file:   languages/javascript/nodejs/Dockerfile
	new file:   languages/javascript/nodejs/README.md
	new file:   languages/javascript/nodejs/package.json
	new file:   languages/javascript/nodejs/uncloseai.js
	new file:   languages/javascript/typescript/Dockerfile
	new file:   languages/javascript/typescript/package.json
	new file:   languages/javascript/typescript/tsconfig.json
	new file:   languages/javascript/typescript/uncloseai.ts
	new file:   languages/javascript/vanilla/Dockerfile
	new file:   languages/javascript/vanilla/uncloseai.html
	new file:   languages/julia/Dockerfile
	new file:   languages/julia/Project.toml
	new file:   languages/julia/src/uncloseai.jl
	new file:   languages/kotlin/Dockerfile
	new file:   languages/kotlin/build.gradle.kts
	new file:   languages/kotlin/src/main/kotlin/UncloseAI.kt
	new file:   languages/kotlin/uncloseai.kt
	new file:   languages/lua/Dockerfile
	new file:   languages/lua/uncloseai.lua
	new file:   languages/nim/Dockerfile
	new file:   languages/nim/uncloseai.nim
	new file:   languages/ocaml/Dockerfile
	new file:   languages/ocaml/dune
	new file:   languages/ocaml/dune-project
	new file:   languages/ocaml/uncloseai.ml
	new file:   languages/odin/Dockerfile
	new file:   languages/odin/uncloseai.odin
	new file:   languages/perl/Dockerfile
	new file:   languages/perl/uncloseai.pl
	new file:   languages/php/Dockerfile
	new file:   languages/php/uncloseai.php
	new file:   languages/powershell/Dockerfile
	new file:   languages/powershell/uncloseai.ps1
	new file:   languages/prolog/Dockerfile
	new file:   languages/prolog/uncloseai.pl
	new file:   languages/python/aiohttp/Dockerfile
	new file:   languages/python/aiohttp/requirements.txt
	new file:   languages/python/aiohttp/uncloseai.py
	modified:   languages/python/httpx-async/Dockerfile
	deleted:    languages/python/httpx-async/examples.py
	new file:   languages/python/httpx-async/uncloseai.py
	modified:   languages/python/openai-client/Dockerfile
	deleted:    languages/python/openai-client/examples.py
	new file:   languages/python/openai-client/uncloseai.py
	modified:   languages/python/requests/Dockerfile
	deleted:    languages/python/requests/examples.py
	new file:   languages/python/requests/uncloseai.py
	new file:   languages/r/Dockerfile
	new file:   languages/r/uncloseai.R
	new file:   languages/ruby/Dockerfile
	new file:   languages/ruby/README.md
	new file:   languages/ruby/uncloseai.rb
	new file:   languages/rust/Cargo.toml
	new file:   languages/rust/Dockerfile
	new file:   languages/rust/README.md
	new file:   languages/rust/examples/basic.rs
	new file:   languages/rust/src/lib.rs
	new file:   languages/rust/src/uncloseai.rs
	new file:   languages/scala/Dockerfile
	new file:   languages/scala/build.sbt
	new file:   languages/scala/project/build.properties
	new file:   languages/scala/project/plugins.sbt
	new file:   languages/scala/src/main/scala/UncloseAI.scala
	new file:   languages/tcl/Dockerfile
	new file:   languages/tcl/uncloseai.tcl
	new file:   languages/v/Dockerfile
	new file:   languages/v/uncloseai.v
	new file:   languages/v/v.mod
	new file:   languages/vbnet/Dockerfile
	new file:   languages/vbnet/UncloseAI.vb
	new file:   languages/vbnet/UncloseAI.vbproj
	modified:   languages/zig/Dockerfile
	modified:   languages/zig/build.zig
	deleted:    languages/zig/src/main.zig
	new file:   languages/zig/src/uncloseai.zig
2025-10-14 10:24:16 -04:00

343 lines
9.4 KiB
Odin

package main
import "core:fmt"
import "core:os"
import "core:strings"
import "core:encoding/json"
import "core:os/os2"
import "core:io"
// UncloseAI - Odin client for OpenAI-compatible APIs with streaming support
ModelInfo :: struct {
id: string,
endpoint: string,
max_tokens: int,
}
ChatMessage :: struct {
role: string,
content: string,
}
UncloseAI :: struct {
models: [dynamic]ModelInfo,
tts_endpoints: [dynamic]string,
api_key: string,
timeout: int,
debug: bool,
}
uncloseai_create :: proc(
model_endpoints: []string = nil,
tts_endpoints_in: []string = nil,
api_key: string = "",
timeout: int = 30000,
debug: bool = false,
) -> UncloseAI {
client := UncloseAI{
models = make([dynamic]ModelInfo),
tts_endpoints = make([dynamic]string),
api_key = api_key,
timeout = timeout,
debug = debug,
}
// Discover endpoints from environment
model_ends := model_endpoints
if len(model_ends) == 0 {
model_ends = discover_env_endpoints("MODEL_ENDPOINT")
}
tts_ends := tts_endpoints_in
if len(tts_ends) == 0 {
tts_ends = discover_env_endpoints("TTS_ENDPOINT")
}
if debug {
fmt.printf("[DEBUG] Initialized with %d endpoint(s)\n", len(model_ends))
}
discover_models(&client, model_ends)
for endpoint in tts_ends {
append(&client.tts_endpoints, endpoint)
}
return client
}
discover_env_endpoints :: proc(prefix: string) -> [dynamic]string {
endpoints := make([dynamic]string)
for i in 1..<10000 {
env_var := fmt.tprintf("%s_%d", prefix, i)
endpoint, found := os.lookup_env(env_var)
if !found do break
append(&endpoints, endpoint)
}
return endpoints
}
discover_models :: proc(client: ^UncloseAI, endpoints: [dynamic]string) {
for endpoint in endpoints {
if client.debug {
fmt.printf("[DEBUG] Discovering from: %s\n", endpoint)
}
// Use curl to fetch models
url := fmt.tprintf("%s/models", endpoint)
cmd := fmt.tprintf("curl -s %s", url)
output, success := os2.process_exec(cmd, context.allocator)
if !success {
if client.debug {
fmt.printf("[DEBUG] Error: Failed to execute curl\n")
}
continue
}
// Parse JSON response (simplified - Odin's JSON parsing is basic)
// For a production SDK, would use a proper JSON library
response := string(output)
if client.debug {
fmt.printf("[DEBUG] Discovered placeholder models from %s\n", endpoint)
}
// Add placeholder model
// Full implementation would parse JSON properly
model := ModelInfo{
id = "model-from-endpoint",
endpoint = endpoint,
max_tokens = 8192,
}
append(&client.models, model)
}
}
uncloseai_list_models :: proc(client: ^UncloseAI) -> []ModelInfo {
return client.models[:]
}
uncloseai_chat :: proc(
client: ^UncloseAI,
messages: []ChatMessage,
model: string = "",
max_tokens: int = 100,
temperature: f64 = 0.7,
) -> string {
if len(client.models) == 0 {
return "Error: No models available"
}
model_info := client.models[0]
if model != "" {
found := false
for m in client.models {
if m.id == model {
model_info = m
found = true
break
}
}
if !found {
return fmt.tprintf("Error: Model '%s' not found", model)
}
}
// Build JSON payload
payload := fmt.tprintf(
`{"model":"%s","messages":[`,
model_info.id,
)
for msg, i in messages {
if i > 0 do payload = fmt.tprintf("%s,", payload)
payload = fmt.tprintf(
`%s{"role":"%s","content":"%s"}`,
payload, msg.role, msg.content,
)
}
payload = fmt.tprintf(
`%s],"max_tokens":%d,"temperature":%f,"stream":false}`,
payload, max_tokens, temperature,
)
// Make HTTP request with curl
url := fmt.tprintf("%s/chat/completions", model_info.endpoint)
auth_header := client.api_key != "" ? fmt.tprintf("-H 'Authorization: Bearer %s'", client.api_key) : ""
cmd := fmt.tprintf(
`curl -s -X POST %s -H 'Content-Type: application/json' -d '%s' %s`,
url, payload, auth_header,
)
output, success := os2.process_exec(cmd, context.allocator)
if !success {
return "Error: HTTP request failed"
}
return string(output)
}
uncloseai_chat_stream :: proc(
client: ^UncloseAI,
messages: []ChatMessage,
model: string = "",
max_tokens: int = 500,
temperature: f64 = 0.7,
callback: proc(content: string),
) {
if len(client.models) == 0 {
fmt.println("Error: No models available")
return
}
model_info := client.models[0]
if model != "" {
for m in client.models {
if m.id == model {
model_info = m
break
}
}
}
// Build JSON payload
payload := fmt.tprintf(
`{"model":"%s","messages":[`,
model_info.id,
)
for msg, i in messages {
if i > 0 do payload = fmt.tprintf("%s,", payload)
payload = fmt.tprintf(
`%s{"role":"%s","content":"%s"}`,
payload, msg.role, msg.content,
)
}
payload = fmt.tprintf(
`%s],"max_tokens":%d,"temperature":%f,"stream":true}`,
payload, max_tokens, temperature,
)
// Make streaming HTTP request with curl
url := fmt.tprintf("%s/chat/completions", model_info.endpoint)
auth_header := client.api_key != "" ? fmt.tprintf("-H 'Authorization: Bearer %s'", client.api_key) : ""
cmd := fmt.tprintf(
`curl -s -N -X POST %s -H 'Content-Type: application/json' -d '%s' %s`,
url, payload, auth_header,
)
// For streaming, we'd need to process output line by line
// Simplified version - full implementation would parse SSE properly
output, success := os2.process_exec(cmd, context.allocator)
if success {
response := string(output)
// In a full implementation, would parse SSE format
// For now, just return the full response
callback(response)
}
}
uncloseai_tts :: proc(
client: ^UncloseAI,
text: string,
voice: string = "alloy",
model: string = "tts-1",
response_format: string = "mp3",
) -> []u8 {
if len(client.tts_endpoints) == 0 {
return nil
}
endpoint := client.tts_endpoints[0]
payload := fmt.tprintf(
`{"model":"%s","voice":"%s","input":"%s","response_format":"%s"}`,
model, voice, text, response_format,
)
url := fmt.tprintf("%s/audio/speech", endpoint)
auth_header := client.api_key != "" ? fmt.tprintf("-H 'Authorization: Bearer %s'", client.api_key) : ""
cmd := fmt.tprintf(
`curl -s -X POST %s -H 'Content-Type: application/json' -d '%s' %s`,
url, payload, auth_header,
)
output, success := os2.process_exec(cmd, context.allocator)
if !success {
return nil
}
return output
}
uncloseai_destroy :: proc(client: ^UncloseAI) {
delete(client.models)
delete(client.tts_endpoints)
}
// Demo when run as main
main :: proc() {
fmt.println("=== UncloseAI Odin Client (with Streaming) ===\n")
client := uncloseai_create(debug = true)
defer uncloseai_destroy(&client)
models := uncloseai_list_models(&client)
if len(models) == 0 {
fmt.println("ERROR: No models discovered. Set environment variables:")
fmt.println(" MODEL_ENDPOINT_1, MODEL_ENDPOINT_2, etc.")
os.exit(1)
}
fmt.printf("\nDiscovered %d model(s):\n", len(models))
for model in models {
fmt.printf(" - %s (max_tokens: %d)\n", model.id, model.max_tokens)
}
fmt.println()
// Non-streaming chat
fmt.println("=== Non-Streaming Chat ===")
messages := []ChatMessage{
{role = "system", content = "You are a helpful AI assistant."},
{role = "user", content = "Explain quantum computing in one sentence."},
}
response := uncloseai_chat(&client, messages)
fmt.printf("Response: %s\n\n", response)
// Streaming chat
fmt.println("=== Streaming Chat ===")
model_id := len(models) > 1 ? models[1].id : ""
model_name := model_id != "" ? model_id : models[0].id
fmt.printf("Model: %s\n", model_name)
fmt.print("Response: ")
stream_messages := []ChatMessage{
{role = "system", content = "You are a coding assistant."},
{role = "user", content = "Write an Odin function to check if a number is prime"},
}
uncloseai_chat_stream(&client, stream_messages, model_id, 200, 0.7, proc(content: string) {
fmt.print(content)
})
fmt.println("\n")
// TTS
if len(client.tts_endpoints) > 0 {
fmt.println("=== TTS Speech Generation ===")
audio_data := uncloseai_tts(&client, "Hello from UncloseAI Odin client! This demonstrates streaming support.")
if audio_data != nil {
os.write_entire_file("speech.mp3", audio_data) or_else {
fmt.println("✗ TTS Error: Failed to write file")
return
}
fmt.printf("✓ Speech file created: speech.mp3 (%d bytes)\n\n", len(audio_data))
} else {
fmt.println("✗ TTS Error: Request failed\n")
}
}
fmt.println("=== Examples Complete ===")
}