import os from langchain_core.tools import tool from langchain_openai import ChatOpenAI from loguru import logger from pydantic import SecretStr @tool("produce_classifier_tool", parse_docstring=True) def produce_classifier(food_name: str) -> str: """ Classifies a food item as either 'fruit' or 'vegetable' from a botanical perspective. Args: food_name (str): The name of the food item to classify. Returns: str: The classification of the food item, either 'fruit' or 'vegetable'. """ logger.info(f"use produce_classifier_tool with param: {food_name}") chat = ChatOpenAI( model="gpt-4o-mini", temperature=0, api_key = SecretStr(os.environ['OPENAI_API_KEY']) ) prompt = (f"From a botanical perspective, classify {food_name} as either 'fruit' or 'vegetable'. " #f"If it's not a produce name, classify as 'neither'. " f"Respond only with a JSON in this exact format: " f"{{\"name\": \"{food_name}\", \"kind\": \"[classification]\"}}, " f"where [classification] should be replaced with just 'fruit' or 'vegetable'." #, or 'neither'. " f"No other text or explanation.") return chat.invoke(prompt).content def _print_produce_kind(food_name: str): print(f'{food_name}: {produce_classifier.invoke(food_name)}') if __name__ == "__main__": #_print_produce_kind("orange") #_print_produce_kind("sweet potatoes") #_print_produce_kind("bell pepper") #_print_produce_kind("egg") #_print_produce_kind("table") _print_produce_kind("zucchini")