Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core[tests]: Better tests for runnable history #3537

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to pass config?

Copy link
Member Author

Choose a reason for hiding this comment

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

no I can make this optional

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`);
});