Skip to content

Commit

Permalink
Merge branch 'master' into test/async-callback-context
Browse files Browse the repository at this point in the history
  • Loading branch information
parambharat committed Sep 27, 2024
2 parents 657c25c + c6350d6 commit ce0123f
Show file tree
Hide file tree
Showing 21 changed files with 986 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/_test_doc_imports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

- name: Install langchain editable
run: |
poetry run pip install -e libs/core libs/langchain libs/community
poetry run pip install langchain-experimental -e libs/core libs/langchain libs/community
- name: Check doc imports
shell: bash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@
"## Set up the base vector store retriever\n",
"Let's start by initializing a simple vector store retriever and storing the 2023 State of the Union speech (in chunks). We can set up the retriever to retrieve a high number (20) of docs. You can use any of the following Embeddings models: ([source](https://docs.voyageai.com/docs/embeddings)):\n",
"\n",
"- `voyage-large-2` (default)\n",
"- `voyage-3`\n",
"- `voyage-3-lite` \n",
"- `voyage-large-2`\n",
"- `voyage-code-2`\n",
"- `voyage-2`\n",
"- `voyage-law-2`\n",
Expand Down Expand Up @@ -341,6 +343,8 @@
"## Doing reranking with VoyageAIRerank\n",
"Now let's wrap our base retriever with a `ContextualCompressionRetriever`. We'll use the Voyage AI reranker to rerank the returned results. You can use any of the following Reranking models: ([source](https://docs.voyageai.com/docs/reranker)):\n",
"\n",
"- `rerank-2`\n",
"- `rerank-2-lite`\n",
"- `rerank-1`\n",
"- `rerank-lite-1`"
]
Expand Down
5 changes: 3 additions & 2 deletions docs/docs/integrations/providers/sqlite.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ pip install SQLAlchemy

## Vector Store

See a [usage example](/docs/integrations/vectorstores/sqlitevss).
See a [usage example](/docs/integrations/vectorstores/sqlitevec).

```python
from langchain_community.vectorstores import SQLiteVSS
from langchain_community.vectorstores import SQLiteVec
from langchain_community.vectorstores import SQLiteVSS # legacy
```

