-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Langchain <> VLite integration script\n", | ||
"# !pip install langchain==0.1.17\n", | ||
"import requests\n", | ||
"from langchain.document_loaders import TextLoader\n", | ||
"from langchain.text_splitter import CharacterTextSplitter\n", | ||
"from langchain.vectorstores import VLite\n", | ||
"\n", | ||
"# Download the PDF\n", | ||
"open('attention.pdf', 'wb').write(requests.get('https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf').content)\n", | ||
"\n", | ||
"# Load the PDF document\n", | ||
"loader = TextLoader('attention.pdf')\n", | ||
"documents = loader.load()\n", | ||
"\n", | ||
"# Split the documents into chunks\n", | ||
"text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)\n", | ||
"texts = text_splitter.split_documents(documents)\n", | ||
"\n", | ||
"# Create a VLite instance\n", | ||
"vlite = VLite(collection=\"attention\")\n", | ||
"\n", | ||
"# Add texts to the VLite vector database\n", | ||
"vlite.add_texts([text.page_content for text in texts])\n", | ||
"\n", | ||
"# Perform a similarity search\n", | ||
"query = \"What is attention?\"\n", | ||
"docs = vlite.similarity_search(query, k=3)\n", | ||
"\n", | ||
"# Print the most relevant chunks\n", | ||
"for doc in docs:\n", | ||
" print(doc.page_content)\n", | ||
" print('---')\n", | ||
"\n", | ||
"# Get collection information\n", | ||
"vlite.info()\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Pure vlite example\n", | ||
"import requests\n", | ||
"from vlite import VLite\n", | ||
"from vlite.utils import process_pdf\n", | ||
"\n", | ||
"# Start VLite\n", | ||
"vdb = VLite('attention2')\n", | ||
"\n", | ||
"# Download the pdf\n", | ||
"open('attention.pdf', 'wb').write(requests.get('https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf').content)\n", | ||
"\n", | ||
"# Process the pdf\n", | ||
"corpus = process_pdf('attention.pdf')\n", | ||
"\n", | ||
"# Add the PDF to the VLite database\n", | ||
"vdb.add(corpus)\n", | ||
"\n", | ||
"# Query the VLite database\n", | ||
"print(vdb.retrieve('what is attention'))\n", | ||
"\n", | ||
"# Print the VLite database\n", | ||
"vdb.info()\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "base", | ||
"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.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters