|
1 | 1 | """ |
2 | | -TODO!! |
| 2 | +Minimal LangGraph example identical to the README snippet. |
3 | 3 |
|
4 | | -This example demonstrates how to use StackOne tools with LangGraph. |
| 4 | +Run: |
| 5 | + uv run examples/langgraph_tool_node.py |
5 | 6 |
|
6 | | -```bash |
7 | | -uv run examples/langgraph_tool_node.py |
8 | | -``` |
| 7 | +Prerequisites: |
| 8 | +- `pip install 'stackone-ai[examples]'` (includes LangGraph and LangChain OpenAI) |
| 9 | +- `STACKONE_API_KEY` (required) and `OPENAI_API_KEY` (required) |
| 10 | +- Optionally set `STACKONE_ACCOUNT_ID` (required by some tools) |
9 | 11 | """ |
10 | 12 |
|
| 13 | +import os |
| 14 | +from typing import Annotated |
| 15 | + |
11 | 16 | from dotenv import load_dotenv |
| 17 | +from langchain_openai import ChatOpenAI |
| 18 | +from langgraph.graph import START, StateGraph |
| 19 | +from langgraph.graph.message import add_messages |
| 20 | +from langgraph.prebuilt import tools_condition |
| 21 | +from typing_extensions import TypedDict |
12 | 22 |
|
13 | 23 | from stackone_ai import StackOneToolSet |
| 24 | +from stackone_ai.integrations.langgraph import bind_model_with_tools, to_tool_node |
14 | 25 |
|
15 | | -load_dotenv() |
16 | | - |
17 | | -account_id = "45072196112816593343" |
18 | | -employee_id = "c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA" |
19 | 26 |
|
| 27 | +def main() -> None: |
| 28 | + load_dotenv() |
20 | 29 |
|
21 | | -def langgraph_tool_node() -> None: |
22 | | - """Demonstrate basic LangGraph integration with StackOne tools.""" |
| 30 | + # Prepare tools |
| 31 | + account_id = os.getenv("STACKONE_ACCOUNT_ID") # Set if your tools require it |
23 | 32 | toolset = StackOneToolSet() |
24 | 33 | tools = toolset.get_tools("hris_*", account_id=account_id) |
| 34 | + langchain_tools = tools.to_langchain() |
25 | 35 |
|
26 | | - # Verify we have the tools we need |
27 | | - assert len(tools) > 0, "Expected at least one HRIS tool" |
28 | | - employee_tool = tools.get_tool("hris_get_employee") |
29 | | - assert employee_tool is not None, "Expected hris_get_employee tool" |
| 36 | + class State(TypedDict): |
| 37 | + messages: Annotated[list, add_messages] |
30 | 38 |
|
31 | | - # TODO: Add LangGraph specific integration |
32 | | - # For now, just verify the tools are properly configured |
33 | | - langchain_tools = tools.to_langchain() |
34 | | - assert len(langchain_tools) > 0, "Expected LangChain tools" |
35 | | - assert all(hasattr(tool, "_run") for tool in langchain_tools), "Expected all tools to have _run method" |
| 39 | + # Build a small agent loop: LLM -> maybe tools -> back to LLM |
| 40 | + graph = StateGraph(State) |
| 41 | + graph.add_node("tools", to_tool_node(langchain_tools)) |
| 42 | + |
| 43 | + def call_llm(state: dict): |
| 44 | + llm = ChatOpenAI(model="gpt-4o-mini") |
| 45 | + llm = bind_model_with_tools(llm, langchain_tools) |
| 46 | + resp = llm.invoke(state["messages"]) # returns AIMessage with optional tool_calls |
| 47 | + return {"messages": state["messages"] + [resp]} |
| 48 | + |
| 49 | + graph.add_node("llm", call_llm) |
| 50 | + graph.add_edge(START, "llm") |
| 51 | + graph.add_conditional_edges("llm", tools_condition) |
| 52 | + graph.add_edge("tools", "llm") |
| 53 | + app = graph.compile() |
| 54 | + |
| 55 | + # Kick off with a simple instruction; replace IDs as needed |
| 56 | + _ = app.invoke({"messages": [("user", "Get employee with id emp123")]}) |
36 | 57 |
|
37 | 58 |
|
38 | 59 | if __name__ == "__main__": |
39 | | - langgraph_tool_node() |
| 60 | + main() |
0 commit comments