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