Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import operator
|
4 |
+
import streamlit as st
|
5 |
+
from typing import TypedDict, Annotated, Sequence
|
6 |
+
from langchain_openai import ChatOpenAI
|
7 |
+
from langchain_core.tools import tool
|
8 |
+
from langchain_core.utils.function_calling import convert_to_openai_tool
|
9 |
+
from langgraph.graph import StateGraph, END
|
10 |
+
|
11 |
+
# Environment Setup
|
12 |
+
os.environ['OPENAI_API_KEY'] = os.getenv("OPENAI_API_KEY")
|
13 |
+
|
14 |
+
# Model Initialization
|
15 |
+
model = ChatOpenAI(temperature=0)
|
16 |
+
|
17 |
+
@tool
|
18 |
+
def multiply(first_number: int, second_number: int):
|
19 |
+
return first_number * second_number
|
20 |
+
|
21 |
+
model_with_tools = model.bind(tools=[convert_to_openai_tool(multiply)])
|
22 |
+
|
23 |
+
# State Setup
|
24 |
+
class AgentState(TypedDict):
|
25 |
+
messages: Annotated[Sequence, operator.add]
|
26 |
+
|
27 |
+
graph = StateGraph(AgentState)
|
28 |
+
|
29 |
+
def invoke_model(state):
|
30 |
+
question = state['messages'][-1]
|
31 |
+
return {"messages": [model_with_tools.invoke(question)]}
|
32 |
+
|
33 |
+
graph.add_node("agent", invoke_model)
|
34 |
+
|
35 |
+
def invoke_tool(state):
|
36 |
+
tool_calls = state['messages'][-1].additional_kwargs.get("tool_calls", [])
|
37 |
+
for tool_call in tool_calls:
|
38 |
+
if tool_call.get("function").get("name") == "multiply":
|
39 |
+
res = multiply.invoke(json.loads(tool_call.get("function").get("arguments")))
|
40 |
+
return {"messages": [f"Tool Result: {res}"]}
|
41 |
+
return {"messages": ["No tool input provided."]}
|
42 |
+
|
43 |
+
graph.add_node("tool", invoke_tool)
|
44 |
+
graph.add_edge("tool", END)
|
45 |
+
graph.set_entry_point("agent")
|
46 |
+
|
47 |
+
def router(state):
|
48 |
+
calls = state['messages'][-1].additional_kwargs.get("tool_calls", [])
|
49 |
+
return "multiply" if calls else "end"
|
50 |
+
|
51 |
+
graph.add_conditional_edges("agent", router, {"multiply": "tool", "end": END})
|
52 |
+
app_graph = graph.compile()
|
53 |
+
|
54 |
+
# Streamlit Interface
|
55 |
+
st.title("Multiplication Tool")
|
56 |
+
|
57 |
+
tab1, tab2 = st.tabs(["Tool Showcase", "Ask the AI"])
|
58 |
+
|
59 |
+
with tab1:
|
60 |
+
st.subheader("Try Multiplication with AI")
|
61 |
+
col1, col2 = st.columns(2)
|
62 |
+
|
63 |
+
with col1:
|
64 |
+
first_number = st.number_input("First Number", value=0, step=1)
|
65 |
+
with col2:
|
66 |
+
second_number = st.number_input("Second Number", value=0, step=1)
|
67 |
+
|
68 |
+
if st.button("Multiply"):
|
69 |
+
question = f"What is {first_number} * {second_number}?"
|
70 |
+
output = app_graph.invoke({"messages": [question]})
|
71 |
+
st.success(output['messages'][-1])
|
72 |
+
|
73 |
+
st.info("The app intelligently routes queries through a graph.")
|
74 |
+
|
75 |
+
with tab2:
|
76 |
+
st.subheader("General Query")
|
77 |
+
user_input = st.text_input("Enter your question here")
|
78 |
+
|
79 |
+
if st.button("Submit"):
|
80 |
+
if user_input:
|
81 |
+
try:
|
82 |
+
result = app_graph.invoke({"messages": [user_input]})
|
83 |
+
st.write("Response:")
|
84 |
+
st.success(result['messages'][-1])
|
85 |
+
except Exception as e:
|
86 |
+
st.error("Something went wrong. Try again!")
|
87 |
+
else:
|
88 |
+
st.warning("Please enter a valid input.")
|
89 |
+
|