UNPKG

@nomyx/assistant

Version:

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

52 lines (51 loc) 1.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.chat = chat; const axios_1 = __importDefault(require("axios")); const tools_1 = require("./tools"); async function chat(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, }; if (tools && tools.length > 0) { requestBody.tools = tools .map(tool => (0, tools_1.convertToolSchema)(tool.getSchema())) .map(tool => ({ type: 'function', function: tool })); //requestBody.tool_choice = 'auto'; } const response = await axios_1.default.post(url, requestBody, { headers: { 'Content-Type': 'application/json', 'api-key': config.apiKey, }, }); return createProviderResponse(response.data); } function createProviderResponse(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.message?.content || '', toolCalls: [], usage: data.usage ? { promptTokens: data.usage.prompt_tokens, completionTokens: data.usage.completion_tokens, totalTokens: data.usage.total_tokens } : undefined, }; if (choice.message?.function_call) { response.toolCalls = [(0, tools_1.convertToolCall)(choice.message.function_call)]; } return response; }