mirror of
https://github.com/hwchase17/langchain
synced 2024-10-29 17:07:25 +00:00
c81fb88035
# Vectara Integration This PR provides integration with Vectara. Implemented here are: * langchain/vectorstore/vectara.py * tests/integration_tests/vectorstores/test_vectara.py * langchain/retrievers/vectara_retriever.py And two IPYNB notebooks to do more testing: * docs/modules/chains/index_examples/vectara_text_generation.ipynb * docs/modules/indexes/vectorstores/examples/vectara.ipynb --------- Co-authored-by: Dev 2049 <dev.dev2049@gmail.com>
319 lines
9.2 KiB
Plaintext
319 lines
9.2 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "683953b3",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Vectara\n",
|
||
"\n",
|
||
">[Vectara](https://Vectara.com/docs/) is a API platform for building LLM-powered applications. It provides a simple to use API for document indexing and query that is managed by Vectara and is optimized for performance and accuracy. \n",
|
||
"\n",
|
||
"\n",
|
||
"This notebook shows how to use functionality related to the `Vectara` vector database. \n",
|
||
"\n",
|
||
"See the [Vectara API documentation ](https://Vectara.com/docs/) for more information on how to use the API."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "7b2f111b-357a-4f42-9730-ef0603bdc1b5",
|
||
"metadata": {},
|
||
"source": [
|
||
"We want to use `OpenAIEmbeddings` so we have to get the OpenAI API Key."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "082e7e8b-ac52-430c-98d6-8f0924457642",
|
||
"metadata": {
|
||
"tags": []
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"OpenAI API Key:········\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import os\n",
|
||
"import getpass\n",
|
||
"\n",
|
||
"os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "aac9563e",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-04-04T10:51:22.282884Z",
|
||
"start_time": "2023-04-04T10:51:21.408077Z"
|
||
},
|
||
"tags": []
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
|
||
"from langchain.text_splitter import CharacterTextSplitter\n",
|
||
"from langchain.vectorstores import Vectara\n",
|
||
"from langchain.document_loaders import TextLoader"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"id": "a3c3999a",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-04-04T10:51:22.520144Z",
|
||
"start_time": "2023-04-04T10:51:22.285826Z"
|
||
},
|
||
"tags": []
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"loader = TextLoader('../../../state_of_the_union.txt')\n",
|
||
"documents = loader.load()\n",
|
||
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
|
||
"docs = text_splitter.split_documents(documents)\n",
|
||
"\n",
|
||
"embeddings = OpenAIEmbeddings()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "eeead681",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Connecting to Vectara from LangChain\n",
|
||
"\n",
|
||
"The Vectara API provides simple API endpoints for indexing and querying."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"id": "8429667e",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-04-04T10:51:22.525091Z",
|
||
"start_time": "2023-04-04T10:51:22.522015Z"
|
||
},
|
||
"tags": []
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"vectara = Vectara.from_documents(docs, embedding=None)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "1f9215c8",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-04-04T09:27:29.920258Z",
|
||
"start_time": "2023-04-04T09:27:29.913714Z"
|
||
}
|
||
},
|
||
"source": [
|
||
"## Similarity search\n",
|
||
"\n",
|
||
"The simplest scenario for using Vectara is to perform a similarity search. "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"id": "a8c513ab",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-04-04T10:51:25.204469Z",
|
||
"start_time": "2023-04-04T10:51:24.855618Z"
|
||
},
|
||
"tags": []
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
|
||
"found_docs = vectara.similarity_search(query)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"id": "fc516993",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-04-04T10:51:25.220984Z",
|
||
"start_time": "2023-04-04T10:51:25.213943Z"
|
||
},
|
||
"tags": []
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence. A former top litigator in private practice. A former federal public defender.\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print(found_docs[0].page_content)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "1bda9bf5",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Similarity search with score\n",
|
||
"\n",
|
||
"Sometimes we might want to perform the search, but also obtain a relevancy score to know how good is a particular result."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"id": "8804a21d",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-04-04T10:51:25.631585Z",
|
||
"start_time": "2023-04-04T10:51:25.227384Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
|
||
"found_docs = vectara.similarity_search_with_score(query)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"id": "756a6887",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-04-04T10:51:25.642282Z",
|
||
"start_time": "2023-04-04T10:51:25.635947Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence. A former top litigator in private practice. A former federal public defender.\n",
|
||
"\n",
|
||
"Score: 1.0046461\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"document, score = found_docs[0]\n",
|
||
"print(document.page_content)\n",
|
||
"print(f\"\\nScore: {score}\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "691a82d6",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Vectara as a Retriever\n",
|
||
"\n",
|
||
"Vectara, as all the other vector stores, is a LangChain Retriever, by using cosine similarity. "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"id": "9427195f",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-04-04T10:51:26.031451Z",
|
||
"start_time": "2023-04-04T10:51:26.018763Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"VectorStoreRetriever(vectorstore=<langchain.vectorstores.vectara.Vectara object at 0x156d3e830>, search_type='similarity', search_kwargs={})"
|
||
]
|
||
},
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"retriever = vectara.as_retriever()\n",
|
||
"retriever"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"id": "f3c70c31",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-04-04T10:51:26.495652Z",
|
||
"start_time": "2023-04-04T10:51:26.046407Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Document(page_content='Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence. A former top litigator in private practice. A former federal public defender.', metadata={'source': '../../modules/state_of_the_union.txt'})"
|
||
]
|
||
},
|
||
"execution_count": 15,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
|
||
"retriever.get_relevant_documents(query)[0]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "2300e785",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3 (ipykernel)",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.11.3"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|