File size: 1,059 Bytes
a03bf1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
from dotenv import load_dotenv

# Load .env file
load_dotenv()

API_KEY = os.getenv("API_KEY")
MODEL_NAME = os.getenv("MODEL_NAME")
BASE_URL = f"https://generativelanguage.googleapis.com/v1beta/models/{MODEL_NAME}:generateContent"

def ask_llm(question: str) -> str:
    """
    Sends the given question to the Gemini LLM and returns its response.
    
    Args:
        question (str): The question to ask the LLM.
    
    Returns:
        str: The response from the LLM or an error message.
    """
    headers = {"Content-Type": "application/json"}
    params = {"key": API_KEY}
    payload = {
        "contents": [
            {"parts": [{"text": f"Answer this financial question clearly:\n\n{question}"}]}
        ]
    }

    try:
        resp = requests.post(BASE_URL, headers=headers, params=params, json=payload)
        resp.raise_for_status()
        data = resp.json()

        return data["candidates"][0]["content"]["parts"][0]["text"]

    except Exception as e:
        return f"⚠️ Error calling LLM: {str(e)}"