consolidate D language into single uncloseai.d file matching other languages

This commit is contained in:
Russell Ballestrini 2025-10-15 16:56:43 -04:00
parent 6318db3d1f
commit 323294dd57
7 changed files with 178 additions and 343 deletions

View file

@ -8,19 +8,11 @@ RUN apt-get update && \
WORKDIR /app
# Copy D source files
COPY hermes_nonstreaming.d .
COPY hermes_streaming.d .
COPY qwen_nonstreaming.d .
COPY qwen_streaming.d .
COPY tts.d .
# Copy D source file
COPY uncloseai.d .
# Compile all examples
RUN dmd -of=hermes_nonstreaming hermes_nonstreaming.d && \
dmd -of=hermes_streaming hermes_streaming.d && \
dmd -of=qwen_nonstreaming qwen_nonstreaming.d && \
dmd -of=qwen_streaming qwen_streaming.d && \
dmd -of=tts tts.d
# Compile uncloseai
RUN dmd -of=uncloseai uncloseai.d
# Default command shows available examples
CMD ["sh", "-c", "echo 'Available examples:' && echo ' ./hermes_nonstreaming' && echo ' ./hermes_streaming' && echo ' ./qwen_nonstreaming' && echo ' ./qwen_streaming' && echo ' ./tts'"]
# Default command runs the examples
CMD ["./uncloseai"]

View file

@ -1,49 +0,0 @@
#!/usr/bin/env rdmd
// Hermes AI Non-Streaming Example in D
// Uses std.net.curl for HTTP requests and std.json for JSON parsing
import std.stdio;
import std.net.curl;
import std.json;
import std.conv;
void main()
{
immutable baseUrl = "https://hermes.ai.unturf.com/v1/chat/completions";
immutable model = "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic";
try
{
writeln("Requesting from Hermes AI...\n");
// Create JSON payload
JSONValue payload;
payload["model"] = model;
payload["messages"] = [
JSONValue([
"role": JSONValue("user"),
"content": JSONValue("Give a Python Fizzbuzz solution in one line of code?")
])
];
payload["temperature"] = 0.5;
payload["max_tokens"] = 150;
payload["stream"] = false;
// Make POST request
auto http = HTTP();
http.addRequestHeader("Content-Type", "application/json");
auto response = post(baseUrl, payload.toString(), http);
// Parse response
auto jsonResponse = parseJSON(response);
auto content = jsonResponse["choices"][0]["message"]["content"].str;
writeln("Response: ", content);
}
catch (Exception e)
{
writeln("Error: ", e.msg);
}
}

View file

@ -1,94 +0,0 @@
#!/usr/bin/env rdmd
// Hermes AI Streaming Example in D
// Uses std.net.curl with byChunk for streaming SSE responses
import std.stdio;
import std.net.curl;
import std.json;
import std.string;
import std.algorithm;
import std.array;
void main()
{
immutable baseUrl = "https://hermes.ai.unturf.com/v1/chat/completions";
immutable model = "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic";
try
{
writeln("Streaming from Hermes AI...\n");
// Create JSON payload
JSONValue payload;
payload.object = [
"model": model,
"messages": [
[
"role": "user",
"content": "Give a Python Fizzbuzz solution in one line of code?"
]
],
"temperature": 0.5,
"max_tokens": 150,
"stream": true
];
// Make streaming POST request
auto http = HTTP();
http.addRequestHeader("Content-Type", "application/json");
http.method = HTTP.Method.post;
http.url = baseUrl;
http.postData = payload.toString();
string buffer = "";
// Process chunks as they arrive
http.onReceive = (ubyte[] data)
{
buffer ~= cast(string)data;
// Process complete lines
while (true)
{
auto idx = buffer.indexOf("\n");
if (idx == -1) break;
auto line = buffer[0..idx].strip();
buffer = buffer[idx+1..$];
// Process SSE data lines
if (line.startsWith("data: "))
{
auto jsonData = line[6..$];
if (jsonData == "[DONE]") continue;
try
{
auto parsed = parseJSON(jsonData);
if ("choices" in parsed && parsed["choices"].array.length > 0)
{
auto delta = parsed["choices"][0]["delta"];
if ("content" in delta)
{
write(delta["content"].str);
stdout.flush();
}
}
}
catch (Exception) {}
}
}
return data.length;
};
http.perform();
writeln("\n\nDone!");
}
catch (Exception e)
{
writeln("\nError: ", e.msg);
}
}

View file

