Shreyas094 commited on
Commit
652197b
·
verified ·
1 Parent(s): 1b0938e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -89,20 +89,26 @@ Write a detailed and complete research document that fulfills the following user
89
  {"role": "user", "content": user_message}
90
  ]
91
 
92
- # Include chat history if provided
93
  if history:
94
  messages = history + messages
95
 
96
  try:
97
  for call in range(num_calls):
98
  try:
99
- for response in client.chat_completion(
100
  messages=messages,
101
  max_tokens=6000,
102
  temperature=temperature,
103
  stream=True,
104
  top_p=0.8,
105
- ):
 
 
 
 
 
 
 
106
  if isinstance(response, dict) and "choices" in response:
107
  for choice in response["choices"]:
108
  if "delta" in choice and "content" in choice["delta"]:
@@ -110,8 +116,11 @@ Write a detailed and complete research document that fulfills the following user
110
  full_response += chunk
111
  yield full_response, ""
112
  else:
113
- logging.error("Unexpected response format or missing attributes in the response object.")
114
- break
 
 
 
115
  except Exception as e:
116
  logging.error(f"Error in API call {call + 1}: {str(e)}")
117
  if "422 Client Error" in str(e):
@@ -119,18 +128,20 @@ Write a detailed and complete research document that fulfills the following user
119
  # You might want to adjust parameters here, e.g., reduce max_tokens
120
  yield f"An error occurred during API call {call + 1}. Retrying...", ""
121
 
122
- # Add a small delay between API calls
123
- await asyncio.sleep(1) # 1 second delay
124
 
125
  except asyncio.CancelledError:
126
  logging.warning("The operation was cancelled.")
127
  yield "The operation was cancelled. Please try again.", ""
 
 
 
128
 
129
  if not full_response:
130
  logging.warning("No response generated from the model")
131
- yield "No response generated from the model.", ""
132
-
133
- yield f"{full_response}\n\nSources:\n{source_list_str}", ""
134
 
135
  async def respond(message, system_prompt, history, model, temperature, num_calls, use_embeddings):
136
  logging.info(f"User Query: {message}")
@@ -166,10 +177,14 @@ async def respond(message, system_prompt, history, model, temperature, num_calls
166
  num_calls=num_calls,
167
  temperature=temperature
168
  ):
169
- # Yield only the new content
170
- new_content = main_content[len(full_response):]
171
- full_response = main_content
172
- yield new_content
 
 
 
 
173
 
174
  # Yield the sources as a separate message
175
  if sources:
 
89
  {"role": "user", "content": user_message}
90
  ]
91
 
 
92
  if history:
93
  messages = history + messages
94
 
95
  try:
96
  for call in range(num_calls):
97
  try:
98
+ response_stream = client.chat_completion(
99
  messages=messages,
100
  max_tokens=6000,
101
  temperature=temperature,
102
  stream=True,
103
  top_p=0.8,
104
+ )
105
+
106
+ if response_stream is None:
107
+ logging.error(f"API call {call + 1} returned None")
108
+ yield "The API returned an empty response. Please try again.", ""
109
+ continue
110
+
111
+ for response in response_stream:
112
  if isinstance(response, dict) and "choices" in response:
113
  for choice in response["choices"]:
114
  if "delta" in choice and "content" in choice["delta"]:
 
116
  full_response += chunk
117
  yield full_response, ""
118
  else:
119
+ logging.error(f"Unexpected response format in API call {call + 1}: {response}")
120
+
121
+ if full_response:
122
+ break # If we got a valid response, exit the loop
123
+
124
  except Exception as e:
125
  logging.error(f"Error in API call {call + 1}: {str(e)}")
126
  if "422 Client Error" in str(e):
 
128
  # You might want to adjust parameters here, e.g., reduce max_tokens
129
  yield f"An error occurred during API call {call + 1}. Retrying...", ""
130
 
131
+ await asyncio.sleep(1) # 1 second delay between calls
 
132
 
133
  except asyncio.CancelledError:
134
  logging.warning("The operation was cancelled.")
135
  yield "The operation was cancelled. Please try again.", ""
136
+ except Exception as e:
137
+ logging.error(f"Unexpected error in get_response_with_search: {str(e)}")
138
+ yield f"An unexpected error occurred: {str(e)}", ""
139
 
140
  if not full_response:
141
  logging.warning("No response generated from the model")
142
+ yield "No response generated from the model. Please try again.", ""
143
+ else:
144
+ yield f"{full_response}\n\nSources:\n{source_list_str}", ""
145
 
146
  async def respond(message, system_prompt, history, model, temperature, num_calls, use_embeddings):
147
  logging.info(f"User Query: {message}")
 
177
  num_calls=num_calls,
178
  temperature=temperature
179
  ):
180
+ if "error" in main_content.lower() or "no response" in main_content.lower():
181
+ # If it's an error message, yield it as is
182
+ yield main_content
183
+ else:
184
+ # Otherwise, yield only the new content
185
+ new_content = main_content[len(full_response):]
186
+ full_response = main_content
187
+ yield new_content
188
 
189
  # Yield the sources as a separate message
190
  if sources: