feat: add lyric music to romanji tool
This commit is contained in:
@@ -1,38 +1,39 @@
|
||||
# app/services/openai_service.py
|
||||
import openai
|
||||
import os
|
||||
from openai import OpenAI
|
||||
from openai import OpenAIError
|
||||
import anyio
|
||||
from openai import OpenAI, OpenAIError
|
||||
from app.core.config import DEEPSEEK_API_BASE, DEEPSEEK_MODEL, DEEPSEEK_API_KEY
|
||||
|
||||
# Ensure the API key is properly set
|
||||
if not DEEPSEEK_API_KEY:
|
||||
raise ValueError("DEEPSEEK_API_KEY is not set in environment variables")
|
||||
|
||||
# Initialize the client with proper configuration
|
||||
# Initialize the client
|
||||
client = OpenAI(
|
||||
api_key=DEEPSEEK_API_KEY,
|
||||
base_url=DEEPSEEK_API_BASE
|
||||
)
|
||||
|
||||
async def chat_with_openai(messages: list):
|
||||
async def chat_with_openai(messages: list[dict[str, str]]) -> str:
|
||||
if not messages:
|
||||
raise ValueError("Messages list cannot be empty")
|
||||
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
model=DEEPSEEK_MODEL,
|
||||
messages=messages,
|
||||
max_tokens=1000,
|
||||
temperature=0.7,
|
||||
stream=False
|
||||
# Run sync client in a thread (non-blocking for FastAPI)
|
||||
response = await anyio.to_thread.run_sync(
|
||||
lambda: client.chat.completions.create(
|
||||
model=DEEPSEEK_MODEL,
|
||||
messages=messages,
|
||||
max_tokens=1000,
|
||||
temperature=0.7,
|
||||
stream=False
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if not response.choices or not response.choices[0].message.content:
|
||||
return "No response content from the model"
|
||||
|
||||
return response.choices[0].message.content
|
||||
|
||||
|
||||
return response.choices[0].message.content.strip()
|
||||
|
||||
except OpenAIError as e:
|
||||
error_msg = f"DeepSeek API Error: {str(e)}"
|
||||
print(error_msg)
|
||||
|
||||
Reference in New Issue
Block a user