Skip to content

Commit 4ac8def

Browse files
PingXiecraig08
andauthored
feat: Add placeholder for LangChain with Memorystore Redis integration (#30)
* feat: Add placeholder for LangChain with Memorystore Redis integration - Introduce initial ipynb file as a scaffold for demonstrating the integration of LangChain with Memorystore for Redis. - The notebook is currently a placeholder, with detailed examples and guidance to be added. * fix: fix formatting of the notebooks --------- Co-authored-by: Craig Chi <craigchi@google.com>
1 parent 5e09c19 commit 4ac8def

3 files changed

Lines changed: 474 additions & 441 deletions

File tree

docs/chat_message_history.ipynb

Lines changed: 181 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -1,184 +1,184 @@
11
{
2-
"cells": [
3-
{
4-
"cell_type": "markdown",
5-
"metadata": {
6-
"id": "6-0_o3DxsFGi"
7-
},
8-
"source": [
9-
"Google Database\n",
10-
"\n",
11-
"Use [Google Memorystore for Redis](https://cloud.google.com/memorystore/docs/redis/memorystore-for-redis-overview) to store chat message history for LangChain."
12-
]
13-
},
14-
{
15-
"cell_type": "markdown",
16-
"metadata": {
17-
"id": "dWakBoPnsFGj"
18-
},
19-
"source": [
20-
"## Pre-reqs"
21-
]
22-
},
23-
{
24-
"cell_type": "markdown",
25-
"metadata": {
26-
"id": "EudfLv_UsFGk"
27-
},
28-
"source": [
29-
"### Setting Up a Memorystore for Redis Instance\n",
30-
"\n",
31-
"Before proceeding, an active Memorystore for Redis instance is needed to store chat message history:\n",
32-
"\n",
33-
"* Create a Memorystore for Redis Instance (>= 5.0): If an instance doesn't exist, follow the instructions at https://cloud.google.com/memorystore/docs/redis/create-instance-console to create a new one. Ensure the version is greater than or equal to 5.0.\n",
34-
"* Obtain Endpoint: Note the endpoint associated with the instance."
35-
]
36-
},
37-
{
38-
"cell_type": "markdown",
39-
"metadata": {
40-
"id": "J5nxjYxHsFGk"
41-
},
42-
"source": [
43-
"### Installing the LangChain Memorystore for Redis Module\n",
44-
"\n",
45-
"Interaction with the Memorystore for Redis instance from LangChain requires installing the necessary module:"
46-
]
47-
},
48-
{
49-
"cell_type": "code",
50-
"execution_count": null,
51-
"metadata": {
52-
"tags": [],
53-
"id": "iLwVMVkYsFGk"
54-
},
55-
"outputs": [],
56-
"source": [
57-
"# Install Memorystore for Redis for LangChain module\n",
58-
"%pip install langchain_google_memorystore_redis"
59-
]
60-
},
61-
{
62-
"cell_type": "markdown",
63-
"metadata": {
64-
"id": "2L7kMu__sFGl"
65-
},
66-
"source": [
67-
"## Basic Usage"
68-
]
69-
},
70-
{
71-
"cell_type": "markdown",
72-
"metadata": {
73-
"id": "A2fT1iEhsFGl"
74-
},
75-
"source": [
76-
"### Initialize a MemorystoreChatMessageHistory\n",
77-
"\n",
78-
"Each chat message history object must have a unique session ID. If the session ID already has messages stored in Redis, they will can be retrieved."
79-
]
80-
},
81-
{
82-
"cell_type": "code",
83-
"execution_count": null,
84-
"metadata": {
85-
"id": "YEDKWR6asFGl"
86-
},
87-
"outputs": [],
88-
"source": [
89-
"import redis\n",
90-
"from langchain_google_memorystore_redis import MemorystoreChatMessageHistory\n",
91-
"\n",
92-
"# Connect to a Memorystore for Redis instance\n",
93-
"redis_client = redis.from_url(\"redis://127.0.0.1:6379\")\n",
94-
"\n",
95-
"message_history = MemorystoreChatMessageHistory(redis_client, session_id='session1')"
96-
]
97-
},
98-
{
99-
"cell_type": "markdown",
100-
"metadata": {
101-
"id": "EmoJcTgosFGl"
102-
},
103-
"source": [
104-
"### Add Messages"
105-
]
106-
},
107-
{
108-
"cell_type": "code",
109-
"execution_count": null,
110-
"metadata": {
111-
"id": "gB1PGe6wsFGm"
112-
},
113-
"outputs": [],
114-
"source": [
115-
"message_history.add_ai_message('Hey! I am AI!')\n",
116-
"message_history.add_user_message('Hey! I am human!')"
117-
]
118-
},
119-
{
120-
"cell_type": "markdown",
121-
"metadata": {
122-
"id": "02xxvmzTsFGm"
123-
},
124-
"source": [
125-
"### Retrieve All Messages Stored in the Session"
126-
]
127-
},
128-
{
129-
"cell_type": "code",
130-
"execution_count": null,
131-
"metadata": {
132-
"id": "BvS3UFsysFGm"
133-
},
134-
"outputs": [],
135-
"source": [
136-
"message_history.messages"
137-
]
138-
},
139-
{
140-
"cell_type": "markdown",
141-
"metadata": {
142-
"id": "sFJdt3ubsFGo"
143-
},
144-
"source": [
145-
"### Clear Messages"
146-
]
147-
},
148-
{
149-
"cell_type": "code",
150-
"execution_count": null,
151-
"metadata": {
152-
"id": "H5I7K3MTsFGo"
153-
},
154-
"outputs": [],
155-
"source": [
156-
"message_history.clear()"
157-
]
158-
}
159-
],
160-
"metadata": {
161-
"kernelspec": {
162-
"display_name": "Python 3 (ipykernel)",
163-
"language": "python",
164-
"name": "python3"
165-
},
166-
"language_info": {
167-
"codemirror_mode": {
168-
"name": "ipython",
169-
"version": 3
170-
},
171-
"file_extension": ".py",
172-
"mimetype": "text/x-python",
173-
"name": "python",
174-
"nbconvert_exporter": "python",
175-
"pygments_lexer": "ipython3",
176-
"version": "3.11.6"
177-
},
178-
"colab": {
179-
"provenance": []
180-
}
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "6-0_o3DxsFGi"
7+
},
8+
"source": [
9+
"Google Database\n",
10+
"\n",
11+
"Use [Google Memorystore for Redis](https://cloud.google.com/memorystore/docs/redis/memorystore-for-redis-overview) to store chat message history for LangChain."
12+
]
18113
},
182-
"nbformat": 4,
183-
"nbformat_minor": 0
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {
17+
"id": "dWakBoPnsFGj"
18+
},
19+
"source": [
20+
"## Pre-reqs"
21+
]
22+
},
23+
{
24+
"cell_type": "markdown",
25+
"metadata": {
26+
"id": "EudfLv_UsFGk"
27+
},
28+
"source": [
29+
"### Setting Up a Memorystore for Redis Instance\n",
30+
"\n",
31+
"Before proceeding, an active Memorystore for Redis instance is needed to store chat message history:\n",
32+
"\n",
33+
"* Create a Memorystore for Redis Instance (>= 5.0): If an instance doesn't exist, follow the instructions at https://cloud.google.com/memorystore/docs/redis/create-instance-console to create a new one. Ensure the version is greater than or equal to 5.0.\n",
34+
"* Obtain Endpoint: Note the endpoint associated with the instance."
35+
]
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"metadata": {
40+
"id": "J5nxjYxHsFGk"
41+
},
42+
"source": [
43+
"### Installing the LangChain Memorystore for Redis Module\n",
44+
"\n",
45+
"Interaction with the Memorystore for Redis instance from LangChain requires installing the necessary module:"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"metadata": {
52+
"tags": [],
53+
"id": "iLwVMVkYsFGk"
54+
},
55+
"outputs": [],
56+
"source": [
57+
"# Install Memorystore for Redis for LangChain module\n",
58+
"%pip install langchain_google_memorystore_redis"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {
64+
"id": "2L7kMu__sFGl"
65+
},
66+
"source": [
67+
"## Basic Usage"
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"metadata": {
73+
"id": "A2fT1iEhsFGl"
74+
},
75+
"source": [
76+
"### Initialize a MemorystoreChatMessageHistory\n",
77+
"\n",
78+
"Each chat message history object must have a unique session ID. If the session ID already has messages stored in Redis, they will can be retrieved."
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"metadata": {
85+
"id": "YEDKWR6asFGl"
86+
},
87+
"outputs": [],
88+
"source": [
89+
"import redis\n",
90+
"from langchain_google_memorystore_redis import MemorystoreChatMessageHistory\n",
91+
"\n",
92+
"# Connect to a Memorystore for Redis instance\n",
93+
"redis_client = redis.from_url(\"redis://127.0.0.1:6379\")\n",
94+
"\n",
95+
"message_history = MemorystoreChatMessageHistory(redis_client, session_id=\"session1\")"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {
101+
"id": "EmoJcTgosFGl"
102+
},
103+
"source": [
104+
"### Add Messages"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {
111+
"id": "gB1PGe6wsFGm"
112+
},
113+
"outputs": [],
114+
"source": [
115+
"message_history.add_ai_message(\"Hey! I am AI!\")\n",
116+
"message_history.add_user_message(\"Hey! I am human!\")"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {
122+
"id": "02xxvmzTsFGm"
123+
},
124+
"source": [
125+
"### Retrieve All Messages Stored in the Session"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": null,
131+
"metadata": {
132+
"id": "BvS3UFsysFGm"
133+
},
134+
"outputs": [],
135+
"source": [
136+
"message_history.messages"
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"metadata": {
142+
"id": "sFJdt3ubsFGo"
143+
},
144+
"source": [
145+
"### Clear Messages"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": null,
151+
"metadata": {
152+
"id": "H5I7K3MTsFGo"
153+
},
154+
"outputs": [],
155+
"source": [
156+
"message_history.clear()"
157+
]
158+
}
159+
],
160+
"metadata": {
161+
"kernelspec": {
162+
"display_name": "Python 3 (ipykernel)",
163+
"language": "python",
164+
"name": "python3"
165+
},
166+
"language_info": {
167+
"codemirror_mode": {
168+
"name": "ipython",
169+
"version": 3
170+
},
171+
"file_extension": ".py",
172+
"mimetype": "text/x-python",
173+
"name": "python",
174+
"nbconvert_exporter": "python",
175+
"pygments_lexer": "ipython3",
176+
"version": "3.11.6"
177+
},
178+
"colab": {
179+
"provenance": []
180+
}
181+
},
182+
"nbformat": 4,
183+
"nbformat_minor": 0
184184
}

0 commit comments

Comments
 (0)