Update app.py
Browse files
app.py
CHANGED
|
@@ -22,16 +22,54 @@ from tools import (
|
|
| 22 |
query_image,
|
| 23 |
query_video
|
| 24 |
)
|
| 25 |
-
|
| 26 |
|
| 27 |
search_tool = DuckDuckGoSearchRun()
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
class BasicAgent:
|
| 33 |
def __init__(self):
|
| 34 |
-
|
| 35 |
|
| 36 |
|
| 37 |
def __call__(self, question, question_attach=""):
|
|
@@ -45,7 +83,7 @@ class BasicAgent:
|
|
| 45 |
question = question[::-1]
|
| 46 |
print(f"Reversed question: {question}")
|
| 47 |
|
| 48 |
-
return self.search_tool(question)
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Error: {str(e)}")
|
|
|
|
| 22 |
query_image,
|
| 23 |
query_video
|
| 24 |
)
|
| 25 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 26 |
|
| 27 |
search_tool = DuckDuckGoSearchRun()
|
| 28 |
+
|
| 29 |
+
"""
|
| 30 |
+
from huggingface_hub import InferenceClient
|
| 31 |
+
|
| 32 |
+
class StrictAnswerAgent:
|
| 33 |
+
def __init__(self):
|
| 34 |
+
|
| 35 |
+
self.llm = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
| 36 |
+
|
| 37 |
+
def _web_search(self, query, num_results=3):
|
| 38 |
+
'Get search results from DuckDuckGo'
|
| 39 |
+
try:
|
| 40 |
+
results = ddg(query, max_results=num_results)
|
| 41 |
+
return [self._clean_text(r['body']) for r in results]
|
| 42 |
+
except:
|
| 43 |
+
return []
|
| 44 |
+
|
| 45 |
+
def answer(self, question):
|
| 46 |
+
# Step 1: Get web search context
|
| 47 |
+
search_results = self._web_search(question)
|
| 48 |
+
context = "\n".join(search_results) if search_results else "No context found"
|
| 49 |
+
|
| 50 |
+
# Step 2: Generate answer with strict 2-word limit
|
| 51 |
+
prompt = f'Context: {context}
|
| 52 |
+
Question: {question}
|
| 53 |
+
Answer concisely in exactly 2 words:'
|
| 54 |
+
|
| 55 |
+
response = self.llm.text_generation(
|
| 56 |
+
prompt,
|
| 57 |
+
max_new_tokens=4, # Strict limit for 2 words
|
| 58 |
+
temperature=0.3,
|
| 59 |
+
truncate=2000
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Post-process to ensure 2-word maximum
|
| 63 |
+
words = response.strip().replace(".", "").split()[:2]
|
| 64 |
+
return ' '.join(words) if words else "Unknown"
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
"""
|
| 68 |
|
| 69 |
|
| 70 |
class BasicAgent:
|
| 71 |
def __init__(self):
|
| 72 |
+
search_tool = DuckDuckGoSearchRun()
|
| 73 |
|
| 74 |
|
| 75 |
def __call__(self, question, question_attach=""):
|
|
|
|
| 83 |
question = question[::-1]
|
| 84 |
print(f"Reversed question: {question}")
|
| 85 |
|
| 86 |
+
return self.search_tool.invoke(question)
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
print(f"Error: {str(e)}")
|