@ -1,50 +0,0 @@
#!/usr/bin/env rdmd
// Qwen 3 Coder Non-Streaming Example in D
import std.stdio;
import std.net.curl;
import std.json;
import std.conv;
void main()
{
immutable baseUrl = "https://qwen.ai.unturf.com/v1/chat/completions";
immutable model = "hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M";
try
{
writeln("Requesting from Qwen 3 Coder...\n");
// Create JSON payload
JSONValue payload;
payload.object = [
"model": model,
"messages": [
[
"role": "user",
"content": "Give a Python Fizzbuzz solution in one line of code?"
]
],
"temperature": 0.5,
"max_tokens": 150,
"stream": false
];
// Make POST request
auto http = HTTP();
http.addRequestHeader("Content-Type", "application/json");
auto response = post(baseUrl, payload.toString(), http);
// Parse response
auto jsonResponse = parseJSON(response);
auto content = jsonResponse["choices"][0]["message"]["content"].str;
writeln("Response: ", content);
}
catch (Exception e)
{
writeln("Error: ", e.msg);
}
}

View file

@ -1,93 +0,0 @@
#!/usr/bin/env rdmd
// Qwen 3 Coder Streaming Example in D
import std.stdio;
import std.net.curl;
import std.json;
import std.string;
import std.algorithm;
import std.array;
void main()
{
immutable baseUrl = "https://qwen.ai.unturf.com/v1/chat/completions";
immutable model = "hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M";
try
{
writeln("Streaming from Qwen 3 Coder...\n");
// Create JSON payload
JSONValue payload;
payload.object = [
"model": model,
"messages": [
[
"role": "user",
"content": "Give a Python Fizzbuzz solution in one line of code?"
]
],
"temperature": 0.5,
"max_tokens": 150,
"stream": true
];
// Make streaming POST request
auto http = HTTP();
http.addRequestHeader("Content-Type", "application/json");
http.method = HTTP.Method.post;
http.url = baseUrl;
http.postData = payload.toString();
string buffer = "";
// Process chunks as they arrive
http.onReceive = (ubyte[] data)
{
buffer ~= cast(string)data;
// Process complete lines
while (true)
{
auto idx = buffer.indexOf("\n");
if (idx == -1) break;
auto line = buffer[0..idx].strip();
buffer = buffer[idx+1..$];
// Process SSE data lines
if (line.startsWith("data: "))
{
auto jsonData = line[6..$];
if (jsonData == "[DONE]") continue;
try
{
auto parsed = parseJSON(jsonData);
if ("choices" in parsed && parsed["choices"].array.length > 0)
{
auto delta = parsed["choices"][0]["delta"];
if ("content" in delta)
{
write(delta["content"].str);
stdout.flush();
}
}
}
catch (Exception) {}
}
}
return data.length;
};
http.perform();
writeln("\n\nDone!");
}
catch (Exception e)
{
writeln("\nError: ", e.msg);
}
}

View file

@ -1,43 +0,0 @@
#!/usr/bin/env rdmd
// Text-to-Speech Example in D
import std.stdio;
import std.net.curl;
import std.json;
import std.file;
void main()
{
immutable baseUrl = "https://speech.ai.unturf.com/v1/audio/speech";
immutable outputFile = "speech.mp3";
try
{
writeln("Generating speech from TTS...\n");
// Create JSON payload
JSONValue payload;
payload.object = [
"model": "tts-1",
"voice": "alloy",
"speed": 0.9,
"input": "I think so therefore, Today is a wonderful day to grow something people love!"
];
// Make POST request and get binary response
auto http = HTTP();
http.addRequestHeader("Content-Type", "application/json");
auto response = post(baseUrl, payload.toString(), http);
// Write binary data to file
std.file.write(outputFile, response);
writeln("Speech saved to: ", outputFile);
}
catch (Exception e)
{
writeln("Error: ", e.msg);
}
}

172
languages/d/uncloseai.d Normal file
View file

