28 lines
860 B
Python
28 lines
860 B
Python
# app/services/openai_service.py
|
|
import openai
|
|
from openai import OpenAI
|
|
from app.core.config import OPENAI_API_KEY, OPENAI_MODEL
|
|
from app.core.config import OPENAI_API_BASE
|
|
|
|
# Set OpenAI API key from the environment
|
|
openai.api_key = OPENAI_API_KEY
|
|
openai.api_base = OPENAI_API_BASE
|
|
|
|
print(openai.api_key)
|
|
print(OPENAI_MODEL)
|
|
print(OPENAI_API_BASE)
|
|
|
|
async def chat_with_openai(messages: list):
|
|
# Use the model from environment variable or fallback to default
|
|
model = OPENAI_MODEL
|
|
client = OpenAI(api_key=openai.api_key, base_url=openai.api_base)
|
|
|
|
response = client.chat.completions.create(
|
|
model="deepseek-chat", # Or the model you want
|
|
messages=messages, # Update this according to the new API syntax
|
|
max_tokens=100, # Example parameter
|
|
stream=False
|
|
)
|
|
|
|
return response.choices[0].message.content
|