Last active
August 28, 2023 19:08
-
-
Save cra/acb63cb9d990168f02a8a6e91b95a663 to your computer and use it in GitHub Desktop.
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
export OPENAI_API_KEY=sk-HS1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "5aa4abd6-3ed3-410f-9f7b-01aaf209586c", | |
"metadata": {}, | |
"source": [ | |
"Сначала надо установить всякое\n", | |
"\n", | |
"```\n", | |
"pip install langchain pypdf chromadb tiktoken\n", | |
"```\n", | |
"\n", | |
"хромадб это база для эмбеддингов, тиктокен режет на токены, остальное понятно" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "86cbf32c-2711-4321-815d-9772527ef58d", | |
"metadata": {}, | |
"source": [ | |
"сначала может пригодится сплиттер чтобы резать текст на чанки когда текст длинный" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"id": "62763214-8e0e-4fbc-bf6e-30040d702e36", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from langchain.text_splitter import CharacterTextSplitter\n", | |
"\n", | |
"splitter = CharacterTextSplitter(\n", | |
" separator='\\n',\n", | |
" chunk_size=512,\n", | |
" chunk_overlap=64,\n", | |
" length_function=len,\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"id": "dd9a5e2c-dd14-45d3-9e06-33bca00a6c7a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from langchain.document_loaders import PyPDFLoader" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"id": "b4b90d84-35a1-4b40-ad69-6504500eba22", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"pdfs = []\n", | |
"for path in (\"./pashka.pdf\", \"./tuesday.pdf\"):\n", | |
" loader = PyPDFLoader(path)\n", | |
" pdf = loader.load()\n", | |
" pdfs.extend(splitter.split_documents(pdf))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"id": "b3ce6128-abe8-44f5-aba9-24e098b56f52", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[Document(page_content='Pashka is a parrot with a green tail.\\nHe is rather smart and his favorite food is rice cracker with pickles', metadata={'source': './pashka.pdf', 'page': 0}),\n", | |
" Document(page_content='On tuesday Pashka went missing but then we found him under the bed', metadata={'source': './tuesday.pdf', 'page': 0})]" | |
] | |
}, | |
"execution_count": 36, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"pdfs # тут их могло бы быть много" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "840a228f-b586-4dab-8306-f2ef35c8409f", | |
"metadata": {}, | |
"source": [ | |
"теперь общаемся с учётом дока\n", | |
"\n", | |
"делаем эмбеддинги. для этого в env долженг быть ключик `OPENAI_API_KEY`, но если что код ниже ругнётся если его нет не переживай" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"id": "2c82af46-12df-4d70-a215-5eb9e8358357", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import os\n", | |
"assert 'OPENAI_API_KEY' in os.environ, 'если ты это проигноришь дальше дело не пойдёт'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"id": "d3841730-8072-4d2d-80ff-6ad385365e29", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from langchain.embeddings import OpenAIEmbeddings\n", | |
"from langchain.vectorstores import Chroma\n", | |
"from langchain.chains import RetrievalQA\n", | |
"\n", | |
"embeddings = OpenAIEmbeddings()\n", | |
"db = Chroma.from_documents(pdfs, embeddings, persist_directory='/tmp/db')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "6c73ad7b-bd65-4460-8006-72c26ee5c637", | |
"metadata": {}, | |
"source": [ | |
"теперь когда есть база и эмбеддинги надо сделать ретривер" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"id": "a167118d-67a0-4072-9366-eeaea891a93a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from langchain.llms import OpenAI\n", | |
"my_retriever = db.as_retriever()\n", | |
"qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type='stuff', retriever=my_retriever)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"id": "39aa50ba-419c-4ee6-a8e9-4ce7f622495a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"' On Tuesday Pashka was under the bed and his tail was green.'" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"query = 'Какого цвета во вторник был ховст у Пашки? И где он был?'\n", | |
"qa.run(query)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "c1916557-a7f9-4812-acd0-4a2a4f2ac0cf", | |
"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.10.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment