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

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -7
main.py CHANGED
@@ -33,6 +33,9 @@ class Payload(BaseModel):
33
  messages: list
34
  stream: bool
35
 
 
 
 
36
  def generate_search(query: str, stream: bool = True) -> str:
37
  headers = {"User-Agent": ""}
38
  prompt = [
@@ -56,20 +59,22 @@ def generate_search(query: str, stream: bool = True) -> str:
56
  # Collect streamed text content
57
  streaming_text = ""
58
  for value in response.iter_lines(decode_unicode=True, chunk_size=12):
59
- modified_value = re.sub(value)
60
- if modified_value:
61
  try:
62
- json_modified_value = json.loads(modified_value)
63
- content = json_modified_value["choices"][0]["delta"]["content"]
 
 
64
  if stream:
65
- yield f"data: {content}\n\n"
 
66
  streaming_text += content
67
- except:
68
  continue
69
 
70
  if not stream:
71
  yield streaming_text
72
-
73
  @app.get("/searchgpt")
74
  async def search_gpt(q: str, stream: Optional[bool] = False):
75
  if not q:
 
33
  messages: list
34
  stream: bool
35
 
36
+ import requests
37
+ import json
38
+
39
  def generate_search(query: str, stream: bool = True) -> str:
40
  headers = {"User-Agent": ""}
41
  prompt = [
 
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:
76
  yield streaming_text
77
+
78
  @app.get("/searchgpt")
79
  async def search_gpt(q: str, stream: Optional[bool] = False):
80
  if not q: