ParthSadaria commited on
Commit
1975e72
·
verified ·
1 Parent(s): 7bee578

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -4
main.py CHANGED
@@ -40,12 +40,13 @@ async def favicon():
40
  return FileResponse(favicon_path, media_type="image/x-icon")
41
 
42
 
43
- def generate_search(query: str, stream: bool = True) -> str:
44
- headers = {"User-Agent": ""}
45
  prompt = [
46
  {"role": "user", "content": query},
47
  ]
48
 
 
49
  prompt.insert(0, {"content": "Be Helpful and Friendly", "role": "system"})
50
 
51
  payload = {
@@ -62,16 +63,21 @@ def generate_search(query: str, stream: bool = True) -> str:
62
  for value in response.iter_lines(decode_unicode=True):
63
  if value.startswith("data: "):
64
  try:
 
65
  json_modified_value = json.loads(value[6:])
66
- content = json_modified_value.get("choices", [{}])[0].get("delta", {}).get("content", "")
 
 
67
  if stream:
68
  yield f"data: {json.dumps(json_modified_value)}\n\n"
 
 
69
  streaming_text += content
70
  except json.JSONDecodeError:
71
  continue
72
 
73
  if not stream:
74
- # For non-streaming, just yield the text content
75
  yield streaming_text
76
 
77
  @app.get("/searchgpt")
@@ -80,6 +86,7 @@ async def search_gpt(q: str, stream: Optional[bool] = False):
80
  raise HTTPException(status_code=400, detail="Query parameter 'q' is required")
81
 
82
  if stream:
 
83
  return StreamingResponse(
84
  generate_search(q, stream=True),
85
  media_type="text/event-stream"
 
40
  return FileResponse(favicon_path, media_type="image/x-icon")
41
 
42
 
43
+ def generate_search(query: str, stream: bool = True):
44
+ headers = {"User-Agent": "Your-User-Agent"}
45
  prompt = [
46
  {"role": "user", "content": query},
47
  ]
48
 
49
+ # Adding system message at the start
50
  prompt.insert(0, {"content": "Be Helpful and Friendly", "role": "system"})
51
 
52
  payload = {
 
63
  for value in response.iter_lines(decode_unicode=True):
64
  if value.startswith("data: "):
65
  try:
66
+ # Parse JSON and get the desired structure
67
  json_modified_value = json.loads(value[6:])
68
+ content = json_modified_value.get("choices", [{}])[0].get("message", {}).get("content", "")
69
+
70
+ # Stream the response with 'data:' prefix if streaming is enabled
71
  if stream:
72
  yield f"data: {json.dumps(json_modified_value)}\n\n"
73
+
74
+ # Accumulate content for non-streaming
75
  streaming_text += content
76
  except json.JSONDecodeError:
77
  continue
78
 
79
  if not stream:
80
+ # For non-streaming, return the accumulated text content
81
  yield streaming_text
82
 
83
  @app.get("/searchgpt")
 
86
  raise HTTPException(status_code=400, detail="Query parameter 'q' is required")
87
 
88
  if stream:
89
+ # Stream the response with 'data:' prefix
90
  return StreamingResponse(
91
  generate_search(q, stream=True),
92
  media_type="text/event-stream"