Manos21's picture
Update app.py
35eb883 verified
import os
import streamlit as st
from dotenv import load_dotenv
from langchain_core.messages import HumanMessage
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
load_dotenv()
api_key = os.getenv("OPENROUTER_API_KEY")
@tool
def calculator(a: float, b: float) -> str:
"""Useful for calculating the sum of two numbers (a + b)."""
return f"The sum of {a} and {b} is {a + b}"
@tool
def say_hello(name: str) -> str:
"""Useful for greeting a user"""
return f"Hello {name}, I hope you are well today"
model = ChatOpenAI(
model="mistralai/mistral-7b-instruct:free",
api_key=api_key,
base_url="https://openrouter.ai/api/v1",
temperature=0,
)
tools = [calculator, say_hello]
agent_executor = create_react_agent(model, tools)
# Streamlit UI
#st.set_page_config(page_title="Tool-Using AI Assistant", page_icon="πŸ€–")
#st.title("🧠 Mistral Agent with LangGraph")
st.title("πŸ€– Mistral Agent with LangGraph")
st.markdown("""
This AI assistant can use custom tools!
### πŸ›  Available Tools:
- `calculator(a, b)` – Calculates the sum of two numbers.
- Example: "What is the sum of 12 and 30?"
- `say_hello(name)` – Greets the person by name.
- Example: "Say hello to Alex."
You can ask the assistant naturally β€” it will decide when to use a tool.
""")
user_input = st.text_input("Ask me anything (type 'quit' to exit):")
if user_input and user_input.lower() != "quit":
with st.spinner("Thinking..."):
chunks = []
for chunk in agent_executor.stream({"messages": [HumanMessage(content=user_input)]}):
if "agent" in chunk and "messages" in chunk["agent"]:
for message in chunk["agent"]["messages"]:
chunks.append(message.content)
st.success(" ".join(chunks))