(options: CallModelOptions)
| 40 | * @returns The model's text response |
| 41 | */ |
| 42 | export async function callModel(options: CallModelOptions): Promise<string> { |
| 43 | const { question, imageUrls, streaming = false, responseFormat, maxTokens } = options; |
| 44 | |
| 45 | // Validate input |
| 46 | if (!question || !question.trim()) { |
| 47 | throw new Error('Question must be a non-empty string'); |
| 48 | } |
| 49 | |
| 50 | // Get and validate config separately |
| 51 | let config: ModelConfig; |
| 52 | try { |
| 53 | config = getModelConfig(); |
| 54 | validateModelConfig(config); |
| 55 | } catch (error) { |
| 56 | if (error instanceof Error) { |
| 57 | logger.printErrorLog(`Configuration error: ${error.message}`); |
| 58 | } |
| 59 | throw error; |
| 60 | } |
| 61 | |
| 62 | try { |
| 63 | const requestConfig = { |
| 64 | modelName: config.model, |
| 65 | apiKey: config.apiKey, |
| 66 | configuration: { |
| 67 | baseURL: config.baseUrl, |
| 68 | }, |
| 69 | ...(maxTokens && { maxTokens }), |
| 70 | temperature: 0.1, |
| 71 | streaming, |
| 72 | ...(streaming && { |
| 73 | streamingOptions: { |
| 74 | includeUsage: true, |
| 75 | }, |
| 76 | }), |
| 77 | ...(!streaming && { streamUsage: true }), |
| 78 | ...(responseFormat && { modelKwargs: { response_format: responseFormat } }), |
| 79 | }; |
| 80 | const agentModel = new ChatOpenAI(requestConfig); |
| 81 | |
| 82 | // Build multimodal content parts: text + image_url |
| 83 | const contentParts: ContentPart[] = []; |
| 84 | contentParts.push({ type: 'text', text: question }); |
| 85 | |
| 86 | // Add images if provided |
| 87 | if (imageUrls) { |
| 88 | const urls = Array.isArray(imageUrls) ? imageUrls : [imageUrls]; |
| 89 | for (const url of urls) { |
| 90 | if (url && typeof url === 'string' && url.trim()) { |
| 91 | contentParts.push({ type: 'image_url', image_url: { url: url.trim() } }); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Create user message - use array if multimodal, string if text-only |
| 97 | const userMessage = new HumanMessage({ |
| 98 | content: contentParts.length > 1 ? contentParts : question, |
| 99 | }); |
no test coverage detected