dmfelder commited on
Commit
71f9fba
·
verified ·
1 Parent(s): f5de157

Create produce_classifier.py

Browse files
Files changed (1) hide show
  1. tools/produce_classifier.py +42 -0
tools/produce_classifier.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ Args:
14
+ food_name (str): The name of the food item to classify.
15
+ Returns:
16
+ str: The classification of the food item, either 'fruit' or 'vegetable'.
17
+ """
18
+ logger.info(f"use produce_classifier_tool with param: {food_name}")
19
+ chat = ChatOpenAI(
20
+ model="gpt-4o-mini",
21
+ temperature=0,
22
+ api_key = SecretStr(os.environ['OPENAI_API_KEY'])
23
+ )
24
+
25
+ prompt = (f"From a botanical perspective, classify {food_name} as either 'fruit' or 'vegetable'. "
26
+ #f"If it's not a produce name, classify as 'neither'. "
27
+ f"Respond only with a JSON in this exact format: "
28
+ f"{{\"name\": \"{food_name}\", \"kind\": \"[classification]\"}}, "
29
+ f"where [classification] should be replaced with just 'fruit' or 'vegetable'." #, or 'neither'. "
30
+ f"No other text or explanation.")
31
+ return chat.invoke(prompt).content
32
+
33
+ def _print_produce_kind(food_name: str):
34
+ print(f'{food_name}: {produce_classifier.invoke(food_name)}')
35
+
36
+ if __name__ == "__main__":
37
+ #_print_produce_kind("orange")
38
+ #_print_produce_kind("sweet potatoes")
39
+ #_print_produce_kind("bell pepper")
40
+ #_print_produce_kind("egg")
41
+ #_print_produce_kind("table")
42
+ _print_produce_kind("zucchini")