通过


对话和内存概述

使用 AgentSession 在调用之间保留对话上下文。

核心使用模式

大多数应用程序遵循相同的流:

  1. 创建会话 (create_session()
  2. 将该会话传递给每个 run(...)
  3. 按服务会话 ID(get_session(...))或序列化状态解除冻结
// Create and reuse a session
AgentSession session = await agent.CreateSessionAsync();

var first = await agent.RunAsync("My name is Alice.", session);
var second = await agent.RunAsync("What is my name?", session);

// Persist and restore later
var serialized = agent.SerializeSession(session);
AgentSession resumed = await agent.DeserializeSessionAsync(serialized);
# Create and reuse a session
session = agent.create_session()

first = await agent.run("My name is Alice.", session=session)
second = await agent.run("What is my name?", session=session)

# Rehydrate by service conversation ID when needed
service_session = agent.get_session(service_session_id="<service-conversation-id>")

# Persist and restore later
serialized = session.to_dict()
resumed = AgentSession.from_dict(serialized)

指南图

焦点
会期 AgentSession 结构(session_idservice_session_idstate)和序列化
上下文提供器 内置和自定义上下文/历史记录提供者模式
存储 内置存储模式和外部持久性策略

后续步骤