{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "lIYdn1woOS1n" }, "outputs": [], "source": [ "!git clone https://huggingface.co./datasets/muellerzr/RAG-accelerate" ] }, { "cell_type": "code", "source": [ "%cd RAG-accelerate" ], "metadata": { "id": "N_0YZlykKdVS" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "!pip install langchain" ], "metadata": { "id": "AK8XGa24Kekk" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from pathlib import Path" ], "metadata": { "id": "Qt4Zu6mfLvW2" }, "execution_count": 19, "outputs": [] }, { "cell_type": "code", "source": [ "from langchain.text_splitter import MarkdownHeaderTextSplitter, RecursiveCharacterTextSplitter" ], "metadata": { "id": "a9AHp1eZKgUw" }, "execution_count": 12, "outputs": [] }, { "cell_type": "code", "source": [ "docs_folder = Path(\"docs/source\")" ], "metadata": { "id": "1gxt3ZcTLxAb" }, "execution_count": 20, "outputs": [] }, { "cell_type": "code", "source": [ "headers_to_split_on = [\n", " (\"#\", \"Header 1\"),\n", " (\"##\", \"Header 2\"),\n", " (\"###\", \"Header 3\"),\n", " (\"####\", \"Header 4\")\n", "]" ], "metadata": { "id": "EFwAEg2mKsNL" }, "execution_count": 29, "outputs": [] }, { "cell_type": "code", "source": [ "markdown_splitter = MarkdownHeaderTextSplitter(\n", " headers_to_split_on=headers_to_split_on,\n", " strip_headers=False\n", ")" ], "metadata": { "id": "HpNxkjoZKs2v" }, "execution_count": 30, "outputs": [] }, { "cell_type": "code", "source": [ "chunk_size = 1024\n", "chunk_overlap = 256\n", "text_splitter = RecursiveCharacterTextSplitter(\n", " chunk_size=chunk_size, chunk_overlap=chunk_overlap\n", ")" ], "metadata": { "id": "ch_UDrYIM4GT" }, "execution_count": 33, "outputs": [] }, { "cell_type": "code", "source": [ "categories = [\"package_reference\", \"basic_tutorials\", \"concept_guides\", \"usage_guides\"]" ], "metadata": { "id": "kKRNe4qPMM_6" }, "execution_count": 34, "outputs": [] }, { "cell_type": "code", "source": [ "data = []" ], "metadata": { "id": "D-rajLfOMynB" }, "execution_count": 44, "outputs": [] }, { "cell_type": "code", "source": [ "for path in docs_folder.glob(\"**/*.md\"):\n", " content = path.read_text()\n", " md_header_splits = markdown_splitter.split_text(content)\n", " for i,split in enumerate(md_header_splits):\n", " if path.parent.name in categories:\n", " md_header_splits[i].metadata.update({\"category\":path.parent.name})\n", " splits = text_splitter.split_documents(md_header_splits)\n", " data += splits" ], "metadata": { "id": "zAqjYMdMMJRH" }, "execution_count": 46, "outputs": [] }, { "cell_type": "code", "source": [ "import numpy as np" ], "metadata": { "id": "OAaI9OV_NsTC" }, "execution_count": 50, "outputs": [] }, { "cell_type": "code", "source": [ "np.save(\"processed_documentation_chunks.npy\", data)" ], "metadata": { "id": "H9FzuYdvOGyM" }, "execution_count": 51, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "BgSCAljkOKLG" }, "execution_count": null, "outputs": [] } ], "metadata": { "colab": { "name": "scratchpad", "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }