ስትሪም
stream ናብ true ኣዘጋጅ እሞ እቲ ምላሽ እቲ ሞዴል ክጽሕፎ ከሎ ከም server-sent events ይመጽእ፦ መጀመርታ reasoning፣ ደሓር እቲ መልሲ፣ ደሓር ናይ መወዳእታ chunk ምስ ምኽንያት መወዳእታን usage ን። ሓደ ሰብ ንጽሑፍ ክጽበ ከሎ፣ ከምኡውን ን ነዋሕቲ ምላሻት ተጠቐሞ።
POST https://api.shannon-ai.com/v1/chat/completions
እቶም OpenAI SDKs chunks ሓደ ብሓደ የሕልፉልካ። ነፍሲ ወከፍ chunk ብዝሕዞ ኣንብቦ፦ ቁራጽ reasoning፣ ቁራጽ መልሲ፣ ጥሪ tool፣ ወይ መወዳእታ ናይ ምላሽ።
from openai import OpenAI, APIError
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.shannon-ai.com/v1",
)
stream = client.chat.completions.create(
model="shannon-3",
messages=[{"role": "user", "content": "Explain server-sent events in three sentences."}],
stream=True,
)
answer, reasoning, tool_calls = [], [], []
finish_reason = usage = None
try:
for chunk in stream:
if chunk.usage:
usage = chunk.usage
if not chunk.choices:
continue
choice = chunk.choices[0]
delta = choice.delta
thought = getattr(delta, "reasoning_content", None)
if thought:
reasoning.append(thought)
if delta.content:
answer.append(delta.content)
print(delta.content, end="", flush=True)
for call in delta.tool_calls or []:
tool_calls.append((call.id, call.function.name, call.function.arguments))
if choice.finish_reason:
finish_reason = choice.finish_reason
except APIError as error:
# A failure after the stream started arrives as an error chunk.
print(f"\nstream failed: {error}")
print(f"\n{finish_reason} {usage}") import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.shannon-ai.com/v1",
});
const stream = await client.chat.completions.create({
model: "shannon-3",
messages: [{ role: "user", content: "Explain server-sent events in three sentences." }],
stream: true,
});
let answer = "";
let reasoning = "";
const toolCalls = [];
let finishReason = null;
let usage = null;
try {
for await (const chunk of stream) {
if (chunk.usage) usage = chunk.usage;
const choice = chunk.choices?.[0];
if (!choice) continue;
const delta = choice.delta;
if (delta.reasoning_content) reasoning += delta.reasoning_content;
if (delta.content) {
answer += delta.content;
process.stdout.write(delta.content);
}
for (const call of delta.tool_calls ?? []) {
toolCalls.push({ id: call.id, name: call.function.name, arguments: call.function.arguments });
}
if (choice.finish_reason) finishReason = choice.finish_reason;
}
} catch (error) {
// A failure after the stream started arrives as an error chunk.
console.error("\nstream failed:", error.message);
}
console.log("\n", finishReason, usage); curl -N https://api.shannon-ai.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "shannon-3",
"messages": [{"role": "user", "content": "Explain server-sent events in three sentences."}],
"stream": true
}' stream ከመይ ይተሓጽር
- እቲ ምላሽ status
200ከምኡውን headerscontent-type: text/event-streamከምኡውንcache-control: no-cacheኣለዎ። - ነፍሲ ወከፍ event ሓንቲ መስመር እያ ብ
data:ትጅምርን ባዶ መስመር ትስዕባን። እቲ ድሕሪdata:ዘሎ ጽሑፍ ሓደ JSON chunk እዩ። መስመራትevent:ወይid:የለን። - መስመር ብነጥቢ ክልተ ዝጅምር፣ ከም
: ping፣ ርእይቶ እዩ ርክብ ክፉት ዝሕዝ። ሕለፎ። - እቲ stream ብ
data: [DONE]ይወድእ። ብዘይ ንሱ ዝቋረጽ stream ዘይተወደአ እዩ።
Chunks ብትእዛዝ
ነፍሲ ወከፍ chunk object chat.completion.chunk እዩ፦
{
"id": "chatcmpl-5f0c1e7a9b3d4c62a8e1f07d2b46c9a3",
"object": "chat.completion.chunk",
"created": 1791625200,
"model": "shannon-3",
"choices": [
{
"index": 0,
"delta": {
"content": "Server-sent"
},
"finish_reason": null
}
]
} ኩሎም chunks ናይ ሓደ stream ተመሳሳሊ id፣ created ከምኡውን model ይሕዙ። choices ኩሉ ግዜ ሓደ ኣተዋብ ይሕዝ። delta ናቱ እቲ ሓድሽ ይሕዝ፣ finish_reason ድማ ክሳብ እቲ ናይ መወዳእታ chunk null እዩ።
| Key ኣብ delta | ዓይነት | መግለጺ | ዝልእኾ |
|---|---|---|---|
role | string | ኩሉ ግዜ assistant። ሓንሳብ፣ ኣብ መጀመርታ ናይ stream ይመጽእ። | ዝተሃንደሱ open-weight ሞዴላት፣ shannon-2-lite፣ shannon-2-pro |
reasoning_content | string | ቁራጽ ናይ reasoning ናይ ሞዴል። ቁራጻት ብትእዛዝ ሓዋህዎም። | ዝተሃንደሱ open-weight ሞዴላት፣ ስድራ Shannon 3፣ shannon-1.6-pro፣ shannon-coder-1 |
content | string | ቁራጽ ናይ መልሲ። ቁራጻት ብትእዛዝ ሓዋህዎም። | ኩሎም ሞዴላት |
tool_calls | array | ሓደ ምሉእ ጥሪ tool። Tool call deltas ርአ። | ኩሎም ሞዴላት |
Reasoning ቀዳማይ፣ ደሓር መልሲ፣ ደሓር ዝኾነ ጥሪታት tool። ሓደ chunk ብ keys ኣብ delta ኡ ኣንብቦ፣ ብቦታኡ ኣብ stream ኣይኮነን፦ delta role ምስ ቀዳማይ ቁራጽ ጽሑፍ ክሕዝ ይኽእል፣ እቲ ናይ መወዳእታ ድማ ባዶ እዩ።
ሓደ ምሉእ stream ከም ዝጓዓዝ። መስመራት ንኽሕጸራ፣ id፣ object፣ created ከምኡውን model ካብ ነፍሲ ወከፍ chunk ኣብዚ ተሓሊፎም ኣለዉ።
data: {"choices":[{"index":0,"delta":{"reasoning_content":"Three sentences:"},"finish_reason":null}]}
data: {"choices":[{"index":0,"delta":{"reasoning_content":" what it is, how it is framed, why it is used."},"finish_reason":null}]}
: ping
data: {"choices":[{"index":0,"delta":{"content":"Server-sent"},"finish_reason":null}]}
data: {"choices":[{"index":0,"delta":{"content":" events let a server push text to a client over one HTTP reply."},"finish_reason":null}]}
data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1190,"completion_tokens":84,"total_tokens":1274}}
data: [DONE] Deltas ናይ reasoning
ዝሓስብ ሞዴል reasoning ናቱ ኣብ delta.reasoning_content ቅድሚ መልሲ ይልእኽ። እቶም ክልተ ጽሑፋት ኣፍሊዮም ሓዞም፦ reasoning ከም ዝተፈልየ ክፋል ዝዕጸው ኣርእዮ፣ ወይ ዕዘቦ።
እቶም ዝተሃንደሱ open-weight ሞዴላት reasoning_effort off ምስ ዝኸውን፣ ከምኡውን እቲ ጠለብ response_format ምስ ዝህልዎ፣ reasoning ኣይልእኩን።
shannon-2-lite ከምኡውን shannon-2-pro ቅድሚ መልሲ እውን ይሓስቡ። ኣብዚ endpoint ክሓስቡ ከለዉ መስመራት ርእይቶ : thinking ይልእኩ፣ ደሓር መልሲ።
reasoning_content ኣብ typed models ናይ OpenAI SDKs የለን። ኣብ Python ብ getattr(delta, "reasoning_content", None) ኣንብቦ፤ ኣብ JavaScript delta.reasoning_content ከም ዘሎ ይሰርሕ።
Deltas ናይ ጥሪ tool
ጥሪ tool ምሉእ ይመጽእ፦ ሓደ chunk ን ነፍሲ ወከፍ ጥሪ፣ delta ናቱ እቲ ጥሪ ምስ ምሉእ string arguments ይሕዝ። index ጥሪታት ናይ ምላሽ ካብ 0 ይቁጽር። ቁራጽ ብቁራጽ arguments ዝሓዋውሕ ኮድ ብዘይ ለውጢ ይሰርሕ፣ ምኽንያቱ ሓደ ቁራጽ ይሓዋውሕ።
{
"tool_calls": [
{
"index": 0,
"id": "call_3d9a7c1e5b2f4a60c8e1d7f09b24a6c5",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"Paris\"}"
}
}
]
} ድሕሪ እቲ ጥሪታት፣ እቲ ናይ መወዳእታ chunk finish_reason tool_calls ኣለዎ።
እቲ ጠለብ tools ምስ ዝህልዎ፣ shannon-1.6-lite፣ shannon-1.6-pro ከምኡውን shannon-coder-1 ጽሑፍ ናይ መልሲ ከም ሓደ delta ኣብ መወዳእታ ናይ ምላሽ ይልእኩ።
እቲ ናይ መወዳእታ chunk
እቲ ናይ መወዳእታ chunk ቅድሚ data: [DONE] ባዶ delta፣ finish_reason ከምኡውን usage ናይ እቲ ጠለብ ኣለዎ፣ ኩሉ ኣብ ተመሳሳሊ chunk። ነፍሲ ወከፍ stream ኣለዎ፤ stream_options ኣየድልየካን።
{
"id": "chatcmpl-5f0c1e7a9b3d4c62a8e1f07d2b46c9a3",
"object": "chat.completion.chunk",
"created": 1791625200,
"model": "shannon-3",
"choices": [
{
"index": 0,
"delta": {},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 1190,
"completion_tokens": 84,
"total_tokens": 1274
}
} ኣብ ዝተሃንደሱ open-weight ሞዴላት usage prompt_tokens_details.cached_tokens ከምኡውን completion_tokens_details.reasoning_tokens እውን ይሕዝ።
ድሕሪ ጠለብ web_search: true ዘለዎን ድለያኡ ገለ ነገር ዝረኸበን፣ እቲ ናይ መወዳእታ chunk sources እውን ይሕዝ፣ ቅድሚኡ ዘሎ ሓደ chunk ድማ ምልክታት ጥቕሲ ከም delta.annotations ይሕዝ። የድር ፍለጋ
| finish_reason | መግለጺ | ዝልእኾ |
|---|---|---|
stop | እቲ ሞዴል መልሱ ወዲኡ፣ ወይ string stop ተራእየ። | ኩሎም ሞዴላት |
tool_calls | እቲ ምላሽ ሓደ ወይ ልዕሊ ጥሪታት tool ይሕዝ። | ኩሎም ሞዴላት |
length | እቲ ምላሽ ኣብ ወሰን output ተቐሪጹ። | ስድራ Shannon 3፣ shannon-1.6-lite፣ shannon-1.6-pro፣ shannon-coder-1 |
እቶም field ታት ናይ usage ምስ endpoint ተገሊጾም ኣለዉ። Chat Completions
መስመራት Keep-alive
እቲ ሞዴል ክሰርሕ ከሎ ግን ገና ዘለኣኾ ዘይብሉ እዋን፣ እቲ stream መስመራት ርእይቶ ይሕዝ። ዳታ የብሎምን። SSE parser ባዕሉ የሕልፎም፤ ጥሬ መስመራት ዝንብብ ኮድ ነፍሲ ወከፍ መስመር ብነጥቢ ክልተ ዝጅምር ክሓልፍ ኣለዎ።
| መስመር | ዝልእኾ | መዓስ |
|---|---|---|
: ping | ስድራ Shannon 3፣ shannon-1.6-lite፣ shannon-1.6-pro፣ shannon-coder-1 | ኣብ ነፍሲ ወከፍ 15 ሰከንድ። |
: keepalive | ዝተሃንደሱ open-weight ሞዴላት | ኣብ ነፍሲ ወከፍ 15 ሰከንድ እቲ ሞዴል ክሰርሕ ከሎ። |
: thinking | shannon-2-lite, shannon-2-pro | እቲ ሞዴል ክሓስብ ከሎ። |
ጌጋታት ውሽጢ stream
stream ምስ ጀመረ status ናቱ 200 እዩ፣ ስለዚ ውድቀት ኣብ ውሽጢ stream ይመጽእ፦ chunk ምስ ኣባል error ኣብ ቦታ choices።
data: {"error":{"message":"The model backend failed to answer. Please retry.","type":"api_error","code":null,"param":null}}
data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1190,"completion_tokens":12,"total_tokens":1202}}
data: [DONE] ድሕሪ chunk ናይ ጌጋ፣ እቲ ናይ መወዳእታ chunk ምስ finish_reason ከምኡውን usage፣ ከምኡውን data: [DONE] ይስዕቡ። ነፍሲ ወከፍ chunk ን error ፈትሽ። ብዘይ እዚ መርመራ ኣብ ማእከል ዝወደቐ ምላሽ ከም ዝወደአ ይመስል።
| ዓይነት | Code | መልእኽቲ | መዓስ |
|---|---|---|---|
api_error | The model backend failed to answer. Please retry. | እቲ ሞዴል ወዲቑ፣ ወይ ዝኾነ ኣይጸሓፈን። እቲ ጠለብ ደጊምካ ልኣኽ። | |
rate_limit_error | Shannon routes are temporarily busy. Please retry. | እቲ ሞዴል ኣብዚ እዋን ተጸሚዱ ኣሎ። ውሑዳት ሰከንድታት ተጸበ እሞ እቲ ጠለብ ደጊምካ ልኣኽ። | |
invalid_request_error | context_length_exceeded | እቲ ዕላል ካብ context window ናይ ሞዴል ይነውሕ። ቅድሚ ደጊምካ ምልኣኽ ኣሕጽሮ። ጽሑፍ ናይ መልእኽቲ ይፈላለ። |
እቶም ዝተሃንደሱ open-weight ሞዴላት ኣባል error ምስ type ከምኡውን message ጥራይ ይልእኩ።
እቶም OpenAI SDKs ናይ Python ከምኡውን JavaScript ንጌጋ chunk ከም API ጌጋ ይልዕልዎ ክሳብ ትድግም፣ ስለዚ እቲ loop ኣብ ልሙድ ኣተሓሕዛ ጌጋታትካ ጠቕልሎ።
Status codes፣ body ናይ ጌጋ ከምኡውን እንታይ ደጊም ክፍተን ናቱ ገጽ ኣለዎ። ሓጋይ ኣስተዳደር
ርክብ ምዕጻው
ርክብ ምዕጻው ምቕባል ጥራይ የቋርጽ እምበር እቲ ጠለብ ኣይኮነን። እቲ ሞዴል ነቲ ምላሽ ክሳብ መወዳእታኡ ይጽሕፎ ከምኡውን እቲ ጠለብ ከም ዘንበብካዮ ኩሉ ይኽፈል። ዝውሑድ ንኽትኸፍል ዝውሑድ ሕተት፦ ኣብ ሞዴል እቲ ዝትግብሮ ንትሕቲ max_tokens ኣዘጋጅ።
stream ኣብ መገዲ ናባኻ ቀልጢፉ ክውድእ ይኽእል እውን፣ ንኣብነት ብምቁራጽ መርበብ ወይ server ክዘምን ከሎ። ሽዑ ብዘይ ናይ መወዳእታ chunk ከምኡውን ብዘይ data: [DONE] ይቋረጽ። እቲ ምላሽ ከም ዘይተወደአ ርአዮ ከምኡውን እቲ ጠለብ ደጊምካ ልኣኽ።
stream ብዘይ SDK ምንባብ
ቅኑዕ ኣንባቢ ሓሙሽተ ነገራት ይገብር፦ ቅድሚ stream ዝመጹ ጌጋታት ናብ ኣተሓሕዛ ጌጋታቱ ይሰድድ፣ እቲ body ናብ መስመራት ይመቕል፣ መስመራት ርእይቶ ይሕልፍ፣ ኣብ data: [DONE] የቋርጽ፣ ከምኡውን ነፍሲ ወከፍ chunk ን error ይፈትሽ።
import json
import requests
response = requests.post(
"https://api.shannon-ai.com/v1/chat/completions",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"model": "shannon-3",
"messages": [{"role": "user", "content": "Explain server-sent events in three sentences."}],
"stream": True,
},
stream=True,
)
# 1. Errors before the stream are plain JSON with their own status.
if response.status_code != 200:
raise RuntimeError(f"{response.status_code}: {response.text}")
answer, done = [], False
for raw in response.iter_lines(): # 2. one line at a time
line = raw.decode("utf-8")
if not line.startswith("data: "): # 3. empty lines and ": ping" comments
continue
data = line[len("data: "):]
if data == "[DONE]": # 4. the end of the stream
done = True
break
chunk = json.loads(data)
if "error" in chunk: # 5. a failure inside the stream
raise RuntimeError(chunk["error"]["message"])
delta = chunk["choices"][0]["delta"]
if delta.get("content"):
answer.append(delta["content"])
if chunk.get("usage"):
print(chunk["choices"][0]["finish_reason"], chunk["usage"])
if not done:
raise RuntimeError("the stream ended before [DONE]")
print("".join(answer)) const response = await fetch("https://api.shannon-ai.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "shannon-3",
messages: [{ role: "user", content: "Explain server-sent events in three sentences." }],
stream: true,
}),
});
// 1. Errors before the stream are plain JSON with their own status.
if (!response.ok) throw new Error(`${response.status}: ${await response.text()}`);
const decoder = new TextDecoder();
let buffer = "";
let answer = "";
let done = false;
read: for await (const bytes of response.body) {
buffer += decoder.decode(bytes, { stream: true });
const lines = buffer.split("\n"); // 2. one line at a time
buffer = lines.pop(); // keep the unfinished line
for (const line of lines) {
if (!line.startsWith("data: ")) continue; // 3. empty lines and ": ping" comments
const data = line.slice(6);
if (data === "[DONE]") { // 4. the end of the stream
done = true;
break read;
}
const chunk = JSON.parse(data);
if (chunk.error) throw new Error(chunk.error.message); // 5. a failure inside the stream
const choice = chunk.choices[0];
if (choice.delta.content) answer += choice.delta.content;
if (chunk.usage) console.log(choice.finish_reason, chunk.usage);
}
}
if (!done) throw new Error("the stream ended before [DONE]");
console.log(answer); እቲ ኣብነት Python ጥቅሊ requests ይጥቀም። እቲ ኣብነት JavaScript ኣብ Node.js 18 ወይ ዝሓድሽ ይሰርሕ።
Streams ናይ ካልኦት ቅርጽታት
እተን ኣብ Responses ከምኡውን Messages endpoints እውን ይውሕዛ። Frames ናተን መስመር event: ምስ ስም event ከምኡውን መስመር data: ምስ JSON ናቱ ይሕዛ። ነፍሲ ወከፍ ገጽ events ናታ ብትእዛዝ ትዝርዝር።
| ኣብ stream | Chat Completions | Responses | Messages |
|---|---|---|---|
| Frames | መስመራት data: | መስመራት event: ከምኡውን data: | መስመራት event: ከምኡውን data: |
| Reasoning ኣብ ይመጽእ | delta.reasoning_content | response.reasoning_summary_text.delta | thinking_delta |
| ጽሑፍ ናይ መልሲ ኣብ ይመጽእ | delta.content | response.output_text.delta | text_delta |
| Usage ኣብ ይመጽእ | እቲ ናይ መወዳእታ chunk | response.completed | message_delta |
| እቲ stream ይወድእ ብ | እቲ ናይ መወዳእታ chunk፣ ደሓር data: [DONE] | response.completed | message_stop |