@ -0,0 +1,172 @@
#!/usr/bin/env rdmd
// uncloseai. - D Language Client
// Multiple AI model examples with streaming and non-streaming chat, plus TTS
import std.stdio;
import std.net.curl;
import std.json;
import std.string;
import std.algorithm;
import std.array;
import std.file;
import std.process;
import std.conv;
void main()
{
writeln("=== uncloseai. D Language Client ===\n");
// Discover endpoints from environment variables
string modelEndpoint1 = environment.get("MODEL_ENDPOINT_1", "");
string modelEndpoint2 = environment.get("MODEL_ENDPOINT_2", "");
string ttsEndpoint1 = environment.get("TTS_ENDPOINT_1", "");
if (modelEndpoint1 == "" || modelEndpoint2 == "" || ttsEndpoint1 == "")
{
writeln("ERROR: No models discovered. Set environment variables:");
writeln(" MODEL_ENDPOINT_1, MODEL_ENDPOINT_2, TTS_ENDPOINT_1");
return;
}
// Discover models from endpoint 1
writeln("Discovering models from ", modelEndpoint1, "...");
auto response1 = get(modelEndpoint1 ~ "/models");
auto models1 = parseJSON(response1);
string model1Id = models1["data"][0]["id"].str;
writeln("Model 1: ", model1Id, "\n");
// Discover models from endpoint 2
writeln("Discovering models from ", modelEndpoint2, "...");
auto response2 = get(modelEndpoint2 ~ "/models");
auto models2 = parseJSON(response2);
string model2Id = models2["data"][0]["id"].str;
writeln("Model 2: ", model2Id, "\n");
// Non-streaming chat with Model 1
writeln("=== Non-Streaming Chat (Model 1) ===");
try
{
JSONValue payload1;
payload1["model"] = model1Id;
payload1["messages"] = [
JSONValue([
"role": JSONValue("user"),
"content": JSONValue("Give a Python Fizzbuzz solution in one line of code?")
])
];
payload1["temperature"] = 0.5;
payload1["max_tokens"] = 150;
payload1["stream"] = false;
auto http1 = HTTP();
http1.addRequestHeader("Content-Type", "application/json");
auto chatResponse1 = post(modelEndpoint1 ~ "/chat/completions", payload1.toString(), http1);
auto jsonResponse1 = parseJSON(chatResponse1);
auto content1 = jsonResponse1["choices"][0]["message"]["content"].str;
writeln("Response: ", content1, "\n");
}
catch (Exception e)
{
writeln("Error: ", e.msg, "\n");
}
// Streaming chat with Model 2
writeln("=== Streaming Chat (Model 2) ===");
write("Response: ");
stdout.flush();
try
{
JSONValue payload2;
payload2["model"] = model2Id;
payload2["messages"] = [
JSONValue([
"role": JSONValue("user"),
"content": JSONValue("Give a Python Fizzbuzz solution in one line of code?")
])
];
payload2["temperature"] = 0.5;
payload2["max_tokens"] = 150;
payload2["stream"] = true;
auto http2 = HTTP();
http2.addRequestHeader("Content-Type", "application/json");
http2.method = HTTP.Method.post;
http2.url = modelEndpoint2 ~ "/chat/completions";
http2.postData = payload2.toString();
string buffer = "";
// Process chunks as they arrive
http2.onReceive = (ubyte[] data)
{
buffer ~= cast(string)data;
// Process complete lines
while (true)
{
auto idx = buffer.indexOf("\n");
if (idx == -1) break;
auto line = buffer[0..idx].strip();
buffer = buffer[idx+1..$];
// Process SSE data lines
if (line.startsWith("data: "))
{
auto jsonData = line[6..$];
if (jsonData == "[DONE]") continue;
try
{
auto parsed = parseJSON(jsonData);
if ("choices" in parsed && parsed["choices"].array.length > 0)
{
auto delta = parsed["choices"][0]["delta"];
if ("content" in delta)
{
write(delta["content"].str);
stdout.flush();
}
}
}
catch (Exception) {}
}
}
return data.length;
};
http2.perform();
writeln("\n");
}
catch (Exception e)
{
writeln("\nError: ", e.msg, "\n");
}
// TTS example
writeln("=== TTS Speech Generation ===");
try
{
JSONValue ttsPayload;
ttsPayload["model"] = "tts-1";
ttsPayload["voice"] = "alloy";
ttsPayload["speed"] = 0.9;
ttsPayload["input"] = "I think so therefore, Today is a wonderful day to grow something people love!";
auto httpTts = HTTP();
httpTts.addRequestHeader("Content-Type", "application/json");
auto ttsResponse = post(ttsEndpoint1 ~ "/audio/speech", ttsPayload.toString(), httpTts);
std.file.write("speech.mp3", ttsResponse);
writeln("[OK] Speech file created: speech.mp3 (", ttsResponse.length, " bytes)\n");
}
catch (Exception e)
{
writeln("[ERROR] TTS failed: ", e.msg, "\n");
}
writeln("=== Examples Complete ===");
}