File size: 2,124 Bytes
66e3a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * 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
};