Pycrolis commited on
Commit
ee92f11
·
1 Parent(s): eb08b08

feat(tool): add produce classification tool

Browse files
Files changed (2) hide show
  1. ShrewdAgent.py +2 -0
  2. tools/produce_classifier.py +44 -0
ShrewdAgent.py CHANGED
@@ -13,6 +13,7 @@ from langgraph.pregel import PregelProtocol
13
  from loguru import logger
14
  from pydantic import SecretStr
15
 
 
16
  from tools.web_page_information_extractor import web_page_information_extractor
17
  from tools.youtube_transcript import youtube_transcript
18
 
@@ -40,6 +41,7 @@ class ShrewdAgent:
40
  TavilySearch(),
41
  web_page_information_extractor,
42
  youtube_transcript,
 
43
  ]
44
  self.llm = ChatOpenAI(
45
  model="gpt-4o-mini",
 
13
  from loguru import logger
14
  from pydantic import SecretStr
15
 
16
+ from tools.produce_classifier import produce_classifier
17
  from tools.web_page_information_extractor import web_page_information_extractor
18
  from tools.youtube_transcript import youtube_transcript
19
 
 
41
  TavilySearch(),
42
  web_page_information_extractor,
43
  youtube_transcript,
44
+ produce_classifier,
45
  ]
46
  self.llm = ChatOpenAI(
47
  model="gpt-4o-mini",
tools/produce_classifier.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from langchain_core.tools import tool
4
+ from langchain_openai import ChatOpenAI
5
+ from loguru import logger
6
+ from pydantic import SecretStr
7
+
8
+
9
+ @tool("produce_classifier_tool", parse_docstring=True)
10
+ def produce_classifier(food_name: str) -> str:
11
+ """
12
+ Classifies a food item as either 'fruit' or 'vegetable' from a botanical perspective.
13
+
14
+ Args:
15
+ food_name (str): The name of the food item to classify.
16
+
17
+ Returns:
18
+ str: The classification of the food item, either 'fruit' or 'vegetable'.
19
+ """
20
+ logger.info(f"use produce_classifier_tool with param: {food_name}")
21
+ chat = ChatOpenAI(
22
+ model="gpt-4o-mini",
23
+ temperature=0,
24
+ api_key = SecretStr(os.environ['OPENAI_API_KEY'])
25
+ )
26
+
27
+ prompt = (f"From a botanical perspective, classify {food_name} as either 'fruit' or 'vegetable'. "
28
+ #f"If it's not a produce name, classify as 'neither'. "
29
+ f"Respond only with a JSON in this exact format: "
30
+ f"{{\"name\": \"{food_name}\", \"kind\": \"[classification]\"}}, "
31
+ f"where [classification] should be replaced with just 'fruit' or 'vegetable'." #, or 'neither'. "
32
+ f"No other text or explanation.")
33
+ return chat.invoke(prompt).content
34
+
35
+ def _print_produce_kind(food_name: str):
36
+ print(f'{food_name}: {produce_classifier.invoke(food_name)}')
37
+
38
+ if __name__ == "__main__":
39
+ #_print_produce_kind("orange")
40
+ #_print_produce_kind("sweet potatoes")
41
+ #_print_produce_kind("bell pepper")
42
+ #_print_produce_kind("egg")
43
+ #_print_produce_kind("table")
44
+ _print_produce_kind("zucchini")