UNPKG

@nomyx/assistant

Version:

A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)

55 lines (54 loc) 2.05 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.streamChat = streamChat; const axios_1 = __importDefault(require("axios")); const tools_1 = require("./tools"); async function* streamChat(config, messages, options, tools) { const url = config.getApiUrl('chat/completions'); const requestBody = { messages: (0, tools_1.convertMessages)(messages), max_tokens: options.maxTokens, temperature: options.temperature, stream: true, }; if (tools && tools.length > 0) { requestBody.tools = tools.map(tool => (0, tools_1.convertToolSchema)(tool.getSchema())); requestBody.tool_choice = 'auto'; } const response = await axios_1.default.post(url, requestBody, { headers: { 'Content-Type': 'application/json', 'api-key': config.apiKey, }, responseType: 'stream', }); for await (const chunk of response.data) { const lines = chunk.toString().split('\n').filter((line) => line.trim() !== ''); for (const line of lines) { if (line.includes('[DONE]')) return; if (line.startsWith('data: ')) { const data = JSON.parse(line.slice(6)); yield createStreamingProviderResponse(data); } } } } function createStreamingProviderResponse(data) { const choice = data.choices && data.choices.length > 0 ? data.choices[0] : null; if (!choice) { throw new Error('Unexpected response format from Azure OpenAI API'); } const response = { content: choice.delta?.content || '', toolCalls: [], usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 }, }; if (choice.delta?.function_call) { response.toolCalls = [(0, tools_1.convertToolCall)(choice.delta.function_call)]; } return response; }