Skip to content

Commit

Permalink
core[tests]: Better tests for runnable history (#3537)
Browse files Browse the repository at this point in the history
* core[tests]: Better tests for runnable history

* cr
  • Loading branch information
bracesproul authored Dec 5, 2023
1 parent 8b9d6c5 commit 7f6df0e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion langchain-core/src/runnables/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ type GetSessionHistoryCallable = (
export interface RunnableWithMessageHistoryInputs<RunInput, RunOutput>
extends Omit<
RunnableBindingArgs<RunInput, RunOutput, BaseCallbackConfig>,
"bound"
"bound" | "config"
> {
runnable: Runnable<RunInput, RunOutput>;
getMessageHistory: GetSessionHistoryCallable;
inputMessagesKey?: string;
outputMessagesKey?: string;
historyMessagesKey?: string;
config?: RunnableConfig;
}

export class RunnableWithMessageHistory<
Expand Down Expand Up @@ -70,8 +71,11 @@ export class RunnableWithMessageHistory<
)
.withConfig({ runName: "RunnableWithMessageHistory" });

const config = fields.config ?? {};

super({
...fields,
config,
bound,
});
this.runnable = fields.runnable;
Expand Down
30 changes: 30 additions & 0 deletions langchain-core/src/runnables/tests/runnable_history.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
} from "../../chat_history.js";
import {
FakeChatMessageHistory,
FakeLLM,
FakeListChatMessageHistory,
} from "../../utils/testing/index.js";
import { ChatPromptTemplate, MessagesPlaceholder } from "../../prompts/chat.js";

// For `BaseChatMessageHistory`
async function getGetSessionHistory(): Promise<
Expand Down Expand Up @@ -90,3 +92,31 @@ test("Runnable with message history work with chat list memory", async () => {
output = await withHistory.invoke([new HumanMessage("good bye")], config);
expect(output).toBe("you said: hello\ngood bye");
});

test("Runnable with message history and RunnableSequence", async () => {
const prompt = ChatPromptTemplate.fromMessages([
["ai", "You are a helpful assistant"],
new MessagesPlaceholder("history"),
["human", "{input}"],
]);
const model = new FakeLLM({});
const chain = prompt.pipe(model);

const getListMessageHistory = await getListSessionHistory();
const withHistory = new RunnableWithMessageHistory({
runnable: chain,
config: {},
getMessageHistory: getListMessageHistory,
inputMessagesKey: "input",
historyMessagesKey: "history",
});
const config: RunnableConfig = { configurable: { sessionId: "1" } };
let output = await withHistory.invoke({ input: "hello" }, config);
expect(output).toBe("AI: You are a helpful assistant\nHuman: hello");
output = await withHistory.invoke({ input: "good bye" }, config);
expect(output).toBe(`AI: You are a helpful assistant
Human: hello
AI: AI: You are a helpful assistant
Human: hello
Human: good bye`);
});

2 comments on commit 7f6df0e

@vercel
Copy link

@vercel vercel bot commented on 7f6df0e Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

langchainjs-docs – ./docs/core_docs/

langchainjs-docs-git-main-langchain.vercel.app
langchainjs-docs-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
js.langchain.com

@vercel
Copy link

@vercel vercel bot commented on 7f6df0e Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.