rename(deepseek): change name openai to deepseek client

This commit is contained in:
bladeclara42
2025-06-25 12:04:18 +07:00
parent f59350cd73
commit fa567efd3a
4 changed files with 28 additions and 16 deletions

View File

@@ -8,3 +8,7 @@ load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "") OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-3.5-turbo") OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-3.5-turbo")
OPENAI_API_BASE = os.getenv("OPENAI_API_BASE", "https://api.openai.com/v1") OPENAI_API_BASE = os.getenv("OPENAI_API_BASE", "https://api.openai.com/v1")
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY", "")
DEEPSEEK_API_BASE = os.getenv("DEEPSEEK_API_BASE", "https://api.deepseek.com/v1")
DEEPSEEK_MODEL = os.getenv("DEEPSEEK_MODEL", "deepseek-chat")

View File

@@ -1,21 +1,23 @@
# app/services/openai_service.py # app/services/openai_service.py
import openai import openai
import os
from openai import OpenAI from openai import OpenAI
import openai from openai import OpenAIError
from app.core.config import OPENAI_API_KEY, OPENAI_MODEL, OPENAI_API_BASE from app.core.config import DEEPSEEK_API_BASE, DEEPSEEK_MODEL, DEEPSEEK_API_KEY
# Initialize OpenAI client with DeepSeek's API # Ensure the API key is properly set
DEEPSEEK_API_BASE = "https://api.deepseek.com/v1" if not DEEPSEEK_API_KEY:
DEEPSEEK_MODEL = "deepseek-chat" raise ValueError("DEEPSEEK_API_KEY is not set in environment variables")
openai.api_key = OPENAI_API_KEY # Initialize the client with proper configuration
openai.api_base = DEEPSEEK_API_BASE 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):
client = OpenAI( if not messages:
api_key=openai.api_key, raise ValueError("Messages list cannot be empty")
base_url=DEEPSEEK_API_BASE
)
try: try:
response = client.chat.completions.create( response = client.chat.completions.create(
@@ -26,10 +28,16 @@ async def chat_with_openai(messages: list):
stream=False stream=False
) )
if response.choices and len(response.choices) > 0: if not response.choices or not response.choices[0].message.content:
return response.choices[0].message.content return "No response content from the model"
return "No response from the model"
return response.choices[0].message.content
except OpenAIError as e:
error_msg = f"DeepSeek API Error: {str(e)}"
print(error_msg)
raise Exception(error_msg) from e
except Exception as e: except Exception as e:
print(f"Error calling DeepSeek API: {str(e)}") error_msg = f"Unexpected error: {str(e)}"
raise print(error_msg)
raise Exception(error_msg) from e