akrstova commited on
Commit
0538019
·
1 Parent(s): 33dc421

Fix search tool

Browse files
Files changed (3) hide show
  1. agent.py +9 -2
  2. app.py +1 -0
  3. tools/search_tools.py +14 -9
agent.py CHANGED
@@ -27,9 +27,15 @@ def build_graph():
27
 
28
  def assistant(state: MessagesState):
29
  """Assistant node for invoking the LLM."""
30
- return {"messages": [llm_with_tools.invoke(state["messages"])]}
 
 
 
 
 
31
 
32
  builder = StateGraph(MessagesState)
 
33
  builder.add_node("assistant", assistant)
34
  builder.add_node("tools", ToolNode(tools))
35
  builder.add_edge(START, "assistant")
@@ -42,8 +48,9 @@ def build_graph():
42
  # Compile graph
43
  return builder.compile()
44
 
 
45
  if __name__ == "__main__":
46
- question = "Give me a summary of the wikipedia page for photosynthesis"
47
  # Build the graph
48
  graph = build_graph()
49
  # Run the graph
 
27
 
28
  def assistant(state: MessagesState):
29
  """Assistant node for invoking the LLM."""
30
+ messages = state["messages"]
31
+ # Add system message if not present
32
+ if not any(isinstance(m, SystemMessage) for m in messages):
33
+ messages = [SystemMessage(content="You are a helpful AI assistant. Use the available tools to answer questions accurately. When providing your final answer, use the format: FINAL ANSWER: [your answer]")] + messages
34
+ response = llm_with_tools.invoke(messages)
35
+ return {"messages": [response]}
36
 
37
  builder = StateGraph(MessagesState)
38
+ # builder.add_node("retriever", retriever)
39
  builder.add_node("assistant", assistant)
40
  builder.add_node("tools", ToolNode(tools))
41
  builder.add_edge(START, "assistant")
 
48
  # Compile graph
49
  return builder.compile()
50
 
51
+
52
  if __name__ == "__main__":
53
+ question = "On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?"
54
  # Build the graph
55
  graph = build_graph()
56
  # Run the graph
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  import inspect
5
  import pandas as pd
6
  from langchain_core.messages import HumanMessage
 
7
  from agent import build_graph
8
 
9
  # (Keep Constants as is)
 
4
  import inspect
5
  import pandas as pd
6
  from langchain_core.messages import HumanMessage
7
+
8
  from agent import build_graph
9
 
10
  # (Keep Constants as is)
tools/search_tools.py CHANGED
@@ -6,16 +6,21 @@ from langchain_community.document_loaders import WikipediaLoader
6
 
7
  @tool
8
  def web_search(query: str) -> str:
9
- """Search the web via Tavily for a given query and return maximum 3 results.
10
-
11
  Args:
12
- query (str): The search query
13
-
14
  Returns:
15
- str: Content from the Tavily search
16
  """
17
- search_results = TavilySearchResults(max_results=3).invoke(query=query)
18
- return {"web_results": search_results}
 
 
 
 
 
 
19
 
20
  @tool
21
  def search_wikipedia(query: str) -> str:
@@ -35,8 +40,8 @@ def search_wikipedia(query: str) -> str:
35
  loader = WikipediaLoader(query=query, load_max_docs=1)
36
  docs = loader.load()
37
  if not docs:
38
- return f"No Wikipedia page found for '{query}'"
39
  return docs[0].page_content
40
  except Exception as e:
41
- return f"Error searching Wikipedia: {str(e)}"
42
 
 
6
 
7
  @tool
8
  def web_search(query: str) -> str:
9
+ """Search Tavily for a query and return maximum 3 results.
 
10
  Args:
11
+ query: The search query.
12
+
13
  Returns:
14
+ str: The result of the search query
15
  """
16
+ search_tool = TavilySearchResults(max_results=3)
17
+ search_docs = search_tool.invoke({"query": query})
18
+ # Convert search results to a single string
19
+ results_text = ""
20
+ for doc in search_docs:
21
+ results_text += f"{doc['title']}: {doc['content']}\n"
22
+ return results_text
23
+
24
 
25
  @tool
26
  def search_wikipedia(query: str) -> str:
 
40
  loader = WikipediaLoader(query=query, load_max_docs=1)
41
  docs = loader.load()
42
  if not docs:
43
+ raise ValueError(f"No Wikipedia page found for query: {query}")
44
  return docs[0].page_content
45
  except Exception as e:
46
+ raise ValueError(f"Error searching Wikipedia: {str(e)}")
47