Update app.py
Browse filestrying to implement internet search by default
app.py
CHANGED
@@ -7,7 +7,7 @@ client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
|
|
7 |
def get_internet_data(query: str) -> str:
|
8 |
"""
|
9 |
Uses DuckDuckGo's Instant Answer API to fetch search data.
|
10 |
-
|
11 |
"""
|
12 |
url = "https://api.duckduckgo.com"
|
13 |
params = {
|
@@ -20,11 +20,11 @@ def get_internet_data(query: str) -> str:
|
|
20 |
response = requests.get(url, params=params, timeout=5)
|
21 |
response.raise_for_status()
|
22 |
data = response.json()
|
23 |
-
#
|
24 |
result = data.get("AbstractText", "")
|
25 |
if not result:
|
26 |
result = "Wala koy nakuha nga impormasyon gikan sa internet."
|
27 |
-
except Exception
|
28 |
result = "Naay problema sa pagkuha sa impormasyon gikan sa internet."
|
29 |
return result
|
30 |
|
@@ -41,19 +41,13 @@ def respond(message, history: list[tuple[str, str]]):
|
|
41 |
if assistant_text:
|
42 |
messages.append({"role": "assistant", "content": assistant_text})
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
"role": "assistant",
|
52 |
-
"content": f"Mga resulta gikan sa internet para sa '{query}': {search_result}"
|
53 |
-
})
|
54 |
-
# Optionally, you can clear the original message if it's only a search command:
|
55 |
-
message = ""
|
56 |
-
|
57 |
messages.append({"role": "user", "content": message})
|
58 |
|
59 |
response = ""
|
@@ -69,9 +63,11 @@ def respond(message, history: list[tuple[str, str]]):
|
|
69 |
if not token:
|
70 |
break
|
71 |
response += token
|
|
|
72 |
if response != previous_response:
|
73 |
yield response
|
74 |
previous_response = response
|
|
|
75 |
if len(response) > 3000:
|
76 |
break
|
77 |
|
|
|
7 |
def get_internet_data(query: str) -> str:
|
8 |
"""
|
9 |
Uses DuckDuckGo's Instant Answer API to fetch search data.
|
10 |
+
Returns a short summary of the query result.
|
11 |
"""
|
12 |
url = "https://api.duckduckgo.com"
|
13 |
params = {
|
|
|
20 |
response = requests.get(url, params=params, timeout=5)
|
21 |
response.raise_for_status()
|
22 |
data = response.json()
|
23 |
+
# Retrieve a summary from the API's result
|
24 |
result = data.get("AbstractText", "")
|
25 |
if not result:
|
26 |
result = "Wala koy nakuha nga impormasyon gikan sa internet."
|
27 |
+
except Exception:
|
28 |
result = "Naay problema sa pagkuha sa impormasyon gikan sa internet."
|
29 |
return result
|
30 |
|
|
|
41 |
if assistant_text:
|
42 |
messages.append({"role": "assistant", "content": assistant_text})
|
43 |
|
44 |
+
# Automatically perform an internet search for the user's query.
|
45 |
+
search_result = get_internet_data(message)
|
46 |
+
messages.append({
|
47 |
+
"role": "assistant",
|
48 |
+
"content": f"Mga resulta gikan sa internet para sa '{message}': {search_result}"
|
49 |
+
})
|
50 |
+
# Include the original query as part of the conversation context.
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
messages.append({"role": "user", "content": message})
|
52 |
|
53 |
response = ""
|
|
|
63 |
if not token:
|
64 |
break
|
65 |
response += token
|
66 |
+
# Yield only if new content was added
|
67 |
if response != previous_response:
|
68 |
yield response
|
69 |
previous_response = response
|
70 |
+
# Optional: break if the response is too long
|
71 |
if len(response) > 3000:
|
72 |
break
|
73 |
|