DevsDoCode commited on
Commit
8ebc4d7
·
verified ·
1 Parent(s): e628215

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -87
app.py CHANGED
@@ -53,93 +53,94 @@ def index():
53
  @app.route('/v1/chat/completions', methods=['POST'])
54
  @requires_api_key
55
  def chat_completions(api_key):
56
- logging.info("Received request for chat completions")
57
- print("requess received")
58
- try:
59
- logging.info("Received request for chat completions")
60
- # Parse and validate request data
61
- try:
62
- data = request.get_json()
63
- chat_request = ChatCompletionRequest(**data)
64
- except ValidationError as e:
65
- return jsonify({'detail': e.errors()}), 400
66
-
67
- # Check API key validity and rate limit
68
- is_valid, error_message = check_api_key_validity(api_key)
69
- if not is_valid:
70
- return jsonify({'detail': error_message}), 401
71
-
72
- messages = [{"role": msg.role, "content": msg.content} for msg in chat_request.messages]
73
-
74
- # Get model info
75
- model_info = get_model_info(chat_request.model)
76
- if not model_info:
77
- return jsonify({'detail': 'Invalid model specified'}), 400
78
-
79
- # Model mapping
80
- model_mapping = {
81
- "meta-llama-405b-turbo": "meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo",
82
- "claude-3.5-sonnet": "claude-3-sonnet-20240229",
83
- }
84
- model_name = model_mapping.get(chat_request.model, chat_request.model)
85
- credits_reduction = {
86
- "gpt-4o": 1,
87
- "claude-3-sonnet-20240229": 1,
88
- "gemini-1.5-pro": 1,
89
- "gemini-1-5-flash": 1,
90
- "meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo": 1,
91
- "o1-mini": 2,
92
- "o1-preview": 3,
93
- }.get(model_name, 0)
94
-
95
- if chat_request.stream:
96
- def generate():
97
- try:
98
- for chunk in API_Inference(messages, model=model_name, stream=True,
99
- max_tokens=chat_request.max_tokens,
100
- temperature=chat_request.temperature,
101
- top_p=chat_request.top_p):
102
- data = json.dumps({'choices': [{'delta': {'content': chunk}}]})
103
- yield f"data: {data}\n\n"
104
- yield f"data: [DONE]\n\nCredits used: {credits_reduction}\n\n"
105
- update_request_count(api_key, credits_reduction)
106
- except Exception as e:
107
- yield f"data: [ERROR] {str(e)}\n\n"
108
-
109
- return Response(generate(), mimetype='text/event-stream')
110
- else:
111
- response = API_Inference(messages, model=model_name, stream=False,
112
- max_tokens=chat_request.max_tokens,
113
- temperature=chat_request.temperature,
114
- top_p=chat_request.top_p)
115
- update_request_count(api_key, credits_reduction)
116
- prompt_tokens = sum(len(msg['content'].split()) for msg in messages)
117
- completion_tokens = len(response.split())
118
- total_tokens = prompt_tokens + completion_tokens
119
- return jsonify({
120
- "id": f"chatcmpl-{str(uuid.uuid4())}",
121
- "object": "chat.completion",
122
- "created": int(uuid.uuid1().time // 1e7),
123
- "model": model_name,
124
- "choices": [
125
- {
126
- "index": 0,
127
- "message": {
128
- "role": "assistant",
129
- "content": response
130
- },
131
- "finish_reason": "stop"
132
- }
133
- ],
134
- "usage": {
135
- "prompt_tokens": prompt_tokens,
136
- "completion_tokens": completion_tokens,
137
- "total_tokens": total_tokens
138
- },
139
- "credits_used": credits_reduction
140
- })
141
- except Exception as e:
142
- return jsonify({'detail': str(e)}), 500
 
143
 
144
  @app.route('/rate_limit/status', methods=['GET'])
145
  @requires_api_key
 
53
  @app.route('/v1/chat/completions', methods=['POST'])
54
  @requires_api_key
55
  def chat_completions(api_key):
56
+ return jsonify({'detail': "YOUUUUUUUUUU"), 500
57
+ # logging.info("Received request for chat completions")
58
+ # print("requess received")
59
+ # try:
60
+ # logging.info("Received request for chat completions")
61
+ # # Parse and validate request data
62
+ # try:
63
+ # data = request.get_json()
64
+ # chat_request = ChatCompletionRequest(**data)
65
+ # except ValidationError as e:
66
+ # return jsonify({'detail': e.errors()}), 400
67
+
68
+ # # Check API key validity and rate limit
69
+ # is_valid, error_message = check_api_key_validity(api_key)
70
+ # if not is_valid:
71
+ # return jsonify({'detail': error_message}), 401
72
+
73
+ # messages = [{"role": msg.role, "content": msg.content} for msg in chat_request.messages]
74
+
75
+ # # Get model info
76
+ # model_info = get_model_info(chat_request.model)
77
+ # if not model_info:
78
+ # return jsonify({'detail': 'Invalid model specified'}), 400
79
+
80
+ # # Model mapping
81
+ # model_mapping = {
82
+ # "meta-llama-405b-turbo": "meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo",
83
+ # "claude-3.5-sonnet": "claude-3-sonnet-20240229",
84
+ # }
85
+ # model_name = model_mapping.get(chat_request.model, chat_request.model)
86
+ # credits_reduction = {
87
+ # "gpt-4o": 1,
88
+ # "claude-3-sonnet-20240229": 1,
89
+ # "gemini-1.5-pro": 1,
90
+ # "gemini-1-5-flash": 1,
91
+ # "meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo": 1,
92
+ # "o1-mini": 2,
93
+ # "o1-preview": 3,
94
+ # }.get(model_name, 0)
95
+
96
+ # if chat_request.stream:
97
+ # def generate():
98
+ # try:
99
+ # for chunk in API_Inference(messages, model=model_name, stream=True,
100
+ # max_tokens=chat_request.max_tokens,
101
+ # temperature=chat_request.temperature,
102
+ # top_p=chat_request.top_p):
103
+ # data = json.dumps({'choices': [{'delta': {'content': chunk}}]})
104
+ # yield f"data: {data}\n\n"
105
+ # yield f"data: [DONE]\n\nCredits used: {credits_reduction}\n\n"
106
+ # update_request_count(api_key, credits_reduction)
107
+ # except Exception as e:
108
+ # yield f"data: [ERROR] {str(e)}\n\n"
109
+
110
+ # return Response(generate(), mimetype='text/event-stream')
111
+ # else:
112
+ # response = API_Inference(messages, model=model_name, stream=False,
113
+ # max_tokens=chat_request.max_tokens,
114
+ # temperature=chat_request.temperature,
115
+ # top_p=chat_request.top_p)
116
+ # update_request_count(api_key, credits_reduction)
117
+ # prompt_tokens = sum(len(msg['content'].split()) for msg in messages)
118
+ # completion_tokens = len(response.split())
119
+ # total_tokens = prompt_tokens + completion_tokens
120
+ # return jsonify({
121
+ # "id": f"chatcmpl-{str(uuid.uuid4())}",
122
+ # "object": "chat.completion",
123
+ # "created": int(uuid.uuid1().time // 1e7),
124
+ # "model": model_name,
125
+ # "choices": [
126
+ # {
127
+ # "index": 0,
128
+ # "message": {
129
+ # "role": "assistant",
130
+ # "content": response
131
+ # },
132
+ # "finish_reason": "stop"
133
+ # }
134
+ # ],
135
+ # "usage": {
136
+ # "prompt_tokens": prompt_tokens,
137
+ # "completion_tokens": completion_tokens,
138
+ # "total_tokens": total_tokens
139
+ # },
140
+ # "credits_used": credits_reduction
141
+ # })
142
+ # except Exception as e:
143
+ # return jsonify({'detail': str(e)}), 500
144
 
145
  @app.route('/rate_limit/status', methods=['GET'])
146
  @requires_api_key