import TimescaleVector1. Similarity Search with Euclidean Distance (Default)‚ÄãFirst, we'll look at an example of doing a similarity search query on the State of the Union speech to find the most similar sentences to a given query sentence. We'll use the Euclidean distance as our similarity metric.# Load the text and split it into chunksloader = TextLoader("../../../extras/modules/state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()Next, we'll load the service URL for our Timescale database. If you haven't already, signup for Timescale, and create a new database.Then, to connect to your PostgreSQL database, you'll need your service URI, which can be found in the cheatsheet or .env file you downloaded after creating a new database. The URI will look something like this: postgres://tsdbadmin:<password>@<id>.tsdb.cloud.timescale.com:<port>/tsdb?sslmode=require. # Timescale Vector needs the service url to your cloud database. You can see this as soon as you create the # service in the cloud UI or in your credentials.sql fileSERVICE_URL = os.environ['TIMESCALE_SERVICE_URL']# Specify directly if testing#SERVICE_URL = "postgres://tsdbadmin:<password>@<id>.tsdb.cloud.timescale.com:<port>/tsdb?sslmode=require"# # You can get also it from an enviornment variables. We suggest using a .env file.# import os# SERVICE_URL = os.environ.get("TIMESCALE_SERVICE_URL", "")Next we create a TimescaleVector vectorstore. We specify a collection name, which will be the name of the table our data is stored in. Note: When creating a new instance of TimescaleVector, the TimescaleVector Module will try to create a table with the name of the collection. So, make sure that the collection name is unique (i.e it doesn't already exist).# The TimescaleVector Module will create a table with the name of the collection.COLLECTION_NAME =
This notebook shows how to use the Postgres vector database Timescale Vector. You'll learn how to use TimescaleVector for (1) semantic search, (2) time-based vector search, (3) self-querying, and (4) how to create indexes to speed up queries.
This notebook shows how to use the Postgres vector database Timescale Vector. You'll learn how to use TimescaleVector for (1) semantic search, (2) time-based vector search, (3) self-querying, and (4) how to create indexes to speed up queries. ->: import TimescaleVector1. Similarity Search with Euclidean Distance (Default)‚ÄãFirst, we'll look at an example of doing a similarity search query on the State of the Union speech to find the most similar sentences to a given query sentence. We'll use the Euclidean distance as our similarity metric.# Load the text and split it into chunksloader = TextLoader("../../../extras/modules/state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()Next, we'll load the service URL for our Timescale database. If you haven't already, signup for Timescale, and create a new database.Then, to connect to your PostgreSQL database, you'll need your service URI, which can be found in the cheatsheet or .env file you downloaded after creating a new database. The URI will look something like this: postgres://tsdbadmin:<password>@<id>.tsdb.cloud.timescale.com:<port>/tsdb?sslmode=require. # Timescale Vector needs the service url to your cloud database. You can see this as soon as you create the # service in the cloud UI or in your credentials.sql fileSERVICE_URL = os.environ['TIMESCALE_SERVICE_URL']# Specify directly if testing#SERVICE_URL = "postgres://tsdbadmin:<password>@<id>.tsdb.cloud.timescale.com:<port>/tsdb?sslmode=require"# # You can get also it from an enviornment variables. We suggest using a .env file.# import os# SERVICE_URL = os.environ.get("TIMESCALE_SERVICE_URL", "")Next we create a TimescaleVector vectorstore. We specify a collection name, which will be the name of the table our data is stored in. Note: When creating a new instance of TimescaleVector, the TimescaleVector Module will try to create a table with the name of the collection. So, make sure that the collection name is unique (i.e it doesn't already exist).# The TimescaleVector Module will create a table with the name of the collection.COLLECTION_NAME =