peerawatchomp commited on
Commit
097d9b3
·
verified ·
1 Parent(s): 0d9ea58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -35,36 +35,36 @@ class BasicAgent:
35
  chat = ChatOpenAI(
36
  model="gpt-4",
37
  temperature=0.0,
38
- openai_api_key=os.environ.get("OPENAI_API_KEY"), # use OpenAI API key securely from environment variable
39
  verbose=True
40
  )
41
 
42
- # chat = ChatHuggingFace(llm=llm, verbose=True)
43
  search_tool = DuckDuckGoSearchRun()
44
  tools = [search_tool]
 
 
45
  chat_with_tools = chat.bind_tools(tools)
46
-
 
47
  def assistant(state: AgentState):
48
  return {
49
- "messages": [chat_with_tools.invoke(state["messages"])],
50
  }
51
-
52
- ## The graph
53
  builder = StateGraph(AgentState)
54
-
55
- # Define nodes: these do the work
56
  builder.add_node("assistant", assistant)
57
  builder.add_node("tools", ToolNode(tools))
58
-
59
- # Define edges: these determine how the control flow moves
60
  builder.add_edge(START, "assistant")
61
- builder.add_conditional_edges(
62
- "assistant",
63
- # If the latest message requires a tool, route to tools
64
- # Otherwise, provide a direct response
65
- tools_condition,
66
- )
67
  builder.add_edge("tools", "assistant")
 
 
68
  self.agent = builder.compile()
69
 
70
  def __call__(self, question: str) -> str:
 
35
  chat = ChatOpenAI(
36
  model="gpt-4",
37
  temperature=0.0,
38
+ openai_api_key=os.environ.get("OPENAI_API_KEY"), # Secure API Key
39
  verbose=True
40
  )
41
 
42
+ # Set up tools
43
  search_tool = DuckDuckGoSearchRun()
44
  tools = [search_tool]
45
+
46
+ # Bind LLM with tools
47
  chat_with_tools = chat.bind_tools(tools)
48
+
49
+ # Assistant function (process one step)
50
  def assistant(state: AgentState):
51
  return {
52
+ "messages": [chat_with_tools.invoke(state["messages"])]
53
  }
54
+
55
+ # Create a StateGraph
56
  builder = StateGraph(AgentState)
57
+
58
+ # Add nodes
59
  builder.add_node("assistant", assistant)
60
  builder.add_node("tools", ToolNode(tools))
61
+
62
+ # Define edges
63
  builder.add_edge(START, "assistant")
64
+ builder.add_conditional_edges("assistant", tools_condition)
 
 
 
 
 
65
  builder.add_edge("tools", "assistant")
66
+
67
+ # Compile the agent
68
  self.agent = builder.compile()
69
 
70
  def __call__(self, question: str) -> str: