langchain-ai / langchain

πŸ¦œπŸ”— Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.59k stars 15.31k forks source link

Issue: LLMChain throwing abstract class BasePromptTemplate error, even when the prompt templates used are not abstract. #8266

Closed lydiayu26 closed 1 year ago

lydiayu26 commented 1 year ago

Issue you'd like to raise.

I am trying to instantiate 2 LLMChains, each of which should use a SystemMessagePromptTemplate to specify the way that it should communicate. Both are using the same LLM from the HF pipeline.

The following code gives the error "ValidationError: 1 validation error for LLMChain prompt Can't instantiate abstract class BasePromptTemplate with abstract methods format, format_prompt (type=type_error)" at the instantiation of the LLMChain.

llm = HuggingFacePipeline.from_model_id(
model_id="microsoft/DialoGPT-medium",
task="text-generation",
model_kwargs={"temperature": 0, "max_length": 100},
)

ai_bot_template = "You are an AI chatbot that interacts with humans. You will also be interacting with a Therapist with the goal of improving the safety, trustworthiness, and ethics of your communication. There will also be a moderator that checks in throughout your interactions with the therapist and the human."
ai_therapist_template = "You are an AI therapist that interacts with AI chat bots. Your goal is to help the chat bot improve the safety, trustworthiness, and ethics of its interactions with humans."

ai_bot_prompt = SystemMessagePromptTemplate.from_template(ai_bot_template)
ai_therapist_prompt = SystemMessagePromptTemplate.from_template(ai_therapist_template)

therapist_chain = LLMChain(prompt=ai_therapist_prompt, llm=llm)
ai_bot_chain = LLMChain(prompt=ai_bot_prompt, llm=llm)

Suggestion:

No response

dosubot[bot] commented 1 year ago

Answer generated by a πŸ€–

Answer

From your description, it appears that you're encountering an error when trying to instantiate two LLMChains, each using a SystemMessagePromptTemplate for communication. The error message suggests that you're trying to instantiate an abstract class, BasePromptTemplate, which is not allowed in Python. This class has two abstract methods, format and format_prompt, that must be implemented in any class that inherits from it.

In your code, you're using the SystemMessagePromptTemplate class, which is a concrete class that inherits from BasePromptTemplate and should implement these abstract methods. The error message suggests that these methods might not be properly implemented in the SystemMessagePromptTemplate class.

To resolve this issue, I would recommend checking the implementation of the SystemMessagePromptTemplate class. Ensure that both the format and format_prompt methods are implemented. If they are not, you will need to implement them. Here's a basic example of what that might look like:

class SystemMessagePromptTemplate(BasePromptTemplate):
    def format(self, *args, **kwargs):
        # existing implementation here
        pass

    def format_prompt(self, *args, **kwargs):
        # implementation here
        pass

This is one potential solution based on the information provided. If you've confirmed that these methods are implemented correctly in the SystemMessagePromptTemplate class and you're still encountering the error, there may be another underlying issue that we'll need to investigate further.

If you find that this is a bug and it could impact other users, we encourage you to make a pull request with the necessary changes. If my suggestion doesn't align with your experience, it might be helpful to ensure you're using the latest version of the LangChain framework.

Sources


This response is meant to be useful, save you time, and share context. 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.

dosubot[bot] commented 1 year ago

Hi, @lydiayu26! I'm Dosu, and I'm helping the LangChain team manage our backlog. I wanted to let you know that we are marking this issue as stale.

Based on my understanding, the issue you reported is about LLMChain throwing an error when trying to instantiate it with a SystemMessagePromptTemplate, even though the prompt templates used are not abstract. I suggested that the error might be related to the BasePromptTemplate class and recommended checking the implementation of the SystemMessagePromptTemplate class to ensure that the format and format_prompt methods are properly implemented. If the issue persists, further investigation may be needed.

Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on this issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your contribution to LangChain!

Matx00 commented 10 months ago

hi @lydiayu26, did you solve it in the end?

KD-TrendOn commented 9 months ago

i had a similar problem and problem was with import. I was importing from langchain_core.prompts import PromptTemplate instead of from langchain.prompts import PromptTemplate Honestly, that problem occured because i used the code that langchain's doc's chatbot writed

jasperblues commented 8 months ago

For me it was a circular import.