An example of using Dapr state stores as chat history memory in langchain.
- Python 3.10 or later
- Dapr 1.10 or later
- Langchain 0.0.137 or later
- Dapr AI Bindings
The Dapr AI bindings assume you have created an Azure Open AI instance in Azure and deployed a language model named gpt-4
. To use another language model service, update the ./resources/ai.yaml
in the example folder.
-
Add Azure Open AI endpoint and key and Redis host and password to a
secrets.json
file in the root of the repo{ "azure-open-ai-endpoint": "<Azure Open AI endpoint>", "azure-open-ai-key": "<Azure Open AI key>", "store-host": "<Redis host>", "store-password": "<Redis password>" }
-
Start the Dapr AI bindings pluggable component
cd <dapr-ai-bindings source>/src/DaprAi.Components dotnet run
-
Start the Dapr sidecar
dapr run --app-id daprai --dapr-grpc-port 50001 --resources-path ./resources
-
Start the Python module multiple times, specifying a session ID and prompt for the AI assistant
python3 main.py "<session ID>" "<prompt>"
The session ID is arbitrary. Each prompt and response will be accumulated in the Dapr state store using the session ID as a key, which enables the AI assistant to maintain history of the chat. Using a new session ID will start a new chat session as there will be no prior history.
For example:
> python3 main.py "100" "My name is Phil" Hello Phil! It's great to meet you. How can I help you today? Do you have any specific topics you would like to talk about or questions you would like to ask? > python3 main.py "100" "Do you remember my name?" Yes, I do remember your name. You mentioned that your name is Phil. It's nice to continue our conversation, Phil. What would you like to discuss? > python3 main.py "101" "Do you remember my name?" As an AI language model, I don't have the ability to remember past interactions or recall specific details like names from a conversation. Each time you ask me something, it's like starting a new conversation. If you tell me your name now, I can use it during this conversation.
Note the change to the session ID in the last command, which causes the AI assistant to no longer have any prior chat history.