File size: 1,194 Bytes
44cae7c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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="deepseek-chat",
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="deepseek-chat",
messages=messages
)
return response.choices[0].message.content
except Exception as e:
print(f"Error in chat: {e}")
return None |