## Memory
Expand Down
4 changes: 3 additions & 1 deletion docs/docs/integrations/text_embedding/voyageai.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"source": [
"Voyage AI utilizes API keys to monitor usage and manage permissions. To obtain your key, create an account on our [homepage](https://www.voyageai.com). Then, create a VoyageEmbeddings model with your API key. You can use any of the following models: ([source](https://docs.voyageai.com/docs/embeddings)):\n",
"\n",
"- `voyage-large-2` (default)\n",
"- `voyage-3`\n",
"- `voyage-3-lite`\n",
"- `voyage-large-2`\n",
"- `voyage-code-2`\n",
"- `voyage-2`\n",
"- `voyage-law-2`\n",
Expand Down
323 changes: 323 additions & 0 deletions docs/docs/integrations/vectorstores/sqlitevec.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,323 @@
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": [
"---\n",
"sidebar_label: SQLiteVec\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"source": [
"# SQLite as a Vector Store with SQLiteVec\n",
"\n",
"This notebook covers how to get started with the SQLiteVec vector store.\n",
"\n",
">[SQLite-Vec](https://alexgarcia.xyz/sqlite-vec/) is an `SQLite` extension designed for vector search, emphasizing local-first operations and easy integration into applications without external servers. It is the successor to [SQLite-VSS](https://alexgarcia.xyz/sqlite-vss/) by the same author. It is written in zero-dependency C and designed to be easy to build and use.\n",
"\n",
"This notebook shows how to use the `SQLiteVec` vector database."
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Setup\n",
"You'll need to install `langchain-community` with `pip install -qU langchain-community` to use this integration"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# You need to install sqlite-vec as a dependency.\n",
"%pip install --upgrade --quiet sqlite-vec"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"### Credentials\n",
"SQLiteVec does not require any credentials to use as the vector store is a simple SQLite file."
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Initialization"
},
{
"metadata": {
"jupyter": {
"is_executing": true
}
},
"cell_type": "code",
"source": [
"from langchain_community.embeddings.sentence_transformer import (\n",
" SentenceTransformerEmbeddings,\n",
")\n",
"from langchain_community.vectorstores import SQLiteVec\n",
"\n",
"embedding_function = SentenceTransformerEmbeddings(model_name=\"all-MiniLM-L6-v2\")\n",
"vector_store = SQLiteVec(\n",
" table=\"state_union\", db_file=\"/tmp/vec.db\", embedding=embedding_function\n",
")"
],
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Manage vector store"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Add items to vector store"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "vector_store.add_texts(texts=[\"Ketanji Brown Jackson is awesome\", \"foo\", \"bar\"])"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"### Update items in vector store\n",
"Not supported yet"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"### Delete items from vector store\n",
"Not supported yet"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Query vector store"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Query directly"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "data = vector_store.similarity_search(\"Ketanji Brown Jackson\", k=4)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"### Query by turning into retriever\n",
"Not supported yet"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Usage for retrieval-augmented generation\n",
"Refer to the documentation on sqlite-vec at https://alexgarcia.xyz/sqlite-vec/ for more information on how to use it for retrieval-augmented generation."
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## API reference\n",
"For detailed documentation of all SQLiteVec features and configurations head to the API reference:https://api.python.langchain.com/en/latest/vectorstores/langchain_community.vectorstores.sqlitevec.SQLiteVec.html"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Other examples"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2023-09-06T14:55:55.370351Z",
"start_time": "2023-09-06T14:55:53.547755Z"
},
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \\n\\nTonight, 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. \\n\\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \\n\\nAnd 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.'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_community.document_loaders import TextLoader\n",
"from langchain_community.embeddings.sentence_transformer import (\n",
" SentenceTransformerEmbeddings,\n",
")\n",
"from langchain_community.vectorstores import SQLiteVec\n",
"from langchain_text_splitters import CharacterTextSplitter\n",
"\n",
"# load the document and split it into chunks\n",
"loader = TextLoader(\"../../how_to/state_of_the_union.txt\")\n",
"documents = loader.load()\n",
"\n",
"# split it into chunks\n",
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
"docs = text_splitter.split_documents(documents)\n",
"texts = [doc.page_content for doc in docs]\n",
"\n",
"\n",
"# create the open-source embedding function\n",
"embedding_function = SentenceTransformerEmbeddings(model_name=\"all-MiniLM-L6-v2\")\n",
"\n",
"\n",
"# load it in sqlite-vss in a table named state_union.\n",
"# the db_file parameter is the name of the file you want\n",
"# as your sqlite database.\n",
"db = SQLiteVec.from_texts(\n",
" texts=texts,\n",
" embedding=embedding_function,\n",
" table=\"state_union\",\n",
" db_file=\"/tmp/vec.db\",\n",
")\n",
"\n",
"# query it\n",
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"data = db.similarity_search(query)\n",
"\n",
"# print results\n",
"data[0].page_content"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"source": "### Example using existing SQLite connection"
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2023-09-06T14:59:22.086252Z",
"start_time": "2023-09-06T14:59:21.693237Z"
},
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Ketanji Brown Jackson is awesome'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_community.document_loaders import TextLoader\n",
"from langchain_community.embeddings.sentence_transformer import (\n",
" SentenceTransformerEmbeddings,\n",
")\n",
"from langchain_community.vectorstores import SQLiteVec\n",
"from langchain_text_splitters import CharacterTextSplitter\n",
"\n",
"# load the document and split it into chunks\n",
"loader = TextLoader(\"../../how_to/state_of_the_union.txt\")\n",
"documents = loader.load()\n",
"\n",
"# split it into chunks\n",
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
"docs = text_splitter.split_documents(documents)\n",
"texts = [doc.page_content for doc in docs]\n",
"\n",
"\n",
"# create the open-source embedding function\n",
"embedding_function = SentenceTransformerEmbeddings(model_name=\"all-MiniLM-L6-v2\")\n",
"connection = SQLiteVec.create_connection(db_file=\"/tmp/vec.db\")\n",
"\n",
"db1 = SQLiteVec(\n",
" table=\"state_union\", embedding=embedding_function, connection=connection\n",
")\n",
"\n",
"db1.add_texts([\"Ketanji Brown Jackson is awesome\"])\n",
"# query it again\n",
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"data = db1.similarity_search(query)\n",
"\n",
"# print results\n",
"data[0].page_content"
]
}
],
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit ce0123f

Please sign in to comment.