Repository navigation
Expand file tree
/
Copy pathlanggraph_integration.py
More file actions
81 lines (63 loc) · 2.3 KB
/
Copy pathlanggraph_integration.py
File metadata and controls
81 lines (63 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
This example demonstrates how to use StackOne tools with LangGraph.
LangGraph uses LangChain tools natively with its prebuilt ReAct agent.
```bash
uv run examples/langgraph_integration.py
```
"""
from __future__ import annotations
import os
import sys
try:
from dotenv import load_dotenv
load_dotenv()
except ModuleNotFoundError:
pass
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from stackone_ai import StackOneToolSet
from stackone_ai.types import StackOneError, ToolsetError
def langgraph_integration() -> None:
for var in ["STACKONE_API_KEY", "STACKONE_ACCOUNT_ID", "OPENAI_API_KEY"]:
if not os.getenv(var):
raise SystemExit(f"Set {var} to run this example.")
account_id = os.environ["STACKONE_ACCOUNT_ID"]
toolset = StackOneToolSet()
tools = toolset.fetch_tools(
actions=["*_list_*"],
account_ids=[account_id],
)
if not tools:
raise SystemExit(f"Account {account_id} served no list tools, so the example would prove nothing.")
# LangGraph uses LangChain tools natively
langchain_tools = tools.to_langchain()
# Hand a rejected call back to the agent instead of killing the graph. Models
# do guess arguments wrong, and StackOne's 400 names the offending field — so
# the agent can read it and retry. Without this, one bad guess ends the run.
for langchain_tool in langchain_tools:
langchain_tool.handle_tool_error = True
print(f"Loaded {len(langchain_tools)} LangGraph tools.")
for tool in langchain_tools:
print(f" - {tool.name}")
# Create a ReAct agent with LangGraph
model = ChatOpenAI(model="gpt-6.2-sol", use_responses_api=True)
agent = create_agent(model, langchain_tools)
result = agent.invoke(
{
"messages": [
(
"user",
"Use one of your tools to list a few records, then summarise them.",
)
]
}
)
final_message = result["messages"][-1]
print(f"Agent response:\n{final_message.text}")
if __name__ == "__main__":
try:
langgraph_integration()
except ToolsetError as exc:
sys.exit(f"Could not load tools: {exc}")
except StackOneError as exc:
sys.exit(f"StackOne API error: {exc}")