Spaces:
Running
Running
added another new route for llm
Browse files- __pycache__/app.cpython-312.pyc +0 -0
- app.py +86 -1
- logs.json +155 -0
- test.py +1 -1
__pycache__/app.cpython-312.pyc
CHANGED
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
|
|
app.py
CHANGED
@@ -290,6 +290,91 @@ async def vercelXaigenerate(json_data: Dict[str, Any]):
|
|
290 |
yield f"data: [Connection error: {str(e)}]\n\n"
|
291 |
|
292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
293 |
@app.get("/")
|
294 |
async def index():
|
295 |
return {"status": "ok", "message": "Welcome to the Chipling API!", "version": "1.0", "routes": ["/chat", "/generate-modules", "/generate-topics"]}
|
@@ -323,7 +408,7 @@ async def chat(request: ChatRequest):
|
|
323 |
'stream': True,
|
324 |
}
|
325 |
|
326 |
-
selected_generator = random.choice([
|
327 |
log_request("/chat", selected_generator.__name__)
|
328 |
return StreamingResponse(selected_generator(json_data), media_type='text/event-stream')
|
329 |
|
|
|
290 |
yield f"data: [Connection error: {str(e)}]\n\n"
|
291 |
|
292 |
|
293 |
+
async def vercelGroqgenerate(json_data: Dict[str, Any]):
|
294 |
+
headers = {
|
295 |
+
'accept': '*/*',
|
296 |
+
'accept-language': 'en-US,en;q=0.9,ja;q=0.8',
|
297 |
+
'content-type': 'application/json',
|
298 |
+
'origin': 'https://ai-sdk-starter-groq.vercel.app',
|
299 |
+
'priority': 'u=1, i',
|
300 |
+
'referer': 'https://ai-sdk-starter-groq.vercel.app/',
|
301 |
+
'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
|
302 |
+
'sec-ch-ua-mobile': '?0',
|
303 |
+
'sec-ch-ua-platform': '"macOS"',
|
304 |
+
'sec-fetch-dest': 'empty',
|
305 |
+
'sec-fetch-mode': 'cors',
|
306 |
+
'sec-fetch-site': 'same-origin',
|
307 |
+
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
|
308 |
+
}
|
309 |
+
|
310 |
+
messages = conver_to_xai_schema(json_data["messages"])
|
311 |
+
|
312 |
+
request_data = {
|
313 |
+
"id": "".join(random.choices("0123456789abcdef", k=16)),
|
314 |
+
"messages": messages,
|
315 |
+
"selectedModel": "deepseek-r1-distill-llama-70b"
|
316 |
+
}
|
317 |
+
|
318 |
+
chunk_id = "vercel-groq-" + "".join(random.choices("0123456789abcdef", k=32))
|
319 |
+
created = int(asyncio.get_event_loop().time())
|
320 |
+
total_tokens = 0
|
321 |
+
|
322 |
+
try:
|
323 |
+
async with httpx.AsyncClient(timeout=None) as client:
|
324 |
+
async with client.stream(
|
325 |
+
"POST",
|
326 |
+
"https://ai-sdk-starter-groq.vercel.app/api/chat",
|
327 |
+
headers=headers,
|
328 |
+
json=request_data
|
329 |
+
) as request_ctx:
|
330 |
+
print(request_ctx.status_code)
|
331 |
+
if request_ctx.status_code == 200:
|
332 |
+
async for line in request_ctx.aiter_lines():
|
333 |
+
if line:
|
334 |
+
if line.startswith('0:'):
|
335 |
+
# Clean up the text and properly escape JSON characters
|
336 |
+
text = line[2:].strip()
|
337 |
+
if text.startswith('"') and text.endswith('"'):
|
338 |
+
text = text[1:-1]
|
339 |
+
text = text.replace('\\n', '\n').replace('\\', '')
|
340 |
+
|
341 |
+
response = {
|
342 |
+
"id": chunk_id,
|
343 |
+
"object": "chat.completion.chunk",
|
344 |
+
"created": created,
|
345 |
+
"model": json_data.get("model", "grok-2-1212"),
|
346 |
+
"choices": [{
|
347 |
+
"index": 0,
|
348 |
+
"text": text,
|
349 |
+
"logprobs": None,
|
350 |
+
"finish_reason": None
|
351 |
+
}],
|
352 |
+
"usage": None
|
353 |
+
}
|
354 |
+
yield f"data: {json.dumps(response)}\n\n"
|
355 |
+
total_tokens += 1
|
356 |
+
elif line.startswith('d:'):
|
357 |
+
final = {
|
358 |
+
"id": chunk_id,
|
359 |
+
"object": "chat.completion.chunk",
|
360 |
+
"created": created,
|
361 |
+
"model": json_data.get("model", "llama-8b"),
|
362 |
+
"choices": [],
|
363 |
+
"usage": {
|
364 |
+
"prompt_tokens": len(messages),
|
365 |
+
"completion_tokens": total_tokens,
|
366 |
+
"total_tokens": len(messages) + total_tokens
|
367 |
+
}
|
368 |
+
}
|
369 |
+
yield f"data: {json.dumps(final)}\n\n"
|
370 |
+
yield "data: [DONE]\n\n"
|
371 |
+
return
|
372 |
+
else:
|
373 |
+
yield f"data: [Unexpected status code: {request_ctx.status_code}]\n\n"
|
374 |
+
except Exception as e:
|
375 |
+
yield f"data: [Connection error: {str(e)}]\n\n"
|
376 |
+
|
377 |
+
|
378 |
@app.get("/")
|
379 |
async def index():
|
380 |
return {"status": "ok", "message": "Welcome to the Chipling API!", "version": "1.0", "routes": ["/chat", "/generate-modules", "/generate-topics"]}
|
|
|
408 |
'stream': True,
|
409 |
}
|
410 |
|
411 |
+
selected_generator = random.choice([generate, groqgenerate, vercelGroqgenerate, vercelXaigenerate])
|
412 |
log_request("/chat", selected_generator.__name__)
|
413 |
return StreamingResponse(selected_generator(json_data), media_type='text/event-stream')
|
414 |
|
logs.json
CHANGED
@@ -158,5 +158,160 @@
|
|
158 |
"timestamp": "2025-04-12T12:06:56.874556",
|
159 |
"endpoint": "/chat",
|
160 |
"query": "vercelXaigenerate"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
}
|
162 |
]
|
|
|
158 |
"timestamp": "2025-04-12T12:06:56.874556",
|
159 |
"endpoint": "/chat",
|
160 |
"query": "vercelXaigenerate"
|
161 |
+
},
|
162 |
+
{
|
163 |
+
"timestamp": "2025-04-12T12:12:39.624859",
|
164 |
+
"endpoint": "/chat",
|
165 |
+
"query": "groqgenerate"
|
166 |
+
},
|
167 |
+
{
|
168 |
+
"timestamp": "2025-04-12T12:12:44.753091",
|
169 |
+
"endpoint": "/chat",
|
170 |
+
"query": "generate"
|
171 |
+
},
|
172 |
+
{
|
173 |
+
"timestamp": "2025-04-12T12:12:49.680739",
|
174 |
+
"endpoint": "/chat",
|
175 |
+
"query": "vercelXaigenerate"
|
176 |
+
},
|
177 |
+
{
|
178 |
+
"timestamp": "2025-04-12T12:12:57.229754",
|
179 |
+
"endpoint": "/chat",
|
180 |
+
"query": "groqgenerate"
|
181 |
+
},
|
182 |
+
{
|
183 |
+
"timestamp": "2025-04-12T12:13:00.767253",
|
184 |
+
"endpoint": "/chat",
|
185 |
+
"query": "generate"
|
186 |
+
},
|
187 |
+
{
|
188 |
+
"timestamp": "2025-04-12T12:13:03.882938",
|
189 |
+
"endpoint": "/chat",
|
190 |
+
"query": "groqgenerate"
|
191 |
+
},
|
192 |
+
{
|
193 |
+
"timestamp": "2025-04-12T12:13:07.323707",
|
194 |
+
"endpoint": "/chat",
|
195 |
+
"query": "vercelXaigenerate"
|
196 |
+
},
|
197 |
+
{
|
198 |
+
"timestamp": "2025-04-12T12:13:15.612926",
|
199 |
+
"endpoint": "/chat",
|
200 |
+
"query": "generate"
|
201 |
+
},
|
202 |
+
{
|
203 |
+
"timestamp": "2025-04-12T12:13:17.961841",
|
204 |
+
"endpoint": "/chat",
|
205 |
+
"query": "groqgenerate"
|
206 |
+
},
|
207 |
+
{
|
208 |
+
"timestamp": "2025-04-12T12:13:22.519624",
|
209 |
+
"endpoint": "/chat",
|
210 |
+
"query": "groqgenerate"
|
211 |
+
},
|
212 |
+
{
|
213 |
+
"timestamp": "2025-04-12T12:13:31.175155",
|
214 |
+
"endpoint": "/chat",
|
215 |
+
"query": "vercelXaigenerate"
|
216 |
+
},
|
217 |
+
{
|
218 |
+
"timestamp": "2025-04-12T12:13:38.396994",
|
219 |
+
"endpoint": "/chat",
|
220 |
+
"query": "groqgenerate"
|
221 |
+
},
|
222 |
+
{
|
223 |
+
"timestamp": "2025-04-12T12:13:42.230876",
|
224 |
+
"endpoint": "/chat",
|
225 |
+
"query": "generate"
|
226 |
+
},
|
227 |
+
{
|
228 |
+
"timestamp": "2025-04-12T12:13:45.023945",
|
229 |
+
"endpoint": "/chat",
|
230 |
+
"query": "groqgenerate"
|
231 |
+
},
|
232 |
+
{
|
233 |
+
"timestamp": "2025-04-12T12:13:48.831325",
|
234 |
+
"endpoint": "/chat",
|
235 |
+
"query": "generate"
|
236 |
+
},
|
237 |
+
{
|
238 |
+
"timestamp": "2025-04-12T12:13:51.526062",
|
239 |
+
"endpoint": "/chat",
|
240 |
+
"query": "groqgenerate"
|
241 |
+
},
|
242 |
+
{
|
243 |
+
"timestamp": "2025-04-12T12:13:56.833256",
|
244 |
+
"endpoint": "/chat",
|
245 |
+
"query": "groqgenerate"
|
246 |
+
},
|
247 |
+
{
|
248 |
+
"timestamp": "2025-04-12T12:14:02.725289",
|
249 |
+
"endpoint": "/chat",
|
250 |
+
"query": "vercelXaigenerate"
|
251 |
+
},
|
252 |
+
{
|
253 |
+
"timestamp": "2025-04-12T12:14:12.979970",
|
254 |
+
"endpoint": "/chat",
|
255 |
+
"query": "generate"
|
256 |
+
},
|
257 |
+
{
|
258 |
+
"timestamp": "2025-04-12T12:14:34.965307",
|
259 |
+
"endpoint": "/chat",
|
260 |
+
"query": "vercelGroqgenerate"
|
261 |
+
},
|
262 |
+
{
|
263 |
+
"timestamp": "2025-04-12T12:14:41.576413",
|
264 |
+
"endpoint": "/chat",
|
265 |
+
"query": "vercelGroqgenerate"
|
266 |
+
},
|
267 |
+
{
|
268 |
+
"timestamp": "2025-04-12T12:15:29.506084",
|
269 |
+
"endpoint": "/chat",
|
270 |
+
"query": "vercelGroqgenerate"
|
271 |
+
},
|
272 |
+
{
|
273 |
+
"timestamp": "2025-04-12T12:17:00.139827",
|
274 |
+
"endpoint": "/chat",
|
275 |
+
"query": "vercelGroqgenerate"
|
276 |
+
},
|
277 |
+
{
|
278 |
+
"timestamp": "2025-04-12T12:18:07.735115",
|
279 |
+
"endpoint": "/chat",
|
280 |
+
"query": "vercelGroqgenerate"
|
281 |
+
},
|
282 |
+
{
|
283 |
+
"timestamp": "2025-04-12T12:19:21.687931",
|
284 |
+
"endpoint": "/chat",
|
285 |
+
"query": "vercelGroqgenerate"
|
286 |
+
},
|
287 |
+
{
|
288 |
+
"timestamp": "2025-04-12T12:22:30.826896",
|
289 |
+
"endpoint": "/chat",
|
290 |
+
"query": "vercelGroqgenerate"
|
291 |
+
},
|
292 |
+
{
|
293 |
+
"timestamp": "2025-04-12T12:22:33.378368",
|
294 |
+
"endpoint": "/chat",
|
295 |
+
"query": "vercelGroqgenerate"
|
296 |
+
},
|
297 |
+
{
|
298 |
+
"timestamp": "2025-04-12T12:22:35.429037",
|
299 |
+
"endpoint": "/chat",
|
300 |
+
"query": "vercelGroqgenerate"
|
301 |
+
},
|
302 |
+
{
|
303 |
+
"timestamp": "2025-04-12T12:22:37.216791",
|
304 |
+
"endpoint": "/chat",
|
305 |
+
"query": "vercelGroqgenerate"
|
306 |
+
},
|
307 |
+
{
|
308 |
+
"timestamp": "2025-04-12T12:23:02.229775",
|
309 |
+
"endpoint": "/chat",
|
310 |
+
"query": "vercelGroqgenerate"
|
311 |
+
},
|
312 |
+
{
|
313 |
+
"timestamp": "2025-04-12T12:23:41.708982",
|
314 |
+
"endpoint": "/chat",
|
315 |
+
"query": "vercelGroqgenerate"
|
316 |
}
|
317 |
]
|
test.py
CHANGED
@@ -5,7 +5,7 @@ url = "http://127.0.0.1:8000/chat"
|
|
5 |
|
6 |
payload = {
|
7 |
"model": "your-model-name",
|
8 |
-
"message": "Create a json object of top 10 anime
|
9 |
"messages": []
|
10 |
}
|
11 |
|
|
|
5 |
|
6 |
payload = {
|
7 |
"model": "your-model-name",
|
8 |
+
"message": "Create a json object of top 10 anime! answer in json only!",
|
9 |
"messages": []
|
10 |
}
|
11 |
|