Download tools/produce_classifier.py from dmfelder/DF_Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/dmfelder/DF_Final_Assignment_Template/resolve/main/tools/produce_classifier.py
- Command line
-
hf download hf://spaces/dmfelder/DF_Final_Assignment_Template/tools/produce_classifier.py
-
curl -L -o produce_classifier.py https://huggingface.co/spaces/dmfelder/DF_Final_Assignment_Template/resolve/main/tools/produce_classifier.py
1.07 kB
| import os | |
| from langchain_core.tools import tool # ← this needs to be before the decorator | |
| from langchain_openai import ChatOpenAI | |
| from loguru import logger | |
| from pydantic import SecretStr | |
| 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"Respond only with a JSON in this exact format: " | |
| f"{{\"name\": \"{food_name}\", \"kind\": \"[classification]\"}}") | |
| return chat.invoke(prompt).content |