langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
88.63k stars 13.92k forks source link

Input variables is ignored even though passed in when MessagesPlaceholder.optional=True #21425

Open LinboYuan opened 1 month ago

LinboYuan commented 1 month ago

Checked other resources

Example Code



memory_key = "chat_context"

prompt = ChatPromptTemplate(
    messages=[
        MessagesPlaceholder(variable_name=memory_key, optional=True),
        HumanMessagePromptTemplate.from_template("{question}")
    ]
)

memory = ConversationBufferMemory(memory_key=memory_key, return_messages=True)
conversation = LLMChain(
    llm=llm,
    verbose=True,
    memory=memory,
    prompt=prompt
)

user_input = input("Me: ")
while user_input != "exit":
    print(conversation.invoke({"question": user_input}))
    user_input = input("Me: ")

### Error Message and Stack Trace (if applicable)

![1](https://github.com/langchain-ai/langchain/assets/12031633/419f2b04-a259-4cec-b66a-2d9ce2d5e112)
![2](https://github.com/langchain-ai/langchain/assets/12031633/462381c0-83dc-4ec2-9d79-5662c3c7d292)

### Description

as the title described, when MessagesPlaceholder.optional=True, the input variables will be ignored, even though I passed the arguments in. **I suppose this to be a bug, because it makes no sense when init a MessagesPlaceholder and optional set True.**

or **if this is by design, could you pls share some original design intentions or use cases.**

![3](https://github.com/langchain-ai/langchain/assets/12031633/893cf930-7cdb-40d9-8341-6fa043ce90ed)

### System Info

Name: langchain-core
Version: 0.1.45
Summary: Building applications with LLMs through composability
Home-page: https://github.com/langchain-ai/langchain
Author: 
Author-email: 
License: MIT
Location: /Users/linbo.yuan/Library/Python/3.9/lib/python/site-packages
Requires: PyYAML, tenacity, pydantic, langsmith, jsonpatch, packaging
Required-by: langchain, langchain-text-splitters, langchain-openai, langchain-community
keenborder786 commented 1 month ago

The problem arises when MessagePlaceHolder is set as optional as according to this code, the input variable name set by MessagePlaceHolder is excluded from the ChatPromptTemplate. Consequently, when you invoke the chain, the MessagePlaceHolder variable is not filled in by the ConversationBufferMemory.