langchain-ai / langchainjs

πŸ¦œπŸ”— Build context-aware reasoning applications πŸ¦œπŸ”—
https://js.langchain.com/docs/
MIT License
12.29k stars 2.08k forks source link

Type 'FewShotChatMessagePromptTemplate<any, any>' is not assignable to type 'ChatPromptTemplate<InputValues, string> | BaseMessagePromptTemplateLike' #5331

Closed youngsupchoi closed 3 months ago

youngsupchoi commented 4 months ago

Checked other resources

Example Code

import { ChatOpenAI } from "@langchain/openai";
import {
  ChatPromptTemplate,
  FewShotChatMessagePromptTemplate,
} from "@langchain/core/prompts";

const chat = new ChatOpenAI({});

const examples = [
  { input: "2+2", output: "4" },
  { input: "2+3", output: "5" },
];

const examplePrompt = ChatPromptTemplate.fromMessages([
  ["human", "{input}"],
  ["ai", "{output}"],
]);

const fewShotPrompt = new FewShotChatMessagePromptTemplate({
  examplePrompt,
  examples,
  inputVariables: ["input"],
});

const finalPrompt = ChatPromptTemplate.fromMessages([
  ["system", "You are a trustworthy AI assistant."],
  fewShotPrompt,
  ["human", "{input}"],
]);

const formattedPrompt = await finalPrompt.format({
  input: "What is 2+2?",
});
Type 'FewShotChatMessagePromptTemplate<any, any>' is not assignable to type 'ChatPromptTemplate<InputValues, string> | BaseMessagePromptTemplateLike'.
  Type 'FewShotChatMessagePromptTemplate<any, any>' is missing the following properties from type 'ChatPromptTemplate<InputValues, string>': promptMessages, _parseImagePromptsts(2322)

Error Message and Stack Trace (if applicable)

Type 'FewShotChatMessagePromptTemplate<any, any>' is not assignable to type 'ChatPromptTemplate<InputValues, string> | BaseMessagePromptTemplateLike'.
  Type 'FewShotChatMessagePromptTemplate<any, any>' is missing the following properties from type 'ChatPromptTemplate<InputValues, string>': promptMessages, _parseImagePromptsts(2322)

https://www.youtube.com/watch?v=5ozH2z_vtj4&list=PLbGui_ZYuhigsI3cWlv-Dl3_R7CDNQR0u&index=8

Description

I followed the instructions in the YouTube video exactly, but I am experiencing a bug like the one mentioned above. I have been trying to resolve this bug for 10 hours but haven't been able to fix it yet, so I need your help.

Additionally, I am curious about something mentioned in the video. It refers to a langchain version 0.1.9, but such a version does not exist.

System Info

"@langchain/openai": "^0.0.28",
"langchain": "^0.1.36",

node -v v20.5.1

dosubot[bot] commented 4 months ago

The issue you're encountering with FewShotChatMessagePromptTemplate not being assignable to ChatPromptTemplate in LangChain.js is due to a type mismatch between these two. This typically happens when the expected properties or methods in one type are not present in the other. To resolve this, you have a few options:

  1. Check for Missing Properties or Methods: Ensure that FewShotChatMessagePromptTemplate includes all properties and methods expected by ChatPromptTemplate. If there are any missing, you'll need to implement them.

  2. Type Assertion: If you're sure that FewShotChatMessagePromptTemplate fulfills the requirements of ChatPromptTemplate at runtime, you can bypass TypeScript's static checks using type assertion:

    const template: ChatPromptTemplate = fewShotChatMessagePromptTemplate as unknown as ChatPromptTemplate;

    Be cautious with this approach, as it skips TypeScript's type checking.

  3. Extend the Class Properly: If FewShotChatMessagePromptTemplate is supposed to extend ChatPromptTemplate, ensure that it does so correctly, including implementing any required methods or properties.

  4. Update Interfaces or Types: If possible, adjust the ChatPromptTemplate interface or type to make it compatible with FewShotChatMessagePromptTemplate, such as by making certain properties optional.

For a more detailed solution, you might need to refer to the LangChain.js documentation or the source code for these templates. If these steps don't resolve your issue, providing more specific details about the missing properties or methods could help in offering further assistance.

#### Details **Sources** - [Type 'FewShotChatMessagePromptTemplate' is not assignable to type 'ChatPromptTemplate | BaseMessagePromptTemplateLike'.](https://github.com/langchain-ai/langchainjs/discussions/5330) - [type error in get started tutorial.](https://github.com/langchain-ai/langchainjs/issues/3935) - [langchain/src/prompts/few_shot.ts](https://github.com/langchain-ai/langchainjs/blob/main/langchain/src/prompts/few_shot.ts) - [langchain-core/src/prompts/few_shot.ts](https://github.com/langchain-ai/langchainjs/blob/main/langchain-core/src/prompts/few_shot.ts) - [langchain-core/src/prompts/tests/few_shot.test.ts](https://github.com/langchain-ai/langchainjs/blob/main/langchain-core/src/prompts/tests/few_shot.test.ts) - [examples/src/prompts/prompts.ts](https://github.com/langchain-ai/langchainjs/blob/main/examples/src/prompts/prompts.ts) - [langchain-core/src/prompts/chat.ts](https://github.com/langchain-ai/langchainjs/blob/main/langchain-core/src/prompts/chat.ts) - [langchain/src/prompts/index.ts](https://github.com/langchain-ai/langchainjs/blob/main/langchain/src/prompts/index.ts) - [examples/src/prompts/few_shot.ts](https://github.com/langchain-ai/langchainjs/blob/main/examples/src/prompts/few_shot.ts)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a πŸ‘ if this is helpful and πŸ‘Ž if it is irrelevant.

To continue the conversation, mention @dosu.

jeasonnow commented 4 months ago

This is because fewShotPromptTemplate is not actually a chatPromptTemplate. fewShot should be a set of question-and-answer pairs used as a knowledge base. You can refer to few-shot-example for more information. @youngsupchoi

youngsupchoi commented 3 months ago

Thankyou!

octopi commented 2 months ago

If anybody else is struggling with the type error based on what the docs said, it appears you have to do a bit more work to fewShotPrompt before it's compatible with ChatPromptTemplate.fromMessages.

If you're following the tutorial, modifying it a bit worked for me:

const fewShotPrompt = new FewShotChatMessagePromptTemplate({
  examplePrompt,
  examples,
  inputVariables: [], // no input variables
});

const result = await fewShotPrompt.invoke({});

// you cannot pass fewShotPrompt directly, as the docs currently suggest. 
// but you can pass in another ChatPromptTemplate which should take `result` from above as input

const finalPrompt = ChatPromptTemplate.fromMessages([
  ["system", "You are a wondrous wizard of math."],
  ChatPromptTemplate.fromMessages(result.toChatMessages()),
  ["human", "{input}"],
]);