Files
api/gpt/__init__.py
2025-07-17 06:55:16 -04:00

46 lines
1.8 KiB
Python

import logging
from typing import Optional
from openai import AsyncOpenAI
class GPT:
def __init__(self, constants) -> None:
self.constants = constants
self.api_key: str = self.constants.OPENAI_API_KEY
self.client: AsyncOpenAI = AsyncOpenAI(
api_key=self.api_key,
timeout=10.0,
)
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.
# IMPORTANT
As an AI assistant, you may not always have much information available to describe the track provided. That is TOTALLY FINE!
What is not acceptable is hallucinations, for example:
- Do NOT mention the name of the album the track was included on. You rarely have correct information in this context.
- If no 100% reliable data is available, do NOT (!!) mention the album..."""
logging.getLogger("httpx").setLevel("CRITICAL")
async def get_completion(
self, prompt: str, system_prompt: Optional[str] = None
) -> Optional[str]:
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",
temperature=1.00,
max_completion_tokens=512,
)
response: Optional[str] = chat_completion.choices[0].message.content
return response