From 30abfc41c24a8b7b003ef94c4cef760da4947a53 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Wed, 18 Jan 2023 00:19:05 -0800 Subject: [PATCH] add instructions for saving loading (#642) --- .../combine_docs_examples/vectorstores.ipynb | 78 ++++++++++++++++++- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/docs/modules/utils/combine_docs_examples/vectorstores.ipynb b/docs/modules/utils/combine_docs_examples/vectorstores.ipynb index 3fdfdec0..81097736 100644 --- a/docs/modules/utils/combine_docs_examples/vectorstores.ipynb +++ b/docs/modules/utils/combine_docs_examples/vectorstores.ipynb @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "id": "965eecee", "metadata": { "pycharm": { @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 7, "id": "68481687", "metadata": { "pycharm": { @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 8, "id": "015f4ff5", "metadata": { "pycharm": { @@ -199,6 +199,78 @@ "docs_and_scores[0]" ] }, + { + "cell_type": "markdown", + "id": "b386dbb8", + "metadata": {}, + "source": [ + "### Saving and loading\n", + "You can also save and load a FAISS index. This is useful so you don't have to recreate it everytime you use it." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "b58b3955", + "metadata": {}, + "outputs": [], + "source": [ + "import pickle" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "1897e23d", + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"foo.pkl\", 'wb') as f:\n", + " pickle.dump(docsearch, f)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "bf3732f1", + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"foo.pkl\", 'rb') as f:\n", + " new_docsearch = pickle.load(f)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "5bf2ee24", + "metadata": {}, + "outputs": [], + "source": [ + "docs = new_docsearch.similarity_search(query)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "edc2aad1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Document(page_content='In state after state, new laws have been passed, not only to suppress the vote, but to subvert entire elections. \\n\\nWe cannot let this happen. \\n\\nTonight. 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.', lookup_str='', metadata={}, lookup_index=0)" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "docs[0]" + ] + }, { "cell_type": "markdown", "id": "eea6e627",