-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
63 lines (49 loc) · 2.05 KB
/
app.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from flask import Flask, request, jsonify, render_template, session
import os
from openai import OpenAI
# Initialize Flask app
app = Flask(__name__)
app.secret_key = os.urandom(24) # For session management
# Initialize OpenAI client (or Dashscope)
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # Ensure you have the API key in your environment
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
# Max history length for each session
MAX_HISTORY_LENGTH = 20
@app.route('/')
def index():
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
user_message = request.form['message']
# Retrieve previous messages from the session or initialize if none exist
if 'messages' not in session:
session['messages'] = []
# Add user message to conversation history
session['messages'].append({'role': 'user', 'content': user_message})
# Automatically delete older messages if there are more than MAX_HISTORY_LENGTH
if len(session['messages']) > MAX_HISTORY_LENGTH:
session['messages'] = session['messages'][-MAX_HISTORY_LENGTH:]
# Call the API with the entire conversation history
try:
completion = client.chat.completions.create(
model="deepseek-r1", # Use your DeepSeek R1 model
messages=session['messages']
)
assistant_message = completion.choices[0].message.content
reasoning_content = completion.choices[0].message.reasoning_content
# Append assistant response to the conversation history
session['messages'].append({'role': 'assistant', 'content': assistant_message})
# Return the assistant's message, reasoning, and full conversation history for the frontend
return jsonify({
'assistant_message': assistant_message,
'reasoning_content': reasoning_content,
'messages': session['messages']
})
except Exception as e:
return jsonify({
'error': str(e)
})
if __name__ == '__main__':
app.run(debug=True)