Update agent.py
Browse files
agent.py
CHANGED
|
@@ -33,14 +33,15 @@ def build_gemini_llm():
|
|
| 33 |
return ChatGoogleGenerativeAI(model = "gemini-3.7-flash", temperature = 0, max_output_tokens = 1025, include_thoughts=True)
|
| 34 |
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
@tool
|
| 46 |
def extract_text_from_image(img_path: str) -> str:
|
|
@@ -172,6 +173,8 @@ def wikipedia_search(query: str):
|
|
| 172 |
|
| 173 |
if not search_results:
|
| 174 |
return f"No Wikipedia results found for {query}. Consider another query or try a web search."
|
|
|
|
|
|
|
| 175 |
return "\n\n---\n\n".join(
|
| 176 |
f"Title: {doc.metadata.get('title', 'Unknown')}\n"
|
| 177 |
f"Content: {doc.page_content}"
|
|
@@ -181,10 +184,8 @@ def wikipedia_search(query: str):
|
|
| 181 |
print(f"Wikipedia search failed for {query}: {e}")
|
| 182 |
return f"Wikipedia search failed for {query} due to {e}."
|
| 183 |
|
| 184 |
-
search_web = DuckDuckGoSearchRun()
|
| 185 |
model = build_gemini_llm()
|
| 186 |
tools = [
|
| 187 |
-
extract_text_from_image,
|
| 188 |
search_web,
|
| 189 |
wikipedia_search
|
| 190 |
]
|
|
@@ -193,9 +194,8 @@ model_with_tools = model.bind_tools(tools)
|
|
| 193 |
def assistant(state: AgentState):
|
| 194 |
response = model_with_tools.invoke(state["messages"])
|
| 195 |
print("--- Thinking Section ---")
|
| 196 |
-
print(response.content[0].get("thinking"))
|
| 197 |
print("--- End of Thinking ---")
|
| 198 |
-
print(response)
|
| 199 |
return {
|
| 200 |
"messages": [response],
|
| 201 |
"file_path": state["file_path"],
|
|
|
|
| 33 |
return ChatGoogleGenerativeAI(model = "gemini-3.7-flash", temperature = 0, max_output_tokens = 1025, include_thoughts=True)
|
| 34 |
|
| 35 |
|
| 36 |
+
@tool
|
| 37 |
+
def search_web(query:str) -> str:
|
| 38 |
+
"""
|
| 39 |
+
Searches the web for the answer to a given question or topic.
|
| 40 |
+
Args:
|
| 41 |
+
query (str): the question or topic to search for.
|
| 42 |
+
"""
|
| 43 |
+
print(f"Query to ddsg: {query}")
|
| 44 |
+
return DuckDuckGoSearchRun()
|
| 45 |
|
| 46 |
@tool
|
| 47 |
def extract_text_from_image(img_path: str) -> str:
|
|
|
|
| 173 |
|
| 174 |
if not search_results:
|
| 175 |
return f"No Wikipedia results found for {query}. Consider another query or try a web search."
|
| 176 |
+
print("Result from wikipedia:")
|
| 177 |
+
print(search_results)
|
| 178 |
return "\n\n---\n\n".join(
|
| 179 |
f"Title: {doc.metadata.get('title', 'Unknown')}\n"
|
| 180 |
f"Content: {doc.page_content}"
|
|
|
|
| 184 |
print(f"Wikipedia search failed for {query}: {e}")
|
| 185 |
return f"Wikipedia search failed for {query} due to {e}."
|
| 186 |
|
|
|
|
| 187 |
model = build_gemini_llm()
|
| 188 |
tools = [
|
|
|
|
| 189 |
search_web,
|
| 190 |
wikipedia_search
|
| 191 |
]
|
|
|
|
| 194 |
def assistant(state: AgentState):
|
| 195 |
response = model_with_tools.invoke(state["messages"])
|
| 196 |
print("--- Thinking Section ---")
|
| 197 |
+
print(response.content[0].get("thinking").strip())
|
| 198 |
print("--- End of Thinking ---")
|
|
|
|
| 199 |
return {
|
| 200 |
"messages": [response],
|
| 201 |
"file_path": state["file_path"],
|