Merge pull request #512 from devchat-ai/bugfix/nvidia-parsing-under-poor-connection-#367
Improve NVIDIA Message Parsing Resilience in Poor Network Conditions
This commit is contained in:
commit
21e14b054c
@ -72,28 +72,41 @@ export async function * nvidiaStarcoderComplete(prompt: string) : AsyncGenerator
|
|||||||
const decoder = new TextDecoder("utf-8");
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
|
||||||
for await (const chunk of stream) {
|
for await (const chunk of stream) {
|
||||||
const chunkText = decoder.decode(chunk).trim();
|
const chunkDataText = decoder.decode(chunk).trim();
|
||||||
// data: {"id":"5d3376e0-2abc-4230-b796-c6fc9ae91cd4","choices":[{"index":0,"delta":"-","finish_reason":null}]}\n\n
|
// split chunkText by "data: ", for example:
|
||||||
if (!chunkText.startsWith("data:")) {
|
// data: 123 data: 456 will split to ["", "data: 123 ", "data: 456"]
|
||||||
// log unexpected data
|
const chunkTexts = chunkDataText.split("data: ");
|
||||||
logger.channel()?.info("Unexpected data: " + chunkText);
|
for (const chunkTextSplit of chunkTexts) {
|
||||||
return;
|
if (chunkTextSplit.trim().length === 0) {
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const chunkText = "data: " + chunkTextSplit.trim();
|
||||||
|
|
||||||
|
// logger.channel()?.info("receve chunk:", chunkText);
|
||||||
|
// data: {"id": "cmpl-1713846153", "created": 1713846160.292709, "object": "completion.chunk", "model": "ollama/starcoder2:7b", "choices": [{"index": 0, "finish_reason": "stop", "text": "\n});"}]}
|
||||||
|
// data: {"id": "cmpl-1713846153", "created": 1713846160.366049, "object": "completion.chunk", "model": "ollama/starcoder2:7b", "choices": [{"index": 0, "finish_reason": "stop", "text": ""}], "usage": {"prompt_tokens": 413, "completion_tokens": 16}}
|
||||||
|
if (!chunkText.startsWith("data:")) {
|
||||||
|
// log unexpected data
|
||||||
|
logger.channel()?.info("Unexpected data: " + chunkText);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const jsonData = chunkText.substring(5).trim();
|
const jsonData = chunkText.substring(5).trim();
|
||||||
if (jsonData === "[DONE]") {
|
if (jsonData === "[DONE]") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(chunkText.substring(5).trim());
|
const data = JSON.parse(chunkText.substring(5).trim());
|
||||||
yield {
|
yield {
|
||||||
text: data.choices[0].delta,
|
text: data.choices[0].delta,
|
||||||
id: data.id
|
id: data.id
|
||||||
};
|
};
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logger.channel()?.info("receve:", chunkText);
|
logger.channel()?.info("receve:", chunkText);
|
||||||
logger.channel()?.error("JSON Parsing Error:", e.message);
|
logger.channel()?.error("JSON Parsing Error:", e.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user