|
1 | 1 | { |
2 | | - "cells": [ |
3 | | - { |
4 | | - "cell_type": "markdown", |
5 | | - "metadata": {}, |
6 | | - "source": [ |
7 | | - "# Google DATABASE\n", |
8 | | - "\n", |
9 | | - "[Google DATABASE](https://cloud.google.com/DATABASE).\n", |
10 | | - "\n", |
11 | | - "Save chat messages into `DATABASE`." |
12 | | - ] |
13 | | - }, |
14 | | - { |
15 | | - "cell_type": "markdown", |
16 | | - "metadata": {}, |
17 | | - "source": [ |
18 | | - "## Pre-reqs" |
19 | | - ] |
20 | | - }, |
21 | | - { |
22 | | - "cell_type": "code", |
23 | | - "execution_count": null, |
24 | | - "metadata": { |
25 | | - "tags": [] |
26 | | - }, |
27 | | - "outputs": [], |
28 | | - "source": [ |
29 | | - "%pip install PACKAGE_NAME" |
30 | | - ] |
31 | | - }, |
32 | | - { |
33 | | - "cell_type": "code", |
34 | | - "execution_count": 3, |
35 | | - "metadata": { |
36 | | - "tags": [] |
37 | | - }, |
38 | | - "outputs": [], |
39 | | - "source": [ |
40 | | - "from PACKAGE import LOADER" |
41 | | - ] |
42 | | - }, |
43 | | - { |
44 | | - "cell_type": "markdown", |
45 | | - "metadata": {}, |
46 | | - "source": [ |
47 | | - "## Basic Usage" |
48 | | - ] |
49 | | - }, |
50 | | - { |
51 | | - "cell_type": "code", |
52 | | - "execution_count": null, |
53 | | - "metadata": {}, |
54 | | - "outputs": [], |
55 | | - "source": [] |
56 | | - } |
57 | | - ], |
58 | | - "metadata": { |
59 | | - "kernelspec": { |
60 | | - "display_name": "Python 3 (ipykernel)", |
61 | | - "language": "python", |
62 | | - "name": "python3" |
63 | | - }, |
64 | | - "language_info": { |
65 | | - "codemirror_mode": { |
66 | | - "name": "ipython", |
67 | | - "version": 3 |
68 | | - }, |
69 | | - "file_extension": ".py", |
70 | | - "mimetype": "text/x-python", |
71 | | - "name": "python", |
72 | | - "nbconvert_exporter": "python", |
73 | | - "pygments_lexer": "ipython3", |
74 | | - "version": "3.10.6" |
75 | | - } |
| 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 | + ] |
76 | 13 | }, |
77 | | - "nbformat": 4, |
78 | | - "nbformat_minor": 4 |
| 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 |
79 | 184 | } |
0 commit comments