File size: 3,740 Bytes
70a4c1e
c6a7187
0a83688
 
 
b3fd3ab
c6a7187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3fd3ab
c6a7187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5d5dc2
b3a8383
b3fd3ab
b3a8383
c6a7187
b3fd3ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6a7187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3fd3ab
c6a7187
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#fix workflow
import os
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_openai import ChatOpenAI
from langgraph.graph import MessagesState
from langgraph.graph import START, StateGraph
from langgraph.prebuilt import tools_condition
from langgraph.prebuilt import ToolNode
from langchain_core.messages import HumanMessage, SystemMessage

# ------------------- Environment Variable Setup -------------------
# Fetch API keys from environment variables
openai_api_key = os.getenv("OPENAI_API_KEY")
tavily_api_key = os.getenv("TAVILY_API_KEY")

# Verify if API keys are set
if not openai_api_key:
    raise ValueError("Missing required environment variable: OPENAI_API_KEY")
if not tavily_api_key:
    raise ValueError("Missing required environment variable: TAVILY_API_KEY")

# ------------------- Tool Definitions -------------------
tavily_tool = TavilySearchResults(max_results=5)

def multiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    return a * b

def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b

def divide(a: int, b: int) -> float:
    """Divide two numbers."""
    if b == 0:
        raise ValueError("Division by zero is not allowed.")
    return a / b

tools = [add, multiply, divide, tavily_tool]

# ------------------- LLM Setup -------------------
llm = ChatOpenAI(model="gpt-4o-mini")
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)
sys_msg = SystemMessage(content="You are a helpful assistant tasked with performing arithmetic and search on a set of inputs.")

# ------------------- LangGraph Workflow -------------------
def assistant(state: MessagesState):
    """Assistant node to invoke LLM with tools."""
    return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}

app_graph = StateGraph(MessagesState)
app_graph.add_node("assistant", assistant)
app_graph.add_node("tools", ToolNode(tools))
app_graph.add_edge(START, "assistant")
app_graph.add_conditional_edges("assistant", tools_condition)
app_graph.add_edge("tools", "assistant")
react_graph = app_graph.compile()

# ------------------- Streamlit Interface -------------------
st.title("ReAct Agent")

# Display the workflow graph using NetworkX
st.header("LangGraph Workflow Visualization")

G = nx.DiGraph()
G.add_edge("START", "assistant")
G.add_edge("assistant", "tools", label="tools_condition")
G.add_edge("tools", "assistant", label="loop back")

plt.figure(figsize=(10, 6))
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, with_labels=True, node_size=3000, node_color="lightblue", font_size=10, font_weight="bold")
nx.draw_networkx_edge_labels(G, pos, edge_labels={
    ("assistant", "tools"): "tools_condition",
    ("tools", "assistant"): "loop back"
}, font_color="red")
st.pyplot(plt)

# User input
user_question = st.text_area("Enter your question:", placeholder="Example: 'Add 3 and 4. Multiply the result by 2. Divide it by 5.'")

if st.button("Submit"):
    if not user_question.strip():
        st.error("Please enter a valid question.")
        st.stop()
    
    st.info("Processing your question...")
    messages = [HumanMessage(content=user_question)]
    response = react_graph.invoke({"messages": messages})
    
    st.subheader("Responses")
    for m in response['messages']:
        st.write(m.content)
    st.success("Processing complete!")

# Example Questions
st.sidebar.subheader("Example Questions")
st.sidebar.write("- Add 3 and 4. Multiply the result by 2. Divide it by 5.")
st.sidebar.write("- Tell me how many centuries Virat Kohli scored.")
st.sidebar.write("- Search for the tallest building in the world.")