File size: 1,612 Bytes
ee92f11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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")