Spaces:
Sleeping
Sleeping
Create actionGraph.py
Browse files- src/graphs/actionGraph.py +54 -0
src/graphs/actionGraph.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langgraph.graph import StateGraph, START, END
|
| 2 |
+
from src.states.actionState import ExecutorState, ExecutorOutputState
|
| 3 |
+
|
| 4 |
+
from src.nodes.actionNode import ExecutorNode
|
| 5 |
+
from src.llms.groqllm import GroqLLM
|
| 6 |
+
from src.utils.prompts import execution_agent_prompt, compress_execution_human_message, compress_execution_system_prompt
|
| 7 |
+
|
| 8 |
+
# Import the MongoDB tools
|
| 9 |
+
from src.utils.utils import tools
|
| 10 |
+
|
| 11 |
+
class ExecutorGraphBuilder:
|
| 12 |
+
def __init__(self, llm):
|
| 13 |
+
self.llm = llm
|
| 14 |
+
self.graph = StateGraph(ExecutorState, output=ExecutorOutputState)
|
| 15 |
+
self.tools = tools # Use MongoDB tools
|
| 16 |
+
self.execution_agent_prompt = execution_agent_prompt
|
| 17 |
+
self.compress_execution_system_prompt = compress_execution_system_prompt
|
| 18 |
+
self.compress_execution_human_message = compress_execution_human_message
|
| 19 |
+
|
| 20 |
+
def build_executor_graph(self):
|
| 21 |
+
"""Build a graph to build the executor with MongoDB tools"""
|
| 22 |
+
self.executor_node_obj = ExecutorNode(self.llm)
|
| 23 |
+
|
| 24 |
+
self.graph.add_node("llm_call", self.executor_node_obj.llm_call)
|
| 25 |
+
self.graph.add_node("tool_node", self.executor_node_obj.tool_node)
|
| 26 |
+
self.graph.add_node("compress_execution", self.executor_node_obj.compress_execution)
|
| 27 |
+
|
| 28 |
+
# Flow
|
| 29 |
+
self.graph.add_edge(START, "llm_call")
|
| 30 |
+
self.graph.add_conditional_edges(
|
| 31 |
+
"llm_call",
|
| 32 |
+
self.executor_node_obj.guard_llm,
|
| 33 |
+
{
|
| 34 |
+
"tool_node": "tool_node",
|
| 35 |
+
"compress_execution": "compress_execution",
|
| 36 |
+
},
|
| 37 |
+
)
|
| 38 |
+
self.graph.add_edge("tool_node", "llm_call")
|
| 39 |
+
self.graph.add_edge("compress_execution", END)
|
| 40 |
+
|
| 41 |
+
return self.graph
|
| 42 |
+
|
| 43 |
+
def setup_graph(self):
|
| 44 |
+
return self.graph.compile()
|
| 45 |
+
|
| 46 |
+
# Initialize with MongoDB-enabled tools
|
| 47 |
+
llm = GroqLLM().get_llm()
|
| 48 |
+
|
| 49 |
+
## Creating the graph with MongoDB tools
|
| 50 |
+
graph_builder = ExecutorGraphBuilder(llm)
|
| 51 |
+
graph = graph_builder.build_executor_graph().compile()
|
| 52 |
+
|
| 53 |
+
print("MongoDB-enabled Action Graph created successfully")
|
| 54 |
+
print(f"Available tools: {[tool.name for tool in tools]}")
|