Skip to content

feat: FirestoreRecordManager #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions docs/record_manager.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Google Firestore (Native Mode)\n",
"\n",
"> [Firestore](https://cloud.google.com/firestore) is a serverless document-oriented database that scales to meet any demand. Extend your database application to build AI-powered experiences leveraging Firestore's LangChain integrations.\n",
"\n",
"This notebook goes over how to use [Firestore](https://cloud.google.com/firestore) as a record manager for [langchain indexing](https://python.langchain.com/v0.1/docs/modules/data_connection/indexing/) your Vectorstore.\n",
"\n",
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/googleapis/langchain-google-firestore-python/blob/main/docs/record_manager.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Before You Begin\n",
"\n",
"To run this notebook, you will need to do the following:\n",
"\n",
"* [Create a Google Cloud Project](https://developers.google.com/workspace/guides/create-project)\n",
"* [Enable the Firestore API](https://console.cloud.google.com/flows/enableapi?apiid=firestore.googleapis.com)\n",
"* [Create a Firestore database](https://cloud.google.com/firestore/docs/manage-databases)\n",
"\n",
"After confirmed access to database in the runtime environment of this notebook, filling the following values and run the cell before running example scripts."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# @markdown Please specify a source for demo purpose.\n",
"COLLECTION_NAME = \"test\" # @param {type:\"CollectionReference\"|\"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 🦜🔗 Library Installation\n",
"\n",
"The integration lives in its own `langchain-google-firestore` package, so we need to install it. For this notebook, we will also install `langchain-google-genai` to use Google Generative AI embeddings."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet langchain langchain-google-firestore langchain-google-vertexai"
]
},
{
"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": [
"# # 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",
"metadata": {},
"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,
"metadata": {},
"outputs": [],
"source": [
"# @markdown Please fill in the value below with your Google Cloud project ID and then run the cell.\n",
"\n",
"PROJECT_ID = \"extensions-testing\" # @param {type:\"string\"}\n",
"\n",
"# Set the project id\n",
"!gcloud config set project {PROJECT_ID}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 🔐 Authentication\n",
"\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": null,
"metadata": {},
"outputs": [],
"source": [
"from google.colab import auth\n",
"\n",
"auth.authenticate_user()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic Usage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Initialize FirestoreRecordManager\n",
"\n",
"`FirestoreRecordManager` allows you to index your vectorstore in a Firestore database. You can use it to store references to embeddings from any model, including those from Google Generative AI."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain.indexes import index\n",
"from langchain_core.documents import Document\n",
"from langchain_google_firestore import FirestoreVectorStore, FirestoreRecordManager\n",
"from langchain_google_vertexai import VertexAIEmbeddings\n",
"\n",
"namespace = f\"firstore/{COLLECTION_NAME}\"\n",
"record_manager = FirestoreRecordManager(namespace)\n",
"\n",
"embedding = VertexAIEmbeddings(model_name=\"textembedding-gecko@latest\")\n",
"vectorstore = FirestoreVectorStore(\n",
" collection=COLLECTION_NAME,\n",
" embedding_service=embedding\n",
")\n",
"\n",
"doc1 = Document(page_content=\"test-doc-1-content\", metadata={\"source\": \"test-doc-1.txt\"})\n",
"doc2 = Document(page_content=\"test-doc-2-content\", metadata={\"source\": \"test-doc-2.txt\"})\n",
"doc3 = Document(page_content=\"test-doc-3-content\", metadata={\"source\": \"test-doc-3.txt\"})\n",
"\n",
"results = index(\n",
" [doc1, doc2, doc3],\n",
" record_manager,\n",
" vectorstore,\n",
" cleanup=\"incremental\",\n",
" source_id_key=\"source\",\n",
")\n",
"\n",
"print(results)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 2 additions & 0 deletions src/langchain_google_firestore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
from .chat_message_history import FirestoreChatMessageHistory
from .document_loader import FirestoreLoader, FirestoreSaver
from .vectorstores import FirestoreVectorStore
from .record_manager import FirestoreRecordManager
from .version import __version__

__all__ = [
"FirestoreChatMessageHistory",
"FirestoreLoader",
"FirestoreSaver",
"FirestoreVectorStore",
"FirestoreRecordManager",
"__version__",
]
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy