Update app/ai_service.py
Browse files- app/ai_service.py +36 -36
app/ai_service.py
CHANGED
|
@@ -1,37 +1,37 @@
|
|
| 1 |
-
from openai import OpenAI
|
| 2 |
-
from flask import current_app
|
| 3 |
-
|
| 4 |
-
def create_ai_client():
|
| 5 |
-
"""Create AI client with configuration from environment"""
|
| 6 |
-
return OpenAI(
|
| 7 |
-
api_key=current_app.config['AI_API_KEY'],
|
| 8 |
-
base_url=current_app.config['AI_BASE_URL']
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
def generate_summary(content):
|
| 12 |
-
"""Generate a summary of the article content"""
|
| 13 |
-
client = create_ai_client()
|
| 14 |
-
prompt = f"请为以下文章生成一个简洁的摘要:\n\n{content}"
|
| 15 |
-
|
| 16 |
-
try:
|
| 17 |
-
response = client.chat.completions.create(
|
| 18 |
-
model="
|
| 19 |
-
messages=[{"role": "user", "content": prompt}]
|
| 20 |
-
)
|
| 21 |
-
return response.choices[0].message.content
|
| 22 |
-
except Exception as e:
|
| 23 |
-
print(f"Error generating summary: {e}")
|
| 24 |
-
return None
|
| 25 |
-
|
| 26 |
-
def chat_with_ai(messages):
|
| 27 |
-
"""Chat with AI about the article content"""
|
| 28 |
-
client = create_ai_client()
|
| 29 |
-
try:
|
| 30 |
-
response = client.chat.completions.create(
|
| 31 |
-
model="
|
| 32 |
-
messages=messages
|
| 33 |
-
)
|
| 34 |
-
return response.choices[0].message.content
|
| 35 |
-
except Exception as e:
|
| 36 |
-
print(f"Error in chat: {e}")
|
| 37 |
return None
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
from flask import current_app
|
| 3 |
+
|
| 4 |
+
def create_ai_client():
|
| 5 |
+
"""Create AI client with configuration from environment"""
|
| 6 |
+
return OpenAI(
|
| 7 |
+
api_key=current_app.config['AI_API_KEY'],
|
| 8 |
+
base_url=current_app.config['AI_BASE_URL']
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def generate_summary(content):
|
| 12 |
+
"""Generate a summary of the article content"""
|
| 13 |
+
client = create_ai_client()
|
| 14 |
+
prompt = f"请为以下文章生成一个简洁的摘要:\n\n{content}"
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
response = client.chat.completions.create(
|
| 18 |
+
model="gpt-4o-mini",
|
| 19 |
+
messages=[{"role": "user", "content": prompt}]
|
| 20 |
+
)
|
| 21 |
+
return response.choices[0].message.content
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Error generating summary: {e}")
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
def chat_with_ai(messages):
|
| 27 |
+
"""Chat with AI about the article content"""
|
| 28 |
+
client = create_ai_client()
|
| 29 |
+
try:
|
| 30 |
+
response = client.chat.completions.create(
|
| 31 |
+
model="gpt-4o-mini",
|
| 32 |
+
messages=messages
|
| 33 |
+
)
|
| 34 |
+
return response.choices[0].message.content
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Error in chat: {e}")
|
| 37 |
return None
|