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

docs[patch]: Fix Code in Managing Conversation History #6252

Merged
merged 5 commits into from
Jul 29, 2024
Merged
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion docs/core_docs/docs/tutorials/chatbot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
" const { chat_history } = input as { chat_history: BaseMessage[] };\n",
" const { chat_history }: { chat_history: BaseMessage[] } = input;\n",

Copy link
Contributor Author

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[] };

Copy link
Member

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

Copy link
Contributor Author

@sarangan12 sarangan12 Jul 29, 2024

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[]; }>'

Copy link
Member

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.

" return chat_history.slice(-10);\n",
"};\n",
"\n",
Expand Down