Spaces:
Runtime error
Runtime error
Update LG_agent.py
Browse files- LG_agent.py +30 -24
LG_agent.py
CHANGED
|
@@ -1,48 +1,52 @@
|
|
| 1 |
-
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 langgraph.prebuilt import tools_condition
|
| 6 |
from langgraph.graph import START, StateGraph
|
| 7 |
-
from
|
| 8 |
from tools import all_tools
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
llm=HuggingFaceEndpoint(
|
| 13 |
-
# repo_id="google/gemma-7b-it",
|
| 14 |
-
repo_id="tiiuae/falcon-7b-instruct",
|
| 15 |
-
temperature=0,
|
| 16 |
-
max_new_tokens=512,
|
| 17 |
-
),
|
| 18 |
-
verbose=True,
|
| 19 |
-
)
|
| 20 |
|
|
|
|
| 21 |
class AgentState(TypedDict):
|
| 22 |
messages: Annotated[list[AnyMessage], add_messages]
|
| 23 |
|
|
|
|
|
|
|
| 24 |
def assistant(state: AgentState):
|
| 25 |
-
chat_with_tools = build_llm().bind_tools(all_tools) # Bind tools dynamically
|
| 26 |
return {
|
| 27 |
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 28 |
}
|
| 29 |
|
|
|
|
| 30 |
def build_graph(max_steps: int = 5):
|
| 31 |
builder = StateGraph(AgentState)
|
| 32 |
-
|
| 33 |
builder.add_node("assistant", assistant)
|
| 34 |
builder.add_node("tools", ToolNode(all_tools))
|
| 35 |
-
|
| 36 |
builder.add_edge(START, "assistant")
|
| 37 |
-
builder.add_conditional_edges(
|
| 38 |
-
"assistant",
|
| 39 |
-
tools_condition,
|
| 40 |
-
)
|
| 41 |
builder.add_edge("tools", "assistant")
|
| 42 |
-
|
| 43 |
graph = builder.compile()
|
| 44 |
|
| 45 |
-
# Wrap the graph with step limiter
|
| 46 |
def limited_invoke(input_state):
|
| 47 |
steps = 0
|
| 48 |
state = input_state
|
|
@@ -50,12 +54,14 @@ def build_graph(max_steps: int = 5):
|
|
| 50 |
state = graph.invoke(state)
|
| 51 |
latest_message = state["messages"][-1] if state["messages"] else None
|
| 52 |
if latest_message and isinstance(latest_message, AIMessage):
|
| 53 |
-
break
|
| 54 |
steps += 1
|
| 55 |
return state
|
| 56 |
|
| 57 |
return limited_invoke
|
| 58 |
|
|
|
|
|
|
|
| 59 |
class BasicAgent:
|
| 60 |
def __init__(self, max_steps: int = 5):
|
| 61 |
self.graph = build_graph(max_steps)
|
|
@@ -66,4 +72,4 @@ class BasicAgent:
|
|
| 66 |
final_message = response["messages"][-1]
|
| 67 |
return final_message.content if hasattr(final_message, "content") else "No final message."
|
| 68 |
else:
|
| 69 |
-
return "No response."
|
|
|
|
|
|
|
| 1 |
from typing import Annotated, TypedDict
|
| 2 |
from langgraph.graph.message import add_messages
|
| 3 |
from langchain_core.messages import HumanMessage, AIMessage, AnyMessage
|
| 4 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 5 |
from langgraph.graph import START, StateGraph
|
| 6 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 7 |
from tools import all_tools
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 11 |
+
|
| 12 |
+
if not HUGGINGFACEHUB_API_TOKEN:
|
| 13 |
+
raise ValueError("Missing Hugging Face token. Set HUGGINGFACEHUB_API_TOKEN environment variable.")
|
| 14 |
+
|
| 15 |
+
# 1. Setup once
|
| 16 |
+
llm = HuggingFaceEndpoint(
|
| 17 |
+
repo_id="tiiuae/falcon-7b-instruct", # Change model here easily
|
| 18 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 19 |
+
temperature=0,
|
| 20 |
+
max_new_tokens=512,
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
chat = ChatHuggingFace(llm=llm, verbose=True)
|
| 24 |
+
chat_with_tools = chat.bind_tools(all_tools)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# 2. Define the agent state
|
| 27 |
class AgentState(TypedDict):
|
| 28 |
messages: Annotated[list[AnyMessage], add_messages]
|
| 29 |
|
| 30 |
+
# 3. Assistant node
|
| 31 |
+
|
| 32 |
def assistant(state: AgentState):
|
|
|
|
| 33 |
return {
|
| 34 |
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 35 |
}
|
| 36 |
|
| 37 |
+
# 4. Build the agent graph
|
| 38 |
def build_graph(max_steps: int = 5):
|
| 39 |
builder = StateGraph(AgentState)
|
| 40 |
+
|
| 41 |
builder.add_node("assistant", assistant)
|
| 42 |
builder.add_node("tools", ToolNode(all_tools))
|
| 43 |
+
|
| 44 |
builder.add_edge(START, "assistant")
|
| 45 |
+
builder.add_conditional_edges("assistant", tools_condition)
|
|
|
|
|
|
|
|
|
|
| 46 |
builder.add_edge("tools", "assistant")
|
| 47 |
+
|
| 48 |
graph = builder.compile()
|
| 49 |
|
|
|
|
| 50 |
def limited_invoke(input_state):
|
| 51 |
steps = 0
|
| 52 |
state = input_state
|
|
|
|
| 54 |
state = graph.invoke(state)
|
| 55 |
latest_message = state["messages"][-1] if state["messages"] else None
|
| 56 |
if latest_message and isinstance(latest_message, AIMessage):
|
| 57 |
+
break
|
| 58 |
steps += 1
|
| 59 |
return state
|
| 60 |
|
| 61 |
return limited_invoke
|
| 62 |
|
| 63 |
+
# 5. BasicAgent class
|
| 64 |
+
|
| 65 |
class BasicAgent:
|
| 66 |
def __init__(self, max_steps: int = 5):
|
| 67 |
self.graph = build_graph(max_steps)
|
|
|
|
| 72 |
final_message = response["messages"][-1]
|
| 73 |
return final_message.content if hasattr(final_message, "content") else "No final message."
|
| 74 |
else:
|
| 75 |
+
return "No response."
|