File size: 1,140 Bytes
1505bbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import os

GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY")

def generate_answer(context, question):

    prompt = f"""
You are answering exam questions.

Use the information in the context to answer the question directly.

Do NOT describe the context.
Do NOT say "the context says".

Give the final answer.

Context:
{context}

Question:
{question}

Answer:
"""

    url = f"https://generativelanguage.googleapis.com/v1/models/gemini-2.5-flash:generateContent?key={GEMINI_API_KEY}"

    headers = {
        "Content-Type": "application/json"
    }

    data = {
        "contents":[
            {
                "parts":[
                    {"text": prompt}
                ]
            }
        ],
        "generationConfig":{
            "temperature":0.3,
            "maxOutputTokens":300
        }
    }

    response = requests.post(url, headers=headers, json=data)

    print("Gemini status:", response.status_code)

    if response.status_code != 200:
        print(response.text)
        raise Exception("LLM failed")

    result = response.json()

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