From 8f4af3df6560f92c26e136ddd4690e7750a9a071 Mon Sep 17 00:00:00 2001 From: FuChen Date: Fri, 6 Sep 2024 12:14:33 +0800 Subject: [PATCH] handle some json error for vllm-openai api format --- core/llm/llms/SageMaker.ts | 49 ++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/core/llm/llms/SageMaker.ts b/core/llm/llms/SageMaker.ts index a59de352c..03b9a5b22 100644 --- a/core/llm/llms/SageMaker.ts +++ b/core/llm/llms/SageMaker.ts @@ -55,12 +55,24 @@ class SageMaker extends BaseLLM { let position; while ((position = buffer.indexOf("\n")) >= 0) { const line = buffer.slice(0, position); - const data = JSON.parse(line.replace(/^data:/, "")); - if ("choices" in data) { - yield data.choices[0].delta.content; - } - else if ("token" in data) { - yield data.token.text; + try { + const data = JSON.parse(line.replace(/^data:/, '')); + let text = undefined; + if ("choices" in data) { + if ("delta" in data.choices[0]){ + text = data.choices[0].delta.content; + } + else{ + text = data.choices[0].text; + } + } + else if ("token" in data) { + text = data.token.text; + } + if (text !== undefined) { + yield text; + } + } catch (e) { } buffer = buffer.slice(position + 1); } @@ -94,12 +106,24 @@ class SageMaker extends BaseLLM { let position; while ((position = buffer.indexOf("\n")) >= 0) { const line = buffer.slice(0, position); - const data = JSON.parse(line.replace(/^data:/, "")); - if ("choices" in data) { - yield { role: "assistant", content: data.choices[0].delta.content }; - } - else if ("token" in data) { - yield { role: "assistant", content: data.token.text }; + try { + const data = JSON.parse(line.replace(/^data:/, '')); + let text = undefined; + if ("choices" in data) { + if ("delta" in data.choices[0]){ + text = data.choices[0].delta.content; + } + else{ + text = data.choices[0].text; + } + } + else if ("token" in data) { + text = data.token.text; + } + if (text !== undefined) { + yield { role: "assistant", content: text }; + } + } catch (e) { } buffer = buffer.slice(position + 1); } @@ -189,6 +213,7 @@ class CompletionAPIToolkit implements SageMakerModelToolkit { ): InvokeEndpointWithResponseStreamCommand { const payload = { inputs: prompt, + prompt: prompt, parameters: this.sagemaker.completionOptions, stream: true, };