Update main.py
Browse files
main.py
CHANGED
@@ -8,6 +8,7 @@ import uuid
|
|
8 |
import logging
|
9 |
import asyncio
|
10 |
from typing import List, Dict, Optional, Union, Generator, Any, AsyncGenerator
|
|
|
11 |
|
12 |
from fastapi import FastAPI, HTTPException, Depends, Request, status
|
13 |
from fastapi.middleware.cors import CORSMiddleware
|
@@ -149,8 +150,7 @@ class QodoAI:
|
|
149 |
self.url = "https://api.cli.qodo.ai/v2/agentic/start-task"
|
150 |
self.info_url = "https://api.cli.qodo.ai/v2/info/get-things"
|
151 |
self.timeout = timeout
|
152 |
-
self.
|
153 |
-
api_key = api_key or "sk-dS7U-extxMWUxc8SbYYOuncqGUIE8-y2OY8oMCpu0eI-qnSUyH9CYWO_eAMpqwfMo7pXU3QNrclfZYMO0M6BJTM"
|
154 |
|
155 |
# Generate fingerprint
|
156 |
self.fingerprint = {"user_agent": "axios/1.10.0", "browser_type": "chrome"}
|
@@ -188,7 +188,7 @@ api_key = api_key or "sk-dS7U-extxMWUxc8SbYYOuncqGUIE8-y2OY8oMCpu0eI-qnSUyH9CYWO
|
|
188 |
"User-Agent": "axios/1.10.0",
|
189 |
}
|
190 |
|
191 |
-
temp_session.headers.update(temp_headers)
|
192 |
|
193 |
response = temp_session.get(self.info_url, timeout=self.timeout, impersonate="chrome110")
|
194 |
|
@@ -234,8 +234,7 @@ temp_session.headers.update(temp_headers)
|
|
234 |
elif isinstance(chunk, str):
|
235 |
try:
|
236 |
parsed = json.loads(chunk)
|
237 |
-
return QodoAI.
|
238 |
-
ractor(parsed)
|
239 |
except json.JSONDecodeError:
|
240 |
if chunk.strip():
|
241 |
return chunk.strip()
|
@@ -288,7 +287,7 @@ ractor(parsed)
|
|
288 |
if message.role == "user":
|
289 |
user_prompt = message.content
|
290 |
|
291 |
-
break
|
292 |
|
293 |
if not user_prompt:
|
294 |
raise HTTPException(status_code=400, detail="No user message found in messages")
|
@@ -333,7 +332,7 @@ break
|
|
333 |
)
|
334 |
|
335 |
for content_chunk in processed_stream:
|
336 |
-
if content_chunk:
|
337 |
chunk_data = {
|
338 |
"id": request_id,
|
339 |
"object": "chat.completion.chunk",
|
@@ -382,8 +381,7 @@ if content_chunk:
|
|
382 |
|
383 |
except Exception as e:
|
384 |
logger.error(f"Stream creation error: {e}")
|
385 |
-
raise HTTPException(status_code=
|
386 |
-
500, detail=str(e))
|
387 |
|
388 |
async def _create_non_stream_response(self, request_id: str, created_time: int, model: str, payload: Dict[str, Any], user_prompt: str) -> ChatCompletionResponse:
|
389 |
"""Create non-streaming response."""
|
@@ -430,7 +428,7 @@ if content_chunk:
|
|
430 |
json_obj = json.loads(json_str)
|
431 |
content = QodoAI._qodo_extractor(json_obj)
|
432 |
if content:
|
433 |
-
full_response += content
|
434 |
except json.JSONDecodeError:
|
435 |
pass
|
436 |
|
@@ -489,8 +487,7 @@ app.add_middleware(
|
|
489 |
allow_origins=["*"],
|
490 |
allow_credentials=True,
|
491 |
allow_methods=["*"],
|
492 |
-
|
493 |
-
allow_headers=["*"],
|
494 |
)
|
495 |
|
496 |
# Authentication dependency
|
@@ -546,8 +543,7 @@ async def list_models(api_key: str = Depends(verify_api_key)):
|
|
546 |
|
547 |
@app.post("/v1/chat/completions")
|
548 |
async def create_chat_completion(
|
549 |
-
request:
|
550 |
-
Request,
|
551 |
api_key: str = Depends(verify_api_key)
|
552 |
):
|
553 |
"""Create a chat completion."""
|
@@ -600,8 +596,7 @@ async def log_requests(request: Request, call_next):
|
|
600 |
# Log request
|
601 |
logger.info(f"{request.method} {request.url.path} - Start")
|
602 |
|
603 |
-
response
|
604 |
-
= await call_next(request)
|
605 |
|
606 |
# Log response
|
607 |
process_time = time.time() - start_time
|
|
|
8 |
import logging
|
9 |
import asyncio
|
10 |
from typing import List, Dict, Optional, Union, Generator, Any, AsyncGenerator
|
11 |
+
from contextlib import asynccontextmanager
|
12 |
|
13 |
from fastapi import FastAPI, HTTPException, Depends, Request, status
|
14 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
150 |
self.url = "https://api.cli.qodo.ai/v2/agentic/start-task"
|
151 |
self.info_url = "https://api.cli.qodo.ai/v2/info/get-things"
|
152 |
self.timeout = timeout
|
153 |
+
self.api_key = api_key
|
|
|
154 |
|
155 |
# Generate fingerprint
|
156 |
self.fingerprint = {"user_agent": "axios/1.10.0", "browser_type": "chrome"}
|
|
|
188 |
"User-Agent": "axios/1.10.0",
|
189 |
}
|
190 |
|
191 |
+
temp_session.headers.update(temp_headers)
|
192 |
|
193 |
response = temp_session.get(self.info_url, timeout=self.timeout, impersonate="chrome110")
|
194 |
|
|
|
234 |
elif isinstance(chunk, str):
|
235 |
try:
|
236 |
parsed = json.loads(chunk)
|
237 |
+
return QodoAI._qodo_extractor(parsed)
|
|
|
238 |
except json.JSONDecodeError:
|
239 |
if chunk.strip():
|
240 |
return chunk.strip()
|
|
|
287 |
if message.role == "user":
|
288 |
user_prompt = message.content
|
289 |
|
290 |
+
break
|
291 |
|
292 |
if not user_prompt:
|
293 |
raise HTTPException(status_code=400, detail="No user message found in messages")
|
|
|
332 |
)
|
333 |
|
334 |
for content_chunk in processed_stream:
|
335 |
+
if content_chunk:
|
336 |
chunk_data = {
|
337 |
"id": request_id,
|
338 |
"object": "chat.completion.chunk",
|
|
|
381 |
|
382 |
except Exception as e:
|
383 |
logger.error(f"Stream creation error: {e}")
|
384 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
385 |
|
386 |
async def _create_non_stream_response(self, request_id: str, created_time: int, model: str, payload: Dict[str, Any], user_prompt: str) -> ChatCompletionResponse:
|
387 |
"""Create non-streaming response."""
|
|
|
428 |
json_obj = json.loads(json_str)
|
429 |
content = QodoAI._qodo_extractor(json_obj)
|
430 |
if content:
|
431 |
+
full_response += content
|
432 |
except json.JSONDecodeError:
|
433 |
pass
|
434 |
|
|
|
487 |
allow_origins=["*"],
|
488 |
allow_credentials=True,
|
489 |
allow_methods=["*"],
|
490 |
+
allow_headers=["*"],
|
|
|
491 |
)
|
492 |
|
493 |
# Authentication dependency
|
|
|
543 |
|
544 |
@app.post("/v1/chat/completions")
|
545 |
async def create_chat_completion(
|
546 |
+
request: ChatCompletionRequest,
|
|
|
547 |
api_key: str = Depends(verify_api_key)
|
548 |
):
|
549 |
"""Create a chat completion."""
|
|
|
596 |
# Log request
|
597 |
logger.info(f"{request.method} {request.url.path} - Start")
|
598 |
|
599 |
+
response = await call_next(request)
|
|
|
600 |
|
601 |
# Log response
|
602 |
process_time = time.time() - start_time
|