Download src/lib/server/endpoints/openai/openAICompletionToTextGenerationStream.ts from WOosts/chat-ui: direct link, hf CLI and curl.
- Browser
- Download file 889 Bytes
-
https://huggingface.co/spaces/WOosts/chat-ui/resolve/main/src/lib/server/endpoints/openai/openAICompletionToTextGenerationStream.ts
- Command line
-
hf download hf://spaces/WOosts/chat-ui/src/lib/server/endpoints/openai/openAICompletionToTextGenerationStream.ts
-
curl -L -o openAICompletionToTextGenerationStream.ts https://huggingface.co/spaces/WOosts/chat-ui/resolve/main/src/lib/server/endpoints/openai/openAICompletionToTextGenerationStream.ts
889 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: last, | |
| }, | |
| generated_text: last ? generatedText : null, | |
| details: null, | |
| }; | |
| yield output; | |
| } | |
| } | |