6.3 Assistant 与配置
本章依据上游 Notebook 与 Python 实现重新整理;教学说明为本站新增中文内容,英文版与之对应。
版本与运行环境
代码对应上游 2025-10-12 快照,并非对最新版 SDK 的兼容承诺。以下原始代码按 Notebook 单元顺序保留,包含 Jupyter %pip / %%capture 等语法,应在 Notebook 环境分单元执行。凭据通过本地环境配置,不要写入网页或提交 Git。本网站只渲染教程,未运行这些付费模型或 Docker 实验。
同一张图的不同配置
Assistant 将配置绑定到图,便于在不复制图代码的情况下实验。configuration.py 提供 user_id、todo_category 与 task_maistro_role。这里指 LangGraph 服务资源。
区分个人与工作任务
为个人与工作助手设置不同的 todo_category 和角色说明。图节点读取这些 configurable 字段,Store namespace 同时包含分类和用户身份,因此同一用户可有多张列表。客户端传入 user_id 并不构成身份认证。
管理助手版本
Notebook 演示搜索助手、更新个人助手配置以创建新版本,以及创建工作助手。还会创建一个备用助手专门演示删除。运行时应将示例 ID 替换为自己服务返回的 ID。
原始代码单元
保留上游代码中的英文注释和标识符。各单元共享前面的变量;Notebook 输出与历史截图不作为本次运行结果。
单元 1
python
%%capture --no-stderr
%pip install -U langgraph_sdk单元 2
python
from langgraph_sdk import get_client
url_for_cli_deployment = "http://localhost:8123"
client = get_client(url=url_for_cli_deployment)单元 3
python
personal_assistant = await client.assistants.create(
# "task_maistro" is the name of a graph we deployed
"task_maistro",
config={"configurable": {"todo_category": "personal"}}
)
print(personal_assistant)单元 4
python
task_maistro_role = """You are a friendly and organized personal task assistant. Your main focus is helping users stay on top of their personal tasks and commitments. Specifically:
- Help track and organize personal tasks
- When providing a 'todo summary':
1. List all current tasks grouped by deadline (overdue, today, this week, future)
2. Highlight any tasks missing deadlines and gently encourage adding them
3. Note any tasks that seem important but lack time estimates
- Proactively ask for deadlines when new tasks are added without them
- Maintain a supportive tone while helping the user stay accountable
- Help prioritize tasks based on deadlines and importance
Your communication style should be encouraging and helpful, never judgmental.
When tasks are missing deadlines, respond with something like "I notice [task] doesn't have a deadline yet. Would you like to add one to help us track it better?"""
configurations = {"todo_category": "personal",
"user_id": "lance",
"task_maistro_role": task_maistro_role}
personal_assistant = await client.assistants.update(
personal_assistant["assistant_id"],
config={"configurable": configurations}
)
print(personal_assistant)单元 5
python
task_maistro_role = """You are a focused and efficient work task assistant.
Your main focus is helping users manage their work commitments with realistic timeframes.
Specifically:
- Help track and organize work tasks
- When providing a 'todo summary':
1. List all current tasks grouped by deadline (overdue, today, this week, future)
2. Highlight any tasks missing deadlines and gently encourage adding them
3. Note any tasks that seem important but lack time estimates
- When discussing new tasks, suggest that the user provide realistic time-frames based on task type:
• Developer Relations features: typically 1 day
• Course lesson reviews/feedback: typically 2 days
• Documentation sprints: typically 3 days
- Help prioritize tasks based on deadlines and team dependencies
- Maintain a professional tone while helping the user stay accountable
Your communication style should be supportive but practical.
When tasks are missing deadlines, respond with something like "I notice [task] doesn't have a deadline yet. Based on similar tasks, this might take [suggested timeframe]. Would you like to set a deadline with this in mind?"""
configurations = {"todo_category": "work",
"user_id": "lance",
"task_maistro_role": task_maistro_role}
work_assistant = await client.assistants.create(
# "task_maistro" is the name of a graph we deployed
"task_maistro",
config={"configurable": configurations}
)
print(work_assistant)单元 6
python
assistants = await client.assistants.search()
for assistant in assistants:
print({
'assistant_id': assistant['assistant_id'],
'version': assistant['version'],
'config': assistant['config']
})单元 7
python
# create a temporary assitant
temp_assistant = await client.assistants.create(
"task_maistro",
config={"configurable": configurations}
)
assistants = await client.assistants.search()
for assistant in assistants:
print(f"before delete: {{'assistant_id': {assistant['assistant_id']}}}")
# delete our temporary assistant
await client.assistants.delete(assistants[-1]["assistant_id"])
print()
assistants = await client.assistants.search()
for assistant in assistants:
print(f"after delete: {{'assistant_id': {assistant['assistant_id']} }}")单元 8
python
work_assistant_id = assistants[0]['assistant_id']
personal_assistant_id = assistants[1]['assistant_id']单元 9
python
from langchain_core.messages import HumanMessage
from langchain_core.messages import convert_to_messages
user_input = "Create or update few ToDos: 1) Re-film Module 6, lesson 5 by end of day today. 2) Update audioUX by next Monday."
thread = await client.threads.create()
async for chunk in client.runs.stream(thread["thread_id"],
work_assistant_id,
input={"messages": [HumanMessage(content=user_input)]},
stream_mode="values"):
if chunk.event == 'values':
state = chunk.data
convert_to_messages(state["messages"])[-1].pretty_print()单元 10
python
user_input = "Create another ToDo: Finalize set of report generation tutorials."
thread = await client.threads.create()
async for chunk in client.runs.stream(thread["thread_id"],
work_assistant_id,
input={"messages": [HumanMessage(content=user_input)]},
stream_mode="values"):
if chunk.event == 'values':
state = chunk.data
convert_to_messages(state["messages"])[-1].pretty_print()单元 11
python
user_input = "OK, for this task let's get it done by next Tuesday."
async for chunk in client.runs.stream(thread["thread_id"],
work_assistant_id,
input={"messages": [HumanMessage(content=user_input)]},
stream_mode="values"):
if chunk.event == 'values':
state = chunk.data
convert_to_messages(state["messages"])[-1].pretty_print()单元 12
python
user_input = "Create ToDos: 1) Check on swim lessons for the baby this weekend. 2) For winter travel, check AmEx points."
thread = await client.threads.create()
async for chunk in client.runs.stream(thread["thread_id"],
personal_assistant_id,
input={"messages": [HumanMessage(content=user_input)]},
stream_mode="values"):
if chunk.event == 'values':
state = chunk.data
convert_to_messages(state["messages"])[-1].pretty_print()单元 13
python
user_input = "Give me a todo summary."
thread = await client.threads.create()
async for chunk in client.runs.stream(thread["thread_id"],
personal_assistant_id,
input={"messages": [HumanMessage(content=user_input)]},
stream_mode="values"):
if chunk.event == 'values':
state = chunk.data
convert_to_messages(state["messages"])[-1].pretty_print()练习与检查
分别通过两个助手新增任务,再查询各自分类。验证角色说明是否影响创建任务的行为,同时列表保持分开。删除操作仅针对备用测试助手。
完整讲解、历史输出和图示请对照 Notebook。