Apex123 commited on
Commit
822fb48
·
verified ·
1 Parent(s): 3819f95

Create server.0522.old

Browse files
Files changed (1) hide show
  1. server.0522.old +55 -0
server.0522.old ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ import time
4
+
5
+ app = Flask(__name__)
6
+ CORS(app) # 讓 Open WebUI 可跨域存取
7
+
8
+ @app.route('/')
9
+ def hello():
10
+ return "✅ Flask API is running!"
11
+
12
+ @app.route('/v1/chat/completions', methods=['POST'])
13
+ def chat():
14
+ print("💬 收到請求")
15
+
16
+ data = request.get_json()
17
+ print("資料內容:", data)
18
+
19
+ # 取得使用者輸入
20
+ messages = data.get("messages", [])
21
+ user_message = ""
22
+ for m in messages[::-1]:
23
+ if m.get("role") == "user":
24
+ user_message = m.get("content", "")
25
+ break
26
+
27
+ # 模擬回覆
28
+ reply = f"你剛剛說的是:「{user_message}」,這是來自 Flask 模型的回覆 ✅"
29
+
30
+ # 回傳格式符合 OpenAI Chat API 格式
31
+ return jsonify({
32
+ "id": "chatcmpl-xyz",
33
+ "object": "chat.completion",
34
+ "created": int(time.time()),
35
+ "model": "local-flask-test-001",
36
+ "choices": [
37
+ {
38
+ "index": 0,
39
+ "message": {
40
+ "role": "assistant",
41
+ "content": reply
42
+ },
43
+ "finish_reason": "stop"
44
+ }
45
+ ],
46
+ "usage": {
47
+ "prompt_tokens": 10,
48
+ "completion_tokens": 10,
49
+ "total_tokens": 20
50
+ }
51
+ })
52
+
53
+ if __name__ == '__main__':
54
+ app.run(host='0.0.0.0', port=7860)
55
+