ParthSadaria commited on
Commit
4d88866
·
verified ·
1 Parent(s): 62c15ef

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -10
main.py CHANGED
@@ -57,18 +57,20 @@ def generate_search(query: str, stream: bool = True) -> str:
57
 
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:
73
  yield streaming_text
74
 
 
57
 
58
  # Collect streamed text content
59
  streaming_text = ""
60
+ for value in response.iter_lines(decode_unicode=True):
61
+ # Ensure the value is clean and processable
62
+ if value.startswith("data: "): # Ensure the prefix matches expected format
63
  try:
64
+ json_modified_value = json.loads(value[6:]) # Remove 'data: ' prefix
65
+ content = json_modified_value.get("choices", [{}])[0].get("delta", {}).get("content", "")
66
+ if content.strip(): # Avoid empty content from delta
67
+ if stream:
68
+ yield f"data: {json.dumps({'response': content})}\n\n"
69
+ streaming_text += content
70
+ except json.JSONDecodeError:
71
+ continue # Skip lines that are not valid JSON
72
 
73
+ # If not streaming, yield the full text
74
  if not stream:
75
  yield streaming_text
76