final_exam / tools /grocery_tool.py
VinogradovAI's picture
Update tools/grocery_tool.py
bcc40de verified
Raw History Blame Contribute Delete
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.")