-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
70 lines (56 loc) · 2.11 KB
/
server.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
63
64
65
66
67
68
69
70
from flask import Flask, request, jsonify, Response # Ensure jsonify is imported
from flask_cors import CORS
from openai import OpenAI
import json
with open('config.json', 'r', encoding='utf-8') as file:
config = json.load(file)
api_key_get = config.get('backend', {}).get('api-key', 'api-key-error')
port_default = config.get('backend', {}).get('port', '5000') # Default port
model_default = config.get('backend', {}).get('model', 'qwen-plus-0806') # Default model
app = Flask(__name__)
CORS(app, origins="*") # Allow all origins
with app.app_context():
client = OpenAI(
api_key= api_key_get,
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
@app.route("/chat", methods=["POST"])
def chat():
try:
model = model_default # default model
data = request.json
messages = data.get("messages")
systemContent = data.get("systemContent")
model_get = data.get("model")
if model_get is None or model_get == "":
model_set = model
print("no model set has been recieved, use default model: " + str(model_set))
else:
model_set = model_get
print("model get is: " + str(model_set))
send_message = [
{
"role": "system",
"content": systemContent,
},
{
"role": "user",
"content": messages,
},
]
completion = client.chat.completions.create(
model=model_set,
messages=send_message,
stream=False,
)
def generate():
if completion:
print(completion.model_dump_json())
print("completed as above")
return completion.choices[0].message.content
# print(completion.model_dump_json())
return Response(generate(), content_type="text/plain")
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=port_default)