Spaces:
Sleeping
Sleeping
Download tools/grocery_tool.py from olcapone/final_exam: direct link, hf CLI and curl.
- Browser
- Download file 1.6 kB
-
https://huggingface.co/spaces/olcapone/final_exam/resolve/main/tools/grocery_tool.py
- Command line
-
hf download hf://spaces/olcapone/final_exam/tools/grocery_tool.py
-
curl -L -o grocery_tool.py https://huggingface.co/spaces/olcapone/final_exam/resolve/main/tools/grocery_tool.py
1.6 kB
| # tools/grocery_tool.py | |
| from langchain.tools import BaseTool | |
| from typing import List | |
| def is_vegetable(item: str) -> bool: | |
| vegs = { | |
| "broccoli", | |
| "bell pepper", | |
| "celery", | |
| "corn", | |
| "green beans", | |
| "lettuce", | |
| "zucchini" | |
| } | |
| return item.lower() in vegs | |
| class GroceryTool(BaseTool): | |
| """ | |
| Инструмент для выделения овощей или фруктов из списка. | |
| Использовать префиксы 'vegetables:' или 'fruits:' в команде. | |
| """ | |
| name: str = "grocery_tool" | |
| description: str = "Categorize grocery items by botanical/culinary class (fruits vs. vegetables). Use when you need to split a shopping list into ‘vegetables’, ‘fruits’, 'peanuts', spices, etc., with strict botanical definitions." | |
| def _run(self, command: str) -> str: | |
| cmd = command.strip() | |
| if cmd.lower().startswith('vegetables:'): | |
| items = [x.strip() for x in cmd[len('vegetables:'):].split(',')] | |
| veg_list: List[str] = [i for i in items if is_vegetable(i)] | |
| return ','.join(sorted(veg_list)) | |
| elif cmd.lower().startswith('fruits:'): | |
| # При необходимости можно добавить проверку фруктов | |
| return "Фруктовый режим пока не реализован." | |
| else: | |
| return f"Неподдерживаемая категория: {command}" | |
| async def _arun(self, command: str) -> str: | |
| raise NotImplementedError("Async not supported.") | |