-
Notifications
You must be signed in to change notification settings - Fork 21
/
llm.py
30 lines (22 loc) · 859 Bytes
/
llm.py
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
from openai import AsyncOpenAI
import asyncio
MAX_RETRIES = 5
async def generate_response(user_prompt, model, open_ai_api_key):
response = None
async_openai_client = AsyncOpenAI(
api_key=open_ai_api_key,
)
for attempt in range(MAX_RETRIES):
try:
completion = await async_openai_client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": user_prompt}],
)
response = completion.choices[0].message.content
except Exception as e:
if attempt < MAX_RETRIES - 1: # don't wait after the last attempt
await asyncio.sleep(1 * (2**attempt))
else:
print(f"Response generation exception after max retries: {e}")
return None
return response