Spaces:
Runtime error
Runtime error
chat gpt 代理接口
Browse files- redis/test_user_redis.py +45 -1
redis/test_user_redis.py
CHANGED
@@ -5,10 +5,13 @@ import uuid
|
|
5 |
import time
|
6 |
import jwt
|
7 |
import datetime
|
|
|
|
|
8 |
from flask import Flask, request, jsonify, Request
|
9 |
from redis import Redis
|
10 |
|
11 |
SECERT_KEY = "8U2LL1"
|
|
|
12 |
|
13 |
app = Flask(__name__)
|
14 |
redis = Redis(host='192.168.3.229', port=6379, password='lizhen-redis')
|
@@ -190,6 +193,47 @@ def validate():
|
|
190 |
return jsonify({'code': 0, 'message': 'Chat limit not exceeded'})
|
191 |
|
192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
def parse_token(request: Request):
|
194 |
token_with_bearer = request.headers.get('Authorization')
|
195 |
|
@@ -208,7 +252,7 @@ def generate_token(user_id, username):
|
|
208 |
'user_id': user_id,
|
209 |
'username': username,
|
210 |
'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=1)
|
211 |
-
|
212 |
|
213 |
# 在这里,您可以使用您的密钥(secret key)来签署令牌
|
214 |
# 选择适当的签名算法,并设置适当的过期时间等参数
|
|
|
5 |
import time
|
6 |
import jwt
|
7 |
import datetime
|
8 |
+
import requests
|
9 |
+
import os
|
10 |
from flask import Flask, request, jsonify, Request
|
11 |
from redis import Redis
|
12 |
|
13 |
SECERT_KEY = "8U2LL1"
|
14 |
+
MY_OPENAI_API_KEY = os.environ.get('MY_OPENAI_API_KEY')
|
15 |
|
16 |
app = Flask(__name__)
|
17 |
redis = Redis(host='192.168.3.229', port=6379, password='lizhen-redis')
|
|
|
193 |
return jsonify({'code': 0, 'message': 'Chat limit not exceeded'})
|
194 |
|
195 |
|
196 |
+
@app.route('/chat/completions', methods=['POST'])
|
197 |
+
def proxy_chat_completions():
|
198 |
+
# token = parse_token(request)
|
199 |
+
|
200 |
+
# # 验证令牌
|
201 |
+
# if not validate_token(token):
|
202 |
+
# return jsonify({'code': 401, 'message': 'Invalid token'})
|
203 |
+
|
204 |
+
# user_id = get_user_id_from_token(token)
|
205 |
+
|
206 |
+
# if not user_id:
|
207 |
+
# return jsonify({'code': 400, 'message': 'User not found'})
|
208 |
+
|
209 |
+
# # 获取用户套餐信息
|
210 |
+
# package = get_user_package(user_id)
|
211 |
+
# if not package:
|
212 |
+
# return jsonify({'code': 400, 'message': 'User has not purchased any package'})
|
213 |
+
|
214 |
+
# # 检查用户聊天次数是否超过限制
|
215 |
+
# if exceeded_chat_limit(user_id, package):
|
216 |
+
# return jsonify({'code': 400, 'message': 'Chat limit exceeded'})
|
217 |
+
|
218 |
+
# 获取请求数据
|
219 |
+
data = request.get_json()
|
220 |
+
|
221 |
+
# 设置请求头部信息
|
222 |
+
headers = {
|
223 |
+
'Authorization': f'Bearer {MY_OPENAI_API_KEY}',
|
224 |
+
'Content-Type': 'application/json'
|
225 |
+
}
|
226 |
+
# 将请求转发到 OpenAI API
|
227 |
+
response = requests.post(
|
228 |
+
'https://api.openai.com/v1/chat/completions', json=data, headers=headers)
|
229 |
+
|
230 |
+
# 获取 OpenAI API 的响应数据
|
231 |
+
result = response.json()
|
232 |
+
|
233 |
+
# 返回 OpenAI API 的响应给客户端
|
234 |
+
return result, response.status_code
|
235 |
+
|
236 |
+
|
237 |
def parse_token(request: Request):
|
238 |
token_with_bearer = request.headers.get('Authorization')
|
239 |
|
|
|
252 |
'user_id': user_id,
|
253 |
'username': username,
|
254 |
'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=1)
|
255 |
+
}
|
256 |
|
257 |
# 在这里,您可以使用您的密钥(secret key)来签署令牌
|
258 |
# 选择适当的签名算法,并设置适当的过期时间等参数
|