lizhen30 commited on
Commit
860b1b9
·
1 Parent(s): c5e8144

添加使用逻辑,每访问一次,可用次数减一,区分gpt-4、gpt3.5

Browse files
Files changed (1) hide show
  1. redis/test_user_redis.py +39 -31
redis/test_user_redis.py CHANGED
@@ -171,6 +171,7 @@ def purchase():
171
  @app.route('/validate', methods=['POST'])
172
  def validate():
173
  token = parse_token(request)
 
174
 
175
  # 验证令牌
176
  if not validate_token(token):
@@ -187,7 +188,7 @@ def validate():
187
  return jsonify({'code': 400, 'message': 'User has not purchased any package'})
188
 
189
  # 检查用户聊天次数是否超过限制
190
- if exceeded_chat_limit(user_id, package):
191
  return jsonify({'code': 400, 'message': 'Chat limit exceeded'})
192
 
193
  return jsonify({'code': 0, 'message': 'Chat limit not exceeded'})
@@ -195,29 +196,33 @@ def validate():
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}',
@@ -230,6 +235,9 @@ def proxy_chat_completions():
230
  # 获取 OpenAI API 的响应数据
231
  result = response.json()
232
 
 
 
 
233
  # 返回 OpenAI API 的响应给客户端
234
  return result, response.status_code
235
 
@@ -374,22 +382,22 @@ def get_package_expiration(user_id):
374
  expiration = redis.ttl(user_package_key)
375
  return expiration
376
 
377
- # 检查用户聊天次数是否超过限制
378
-
379
-
380
- def exceeded_chat_limit(user_id, package):
381
- user_basic_chat_key = f'user:{user_id}:basic_chat'
382
- user_advanced_chat_key = f'user:{user_id}:advanced_chat'
383
 
384
- basic_chat_limit = int(package.get(b'basic_chat_limit', 0).decode('utf-8'))
385
- advanced_chat_limit = int(package.get(
386
- b'advanced_chat_limit', 0).decode('utf-8'))
387
-
388
- if basic_chat_limit >= 0 and int(redis.get(user_basic_chat_key) or 0) >= basic_chat_limit:
389
- return True
390
-
391
- if advanced_chat_limit >= 0 and int(redis.get(user_advanced_chat_key) or 0) >= advanced_chat_limit:
392
- return True
 
 
 
 
 
 
393
 
394
  return False
395
 
 
171
  @app.route('/validate', methods=['POST'])
172
  def validate():
173
  token = parse_token(request)
174
+ model = request.json.get('model')
175
 
176
  # 验证令牌
177
  if not validate_token(token):
 
188
  return jsonify({'code': 400, 'message': 'User has not purchased any package'})
189
 
190
  # 检查用户聊天次数是否超过限制
191
+ if exceeded_chat_limit(user_id, package, model):
192
  return jsonify({'code': 400, 'message': 'Chat limit exceeded'})
193
 
194
  return jsonify({'code': 0, 'message': 'Chat limit not exceeded'})
 
196
 
197
  @app.route('/chat/completions', methods=['POST'])
198
  def proxy_chat_completions():
199
+ token = parse_token(request)
200
+ model = request.json.get('model')
201
 
202
+ # 验证令牌
203
+ if not validate_token(token):
204
+ return jsonify({'code': 401, 'message': 'Invalid token'})
205
 
206
+ user_id = get_user_id_from_token(token)
207
 
208
+ if not user_id:
209
+ return jsonify({'code': 400, 'message': 'User not found'})
210
 
211
+ # 获取用户套餐信息
212
+ package = get_user_package(user_id)
213
+ if not package:
214
+ return jsonify({'code': 400, 'message': 'User has not purchased any package'})
215
 
216
+ # 检查用户聊天次数是否超过限制
217
+ if exceeded_chat_limit(user_id, package, model):
218
+ if model == 'gpt-3.5-turbo':
219
+ return jsonify({'code': 400, 'message': 'model3.5基础访问次数已用完'})
220
+ if model == 'gpt-4':
221
+ return jsonify({'code': 400, 'message': 'model4高级访问次数已用完'})
222
 
223
  # 获取请求数据
224
  data = request.get_json()
225
+
226
  # 设置请求头部信息
227
  headers = {
228
  'Authorization': f'Bearer {MY_OPENAI_API_KEY}',
 
235
  # 获取 OpenAI API 的响应数据
236
  result = response.json()
237
 
238
+ user_package_key = f'user:{user_id}:package'
239
+ redis.hincrby(user_package_key, 'basic_chat_limit', -1)
240
+
241
  # 返回 OpenAI API 的响应给客户端
242
  return result, response.status_code
243
 
 
382
  expiration = redis.ttl(user_package_key)
383
  return expiration
384
 
 
 
 
 
 
 
385
 
386
+ # 检查用户聊天次数是否超过限制
387
+ def exceeded_chat_limit(user_id, package, model):
388
+ if model == 'gpt-3.5-turbo':
389
+ user_basic_chat_key = f'user:{user_id}:basic_chat'
390
+ basic_chat_limit = int(package.get(
391
+ b'basic_chat_limit', 0).decode('utf-8'))
392
+ if basic_chat_limit >= 0 and int(redis.get(user_basic_chat_key) or 0) >= basic_chat_limit:
393
+ return True
394
+
395
+ if model == 'gpt-4':
396
+ user_advanced_chat_key = f'user:{user_id}:advanced_chat'
397
+ advanced_chat_limit = int(package.get(
398
+ b'advanced_chat_limit', 0).decode('utf-8'))
399
+ if advanced_chat_limit >= 0 and int(redis.get(user_advanced_chat_key) or 0) >= advanced_chat_limit:
400
+ return True
401
 
402
  return False
403