Mohit5899 commited on
Commit
1a24190
·
verified ·
1 Parent(s): 14b20cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -116,7 +116,7 @@ def search_duckduckgo(query):
116
  except Exception as e:
117
  return f"Error during DuckDuckGo search: {str(e)}"
118
 
119
- # Function to chat with JSON content using Groq API and DuckDuckGo fallback
120
  def chat_with_json(api_key, model_name, filename, question):
121
  if not api_key or not model_name or not filename or not question:
122
  return "Please provide all required inputs: API key, model, JSON file, and question."
@@ -126,7 +126,16 @@ def chat_with_json(api_key, model_name, filename, question):
126
  with open(filename, 'r') as json_file:
127
  data = json.load(json_file)
128
 
129
- prompt = f"JSON Data:\n{json.dumps(data, indent=2)}\n\nUser question: {question}\n\nAnswer based on the JSON data if possible. If the question cannot be answered based on the JSON, respond with 'SEARCH_WEB'."
 
 
 
 
 
 
 
 
 
130
 
131
  completion = client.chat.completions.create(
132
  model=model_name,
@@ -138,8 +147,9 @@ def chat_with_json(api_key, model_name, filename, question):
138
  )
139
  answer = completion.choices[0].message.content
140
 
141
- if 'SEARCH_WEB' in answer:
142
- return search_duckduckgo(question)
 
143
  return answer
144
 
145
  except (FileNotFoundError, json.JSONDecodeError) as e:
@@ -172,7 +182,7 @@ chat_iface = gr.Interface(
172
  ],
173
  outputs=gr.Markdown(),
174
  title="Chat with JSON and DuckDuckGo",
175
- description="Ask questions about a JSON file. If the answer isn’t in the JSON, it will search DuckDuckGo."
176
  )
177
 
178
  # Launch the tabbed interface
 
116
  except Exception as e:
117
  return f"Error during DuckDuckGo search: {str(e)}"
118
 
119
+ # Function to chat with JSON content using Groq API with smarter handling
120
  def chat_with_json(api_key, model_name, filename, question):
121
  if not api_key or not model_name or not filename or not question:
122
  return "Please provide all required inputs: API key, model, JSON file, and question."
 
126
  with open(filename, 'r') as json_file:
127
  data = json.load(json_file)
128
 
129
+ # Enhanced prompt to guide the LLM
130
+ prompt = (
131
+ f"Here is the JSON data about a company:\n{json.dumps(data, indent=2)}\n\n"
132
+ f"User question: {question}\n\n"
133
+ "Answer the question based solely on the provided JSON data. "
134
+ "If the exact information is not available in the JSON, provide a thoughtful response using related information from the JSON. "
135
+ "For example, if asked about the CEO and the CEO is not listed, state that the CEO is not specified and provide other relevant details like the company's headquarters, founded year, or business description. "
136
+ "Do NOT suggest searching the web unless the user explicitly asks for a web search or the question is completely unrelated to the JSON content (e.g., asking about weather or unrelated topics). "
137
+ "If a web search is needed, respond with 'I can search the web for this information if you'd like. Please confirm.'"
138
+ )
139
 
140
  completion = client.chat.completions.create(
141
  model=model_name,
 
147
  )
148
  answer = completion.choices[0].message.content
149
 
150
+ # Check if the LLM suggests a web search
151
+ if "I can search the web" in answer:
152
+ return answer # Let the user decide if they want a web search
153
  return answer
154
 
155
  except (FileNotFoundError, json.JSONDecodeError) as e:
 
182
  ],
183
  outputs=gr.Markdown(),
184
  title="Chat with JSON and DuckDuckGo",
185
+ description="Ask questions about a JSON file. The system will answer based on the JSON data, providing related insights if the exact answer is missing. Web searches are performed only if explicitly requested."
186
  )
187
 
188
  # Launch the tabbed interface