chat-ui_test / src /lib /server /endpoints /openai /openAICompletionToTextGenerationStream.ts
nsarrazin's picture
nsarrazin HF staff
Modular backends & support for openAI & AWS endpoints (#541)
9db8ced unverified
raw
history blame
890 Bytes
import type { TextGenerationStreamOutput } from "@huggingface/inference";
import type OpenAI from "openai";
import type { Stream } from "openai/streaming";
/**
* Transform a stream of OpenAI.Completions.Completion into a stream of TextGenerationStreamOutput
*/
export async function* openAICompletionToTextGenerationStream(
completionStream: Stream<OpenAI.Completions.Completion>
) {
let generatedText = "";
let tokenId = 0;
for await (const completion of completionStream) {
const { choices } = completion;
const text = choices[0]?.text ?? "";
const last = choices[0]?.finish_reason === "stop";
if (text) {
generatedText = generatedText + text;
}
const output: TextGenerationStreamOutput = {
token: {
id: tokenId++,
text,
logprob: 0,
special: false,
},
generated_text: last ? generatedText : null,
details: null,
};
yield output;
}
}