UNPKG

svector-sdk

Version:

Official JavaScript and TypeScript SDK for accessing SVECTOR APIs.

141 lines (140 loc) 5.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Conversations = void 0; class Conversations { constructor(client) { this.client = client; } async create(params, options) { const systemInstructions = this.normalizeSystemInstructions(params.instructions); const messages = this.buildMessagesFromConversation(params, systemInstructions); const chatRequest = { model: params.model, messages, max_tokens: params.max_tokens, temperature: params.temperature, stream: false, files: params.files, }; const response = await this.client.chat.create(chatRequest, options); return { output: response.choices[0]?.message?.content || '', usage: response.usage, _request_id: response._request_id, }; } async createStream(params, options) { const systemInstructions = this.normalizeSystemInstructions(params.instructions); const messages = this.buildMessagesFromConversation(params, systemInstructions); const chatRequest = { model: params.model, messages, max_tokens: params.max_tokens, temperature: params.temperature, stream: true, files: params.files, }; const stream = await this.client.chat.createStream(chatRequest, options); return this.transformStream(stream); } async createWithResponse(params, options) { const systemInstructions = this.normalizeSystemInstructions(params.instructions); const messages = this.buildMessagesFromConversation(params, systemInstructions); const chatRequest = { model: params.model, messages, max_tokens: params.max_tokens, temperature: params.temperature, stream: false, files: params.files, }; const { data, response } = await this.client.chat.createWithResponse(chatRequest, options); return { data: { output: data.choices[0]?.message?.content || '', usage: data.usage, _request_id: data._request_id, }, response, }; } normalizeSystemInstructions(instructions) { if (typeof instructions === 'string') { return instructions; } if (typeof instructions === 'function') { try { const result = instructions(); return typeof result === 'string' ? result : String(result); } catch (error) { console.warn('SVECTOR: Failed to execute system instructions function:', error); return 'You are a helpful assistant.'; } } if (instructions && typeof instructions === 'object') { if (instructions.content) return String(instructions.content); if (instructions.text) return String(instructions.text); if (instructions.value) return String(instructions.value); try { return JSON.stringify(instructions); } catch (error) { return String(instructions); } } return instructions ? String(instructions) : 'You are a helpful assistant.'; } buildMessagesFromConversation(params, systemInstructions) { const messages = []; const finalInstructions = systemInstructions || this.normalizeSystemInstructions(params.instructions); if (finalInstructions && finalInstructions.trim()) { messages.push({ role: 'system', content: finalInstructions, }); } if (params.context && params.context.length > 0) { for (let i = 0; i < params.context.length; i++) { const role = i % 2 === 0 ? 'user' : 'assistant'; messages.push({ role: role, content: params.context[i], }); } } if (typeof params.input === 'string') { messages.push({ role: 'user', content: params.input, }); } else { messages.push({ role: 'user', content: params.input, }); } return messages; } async *transformStream(stream) { for await (const event of stream) { if (event.choices?.[0]?.delta?.content) { yield { content: event.choices[0].delta.content, done: false, }; } else if (event.choices?.[0]?.finish_reason) { yield { content: '', done: true, }; } } } } exports.Conversations = Conversations;