fix a bug of undisired stream output (#1966)
This commit is contained in:
parent
1272f3402a
commit
042f308b44
1 changed files with 44 additions and 49 deletions
|
|
@ -21,7 +21,7 @@ class SageMaker extends BaseLLM {
|
|||
static defaultOptions: Partial<LLMOptions> = {
|
||||
region: "us-west-2",
|
||||
contextLength: 200_000,
|
||||
};
|
||||
};
|
||||
|
||||
constructor(options: LLMOptions) {
|
||||
super(options);
|
||||
|
|
@ -47,10 +47,22 @@ class SageMaker extends BaseLLM {
|
|||
const command = toolkit.generateCommand([], prompt, options);
|
||||
const response = await client.send(command);
|
||||
if (response.Body) {
|
||||
for await (const value of response.Body) {
|
||||
const text = toolkit.unwrapResponseChunk(value);
|
||||
if (text) {
|
||||
yield text;
|
||||
let buffer = "";
|
||||
for await (const rawValue of response.Body) {
|
||||
const binaryChunk = rawValue.PayloadPart?.Bytes;
|
||||
let value = new TextDecoder().decode(binaryChunk);
|
||||
buffer += value;
|
||||
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;
|
||||
}
|
||||
buffer = buffer.slice(position + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -74,10 +86,22 @@ class SageMaker extends BaseLLM {
|
|||
const command = toolkit.generateCommand(messages, "", options);
|
||||
const response = await client.send(command);
|
||||
if (response.Body) {
|
||||
for await (const value of response.Body) {
|
||||
const text = toolkit.unwrapResponseChunk(value);
|
||||
if (text) {
|
||||
yield { role: "assistant", content: text };
|
||||
let buffer = "";
|
||||
for await (const rawValue of response.Body) {
|
||||
const binaryChunk = rawValue.PayloadPart?.Bytes;
|
||||
let value = new TextDecoder().decode(binaryChunk);
|
||||
buffer += value;
|
||||
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 };
|
||||
}
|
||||
buffer = buffer.slice(position + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -104,11 +128,10 @@ interface SageMakerModelToolkit {
|
|||
prompt: string,
|
||||
options: CompletionOptions,
|
||||
): InvokeEndpointWithResponseStreamCommand;
|
||||
unwrapResponseChunk(rawValue: any): string;
|
||||
}
|
||||
|
||||
class MessageAPIToolkit implements SageMakerModelToolkit {
|
||||
constructor(private sagemaker: SageMaker) {}
|
||||
constructor(private sagemaker: SageMaker) { }
|
||||
generateCommand(
|
||||
messages: ChatMessage[],
|
||||
prompt: string,
|
||||
|
|
@ -118,15 +141,15 @@ class MessageAPIToolkit implements SageMakerModelToolkit {
|
|||
if ("chat_template" in this.sagemaker.completionOptions) {
|
||||
// for some model you can apply chat_template to the model
|
||||
let prompt = jinja.compile(this.sagemaker.completionOptions.chat_template).render(
|
||||
{messages: messages, add_generation_prompt: true},
|
||||
{autoEscape: false}
|
||||
{ messages: messages, add_generation_prompt: true },
|
||||
{ autoEscape: false }
|
||||
)
|
||||
const payload = {
|
||||
inputs: prompt,
|
||||
parameters: this.sagemaker.completionOptions,
|
||||
stream: true,
|
||||
};
|
||||
|
||||
|
||||
return new InvokeEndpointWithResponseStreamCommand({
|
||||
EndpointName: options.model,
|
||||
Body: new TextEncoder().encode(JSON.stringify(payload)),
|
||||
|
|
@ -140,9 +163,13 @@ class MessageAPIToolkit implements SageMakerModelToolkit {
|
|||
max_tokens: options.maxTokens,
|
||||
temperature: options.temperature,
|
||||
top_p: options.topP,
|
||||
stream: "true",
|
||||
top_k: options.topK,
|
||||
stop: options.stop,
|
||||
frequencyPenalty: options.frequencyPenalty,
|
||||
presencePenalty: options.presencePenalty,
|
||||
stream: true,
|
||||
};
|
||||
|
||||
|
||||
return new InvokeEndpointWithResponseStreamCommand({
|
||||
EndpointName: options.model,
|
||||
Body: new TextEncoder().encode(JSON.stringify(payload)),
|
||||
|
|
@ -152,29 +179,9 @@ class MessageAPIToolkit implements SageMakerModelToolkit {
|
|||
}
|
||||
|
||||
}
|
||||
unwrapResponseChunk(rawValue: any): string {
|
||||
const binaryChunk = rawValue.PayloadPart?.Bytes;
|
||||
const textChunk = new TextDecoder().decode(binaryChunk);
|
||||
try {
|
||||
const chunk = JSON.parse(textChunk)
|
||||
if ("choices" in chunk) {
|
||||
return chunk.choices[0].delta.content;
|
||||
}
|
||||
else if ("token" in chunk) {
|
||||
return chunk.token.text;
|
||||
}
|
||||
else {
|
||||
return "";
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(textChunk);
|
||||
console.error(error);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
class CompletionAPIToolkit implements SageMakerModelToolkit {
|
||||
constructor(private sagemaker: SageMaker) {}
|
||||
constructor(private sagemaker: SageMaker) { }
|
||||
generateCommand(
|
||||
messages: ChatMessage[],
|
||||
prompt: string,
|
||||
|
|
@ -193,18 +200,6 @@ class CompletionAPIToolkit implements SageMakerModelToolkit {
|
|||
CustomAttributes: "accept_eula=false",
|
||||
});
|
||||
}
|
||||
unwrapResponseChunk(rawValue: any): string {
|
||||
const binaryChunk = rawValue.PayloadPart?.Bytes;
|
||||
const textChunk = new TextDecoder().decode(binaryChunk);
|
||||
try {
|
||||
return JSON.parse(textChunk).token.text;
|
||||
} catch (error) {
|
||||
console.error(textChunk);
|
||||
console.error(error);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default SageMaker;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue