tianlong12 commited on
Commit
4965971
·
verified ·
1 Parent(s): b3531de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -39
app.py CHANGED
@@ -1,8 +1,9 @@
1
- from flask import Flask, request, Response, jsonify
2
- import requests
 
3
 
4
  app = Flask(__name__)
5
-
6
  @app.route('/')
7
  def index():
8
  return "Hello, this is the root page of your Flask application!"
@@ -10,57 +11,73 @@ def index():
10
  @app.route('/hf/v1/chat/completions', methods=['POST'])
11
  def chat():
12
  try:
13
- # 从请求头中获取 API 密钥和基础 URL
14
  auth_header = request.headers.get('Authorization')
15
  if not auth_header or not auth_header.startswith('Bearer '):
16
- return Response("Unauthorized", status=401)
17
 
18
- api_key, base_url = auth_header.split(" ")[1:]
19
-
20
- # 构建目标 URL
21
- target_url = f"{base_url}/v1/chat/completions"
22
 
23
- # 准备请求头
24
- headers = {
25
- 'Authorization': f'Bearer {api_key}',
26
- 'Content-Type': 'application/json'
27
- }
 
28
 
29
- # 检查是否为流式请求
30
- is_stream = request.json.get('stream', False)
 
 
 
 
 
 
 
31
 
32
- # 转发请求
33
- response = requests.post(
34
- target_url,
35
- headers=headers,
36
- json=request.json,
37
- stream=is_stream
38
  )
39
- print(target_url,api_key)
40
 
41
- if is_stream:
42
- # 流式响应
 
43
  def generate():
44
- for chunk in response.iter_content(chunk_size=1024):
45
- yield chunk
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- return Response(
48
- generate(),
49
- content_type=response.headers['Content-Type'],
50
- status=response.status_code
51
- )
52
  else:
53
- # 非流式响应
54
- return Response(
55
- response.content,
56
- content_type=response.headers['Content-Type'],
57
- status=response.status_code
 
 
 
 
 
58
  )
 
59
 
60
  except Exception as e:
61
  print("Exception:", e)
62
  return jsonify({"error": str(e)}), 500
63
 
64
  if __name__ == "__main__":
65
- app.run(host='0.0.0.0', port=7860, threaded=True)
66
-
 
1
+ from flask import Flask, request, Response, stream_with_context, jsonify
2
+ from openai import OpenAI
3
+ import json
4
 
5
  app = Flask(__name__)
6
+
7
  @app.route('/')
8
  def index():
9
  return "Hello, this is the root page of your Flask application!"
 
11
  @app.route('/hf/v1/chat/completions', methods=['POST'])
12
  def chat():
13
  try:
14
+ # 验证请求头中的API密钥
15
  auth_header = request.headers.get('Authorization')
16
  if not auth_header or not auth_header.startswith('Bearer '):
17
+ return jsonify({"error": "Unauthorized"}), 401
18
 
19
+ api_key = auth_header.split(" ")[1]
20
+ base_url= auth_header.split(" ")[2]
 
 
21
 
22
+ data = request.json
23
+ #print("Received data:", data) # 打印请求体以进行调试
24
+
25
+ # 验证请求格式
26
+ if not data or 'messages' not in data or 'model' not in data:
27
+ return jsonify({"error": "Missing 'messages' or 'model' in request body"}), 400
28
 
29
+ model = data['model']
30
+ messages = data['messages']
31
+ temperature = data.get('temperature', 0.7) # 默认值0.7
32
+ #max_tokens = calculate_max_tokens(model, messages, requested_max_tokens)
33
+ top_p = data.get('top_p', 1.0) # 默认值1.0
34
+ n = data.get('n', 1) # 默认值1
35
+ stream = data.get('stream', False) # 默认值False
36
+ functions = data.get('functions', None) # Functions for function calling
37
+ function_call = data.get('function_call', None) # Specific function call request
38
 
39
+ # 创建每个请求的 OpenAI 客户端实例
40
+ client = OpenAI(
41
+ api_key=api_key,
42
+ base_url=base_url,
 
 
43
  )
 
44
 
45
+ # 处理模型响应
46
+ if stream:
47
+ # 处理流式响应
48
  def generate():
49
+ response = client.chat.completions.create(
50
+ model=model,
51
+ messages=messages,
52
+ temperature=temperature,
53
+ #max_tokens=max_tokens,
54
+ top_p=top_p,
55
+ n=n,
56
+ stream=True,
57
+ functions=functions,
58
+ function_call=function_call
59
+ )
60
+ for chunk in response:
61
+ yield f"data: {json.dumps(chunk.to_dict())}\n\n"
62
 
63
+ return Response(stream_with_context(generate()), content_type='text/event-stream')
 
 
 
 
64
  else:
65
+ # 处理非流式响应
66
+ response = client.chat.completions.create(
67
+ model=model,
68
+ messages=messages,
69
+ temperature=temperature,
70
+ #max_tokens=max_tokens,
71
+ top_p=top_p,
72
+ n=n,
73
+ functions=functions,
74
+ function_call=function_call,
75
  )
76
+ return jsonify(response.to_dict())
77
 
78
  except Exception as e:
79
  print("Exception:", e)
80
  return jsonify({"error": str(e)}), 500
81
 
82
  if __name__ == "__main__":
83
+ app.run(host='0.0.0.0', port=7860, threaded=True)