2025-02-10 20:29:57 -05:00
|
|
|
#!/usr/bin/env python3.12
|
|
|
|
from typing import Optional
|
|
|
|
from openai import AsyncOpenAI
|
|
|
|
|
|
|
|
class GPT:
|
2025-02-15 21:09:33 -05:00
|
|
|
def __init__(self, constants) -> None:
|
2025-02-10 20:29:57 -05:00
|
|
|
self.constants = constants
|
2025-02-15 21:09:33 -05:00
|
|
|
self.api_key: str = self.constants.OPENAI_API_KEY
|
|
|
|
self.client: AsyncOpenAI = AsyncOpenAI(
|
2025-02-10 20:29:57 -05:00
|
|
|
api_key=self.api_key,
|
|
|
|
timeout=10.0,
|
|
|
|
)
|
2025-02-15 21:09:33 -05:00
|
|
|
self.default_system_prompt: str = """You are a helpful assistant who will provide only totally accurate tidbits of \
|
|
|
|
info on the specific songs the user may listen to."""
|
2025-02-10 20:29:57 -05:00
|
|
|
|
2025-02-15 21:09:33 -05:00
|
|
|
async def get_completion(self, prompt: str,
|
|
|
|
system_prompt: Optional[str] = None) -> Optional[str]:
|
2025-02-10 20:29:57 -05:00
|
|
|
if not system_prompt:
|
|
|
|
system_prompt = self.default_system_prompt
|
|
|
|
chat_completion = await self.client.chat.completions.create(
|
|
|
|
messages=[
|
|
|
|
{
|
|
|
|
"role": "system",
|
|
|
|
"content": system_prompt,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"role": "user",
|
|
|
|
"content": prompt,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
model="gpt-4o-mini",
|
2025-02-11 11:19:52 -05:00
|
|
|
temperature=0.35,
|
2025-02-10 20:29:57 -05:00
|
|
|
)
|
2025-02-15 21:09:33 -05:00
|
|
|
response: Optional[str] = chat_completion.choices[0].message.content
|
|
|
|
return response
|