Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
averikitsch committed Feb 20, 2024
1 parent db7d032 commit 20e50c3
Showing 1 changed file with 209 additions and 27 deletions.
236 changes: 209 additions & 27 deletions docs/chat_message_history.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,251 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"# Cloud Spanner\n",
"> [Cloud Spanner](https://cloud.google.com/spanner) is a highly scalable database that combines unlimited scalability with relational semantics, such as secondary indexes, strong consistency, schemas, and SQL providing 99.999% availability in one easy solution.\n",
"\n",
"[Cloud Spanner](https://cloud.google.com/spanner) is a highly scalable database that combines unlimited scalability with relational semantics, such as secondary indexes, strong consistency, schemas, and SQL providing 99.999% availability in one easy solution."
],
"This notebook goes over how to use `Spanner` to store chat message history with the `SpannerChatMessageHistory` class."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
}
},
"source": [
"## Before You Begin\n",
"\n",
"To run this notebook, you will need to do the following:\n",
" * [Create a Google Cloud Project](https://developers.google.com/workspace/guides/create-project)\n",
" * [Create a Spanner instance](https://cloud.google.com/spanner/docs/create-manage-instances)\n",
" * [Create a Spanner database](https://cloud.google.com/spanner/docs/create-manage-databases)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 🦜🔗 Library Installation\n",
"The integration lives in its own `langchain-google-spanner` package, so we need to install it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet langchain-google-spanner"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Colab only:** Uncomment the following cell to restart the kernel or use the button to restart the kernel. For Vertex AI Workbench you can restart the terminal using the button on top."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Setting up"
],
"# # Automatically restart kernel after installs so that your environment can access the new packages\n",
"# import IPython\n",
"\n",
"# app = IPython.Application.instance()\n",
"# app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"id": "yygMe6rPWxHS",
"metadata": {
"collapsed": false
}
"id": "yygMe6rPWxHS"
},
"source": [
"### 🔐 Authentication\n",
"Authenticate to Google Cloud as the IAM user logged into this notebook in order to access your Google Cloud Project.\n",
"\n",
"* If you are using Colab to run this notebook, use the cell below and continue.\n",
"* If you are using Vertex AI Workbench, check out the setup instructions [here](https://github.com/GoogleCloudPlatform/generative-ai/tree/main/setup-env)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "PTXN1_DSXj2b",
"metadata": {
"id": "PTXN1_DSXj2b"
},
"outputs": [],
"source": [
"To run this notebook, you will need a Google Cloud Project, a Cloud Spanner instance, and Google credentials."
],
"from google.colab import auth\n",
"\n",
"auth.authenticate_user()"
]
},
{
"cell_type": "markdown",
"id": "NEvB9BoLEulY",
"metadata": {
"collapsed": false
}
"id": "NEvB9BoLEulY"
},
"source": [
"### ☁ Set Your Google Cloud Project\n",
"Set your Google Cloud project so that you can leverage Google Cloud resources within this notebook.\n",
"\n",
"If you don't know your project ID, try the following:\n",
"\n",
"* Run `gcloud config list`.\n",
"* Run `gcloud projects list`.\n",
"* See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "gfkS3yVRE4_W",
"metadata": {
"cellView": "form",
"id": "gfkS3yVRE4_W"
},
"outputs": [],
"source": [
"# @markdown Please fill in the value below with your Google Cloud project ID and then run the cell.\n",
"\n",
"PROJECT_ID = \"my-project-id\" # @param {type:\"string\"}\n",
"\n",
"# Set the project id\n",
"!gcloud config set project {PROJECT_ID}"
]
},
{
"cell_type": "markdown",
"id": "rEWWNoNnKOgq",
"metadata": {
"id": "rEWWNoNnKOgq"
},
"source": [
"%pip install langchain-google-spanner"
],
"### 💡 API Enablement\n",
"The `langchain-google-spanner` package requires that you [enable the Spanner API](https://console.cloud.google.com/flows/enableapi?apiid=spanner.googleapis.com) in your Google Cloud Project."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5utKIdq7KYi5",
"metadata": {
"collapsed": false
}
"id": "5utKIdq7KYi5"
},
"outputs": [],
"source": [
"# enable Cloud SQL Admin API\n",
"!gcloud services enable spanner.googleapis.com"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "f8f2830ee9ca1e01",
"metadata": {
"id": "f8f2830ee9ca1e01"
},
"source": [
"## Basic Usage"
]
},
{
"cell_type": "markdown",
"id": "OMvzMWRrR6n7",
"metadata": {
"id": "OMvzMWRrR6n7"
},
"source": [
"### Set Cloud SQL database values\n",
"Find your database values, in the [Cloud SQL Instances page](https://console.cloud.google.com/sql?_ga=2.223735448.2062268965.1707700487-2088871159.1707257687)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "irl7eMFnSPZr",
"metadata": {
"id": "irl7eMFnSPZr"
},
"outputs": [],
"source": [
"# @title Set Your Values Here { display-mode: \"form\" }\n",
"INSTANCE = \"my-instance\" # @param {type: \"string\"}\n",
"DATABASE = \"my-database\" # @param {type: \"string\"}\n",
"TABLE_NAME = \"message_store\" # @param {type: \"string\"}"
]
},
{
"cell_type": "markdown",
"id": "qPV8WfWr7O54",
"metadata": {
"id": "qPV8WfWr7O54"
},
"source": [
"### Initialize a table\n",
"The `SpannerChatMessageHistory` class requires a database table with a specific schema in order to store the chat message history.\n",
"\n",
"The helper method `init_chat_history_table()` that can be used to create a table with the proper schema for you."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "TEu4VHArRttE",
"metadata": {
"id": "TEu4VHArRttE"
},
"outputs": [],
"source": [
"from langchain_google_spanner import (\n",
" SpannerChatMessageHistory,\n",
")\n",
"\n",
"SpannerChatMessageHistory.init_chat_history_table(table_name=TABLE_NAME)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### SpannerChatMessageHistory\n",
"\n",
"To initialize the `SpannerChatMessageHistory` class you need to provide only 3 things:\n",
"\n",
"1. `instance_id` - The name of the Spanner instance\n",
"1. `database_id` - The name of the Spanner database\n",
"1. `session_id` - A unique identifier string that specifies an id for the session.\n",
"1. `table_name` - The name of the table within the database to store the chat message history."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"message_history = SpannerChatMessageHistory(\n",
" instance_id=\"my-instance\",\n",
" database_id=\"my-database\",\n",
" instance_id=INSTANCE,\n",
" database_id=DATABASE,\n",
" table_name=TABLE_NAME,\n",
" session_id=\"user-session-id\",\n",
")\n",
"\n",
"message_history.add_user_message(\"hi!\")\n",
"message_history.add_ai_message(\"whats up?\")"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
Expand All @@ -81,7 +262,7 @@
"metadata": {},
"source": [
"## Custom client\n",
"The client created by default is the default client. To use a non-default, a [custom client]https://cloud.google.com/spanner/docs/samples/spanner-create-client-with-query-options#spanner_create_client_with_query_options-python) can be passed to the constructor."
"The client created by default is the default client. To use a non-default, a [custom client](https://cloud.google.com/spanner/docs/samples/spanner-create-client-with-query-options#spanner_create_client_with_query_options-python) can be passed to the constructor."
]
},
{
Expand Down Expand Up @@ -116,9 +297,10 @@
"outputs": [],
"source": [
"message_history = SpannerChatMessageHistory(\n",
" instance_id=\"my-instance\",\n",
" database_id=\"my-database\",\n",
" session_id=\"obsolete-session-id\",\n",
" instance_id=INSTANCE,\n",
" database_id=DATABASE,\n",
" table_name=TABLE_NAME,\n",
" session_id=\"user-session-id\",\n",
")\n",
"\n",
"message_history.clear()"
Expand All @@ -141,7 +323,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 20e50c3

Please sign in to comment.