호출 간에 대화 컨텍스트를 유지하는 데 사용합니다 AgentSession .
세션이 서비스 관리 스토리지를 사용하는 경우 불투명한 서비스 쪽 세션 ID를 포함할 수 있습니다. OpenAI 응답 및 대화 ID는 기본적으로 지원 API 키 또는 프로젝트로 범위가 지정됩니다. 호스트된 에이전트가 여러 최종 사용자에 대해 동일한 키 또는 프로젝트를 사용하는 경우 다시 시작하기 전에 해당 ID 서버 쪽을 저장하고 인증된 사용자 또는 테넌트를 확인합니다. 자세한 내용은 세션을 참조 하세요.
핵심 사용 패턴
대부분의 애플리케이션은 동일한 흐름을 따릅니다.
- 세션 만들기(
CreateSessionAsync()) - 각
RunAsync(...)에 해당 세션 전달 - 직렬화된 상태에서 리하이드레이션(
DeserializeSessionAsync(...)) - 서비스 대화 ID를 계속 사용합니다(예: 에이전트에 따라 다름).
myChatClientAgent.CreateSessionAsync("existing-id")
- 세션 만들기(
create_session()) - 각
run(...)에 해당 세션 전달 - 서비스 대화 ID(
get_session(...)) 또는 직렬화된 상태로 리하이드레이션
- 세션 만들기(
CreateSession(...)) -
agent.WithSession(session)을 사용하여 해당 세션을 각RunText(...)에 전달합니다 - 직렬화된 상태에서
json.Unmarshal(...)를 사용해agent.Session로 재구성합니다.
Go agent 패키지는 대화 상태를 위한 핵심 유형을 제공합니다. 즉, agent.Session는 대화에 연결된 키-값 상태를 위한 것이고 agent.ContextProvider는 컨텍스트 주입 및 지속성을 위한 것입니다.
// 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)
session, err := a.CreateSession(ctx)
if err != nil {
panic(err)
}
멀티턴 대화에 세션 사용하기
resp, _ := a.RunText(ctx, "My name is Alice.", agent.WithSession(session)).Collect()
resp, _ = a.RunText(ctx, "What is my name?", agent.WithSession(session)).Collect()
세션 유지
스토리지를 위해 세션을 JSON으로 직렬화하고 나중에 다시 시작합니다.
data, err := json.Marshal(session)
if err != nil {
panic(err)
}
// store data...
// later:
var resumed agent.Session
if err := json.Unmarshal(data, &resumed); err != nil {
panic(err)
}
resp, err := a.RunText(ctx, "Continue from where we left off.", agent.WithSession(&resumed)).Collect()
가이드 맵
| 페이지 | 포커스 |
|---|---|
| 세션 |
AgentSession 구조 및 시리얼라이제이션 |
| 컨텍스트 공급자 | 기본 제공 및 사용자 지정 컨텍스트/기록 공급자 패턴 |
| 컨텍스트 압축 | 대화 증가를 효율적으로 관리 |
| 스토리지 | 기본 제공 스토리지 모드 및 외부 지속성 전략 |