AbuAlone09 commited on
Commit
b273b06
·
verified ·
1 Parent(s): c36bf37

Update backend_api.py

Browse files
Files changed (1) hide show
  1. backend_api.py +35 -2
backend_api.py CHANGED
@@ -845,14 +845,47 @@ async def generate_code(
845
  {"role": "user", "content": user_content}
846
  ]
847
 
848
- # Tìm đoạn "try: # Stream the response" trong backend_api.py và thay bằng:
 
 
849
  try:
850
- # Sử dụng Gemini stream
851
  response = client.generate_content(
852
  f"Generate a {language} application: {query}",
853
  stream=True
854
  )
855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
856
  for chunk in response:
857
  if chunk.text:
858
  chunk_content = chunk.text
 
845
  {"role": "user", "content": user_content}
846
  ]
847
 
848
+ # ... (Các phần khởi tạo trước đó giữ nguyên)
849
+
850
+ # GỌI GEMINI VÀ XỬ LÝ STREAM
851
  try:
852
+ # 1. Gọi Gemini
853
  response = client.generate_content(
854
  f"Generate a {language} application: {query}",
855
  stream=True
856
  )
857
 
858
+ # 2. Xử lý stream từ Gemini
859
+ for chunk in response:
860
+ if chunk.text:
861
+ chunk_content = chunk.text
862
+ generated_code += chunk_content
863
+
864
+ # Gửi chunk về frontend
865
+ event_data = json.dumps({
866
+ "type": "chunk",
867
+ "content": chunk_content
868
+ })
869
+ yield f"data: {event_data}\n\n"
870
+
871
+ # 3. Gửi sự kiện hoàn tất (Completion)
872
+ # Lưu ý: Không cần biến 'stream' cũ nữa
873
+ reasoning = extract_reasoning(generated_code, language)
874
+ generated_code = cleanup_generated_code(generated_code, language)
875
+
876
+ completion_dict = {
877
+ "type": "complete",
878
+ "code": generated_code
879
+ }
880
+ # Thêm logic reasoning nếu cần
881
+ if selected_model_id == "zai-org/GLM-4.7" and reasoning:
882
+ completion_dict["reasoning"] = reasoning
883
+
884
+ completion_data = json.dumps(completion_dict)
885
+ yield f"data: {completion_data}\n\n"
886
+
887
+ # ... (Phần logic Auto-Deploy giữ nguyên như cũ) ...
888
+
889
  for chunk in response:
890
  if chunk.text:
891
  chunk_content = chunk.text