leeBlog / app /ai_service.py
mistpe's picture
Update app/ai_service.py
fee86ca verified
from openai import OpenAI
from flask import current_app
def create_ai_client():
"""Create AI client with configuration from environment"""
return OpenAI(
api_key=current_app.config['AI_API_KEY'],
base_url=current_app.config['AI_BASE_URL']
)
def generate_summary(content):
"""Generate a summary of the article content"""
client = create_ai_client()
prompt = f"请为以下文章生成一个简洁的摘要:\n\n{content}"
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
except Exception as e:
print(f"Error generating summary: {e}")
return None
def chat_with_ai(messages):
"""Chat with AI about the article content"""
client = create_ai_client()
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
return response.choices[0].message.content
except Exception as e:
print(f"Error in chat: {e}")
return None