DarkRodry commited on
Commit
f9190e8
·
1 Parent(s): 899ced0

feat: add duckduckgo search tool

Browse files
Files changed (3) hide show
  1. agent.py +9 -6
  2. requirements.txt +2 -1
  3. tools.py +3 -0
agent.py CHANGED
@@ -1,5 +1,6 @@
1
  from langchain_google_genai import ChatGoogleGenerativeAI
2
  from langchain_core.messages import HumanMessage, SystemMessage
 
3
 
4
  model = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)
5
 
@@ -9,16 +10,18 @@ messages = [
9
  HumanMessage("I'm making a grocery list for my mom, but she's a professor of botany and she's a real stickler when it comes to categorizing things. I need to add different foods to different categories on the grocery list, but if I make a mistake, she won't buy anything inserted in the wrong category. Here's the list I have so far:\n\nmilk, eggs, flour, whole bean coffee, Oreos, sweet potatoes, fresh basil, plums, green beans, rice, corn, bell pepper, whole allspice, acorns, broccoli, celery, zucchini, lettuce, peanuts\n\nI need to make headings for the fruits and vegetables. Could you please create a list of just the vegetables from my list? If you could do that, then I can figure out how to categorize the rest of the list into the appropriate categories. But remember that my mom is a real stickler, so make sure that no botanical fruits end up on the vegetable list, or she won't get them when she's at the store. Please alphabetize the list of vegetables, and place each item in a comma separated list."),
10
  ]
11
 
12
- def invoke_agent(question: str) -> str:
 
 
 
 
13
  """
14
  Invoke the agent with a given question.
15
  """
16
  messages = [
17
  SystemMessage(system_template),
18
- HumanMessage(question),
19
  ]
20
- response = model.invoke(messages)
21
- return response.content
22
 
23
- response = invoke_agent("I'm making a grocery list for my mom, but she's a professor of botany and she's a real stickler when it comes to categorizing things. I need to add different foods to different categories on the grocery list, but if I make a mistake, she won't buy anything inserted in the wrong category. Here's the list I have so far:\n\nmilk, eggs, flour, whole bean coffee, Oreos, sweet potatoes, fresh basil, plums, green beans, rice, corn, bell pepper, whole allspice, acorns, broccoli, celery, zucchini, lettuce, peanuts\n\nI need to make headings for the fruits and vegetables. Could you please create a list of just the vegetables from my list? If you could do that, then I can figure out how to categorize the rest of the list into the appropriate categories. But remember that my mom is a real stickler, so make sure that no botanical fruits end up on the vegetable list, or she won't get them when she's at the store. Please alphabetize the list of vegetables, and place each item in a comma separated list.")
24
- print(response)
 
1
  from langchain_google_genai import ChatGoogleGenerativeAI
2
  from langchain_core.messages import HumanMessage, SystemMessage
3
+ from tools import *
4
 
5
  model = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)
6
 
 
10
  HumanMessage("I'm making a grocery list for my mom, but she's a professor of botany and she's a real stickler when it comes to categorizing things. I need to add different foods to different categories on the grocery list, but if I make a mistake, she won't buy anything inserted in the wrong category. Here's the list I have so far:\n\nmilk, eggs, flour, whole bean coffee, Oreos, sweet potatoes, fresh basil, plums, green beans, rice, corn, bell pepper, whole allspice, acorns, broccoli, celery, zucchini, lettuce, peanuts\n\nI need to make headings for the fruits and vegetables. Could you please create a list of just the vegetables from my list? If you could do that, then I can figure out how to categorize the rest of the list into the appropriate categories. But remember that my mom is a real stickler, so make sure that no botanical fruits end up on the vegetable list, or she won't get them when she's at the store. Please alphabetize the list of vegetables, and place each item in a comma separated list."),
11
  ]
12
 
13
+ tools = [web_search]
14
+
15
+ model_with_tools = model.bind_tools(tools)
16
+
17
+ def invoke_agent(query: str) -> str:
18
  """
19
  Invoke the agent with a given question.
20
  """
21
  messages = [
22
  SystemMessage(system_template),
23
+ HumanMessage(query),
24
  ]
 
 
25
 
26
+ response = model.invoke(messages)
27
+ return response.text()
requirements.txt CHANGED
@@ -2,4 +2,5 @@ gradio
2
  requests
3
  langchain
4
  langchain-community
5
- langchain-google-genai
 
 
2
  requests
3
  langchain
4
  langchain-community
5
+ langchain-google-genai
6
+ duckduckgo-search
tools.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from langchain_community.tools import DuckDuckGoSearchRun
2
+
3
+ web_search = DuckDuckGoSearchRun()