Spaces:
Sleeping
Sleeping
| /** | |
| * LLM Service | |
| * Handles communication with Google Gemini API. | |
| */ | |
| const axios = require('axios'); | |
| const { llm } = require('./settings'); | |
| // Configuration | |
| const API_KEY = process.env.LLM_API_KEY; // Using API Key from .env | |
| const BASE_URL = llm.baseUrl; | |
| /** | |
| * Sends a prompt to the Gemini LLM and returns the generated text. | |
| * | |
| * @param {string} promptText - The fully constructed prompt string (including system instructions if embedded). | |
| * @param {Object} config - Optional configuration overrides (temperature, etc). | |
| * @returns {Promise<string>} - The generated response text. | |
| */ | |
| async function generateText(promptText, config = {}) { | |
| if (!API_KEY) { | |
| throw new Error("API_KEY is not defined in environment variables."); | |
| } | |
| try { | |
| const url = `${BASE_URL}?key=${API_KEY}`; | |
| const payload = { | |
| contents: [{ | |
| parts: [{ | |
| text: promptText | |
| }] | |
| }], | |
| generationConfig: { | |
| temperature: config.temperature || 0.7, | |
| maxOutputTokens: config.maxTokens || 8192, | |
| } | |
| }; | |
| console.log(`[LLM Service] Sending request to ${llm.model}...`); | |
| const response = await axios.post(url, payload, { | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| } | |
| }); | |
| // Extract text from Gemini response structure | |
| if (response.data && response.data.candidates && response.data.candidates.length > 0) { | |
| const candidate = response.data.candidates[0]; | |
| if (candidate.content && candidate.content.parts && candidate.content.parts.length > 0) { | |
| return candidate.content.parts[0].text; | |
| } | |
| } | |
| console.warn("[LLM Service] Unexpected response structure:", JSON.stringify(response.data)); | |
| return "Error: Empty response from LLM."; | |
| } catch (error) { | |
| console.error('[LLM Service] Error generating text:', error.response ? error.response.data : error.message); | |
| throw error; | |
| } | |
| } | |
| module.exports = { | |
| generateText | |
| }; | |