ParthSadaria commited on
Commit
8ce4a3a
·
verified ·
1 Parent(s): 4e5813e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -16
main.py CHANGED
@@ -39,8 +39,6 @@ async def favicon():
39
  favicon_path = Path(__file__).parent / "favicon.ico"
40
  return FileResponse(favicon_path, media_type="image/x-icon")
41
 
42
- import requests
43
- import json
44
 
45
  def generate_search(query: str, stream: bool = True) -> str:
46
  headers = {"User-Agent": ""}
@@ -48,7 +46,6 @@ def generate_search(query: str, stream: bool = True) -> str:
48
  {"role": "user", "content": query},
49
  ]
50
 
51
- # Insert the system prompt at the beginning of the conversation history
52
  prompt.insert(0, {"content": "Be Helpful and Friendly", "role": "system"})
53
 
54
  payload = {
@@ -58,30 +55,28 @@ def generate_search(query: str, stream: bool = True) -> str:
58
  "user_input": prompt[-1]["content"],
59
  }
60
 
61
- # Use the newly added SECRET_API_ENDPOINT_3 for the search API call
62
  chat_endpoint = secret_api_endpoint_3
63
  response = requests.post(chat_endpoint, headers=headers, json=payload, stream=True)
64
 
65
- # Collect streamed text content
66
  streaming_text = ""
67
  for value in response.iter_lines(decode_unicode=True):
68
- # Ensure the value starts with 'data: ' and process it
69
  if value.startswith("data: "):
70
  try:
71
- # Parse JSON and remove 'data: ' prefix
72
  json_modified_value = json.loads(value[6:])
73
- content = json_modified_value.get("content", "") # Get the actual content
74
-
75
- if content.strip(): # Ensure non-empty content
76
- if stream:
77
- yield content # Pass content directly, removing 'data: ' and JSON overhead
78
- streaming_text += content # Accumulate content for non-streaming case
79
  except json.JSONDecodeError:
80
- continue # Skip lines that are not valid JSON
81
 
82
- # If not streaming, yield the full collected content
83
  if not stream:
84
- yield streaming_text
 
 
 
 
 
85
  @app.get("/searchgpt")
86
  async def search_gpt(q: str, stream: Optional[bool] = False):
87
  if not q:
 
39
  favicon_path = Path(__file__).parent / "favicon.ico"
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": ""}
 
46
  {"role": "user", "content": query},
47
  ]
48
 
 
49
  prompt.insert(0, {"content": "Be Helpful and Friendly", "role": "system"})
50
 
51
  payload = {
 
55
  "user_input": prompt[-1]["content"],
56
  }
57
 
 
58
  chat_endpoint = secret_api_endpoint_3
59
  response = requests.post(chat_endpoint, headers=headers, json=payload, stream=True)
60
 
 
61
  streaming_text = ""
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({'choices': [{'delta': {'content': content}}]})}\n\n"
69
+ streaming_text += content
 
 
70
  except json.JSONDecodeError:
71
+ continue
72
 
 
73
  if not stream:
74
+ yield json.dumps({
75
+ "choices": [{
76
+ "message": {"content": streaming_text},
77
+ "finish_reason": "stop"
78
+ }]
79
+ })
80
  @app.get("/searchgpt")
81
  async def search_gpt(q: str, stream: Optional[bool] = False):
82
  if not q: