Merge pull request #2195 from yytdfc/dev

Compatible with input and output processing in OpenAI API format for BYOC vllm on SageMaker
This commit is contained in:
Nate Sesti 2024-09-06 14:08:14 -07:00 • committed by GitHub
commit f7ee8b66b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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,
};