Shreyas094 commited on
Commit
15f3dd6
·
verified ·
1 Parent(s): cd561a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -100,6 +100,8 @@ def create_web_search_vectors(search_results):
100
 
101
  return FAISS.from_documents(documents, embed)
102
 
 
 
103
  def get_response_with_search(query, model, num_calls=3, temperature=0.2):
104
  search_results = duckduckgo_search(query)
105
  web_search_database = create_web_search_vectors(search_results)
@@ -132,16 +134,38 @@ After writing the document, please provide a list of sources used in your respon
132
 
133
  main_content = ""
134
  for i in range(num_calls):
135
- for message in client.chat_completion(
136
- messages=[{"role": "user", "content": prompt}],
137
- max_tokens=max_new_tokens,
138
- temperature=temperature,
139
- stream=False,
140
- ):
141
- if message.choices and message.choices[0].delta and message.choices[0].delta.content:
142
- chunk = message.choices[0].delta.content
143
- main_content += chunk
144
- yield main_content, "" # Yield partial main content without sources
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  def vote(data: gr.LikeData):
147
  if data.liked:
 
100
 
101
  return FAISS.from_documents(documents, embed)
102
 
103
+ import json
104
+
105
  def get_response_with_search(query, model, num_calls=3, temperature=0.2):
106
  search_results = duckduckgo_search(query)
107
  web_search_database = create_web_search_vectors(search_results)
 
134
 
135
  main_content = ""
136
  for i in range(num_calls):
137
+ try:
138
+ response = client.chat_completion(
139
+ messages=[{"role": "user", "content": prompt}],
140
+ max_new_tokens=max_new_tokens,
141
+ temperature=temperature,
142
+ stream=False,
143
+ )
144
+
145
+ # Log the raw response for debugging
146
+ logging.info(f"Raw API response: {response}")
147
+
148
+ # Check if the response is a string (which might be an error message)
149
+ if isinstance(response, str):
150
+ logging.error(f"API returned an unexpected string response: {response}")
151
+ yield f"An error occurred: {response}", ""
152
+ return
153
+
154
+ # If it's not a string, assume it's the expected object structure
155
+ if hasattr(response, 'choices') and response.choices:
156
+ for choice in response.choices:
157
+ if hasattr(choice, 'message') and hasattr(choice.message, 'content'):
158
+ chunk = choice.message.content
159
+ main_content += chunk
160
+ yield main_content, "" # Yield partial main content without sources
161
+ else:
162
+ logging.error(f"Unexpected response structure: {response}")
163
+ yield "An unexpected error occurred. Please try again.", ""
164
+
165
+ except Exception as e:
166
+ logging.error(f"Error in API call: {str(e)}")
167
+ yield f"An error occurred: {str(e)}", ""
168
+ return
169
 
170
  def vote(data: gr.LikeData):
171
  if data.liked: