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'