alisamak commited on
Commit
9bc6cdb
·
verified ·
1 Parent(s): 043b7cd

Rename LG_agent_disabled.py to LG_agent.py

Browse files
Files changed (1) hide show
  1. LG_agent_disabled.py → LG_agent.py +36 -24
LG_agent_disabled.py → LG_agent.py RENAMED
@@ -2,24 +2,21 @@ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
2
  from typing import Annotated, TypedDict
3
  from langgraph.graph.message import add_messages
4
  from langchain_core.messages import HumanMessage, AIMessage, AnyMessage
5
- from tools import search_duckduckgo
6
  from langgraph.prebuilt import tools_condition
7
  from langgraph.graph import START, StateGraph
8
  from langgraph.prebuilt import ToolNode
 
9
 
 
 
 
 
 
 
 
 
 
10
 
11
- llm_endpoint = HuggingFaceEndpoint(
12
- repo_id="google/flan-t5-small",
13
- temperature=0,
14
- max_new_tokens=512
15
- )
16
-
17
- chat = ChatHuggingFace(llm=llm_endpoint, verbose=True)
18
-
19
- tools = [search_duckduckgo]
20
- chat_with_tools = chat.bind_tools(tools)
21
-
22
- # Generate the AgentState and Agent graph
23
  class AgentState(TypedDict):
24
  messages: Annotated[list[AnyMessage], add_messages]
25
 
@@ -28,28 +25,43 @@ def assistant(state: AgentState):
28
  "messages": [chat_with_tools.invoke(state["messages"])],
29
  }
30
 
31
- def build_graph():
32
  builder = StateGraph(AgentState)
33
 
34
  builder.add_node("assistant", assistant)
35
- builder.add_node("tools", ToolNode(tools))
36
 
37
  builder.add_edge(START, "assistant")
38
  builder.add_conditional_edges(
39
  "assistant",
40
- # If the latest message requires a tool, route to tools
41
- # Otherwise, provide a direct response
42
  tools_condition,
43
  )
44
  builder.add_edge("tools", "assistant")
45
- agent = builder.compile()
46
- return agent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  class BasicAgent:
49
- def __init__(self):
50
- self.graph = build_graph()
51
 
52
  def __call__(self, question: str) -> str:
53
- response = self.graph.invoke({"messages": [HumanMessage(content=question)]})
54
- final_message = response['messages'][-1]
55
- return final_message.content if final_message else "No response."
 
 
 
 
2
  from typing import Annotated, TypedDict
3
  from langgraph.graph.message import add_messages
4
  from langchain_core.messages import HumanMessage, AIMessage, AnyMessage
 
5
  from langgraph.prebuilt import tools_condition
6
  from langgraph.graph import START, StateGraph
7
  from langgraph.prebuilt import ToolNode
8
+ from tools import all_tools
9
 
10
+ def build_llm():
11
+ return ChatHuggingFace(
12
+ llm=HuggingFaceEndpoint(
13
+ repo_id="google/flan-t5-small", # Change easily here
14
+ temperature=0,
15
+ max_new_tokens=512,
16
+ ),
17
+ verbose=True,
18
+ )
19
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  class AgentState(TypedDict):
21
  messages: Annotated[list[AnyMessage], add_messages]
22
 
 
25
  "messages": [chat_with_tools.invoke(state["messages"])],
26
  }
27
 
28
+ def build_graph(max_steps: int = 5):
29
  builder = StateGraph(AgentState)
30
 
31
  builder.add_node("assistant", assistant)
32
+ builder.add_node("tools", ToolNode(all_tools))
33
 
34
  builder.add_edge(START, "assistant")
35
  builder.add_conditional_edges(
36
  "assistant",
 
 
37
  tools_condition,
38
  )
39
  builder.add_edge("tools", "assistant")
40
+
41
+ graph = builder.compile()
42
+
43
+ # Wrap the graph with step limiter
44
+ def limited_invoke(input_state):
45
+ steps = 0
46
+ state = input_state
47
+ while steps < max_steps:
48
+ state = graph.invoke(state)
49
+ latest_message = state["messages"][-1] if state["messages"] else None
50
+ if latest_message and isinstance(latest_message, AIMessage):
51
+ break # Agent finished with direct answer
52
+ steps += 1
53
+ return state
54
+
55
+ return limited_invoke
56
 
57
  class BasicAgent:
58
+ def __init__(self, max_steps: int = 5):
59
+ self.graph = build_graph(max_steps)
60
 
61
  def __call__(self, question: str) -> str:
62
+ response = self.graph({"messages": [HumanMessage(content=question)]})
63
+ if response.get("messages"):
64
+ final_message = response["messages"][-1]
65
+ return final_message.content if hasattr(final_message, "content") else "No final message."
66
+ else:
67
+ return "No response."