-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm.py
More file actions
48 lines (40 loc) · 1.47 KB
/
Copy pathllm.py
File metadata and controls
48 lines (40 loc) · 1.47 KB
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
"""
llm.py - OpenRouter LLM API call with exponential-backoff retry logic.
"""
import os
import time
from openai import OpenAI, RateLimitError
from config import OPENROUTER_MODEL, LLM_MAX_RETRIES
_SYSTEM_PROMPT = (
"You are a helpful assistant that answers questions strictly based on the "
"provided PDF context. If the answer is not in the context, say so clearly."
)
def get_answer(context: str, question: str) -> str:
"""
Send the retrieved context and user question to OpenRouter's chat API
and return the model's answer.
Retries up to LLM_MAX_RETRIES times with exponential backoff (2s, 4s, 8s)
on 429 rate-limit errors before re-raising.
"""
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.getenv("OPENROUTER_API_KEY"),
)
for attempt in range(LLM_MAX_RETRIES):
try:
response = client.chat.completions.create(
model=OPENROUTER_MODEL,
messages=[
{"role": "system", "content": _SYSTEM_PROMPT},
{
"role": "user",
"content": f"Context from PDF:\n{context}\n\nQuestion: {question}",
},
],
)
return response.choices[0].message.content
except RateLimitError:
if attempt < LLM_MAX_RETRIES - 1:
time.sleep(2 ** (attempt + 1)) # 2s -> 4s -> 8s
else:
raise