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

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +7 -7
main.py CHANGED
@@ -58,19 +58,19 @@ def generate_search(query: str, stream: bool = True) -> str:
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
 
 
58
  # Collect streamed text content
59
  streaming_text = ""
60
  for value in response.iter_lines(decode_unicode=True):
61
+ # Ensure the value starts with 'data: ' and process it
62
+ if value.startswith("data: "):
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
+ # Include everything, even if it's just whitespace
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 collected content
74
  if not stream:
75
  yield streaming_text
76