AbuAlone09 commited on
Commit
0de18da
·
verified ·
1 Parent(s): 1c06a35

Update backend_api.py

Browse files
Files changed (1) hide show
  1. backend_api.py +16 -11
backend_api.py CHANGED
@@ -743,29 +743,34 @@ def extract_reasoning(code: str, language: str) -> str:
743
 
744
  @app.post("/api/generate")
745
  async def generate_code(request: CodeGenerationRequest, authorization: Optional[str] = Header(None)):
 
746
  query = request.query
747
  language = request.language
748
- model_id = request.model_id # Ví dụ: "gemini-1.5-flash"
749
 
750
- # Đảm bảo model_id tiền tố 'models/' để tránh lỗi 404
751
- if not model_id.startswith("models/"):
752
- model_id = f"models/{model_id}"
753
-
754
- async def event_stream():
755
  try:
756
- # Dùng client mới để gọi model
757
- # Lưu ý: Thư viện mới dùng model_id trực tiếp, không cần tiền tố 'models/' nếu gọi qua client
 
 
 
758
  response = client.models.generate_content_stream(
759
- model=request.model_id,
760
  contents=full_prompt
761
  )
762
 
763
  for chunk in response:
764
  if chunk.text:
765
  yield f"data: {json.dumps({'type': 'chunk', 'content': chunk.text})}\n\n"
766
-
 
767
  yield f"data: {json.dumps({'type': 'complete'})}\n\n"
768
-
769
  except Exception as e:
770
  yield f"data: {json.dumps({'type': 'error', 'message': str(e)})}\n\n"
771
 
 
743
 
744
  @app.post("/api/generate")
745
  async def generate_code(request: CodeGenerationRequest, authorization: Optional[str] = Header(None)):
746
+ # 1. Lấy dữ liệu từ request
747
  query = request.query
748
  language = request.language
749
+ model_id = request.model_id # Ví dụ: "gemini-3.5-flash"
750
 
751
+ # 2. Định nghĩa system prompt đây để khả dụng trong scope của event_stream
752
+ system_prompt = SYSTEM_PROMPT_CACHE.get(language, GENERIC_SYSTEM_PROMPT.format(language=language))
753
+ full_prompt = f"{system_prompt}\n\nTask: {query}"
754
+
755
+ async def event_stream() -> AsyncGenerator[str, None]:
756
  try:
757
+ # 3. Lấy client từ backend_models
758
+ client = get_genai_client()
759
+
760
+ # 4. Sử dụng client mới để tạo stream
761
+ # Lưu ý: google-genai dùng 'contents', không phải 'full_prompt'
762
  response = client.models.generate_content_stream(
763
+ model=model_id,
764
  contents=full_prompt
765
  )
766
 
767
  for chunk in response:
768
  if chunk.text:
769
  yield f"data: {json.dumps({'type': 'chunk', 'content': chunk.text})}\n\n"
770
+ await asyncio.sleep(0.001)
771
+
772
  yield f"data: {json.dumps({'type': 'complete'})}\n\n"
773
+
774
  except Exception as e:
775
  yield f"data: {json.dumps({'type': 'error', 'message': str(e)})}\n\n"
776