LangChain: Multi-User Conversation(follow-up)

TeeTracker
4 min readApr 7, 2024

--

Use Cohere’s documents and a special field in additional_kwargsfeature to enhance the organization of information in multi-user conversation.

· Intro
· Cohere command-r model
OpenAI pre-request
Cohere pre-request
· Code adjustments
method 1
method 2
More to colab
· One more thing

https://chrome.google.com/webstore/detail/sider-chatgpt-sidebar-%20-g/difoiogjjojoaoomphldepapgpbgkhkb/reviews

Intro

This is an additional article about how to make users aware of the history of “multi-user” interaction in human-messages. A previous article has solved this problem through several different methods, but this article will provide a more “elegant” way, using the natural support for RAG features of Cohere’s command-r model to achieve this goal.

In addition, it is also mentioned how to look at the source code of langchain to help find a possible special field in additional_kwargs to carry user identification, using OpenAI’s model as an example.

Cohere command-r model

Unlike other models, instead of just sending a message request to the model, all conversation history is sent as a list along with the current content as one message. In contrast, cohereincorporates the history as an attribute of the current message being submitted, and each message also has a documents attribute that accepts a list of dictionaries.

OpenAI pre-request

Look, all the messages are packaged and sent out as a list.

https://github.com/langchain-ai/langchain/blob/039b7a472d3628f2b484bf2814f9e7e677adf738/libs/community/langchain_community/chat_models/openai.py#L446

Cohere pre-request

The previous requests are saved as chat_history, and the current content is placed in the message.

https://github.com/langchain-ai/langchain-cohere/blob/main/libs%2Fcohere%2Flangchain_cohere%2Fchat_models.py#L130

However, if we consider chat_history as our touchdown goal, then we will be back to the dilemma in the previous article. Obviously, we cannot input different users’ identifiers into chat_history. Luckily, we have the documents attribute. Based on the langchain source code, we can infer that it accepts a list of dictionaries (everything happens before submitting the request).

Code adjustments

method 1

Rewrite the statically defined prompt template to be dynamic, meaning use a lambda function to dynamically generate the prompt (LCEL powered). Remove the history placeholder and instead pass the cxt_dict (conversation history in the form of a list of dictionaries) to documents .

method 2

Static configuration prompt, directly pass the cxt_dict to documents. Although it simplifies the invoke() , it doesn’t go through LCEL, so cxt_dict will be treated as a string. Here, the dumpd helper function is used to convert it to an object. Actually, I was also confused here for a long time, but langsmith helped me see everything. There’s no reason for such details, only through langsmith debugging can these be discovered.

More to colab

You can check the section and subsection “Failed approach: Convert all history into a list of dictionaries: `cxt_dict`” in the notebook to see the differences in output.

One more thing

When I was exploring the langchain code, I found that when submitting the OpenAI model, the additional_kwargs can carry the function_call outside, and it can also carry the name, so the model can see the name, allowing us to put all user identification of the message into the name.

As a supplement to the previous article, it is recommended to refer to the source code of langchain pre-request information (a special field in additional_kwargs) to the model to find out the properties that the model can see.

Check the section and subsection of code Failed approach: Directly use the history of `ConversationBufferMemory`: `cxt_history`.

Thank you for reading. 👍

--

--

TeeTracker
TeeTracker

No responses yet