Spaces:
Sleeping
Sleeping
| 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") | |
| 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}" | |
| 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)) | |