aimazor commited on
Commit
3ea4cc7
·
1 Parent(s): 470c9b7

Implement agent functionality in app.py and enhance assistant logic in agent.py

Browse files
Files changed (2) hide show
  1. agent.py +12 -7
  2. app.py +2 -1
agent.py CHANGED
@@ -22,6 +22,12 @@ chat = ChatHuggingFace(llm=llm, verbose=True)
22
  tools = [search_tool]
23
  chat_with_tools = chat.bind_tools(tools)
24
 
 
 
 
 
 
 
25
  # The graph
26
  builder = StateGraph(AgentState)
27
 
@@ -40,10 +46,9 @@ builder.add_conditional_edges(
40
  builder.add_edge("tools", "assistant")
41
  react_graph = builder.compile()
42
 
43
- messages=[
44
- SystemMessage(
45
- content="You are a helpful assistant that can answer questions and use tools when necessary."
46
- ),
47
- HumanMessage(content="What is the capital of France?"),
48
- ]
49
- chat_with_tools.invoke(messages=messages)
 
22
  tools = [search_tool]
23
  chat_with_tools = chat.bind_tools(tools)
24
 
25
+ def assistant(state: AgentState) -> AgentState:
26
+ messages = state.get("messages", [])
27
+ response = chat_with_tools.invoke(messages=messages)
28
+ messages.append(response)
29
+ return {"messages": messages}
30
+
31
  # The graph
32
  builder = StateGraph(AgentState)
33
 
 
46
  builder.add_edge("tools", "assistant")
47
  react_graph = builder.compile()
48
 
49
+ def agent(question: str) -> str:
50
+ initial_state: AgentState = {"messages": [SystemMessage(content="You are a helpful assistant that can answer questions and use tools when necessary."), HumanMessage(content=question)]}
51
+ final_state = react_graph.run(initial_state)
52
+ # The last message in the final state is the assistant's response
53
+ last_message = final_state["messages"][-1]
54
+ return last_message.content
 
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -15,7 +16,7 @@ class BasicAgent:
15
  print("BasicAgent initialized.")
16
  def __call__(self, question: str) -> str:
17
  print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
  print(f"Agent returning fixed answer: {fixed_answer}")
20
  return fixed_answer
21
 
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from agent import agent # Import the agent function from agent.py
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
 
16
  print("BasicAgent initialized.")
17
  def __call__(self, question: str) -> str:
18
  print(f"Agent received question (first 50 chars): {question[:50]}...")
19
+ fixed_answer = agent(question)
20
  print(f"Agent returning fixed answer: {fixed_answer}")
21
  return fixed_answer
22