Skip to content

Commit 2fe4460

Browse files
authored
docs: update the code snippet in the session (#769)
1 parent f7a4a54 commit 2fe4460

File tree

2 files changed

+14
-46
lines changed

2 files changed

+14
-46
lines changed

docs/mkdocs/en/session.md

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,8 @@ import (
4444
)
4545

4646
func main() {
47-
ctx := context.Background()
48-
4947
// 1. Create LLM model
50-
llm, err := openai.NewModel(
51-
openai.WithAPIKey("your-api-key"),
52-
openai.WithModelName("gpt-4"),
53-
)
54-
if err != nil {
55-
panic(err)
56-
}
48+
llm := openai.New("gpt-4", openai.WithAPIKey("your-api-key"))
5749

5850
// 2. (Optional) Create summarizer - automatically compress long conversation history
5951
summarizer := summary.NewSummarizer(
@@ -77,7 +69,7 @@ func main() {
7769
agent := llmagent.New(
7870
"my-agent",
7971
llmagent.WithModel(llm),
80-
llmagent.WithSystemPrompt("You are a helpful assistant"),
72+
llmagent.WithInstruction("You are a helpful assistant"),
8173
llmagent.WithAddSessionSummary(true), // Optional: enable summary injection to context
8274
// Note: WithAddSessionSummary(true) ignores WithMaxHistoryRuns configuration
8375
// Summary includes all history, incremental events fully retained
@@ -91,6 +83,7 @@ func main() {
9183
)
9284

9385
// 6. First conversation
86+
ctx := context.Background()
9487
userMsg1 := model.NewUserMessage("My name is Alice")
9588
eventChan, err := r.Run(ctx, "user123", "session-001", userMsg1)
9689
if err != nil {
@@ -1108,13 +1101,7 @@ import (
11081101
)
11091102

11101103
// Create LLM model for summarization.
1111-
summaryModel, err := openai.NewModel(
1112-
openai.WithAPIKey("your-api-key"),
1113-
openai.WithModelName("gpt-4"),
1114-
)
1115-
if err != nil {
1116-
panic(err)
1117-
}
1104+
summaryModel := openai.New("gpt-4", openai.WithAPIKey("your-api-key"))
11181105

11191106
// Create summarizer with trigger conditions.
11201107
summarizer := summary.NewSummarizer(
@@ -1434,10 +1421,7 @@ func main() {
14341421
ctx := context.Background()
14351422

14361423
// Create LLM model for both chat and summarization.
1437-
llm, _ := openai.NewModel(
1438-
openai.WithAPIKey("your-api-key"),
1439-
openai.WithModelName("gpt-4"),
1440-
)
1424+
llm := openai.New("gpt-4", openai.WithAPIKey("your-api-key"))
14411425

14421426
// Create summarizer with flexible trigger conditions.
14431427
summarizer := summary.NewSummarizer(

docs/mkdocs/zh/session.md

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,13 @@ import (
4444
)
4545

4646
func main() {
47-
ctx := context.Background()
48-
4947
// 1. 创建 LLM 模型
50-
llm, err := openai.NewModel(
51-
openai.WithAPIKey("your-api-key"),
52-
openai.WithModelName("gpt-4"),
53-
)
54-
if err != nil {
55-
panic(err)
56-
}
48+
llm := openai.New("gpt-4", openai.WithAPIKey("your-api-key"))
5749

5850
// 2. (可选)创建摘要器 - 自动压缩长对话历史
5951
summarizer := summary.NewSummarizer(
6052
llm, // 使用相同的 LLM 模型生成摘要
61-
summary.WithChecksAny( // 任一条件满足即触发摘要
53+
summary.WithChecksAny( // 任一条件满足即触发摘要
6254
summary.CheckEventThreshold(20), // 20 个事件后触发
6355
summary.CheckTokenThreshold(4000), // 4000 个 token 后触发
6456
summary.CheckTimeThreshold(5*time.Minute), // 5 分钟无活动后触发
@@ -68,16 +60,16 @@ func main() {
6860

6961
// 3. 创建 Session Service(可选,不配置则使用默认内存存储)
7062
sessionService := inmemory.NewSessionService(
71-
inmemory.WithSummarizer(summarizer), // 可选:注入摘要器
72-
inmemory.WithAsyncSummaryNum(2), // 可选:2 个异步 worker
73-
inmemory.WithSummaryQueueSize(100), // 可选:队列大小 100
63+
inmemory.WithSummarizer(summarizer), // 可选:注入摘要器
64+
inmemory.WithAsyncSummaryNum(2), // 可选:2 个异步 worker
65+
inmemory.WithSummaryQueueSize(100), // 可选:队列大小 100
7466
)
7567

7668
// 4. 创建 Agent
7769
agent := llmagent.New(
7870
"my-agent",
7971
llmagent.WithModel(llm),
80-
llmagent.WithSystemPrompt("你是一个智能助手"),
72+
llmagent.WithInstruction("你是一个智能助手"),
8173
llmagent.WithAddSessionSummary(true), // 可选:启用摘要注入到上下文
8274
// 注意:WithAddSessionSummary(true) 时会忽略 WithMaxHistoryRuns 配置
8375
// 摘要会包含所有历史,增量事件会完整保留
@@ -91,6 +83,7 @@ func main() {
9183
)
9284

9385
// 6. 第一次对话
86+
ctx := context.Background()
9487
userMsg1 := model.NewUserMessage("我叫张三")
9588
eventChan, err := r.Run(ctx, "user123", "session-001", userMsg1)
9689
if err != nil {
@@ -1106,13 +1099,7 @@ import (
11061099
)
11071100

11081101
// 创建用于摘要的 LLM 模型
1109-
summaryModel, err := openai.NewModel(
1110-
openai.WithAPIKey("your-api-key"),
1111-
openai.WithModelName("gpt-4"),
1112-
)
1113-
if err != nil {
1114-
panic(err)
1115-
}
1102+
summaryModel := openai.New("gpt-4", openai.WithAPIKey("your-api-key"))
11161103

11171104
// 创建摘要器并配置触发条件
11181105
summarizer := summary.NewSummarizer(
@@ -1517,10 +1504,7 @@ func main() {
15171504
ctx := context.Background()
15181505

15191506
// 创建用于聊天和摘要的 LLM 模型
1520-
llm, _ := openai.NewModel(
1521-
openai.WithAPIKey("your-api-key"),
1522-
openai.WithModelName("gpt-4"),
1523-
)
1507+
llm := openai.New("gpt-4", openai.WithAPIKey("your-api-key"))
15241508

15251509
// 创建带灵活触发条件的摘要器
15261510
summarizer := summary.NewSummarizer(

0 commit comments

Comments
 (0)