Ai-agent / app.py
Rohinth-Thinker's picture
Create app.py
f90e8f2 verified
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(description="Calculate math expressions like '2+2' or '10*5'")
def calculator(expression: str) -> str:
try: return str(eval(expression))
except: return "Error"
def calculate(message: str, history: list) -> str:
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash-lite")
system_message = "You are a helpful math assistant. Use the calculator tool when you need to perform calculations."
prompt = ChatPromptTemplate.from_messages([("system", system_message), ("human", "{input}"), ("placeholder", "{agent_scratchpad}")])
agent = create_tool_calling_agent(llm, [calculator], prompt)
executor = AgentExecutor(agent=agent, tools=[calculator], verbose=True)
result = executor.invoke({"input": message})
return result["output"]
if __name__ == "__main__":
print(calculate("2+2",[]))
LOGO_URL = "https://raw.githubusercontent.com/hereandnowai/images/refs/heads/main/logos/logo-of-here-and-now-ai.png"
ASSISTANT_AVATAR_URL = "https://raw.githubusercontent.com/hereandnowai/images/refs/heads/main/logos/caramel-face.jpeg"
with gr.Blocks() as demo:
gr.HTML(f'<div style="text-align: center;"><img src="{LOGO_URL}" style="height: 60px;"><h1>Caramel AI from HERE AND NOW AI !</h1></div>')
gr.ChatInterface(
fn=calculate,
description="Ask me to perform calculations or solve math problems!",
examples=["What is 25 * 47?", "Calculate 144 divide 12", "What's 15% of 200?", "Solve: (10 + 5) multiply 3 and then subract by 8"],
type="messages"
)
if __name__ == "__main__":
demo.launch(share=True)