36 lines
991 B
Python
36 lines
991 B
Python
# app/services/openai_service.py
|
|
import openai
|
|
from openai import OpenAI
|
|
import openai
|
|
from app.core.config import OPENAI_API_KEY, OPENAI_MODEL, OPENAI_API_BASE
|
|
|
|
# Initialize OpenAI client with DeepSeek's API
|
|
DEEPSEEK_API_BASE = "https://api.deepseek.com/v1"
|
|
DEEPSEEK_MODEL = "deepseek-chat"
|
|
|
|
openai.api_key = OPENAI_API_KEY
|
|
openai.api_base = DEEPSEEK_API_BASE
|
|
|
|
async def chat_with_openai(messages: list):
|
|
client = OpenAI(
|
|
api_key=openai.api_key,
|
|
base_url=DEEPSEEK_API_BASE
|
|
)
|
|
|
|
try:
|
|
response = client.chat.completions.create(
|
|
model=DEEPSEEK_MODEL,
|
|
messages=messages,
|
|
max_tokens=1000,
|
|
temperature=0.7,
|
|
stream=False
|
|
)
|
|
|
|
if response.choices and len(response.choices) > 0:
|
|
return response.choices[0].message.content
|
|
return "No response from the model"
|
|
|
|
except Exception as e:
|
|
print(f"Error calling DeepSeek API: {str(e)}")
|
|
raise
|