avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2457

Abstract

Create a Timescale Vector index</span> db.create_index()</pre></div><p id="594d">You can also specify the exact parameters for index creation:</p><div id="95f4"><pre><span class="hljs-comment"># Create an timescale vector index (DiskANN) with specified parameters</span> db.create_index(<span class="hljs-attribute">index_type</span>=<span class="hljs-string">"tsv"</span>, <span class="hljs-attribute">max_alpha</span>=1.0, <span class="hljs-attribute">num_neighbors</span>=50)</pre></div><h2 id="0a49">Add Efficient Time-Based Search Functionality to Your LangChain AI Application</h2><p id="4d6f">Timescale Vector optimizes time-based vector search by leveraging the automatic time-based partitioning and indexing of Timescale’s hypertables. You can take advantage of this feature by creating a Timescale Vector instance from the collection of documents and specifying the time partition interval:</p><div id="e6f3"><pre><span class="hljs-comment"># Create a Timescale Vector instance from the collection of documents</span> db = TimescaleVector.from_documents( <span class="hljs-attribute">embedding</span>=embeddings, ids = [doc.metadata[<span class="hljs-string">"id"</span>] <span class="hljs-keyword">for</span> doc <span class="hljs-keyword">in</span> docs], <span class="hljs-attribute">documents</span>=docs, <span class="hljs-attribute">collection_name</span>=COLLECTION_NAME, <span class="hljs-attribute">service_url</span>=SERVICE_URL, <span class="hljs-attribute">time_partition_interval</span>=timedelta(days = 7), )</pre></div><h2 id="8a82">Powering Retrieval Augmented Generation With Time-Based Context Retrieval in LangChain Applications with Timescale Vector</h2><p id="519f">To power Retrieval Augmented Generation (RAG) with time-based context retrieval, you can create a retriever from the TimescaleVector store and use it in a RetrievalQA chain:</p><div id="d128"><pre><span class="hljs-comment"># Set timescale vector as a retriever and specify start and end dates via kwargs</span> retriever = db.as_retriever(search_kwargs={<span class="hljs-string">"start_date"</span>: start_dt, <span class="hljs-string">"end_date"</span>: end_dt})

<span class="hljs-comment"># Create a RetrievalQA chain</span> qa_stuff = RetrievalQA.from_chain_type( <span class="hljs-attribute">llm</span>=llm, <span class="hljs-attribute">chain_type</span>=<span class="hljs-string">"stuff"</span>, <span class="hljs

Options

-attribute">retriever</span>=retriever, <span class="hljs-attribute">verbose</span>=<span class="hljs-literal">True</span>, )

<span class="hljs-comment"># Query the RetrievalQA chain</span> response = qa_stuff.<span class="hljs-built_in">run</span>(query) <span class="hljs-built_in">print</span>(response)</pre></div><h2 id="21bc">Advanced LangChain Self-Querying Capabilities With Timescale Vector</h2><p id="a76d">Timescale Vector also supports one of LangChain’s coolest features: the Self-Querying retriever. You can create a self-query retriever from the TimescaleVector store and use it to perform complex searches over your vector store without writing any SQL directly:</p><div id="aac3"><pre><span class="hljs-comment"># Instantiate the self-query retriever from an LLM</span> retriever = SelfQueryRetriever.from_llm( llm, vectorstore, document_content_description, metadata_field_info, <span class="hljs-attribute">enable_limit</span>=<span class="hljs-literal">True</span>, <span class="hljs-attribute">verbose</span>=<span class="hljs-literal">True</span> )

<span class="hljs-comment"># Query the self-query retriever</span> retriever.get_relevant_documents(<span class="hljs-string">"What commits about timescaledb_functions did Sven Klemm add?"</span>)</pre></div><p id="aab8">By leveraging Timescale Vector’s advanced features, LangChain developers can build better AI applications with PostgreSQL as their vector database. For more detailed tutorials and resources, you can explore the Up and Running Tutorial, Self-query retriever tutorial, Timescale Vector explainer, and Timescale Vector website.</p><div id="8816" class="link-block"> <a href="https://readmedium.com/langchain-what-is-eden-ai-x-langchain-5ea7213ee7f7"> <div> <div> <h2>LANGCHAIN — What Is Eden AI X Langchain?</h2> <div><h3>I’m not a great programmer; I’m just a good programmer with great habits. — Kent Beck</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*nu7ZXSdSXeo6aCLEJYoZpg.jpeg)"></div> </div> </div> </a> </div><p id="dcb3">With Timescale offering a free 90-day trial for LangChain users, now is the perfect time to test and develop your applications with Timescale Vector.</p></article></body>

