Skip to content

Commit 393b6b0

Browse files
authored
docs: fix delete_old_messages example to properly demonstrate deletion effect (#4009)
1 parent dcfc27a commit 393b6b0

1 file changed

Lines changed: 34 additions & 12 deletions

File tree

src/oss/langchain/short-term-memory.mdx

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,13 @@ for event in agent.stream(
392392
):
393393
print([(message.type, message.content) for message in event["messages"]])
394394

395+
for event in agent.stream(
396+
{"messages": [{"role": "user", "content": "write a short poem about cats"}]},
397+
config,
398+
stream_mode="values",
399+
):
400+
print([(message.type, message.content) for message in event["messages"]])
401+
395402
for event in agent.stream(
396403
{"messages": [{"role": "user", "content": "what's my name?"}]},
397404
config,
@@ -403,9 +410,12 @@ for event in agent.stream(
403410
```
404411
[('human', "hi! I'm bob")]
405412
[('human', "hi! I'm bob"), ('ai', 'Hi Bob! Nice to meet you. How can I help you today? I can answer questions, brainstorm ideas, draft text, explain things, or help with code.')]
406-
[('human', "hi! I'm bob"), ('ai', 'Hi Bob! Nice to meet you. How can I help you today? I can answer questions, brainstorm ideas, draft text, explain things, or help with code.'), ('human', "what's my name?")]
407-
[('human', "hi! I'm bob"), ('ai', 'Hi Bob! Nice to meet you. How can I help you today? I can answer questions, brainstorm ideas, draft text, explain things, or help with code.'), ('human', "what's my name?"), ('ai', 'Your name is Bob. How can I help you today, Bob?')]
408-
[('human', "what's my name?"), ('ai', 'Your name is Bob. How can I help you today, Bob?')]
413+
[('human', "hi! I'm bob"), ('ai', 'Hi Bob! Nice to meet you. How can I help you today? I can answer questions, brainstorm ideas, draft text, explain things, or help with code.'), ('human', "write a short poem about cats")]
414+
[('human', "hi! I'm bob"), ('ai', 'Hi Bob! Nice to meet you. How can I help you today? I can answer questions, brainstorm ideas, draft text, explain things, or help with code.'), ('human', "write a short poem about cats"), ('ai', 'There once was a cat on a wall, Who barely moved at all...')]
415+
[('human', 'write a short poem about cats'), ('ai', 'There once was a cat on a wall, Who barely moved at all...')]
416+
[('human', 'write a short poem about cats'), ('ai', 'There once was a cat on a wall, Who barely moved at all...'), ('human', "what's my name?")]
417+
[('human', 'write a short poem about cats'), ('ai', 'There once was a cat on a wall, Who barely moved at all...'), ('human', "what's my name?"), ('ai', "I don't know your name - you haven't told me!")]
418+
[('human', "what's my name?"), ('ai', "I don't know your name - you haven't told me!")]
409419
```
410420
:::
411421

@@ -454,9 +464,7 @@ for await (const event of streamA) {
454464
}
455465

456466
const streamB = await agent.stream(
457-
{
458-
messages: [{ role: "user", content: "what's my name?" }],
459-
},
467+
{ messages: [{ role: "user", content: "write a short poem about cats" }] },
460468
{ ...config, streamMode: "values" }
461469
);
462470
for await (const event of streamB) {
@@ -466,15 +474,29 @@ for await (const event of streamB) {
466474
]);
467475
console.log(messageDetails);
468476
}
477+
478+
const streamC = await agent.stream(
479+
{ messages: [{ role: "user", content: "what's my name?" }] },
480+
{ ...config, streamMode: "values" }
481+
);
482+
for await (const event of streamC) {
483+
const messageDetails = event.messages.map((message) => [
484+
message.getType(),
485+
message.content,
486+
]);
487+
console.log(messageDetails);
488+
}
469489
```
470490

471491
```
472-
[[ "human", "hi! I'm bob" ]]
473-
[[ "human", "hi! I'm bob" ], [ "ai", "Hello, Bob! How can I assist you today?" ]]
474-
[[ "human", "hi! I'm bob" ], [ "ai", "Hello, Bob! How can I assist you today?" ]]
475-
[[ "human", "hi! I'm bob" ], [ "ai", "Hello, Bob! How can I assist you today" ], ["human", "what's my name?" ]]
476-
[[ "human", "hi! I'm bob" ], [ "ai", "Hello, Bob! How can I assist you today?" ], ["human", "what's my name?"], [ "ai", "Your name is Bob, as you mentioned. How can I help you further?" ]]
477-
[[ "human", "what's my name?" ], [ "ai", "Your name is Bob, as you mentioned. How can I help you further?" ]]
492+
[["human", "hi! I'm bob"]]
493+
[["human", "hi! I'm bob"], ["ai", "Hi Bob! Nice to meet you. How can I help you today? I can answer questions, brainstorm ideas, draft text, explain things, or help with code."]]
494+
[["human", "hi! I'm bob"], ["ai", "Hi Bob! Nice to meet you. How can I help you today? I can answer questions, brainstorm ideas, draft text, explain things, or help with code."], ["human", "write a short poem about cats"]]
495+
[["human", "hi! I'm bob"], ["ai", "Hi Bob! Nice to meet you. How can I help you today? I can answer questions, brainstorm ideas, draft text, explain things, or help with code."], ["human", "write a short poem about cats"], ["ai", "There once was a cat on a wall, Who barely moved at all..."]]
496+
[["human", "write a short poem about cats"], ["ai", "There once was a cat on a wall, Who barely moved at all..."]]
497+
[["human", "write a short poem about cats"], ["ai", "There once was a cat on a wall, Who barely moved at all..."], ["human", "what's my name?"]]
498+
[["human", "write a short poem about cats"], ["ai", "There once was a cat on a wall, Who barely moved at all..."], ["human", "what's my name?"], ["ai", "I don't know your name - you haven't told me!"]]
499+
[["human", "what's my name?"], ["ai", "I don't know your name - you haven't told me!"]]
478500
```
479501
:::
480502

0 commit comments

Comments
 (0)