ParthSadaria commited on
Commit
ae4e87c
·
verified ·
1 Parent(s): ae066fd

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +6 -9
main.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import os
3
  from dotenv import load_dotenv
4
  from fastapi import FastAPI, HTTPException, Request
@@ -59,17 +58,15 @@ def generate_search(query: str, stream: bool = True) -> str:
59
  # Collect streamed text content
60
  streaming_text = ""
61
  for value in response.iter_lines(decode_unicode=True, chunk_size=12):
62
- if value:
 
63
  try:
64
- json_value = json.loads(value) # Process the JSON content directly
65
- content = json_value["choices"][0]["delta"]["content"]
66
-
67
- # Stream the raw content directly (no 'data:' prefix)
68
  if stream:
69
- yield content
70
-
71
  streaming_text += content
72
- except json.JSONDecodeError:
73
  continue
74
 
75
  if not stream:
 
 
1
  import os
2
  from dotenv import load_dotenv
3
  from fastapi import FastAPI, HTTPException, Request
 
58
  # Collect streamed text content
59
  streaming_text = ""
60
  for value in response.iter_lines(decode_unicode=True, chunk_size=12):
61
+ modified_value = re.sub("data:", "", value)
62
+ if modified_value:
63
  try:
64
+ json_modified_value = json.loads(modified_value)
65
+ content = json_modified_value["choices"][0]["delta"]["content"]
 
 
66
  if stream:
67
+ yield f"data: {content}\n\n"
 
68
  streaming_text += content
69
+ except:
70
  continue
71
 
72
  if not stream: