Replies: 2 comments
-
🤖 Hey @ZaidHani! Great to see you back with another interesting question. Let's dive into it. Based on the context provided, it seems you want to create a chain that includes a System Message, a Retriever, and a Memory all at once. While there isn't a built-in way to do this in LangChain, you can extend the from langchain_core.language_models import BaseLanguageModel
from langchain_core.retrievers import BaseRetriever
from langchain_core.messages import SystemMessage
from langchain.chains.conversational_retrieval import BaseConversationalRetrievalChain
class MyChain(BaseConversationalRetrievalChain):
def __init__(self, system_message: SystemMessage, *args, **kwargs):
super().__init__(*args, **kwargs)
self.system_message = system_message
def _get_docs(
self,
question: str,
inputs: Dict[str, Any],
*,
run_manager: CallbackManagerForChainRun,
) -> List[Document]:
# Incorporate the system message into the retrieval process
question = f"{self.system_message.content} {question}"
return super()._get_docs(question, inputs, run_manager=run_manager)
# Usage
llm = BaseLanguageModel() # Replace with your actual language model
retriever = BaseRetriever() # Replace with your actual retriever
system_message = SystemMessage(content="System message content")
chain = MyChain(system_message, llm=llm, retriever=retriever) In this example, a new class Please replace You can find more details in the source code of the As for the memory, you can use the Here's an example: from langchain.chains.conversation.memory import ConversationBufferWindowMemory
# Create the memory
memory = ConversationBufferWindowMemory(
memory_key="chat_history", return_messages=True
)
# Pass the memory to the initialize_agent function
executor = initialize_agent(
agent=AgentType.OPENAI_FUNCTIONS,
tools=tools,
llm=llm,
memory=memory,
system_message=system_message,
agent_kwargs={
"system_message": system_message,
"extra_prompt_messages": [MessagesPlaceholder(variable_name="memory")]
},
verbose=False,
handle_parsing_errors=True
) In this example, You can find more details in the source code of the I hope this helps! If you have any other questions, feel free to ask. Sources
|
Beta Was this translation helpful? Give feedback.
-
I'm also interested on the answer to this. Did you managed to make it work @ZaidHani ? |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I'm trying to build an equivalent to this agent code using chains, I want to use chains because they are much faster than agents, but so far I'm facing a problem. I can't seem to find a way to insert a system message, a retriever and a memory all at once into a chain, each solution I've found solve this problem by providing 2 of these previous features and excluding the last one, I've found someone who built a chain and used a system message and a retriever but no memory, and I've found someone who used a retriever and a memory but with no system message... so far I've opted to use an agent because it has all of these features combined, is there a way to build the same code using only chains. thank you.
System Info
langchain==0.1.4
langchain-community==0.0.16
langchain-core==0.1.17
langchain-openai==0.0.5
platform widows
python version 3.12.1
Beta Was this translation helpful? Give feedback.
All reactions