-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
docs[patch]: Fix Code in Managing Conversation History #6252
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
@@ -526,7 +526,8 @@ | |||
"import type { BaseMessage } from \"@langchain/core/messages\";\n", | |||
"import { RunnablePassthrough, RunnableSequence } from \"@langchain/core/runnables\";\n", | |||
"\n", | |||
"const filterMessages = ({ chat_history }: { chat_history: BaseMessage[]; }) => {\n", | |||
"const filterMessages = (input: Record<string, unknown>) => {\n", | |||
" const { chat_history } = input as { chat_history: BaseMessage[] };\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
" const { chat_history } = input as { chat_history: BaseMessage[] };\n", | |
" const { chat_history }: { chat_history: BaseMessage[] } = input;\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bracesproul The previous suggestion (from unknown to any) will work fine. But, this line will cause the same issue.
Property 'chat_history' is missing in type 'Record<string, any>' but required in type '{ chat_history: BaseMessage[]; }'.ts(2741)
tutorial2_updated.ts(31, 31): 'chat_history' is declared here.
So, I believe we should have it as:
const { chat_history } = input as { chat_history: BaseMessage[] };
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jeez you're right, that's annoying. I pushed a commit doing this, which is a little more concise and does not throw any type errors const filterMessages = (input: { chat_history: BaseMessage[] }) => input.chat_history.slice(-10);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bracesproul Actually, even with the new line, the code will still cause build error. But, in a different place. If you add the new line, it will error out in the following lines:
const filterMessages = (input: { chat_history: BaseMessage[] }) => input.chat_history.slice(-10);
const chain = RunnableSequence.from([
RunnablePassthrough.assign({
chat_history: filterMessages,
}),
prompt,
model,
]).pipe(parser);
The following error will be reported:
Type '(input: { chat_history: BaseMessage[]; }) => BaseMessage[]' is not assignable to type 'RunnableLike<Record<string, unknown>, BaseMessage[]>'.
Type '(input: { chat_history: BaseMessage[]; }) => BaseMessage[]' is not assignable to type 'RunnableFunc<Record<string, unknown>, BaseMessage[]>'.
Types of parameters 'input' and 'input' are incompatible.
Property 'chat_history' is missing in type 'Record<string, unknown>' but required in type '{ chat_history: BaseMessage[]; }'.ts(2322)
tutorial2_updated.ts(30, 36): 'chat_history' is declared here.
tutorial2_updated.ts(34, 7): The expected type comes from property 'chat_history' which is declared here on type 'RunnableMapLike<Record<string, unknown>, { chat_history: BaseMessage[]; }>'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for notifying me of this. I'll just push a larger commit adding proper types throughout this entire doc. Give me a few min to do so.
Please refer #6246 for the details. The code mentioned in the site gives build error. The code change in this PR fixes the issue.