-
Checked other resources
Commit to Help
Example Codefrom langchain_core.messages import HumanMessage, AIMessage
from langchain.chains import TransformChain
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.chat_history import (
BaseChatMessageHistory,
InMemoryChatMessageHistory,
)
from langchain_core.runnables.history import RunnableWithMessageHistory
def f1(data: dict):
print(data)
return {'output': 'aaa'}
subchain1 = TransformChain(
input_variables=["history"], output_variables=["output"], transform=f1
)
prompt = ChatPromptTemplate.from_messages(
[
MessagesPlaceholder("user"),
]
)
chain = prompt | subchain1
store = {}
def get_session_history(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = InMemoryChatMessageHistory()
return store[session_id]
chain_with_history = RunnableWithMessageHistory(chain,
get_session_history,
input_messages_key='user',
history_messages_key='history',
output_messages_key="output",)
user_message = [HumanMessage(content="How are you ?")]
config = {"configurable": {"session_id": "abc5"}}
response = chain_with_history.invoke(
{'user': user_message},
config=config,
)
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
I found a similar discussion that addresses the issue of using 'history' inside a subchain in LangChain: How to use RunnableWithMessageHistory? [1]. To use 'history' inside Here is an example of how to set up and use from langchain_core.messages import HumanMessage, AIMessage
from langchain.chains import TransformChain
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.chat_history import (
BaseChatMessageHistory,
InMemoryChatMessageHistory,
)
from langchain_core.runnables.history import RunnableWithMessageHistory
def f1(data: dict):
print(data)
return {'output': 'aaa'}
subchain1 = TransformChain(
input_variables=["history"], output_variables=["output"], transform=f1
)
prompt = ChatPromptTemplate.from_messages(
[
MessagesPlaceholder("user"),
]
)
chain = prompt | subchain1
store = {}
def get_session_history(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = InMemoryChatMessageHistory()
return store[session_id]
chain_with_history = RunnableWithMessageHistory(
chain,
get_session_history,
input_messages_key='user',
history_messages_key='history',
output_messages_key="output",
)
user_message = [HumanMessage(content="How are you ?")]
config = {"configurable": {"session_id": "abc5"}}
response = chain_with_history.invoke(
{'user': user_message},
config=config,
)
print(response) In this example:
This setup ensures that the |
Beta Was this translation helpful? Give feedback.
-
I am still getting ValueError: Missing some input keys: {'history'} for subchain2 Should I pass history through subchain 1's output to see it in subchain 2's input ? Is this the only way ? |
Beta Was this translation helpful? Give feedback.
-
I don't have a possibility to pass history through subchain 1 . Any other way to pass "history" from prompt to subchain2 ? Might be RunnableBranch can be useful here ? |
Beta Was this translation helpful? Give feedback.
-
My case is more complicated . I need an output from subchain1 inside subchain2 along with 'history'. |
Beta Was this translation helpful? Give feedback.
To make 'history' accessible over the whole chain while also passing the output from
subchain1
tosubchain2
, you can use theRunnableBranch
to handle the conditional logic and ensure that the history is passed along with the input. Here is an example based on your requirements: