File size: 2,932 Bytes
9c91662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
import gradio as gr

load_dotenv()

# Tool: Search or generate recipes from ingredients
@tool(description="Suggest a recipe based on ingredients, cuisine, or dietary preferences")
def recipe_finder(query: str) -> str:
    recipes = {
        "pasta": "Try Creamy Garlic Pasta: Cook pasta, sauté garlic in butter, add cream, parmesan, and toss with parsley.",
        "chicken": "Grilled Lemon Chicken: Marinate chicken in lemon juice, olive oil, garlic, and grill until golden brown.",
        "vegetarian": "Vegetable Stir Fry: Sauté bell peppers, broccoli, carrots, add soy sauce and sesame seeds.",
        "dessert": "Chocolate Mug Cake: Mix flour, cocoa powder, sugar, milk, oil, microwave for 2 minutes."
    }
    for key, val in recipes.items():
        if key in query.lower():
            return val
    return f"Sorry, I don’t have a preset recipe for '{query}', but try asking me to generate one!"

# Main assistant function
def cooking_assistant(message: str, history: list) -> str:
    llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash-lite")
    system_message = (
        "You are a friendly AI Recipe Assistant. "
        "Suggest recipes, cooking steps, and variations. "
        "If users mention ingredients, cuisine, or dietary preferences, use the recipe_finder tool. "
        "Otherwise, answer with your own cooking knowledge."
    )
    prompt = ChatPromptTemplate.from_messages([
        ("system", system_message),
        ("human", "{input}"),
        ("placeholder", "{agent_scratchpad}")
    ])
    agent = create_tool_calling_agent(llm, [recipe_finder], prompt)
    executor = AgentExecutor(agent=agent, tools=[recipe_finder], verbose=True)
    result = executor.invoke({"input": message})
    return result["output"]

# Quick test
if __name__ == "__main__":
    print(cooking_assistant("I have chicken and garlic. Suggest a recipe", []))

# --- Gradio UI ---
LOGO_URL = "https://raw.githubusercontent.com/hereandnowai/images/refs/heads/main/logos/logo-of-here-and-now-ai.png"

with gr.Blocks() as demo:
    gr.HTML(f'<div style="text-align: center;"><img src="{LOGO_URL}" style="height: 60px;"><h1>🍳 Caramel AI - Recipe Assistant</h1></div>')
    gr.ChatInterface(
        fn=cooking_assistant,
        description="Ask me for recipes, cooking steps, or meal ideas! 🍲",
        examples=[
            "Suggest a pasta recipe with tomatoes and cheese",
            "Give me a vegetarian dinner idea",
            "How to make chocolate cake?",
            "Quick breakfast with oats?",
            "Healthy lunch with chicken and spinach"
        ],
        type="messages"
    )

if __name__ == "__main__":
    demo.launch(share=True)