ameglei-external commited on
Commit
4399d22
·
verified ·
1 Parent(s): 3c71b5b

Try to use tool decorator instead of duplicating function docstring and tool doc logic

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -16,6 +16,7 @@ from langgraph.prebuilt import ToolNode, tools_condition
16
  from langchain_openai import ChatOpenAI
17
  # from langchain_community.tools import DuckDuckGoSearchRun
18
  from langchain_core.messages import SystemMessage, HumanMessage, AnyMessage
 
19
 
20
 
21
  # --- Constants ---
@@ -49,17 +50,17 @@ class BasicAgent:
49
  def __call__(self, question: str) -> Tuple[str, List[Dict[str, Any]]]:
50
  print(f"Agent received question: {question}")
51
 
52
- tool_doc = """
53
- search_tool(question: str, max_length: int = 2048) -> str:
54
- Search info on the web.
55
- Args:
56
- question: Question string
57
- max_length: maximum characters in the output
58
- Returns:
59
- A single string containing the info from the web or a fallback message.
60
- """
61
  sys_msg = SystemMessage(
62
- content=f"""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. You can use provided tools:\n{tool_doc}"""
63
  )
64
 
65
  state = State(
@@ -81,16 +82,11 @@ class BasicAgent:
81
  "messages": [response]
82
  }
83
 
 
 
 
 
84
  def search_tool(self, question: str, max_length: int = 2048) -> str:
85
- """
86
- Use DuckDuckGo’s API to fetch a direct answer.
87
-
88
- Args:
89
- question: The question to ask.
90
- max_length: Max chars to return.
91
- Returns:
92
- Answer text, or a fallback message.
93
- """
94
  print(f"Calling search tool with: {question}, max_lentgh: {max_length}")
95
  with DDGS() as ddgs:
96
  hits = list(ddgs.text(question, max_results=5))
 
16
  from langchain_openai import ChatOpenAI
17
  # from langchain_community.tools import DuckDuckGoSearchRun
18
  from langchain_core.messages import SystemMessage, HumanMessage, AnyMessage
19
+ from langchain_core.tools import tool
20
 
21
 
22
  # --- Constants ---
 
50
  def __call__(self, question: str) -> Tuple[str, List[Dict[str, Any]]]:
51
  print(f"Agent received question: {question}")
52
 
53
+ # tool_doc = """
54
+ # search_tool(question: str, max_length: int = 2048) -> str:
55
+ # Search info on the web.
56
+ # Args:
57
+ # question: Question string
58
+ # max_length: maximum characters in the output
59
+ # Returns:
60
+ # A single string containing the info from the web or a fallback message.
61
+ # """
62
  sys_msg = SystemMessage(
63
+ content=f"""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings."""
64
  )
65
 
66
  state = State(
 
82
  "messages": [response]
83
  }
84
 
85
+ @tool(
86
+ name="search_tool",
87
+ description="Search the web using DuckDuckGo and return the best result snippet.",
88
+ )
89
  def search_tool(self, question: str, max_length: int = 2048) -> str:
 
 
 
 
 
 
 
 
 
90
  print(f"Calling search tool with: {question}, max_lentgh: {max_length}")
91
  with DDGS() as ddgs:
92
  hits = list(ddgs.text(question, max_results=5))