Files
api/gpt/__init__.py

46 lines
1.8 KiB
Python
Raw Normal View History

import logging
2025-02-10 20:29:57 -05:00
from typing import Optional
from openai import AsyncOpenAI
2025-02-10 20:29:57 -05:00
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-07-15 13:48:51 -04: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.
# 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..."""
2025-02-10 20:29:57 -05:00
logging.getLogger("httpx").setLevel("CRITICAL")
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,
},
2025-02-10 20:29:57 -05:00
],
model="gpt-4o-mini",
2025-07-15 13:48:51 -04:00
temperature=1.00,
2025-07-17 06:55:16 -04:00
max_completion_tokens=512,
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