LANGCHAIN — Is Timescale Vector X LangChain Making PostgreSQL a Better Vector Database for AI Applications?

Technology’s future is in the hands of the dreamers, not the regulators. — Robin Chase

Timescale Vector integration with LangChain brings the power of PostgreSQL as a vector database for AI applications, enabling faster similarity search and efficient time-based context retrieval. In this tutorial, we’ll dive into the unique capabilities of Timescale Vector and provide code snippets to demonstrate how to leverage its features in LangChain.

Faster Vector Similarity Search in PostgreSQL

Timescale Vector speeds up Approximate Nearest Neighbor (ANN) search on large scale vector datasets, enhancing pgvector with a state-of-the-art ANN index inspired by the DiskANN algorithm. Using Timescale Vector’s DiskANN, HNSW, or IVFFLAT indexes in LangChain is incredibly straightforward:

from langchain.vectorstores.timescalevector import TimescaleVector

# Create a Timescale Vector instance from the collection of documents
db = TimescaleVector.from_documents(
   embedding=embeddings,
   documents=docs,
   collection_name=COLLECTION_NAME,
   service_url=SERVICE_URL,
)

# Create a Timescale Vector index
db.create_index()

You can also specify the exact parameters for index creation:

# Create an timescale vector index (DiskANN) with specified parameters
db.create_index(index_type="tsv", max_alpha=1.0, num_neighbors=50)

Add Efficient Time-Based Search Functionality to Your LangChain AI Application

Timescale Vector optimizes time-based vector search by leveraging the automatic time-based partitioning and indexing of Timescale’s hypertables. You can take advantage of this feature by creating a Timescale Vector instance from the collection of documents and specifying the time partition interval:

# Create a Timescale Vector instance from the collection of documents
db = TimescaleVector.from_documents(
     embedding=embeddings,
     ids = [doc.metadata["id"] for doc in docs],
     documents=docs,
     collection_name=COLLECTION_NAME,
     service_url=SERVICE_URL,
     time_partition_interval=timedelta(days = 7),
)

Powering Retrieval Augmented Generation With Time-Based Context Retrieval in LangChain Applications with Timescale Vector

To power Retrieval Augmented Generation (RAG) with time-based context retrieval, you can create a retriever from the TimescaleVector store and use it in a RetrievalQA chain:

# Set timescale vector as a retriever and specify start and end dates via kwargs
retriever = db.as_retriever(search_kwargs={"start_date": start_dt, "end_date": end_dt})

# Create a RetrievalQA chain
qa_stuff = RetrievalQA.from_chain_type(
   llm=llm,
   chain_type="stuff",
   retriever=retriever,
   verbose=True,
)

# Query the RetrievalQA chain
response = qa_stuff.run(query)
print(response)

Advanced LangChain Self-Querying Capabilities With Timescale Vector

Timescale Vector also supports one of LangChain’s coolest features: the Self-Querying retriever. You can create a self-query retriever from the TimescaleVector store and use it to perform complex searches over your vector store without writing any SQL directly:

# Instantiate the self-query retriever from an LLM
retriever = SelfQueryRetriever.from_llm(
   llm, vectorstore, document_content_description, metadata_field_info, enable_limit=True, verbose=True
)

# Query the self-query retriever
retriever.get_relevant_documents("What commits about timescaledb_functions did Sven Klemm add?")

By leveraging Timescale Vector’s advanced features, LangChain developers can build better AI applications with PostgreSQL as their vector database. For more detailed tutorials and resources, you can explore the Up and Running Tutorial, Self-query retriever tutorial, Timescale Vector explainer, and Timescale Vector website.

With Timescale offering a free 90-day trial for LangChain users, now is the perfect time to test and develop your applications with Timescale Vector.

Making
Timescale
Langchain
Vector
Database
Recommended from ReadMedium