Spaces:
Runtime error
Runtime error
File size: 998 Bytes
fe893f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import os
from openai import OpenAI
from typing import Optional
class OpenAIClient:
def __init__(self, api_key: Optional[str] = None):
self.client = OpenAI(api_key=api_key or os.getenv("OPENAI_API_KEY"))
self.default_model = "gpt-4o"
def chat(self, prompt: str, system_instruction: str = "You are the 'Grand Tech Wizard'. You are a master Software Architect, Senior Engineer, API/Webhooks Specialist, and App Builder. You design scalable, secure, and idiomatic technical systems.", model: Optional[str] = None) -> str:
try:
response = self.client.chat.completions.create(
model=model or self.default_model,
messages=[
{"role": "system", "content": system_instruction},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content
except Exception as e:
return f"Error calling OpenAI API: {str(e)}"
|