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