chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# patterns/langgraph-single-agent/tools/__init__.py
|
||||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from .query_data import query_data
|
||||
from .todos import AgentState, todo_tools
|
||||
|
||||
__all__ = ["query_data", "AgentState", "todo_tools"]
|
||||
@@ -0,0 +1,16 @@
|
||||
date,category,amount,type
|
||||
2026-01-05,Food,42.50,expense
|
||||
2026-01-10,Transport,15.00,expense
|
||||
2026-01-15,Salary,3500.00,income
|
||||
2026-01-20,Entertainment,80.00,expense
|
||||
2026-01-25,Utilities,120.00,expense
|
||||
2026-02-03,Food,55.20,expense
|
||||
2026-02-08,Freelance,800.00,income
|
||||
2026-02-14,Dining,65.00,expense
|
||||
2026-02-20,Transport,22.50,expense
|
||||
2026-02-28,Salary,3500.00,income
|
||||
2026-03-05,Groceries,95.40,expense
|
||||
2026-03-10,Gym,40.00,expense
|
||||
2026-03-15,Salary,3500.00,income
|
||||
2026-03-18,Coffee,18.75,expense
|
||||
2026-03-22,Books,35.00,expense
|
||||
|
@@ -0,0 +1,25 @@
|
||||
# patterns/langgraph-single-agent/tools/query_data.py
|
||||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import csv
|
||||
from pathlib import Path
|
||||
|
||||
from langchain.tools import tool
|
||||
|
||||
# Read at module load time — avoids file I/O on every tool invocation.
|
||||
_csv_path = Path(__file__).parent / "db.csv"
|
||||
try:
|
||||
with open(_csv_path) as _f:
|
||||
_cached_data = list(csv.DictReader(_f))
|
||||
except (FileNotFoundError, OSError) as e:
|
||||
raise RuntimeError(f"query_data: cannot load sample data from {_csv_path}") from e
|
||||
|
||||
|
||||
@tool
|
||||
def query_data(query: str) -> list[dict]:
|
||||
"""
|
||||
Query the database. Accepts natural language.
|
||||
Always call this tool before displaying a chart or graph.
|
||||
"""
|
||||
return _cached_data
|
||||
@@ -0,0 +1,66 @@
|
||||
# patterns/langgraph-single-agent/tools/todos.py
|
||||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import uuid
|
||||
from typing import Literal, TypedDict
|
||||
|
||||
from langchain.agents import AgentState as BaseAgentState
|
||||
from langchain.tools import ToolRuntime, tool
|
||||
from langchain_core.messages import ToolMessage
|
||||
from langgraph.types import Command
|
||||
|
||||
# ToolRuntime is confirmed available at langchain.tools (langchain >= 1.2).
|
||||
# If you see an ImportError, verify your langchain version is >= 0.3.
|
||||
|
||||
|
||||
class Todo(TypedDict):
|
||||
id: str
|
||||
title: str
|
||||
description: str
|
||||
emoji: str
|
||||
status: Literal["pending", "completed"]
|
||||
|
||||
|
||||
class AgentState(BaseAgentState):
|
||||
todos: list[Todo]
|
||||
|
||||
|
||||
def _assign_ids(todos: list[dict]) -> list[dict]:
|
||||
"""Assign a uuid4 to any todo that has a missing or empty 'id'."""
|
||||
for todo in todos:
|
||||
if not todo.get("id"):
|
||||
todo["id"] = str(uuid.uuid4())
|
||||
return todos
|
||||
|
||||
|
||||
@tool
|
||||
def manage_todos(todos: list[Todo], runtime: ToolRuntime) -> Command:
|
||||
"""
|
||||
Manage the current todos. Replaces the entire todo list.
|
||||
Assigns a unique UUID to any todo that is missing one.
|
||||
"""
|
||||
_assign_ids(todos) # type: ignore[arg-type]
|
||||
|
||||
return Command(
|
||||
update={
|
||||
"todos": todos,
|
||||
"messages": [
|
||||
ToolMessage(
|
||||
content="Successfully updated todos",
|
||||
tool_call_id=runtime.tool_call_id,
|
||||
)
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@tool
|
||||
def get_todos(runtime: ToolRuntime) -> list[Todo]:
|
||||
"""
|
||||
Get the current todo list from agent state.
|
||||
"""
|
||||
return runtime.state.get("todos", [])
|
||||
|
||||
|
||||
todo_tools = [manage_todos, get_todos]
|
||||
Reference in New Issue
Block a user