Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 142 |
-
|
|
|
|
| 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.
|
| 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
|