UNPKG

@langchain/core

Version:
160 lines (159 loc) 3.12 kB
//#region src/utils/testing/openai_stream_fixtures.ts function asAsyncIterable(items) { return { async *[Symbol.asyncIterator]() { for (const item of items) yield item; } }; } function openAITextOnlyChunksWithUsage(model = "test-model") { const chunks = openAITextOnlyChunks(model); const last = chunks[chunks.length - 1]; chunks[chunks.length - 1] = { ...last, usage: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12 } }; return chunks; } function openAITextOnlyChunks(model = "test-model") { return [ { id: "chatcmpl-text", model, choices: [{ index: 0, delta: { role: "assistant", content: "Hello" }, finish_reason: null }] }, { id: "chatcmpl-text", model, choices: [{ index: 0, delta: { content: " world" }, finish_reason: null }] }, { id: "chatcmpl-text", model, choices: [{ index: 0, delta: {}, finish_reason: "stop" }] } ]; } function openAIReasoningTextChunks(model = "test-model") { return [ { id: "chatcmpl-reason", model, choices: [{ index: 0, delta: { role: "assistant", reasoning_content: "Let me reason..." }, finish_reason: null }] }, { id: "chatcmpl-reason", model, choices: [{ index: 0, delta: { content: "Answer." }, finish_reason: null }] }, { id: "chatcmpl-reason", model, choices: [{ index: 0, delta: {}, finish_reason: "stop" }] } ]; } function openAIToolCallChunks(model = "test-model") { return [ { id: "chatcmpl-tools", model, choices: [{ index: 0, delta: { role: "assistant", content: "Let me search." }, finish_reason: null }] }, { id: "chatcmpl-tools", model, choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_abc", type: "function", function: { name: "web_search", arguments: "{\"query\"" } }] }, finish_reason: null }] }, { id: "chatcmpl-tools", model, choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: ":\"weather\"}" } }] }, finish_reason: null }] }, { id: "chatcmpl-tools", model, choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] } ]; } function sseResponseFromOpenAIChunks(chunks) { const encoder = new TextEncoder(); return new Response(new ReadableStream({ start(controller) { for (const chunk of chunks) controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`)); controller.close(); } }), { status: 200, headers: { "Content-Type": "text/event-stream" } }); } //#endregion exports.asAsyncIterable = asAsyncIterable; exports.openAIReasoningTextChunks = openAIReasoningTextChunks; exports.openAITextOnlyChunks = openAITextOnlyChunks; exports.openAITextOnlyChunksWithUsage = openAITextOnlyChunksWithUsage; exports.openAIToolCallChunks = openAIToolCallChunks; exports.sseResponseFromOpenAIChunks = sseResponseFromOpenAIChunks; //# sourceMappingURL=openai_stream_fixtures.cjs.map