diff --git a/Untitled-1.ipynb b/Untitled-1.ipynb new file mode 100644 index 0000000..4c2a575 --- /dev/null +++ b/Untitled-1.ipynb @@ -0,0 +1,196 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%pip install -q unstructured-client unstructured[all-docs] langchain transformers accelerate bitsandbytes sentence-transformers faiss-gpu" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from unstructured_client import UnstructuredClient\n", + "\n", + "unstructured_api_key = os.environ.get(\"UNSTRUCTURED_API_KEY\")\n", + "\n", + "client = UnstructuredClient(\n", + " api_key_auth=unstructured_api_key,\n", + " # if using paid API, provide your unique API URL:\n", + " # server_url=\"YOUR_API_URL\",\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from unstructured_client.models import shared # type: ignore\n", + "from unstructured_client.models.errors import SDKError # type: ignore\n", + "from unstructured.staging.base import dict_to_elements # type: ignore\n", + "\n", + "path_to_pdf=\"PATH_TO_YOUR_PDF_FILE\"\n", + "\n", + "with open(path_to_pdf, \"rb\") as f:\n", + " files=shared.Files(\n", + " content=f.read(),\n", + " file_name=path_to_pdf,\n", + " )\n", + " req = shared.PartitionParameters(\n", + " files=files,\n", + " chunking_strategy=\"by_title\",\n", + " max_characters=512,\n", + " )\n", + " try:\n", + " resp = client.general.partition(req)\n", + " except SDKError as e:\n", + " print(e)\n", + "\n", + "elements = dict_to_elements(resp.elements)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.documents import Document\n", + "from langchain.vectorstores import FAISS\n", + "from langchain.embeddings import HuggingFaceEmbeddings\n", + "\n", + "documents = []\n", + "for element in elements:\n", + " metadata = element.metadata.to_dict()\n", + " documents.append(Document(page_content=element.text, metadata=metadata))\n", + "\n", + "db = FAISS.from_documents(documents, HuggingFaceEmbeddings(model_name=\"BAAI/bge-base-en-v1.5\"))\n", + "retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": 4})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from huggingface_hub.hf_api import HfFolder\n", + "\n", + "HfFolder.save_token('YOUR_HF_TOKEN')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n", + "\n", + "model_name = \"meta-llama/Meta-Llama-3-8B-Instruct\"\n", + "\n", + "bnb_config = BitsAndBytesConfig(\n", + " load_in_4bit=True, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type=\"nf4\", bnb_4bit_compute_dtype=torch.bfloat16\n", + ")\n", + "\n", + "model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=bnb_config)\n", + "tokenizer = AutoTokenizer.from_pretrained(model_name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.llms import HuggingFacePipeline\n", + "from langchain.prompts import PromptTemplate\n", + "from transformers import pipeline\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_core.runnables import RunnablePassthrough\n", + "\n", + "terminators = [\n", + " tokenizer.eos_token_id,\n", + " tokenizer.convert_tokens_to_ids(\"<|eot_id|>\")\n", + "]\n", + "\n", + "text_generation_pipeline = pipeline(\n", + " model=model,\n", + " tokenizer=tokenizer,\n", + " task=\"text-generation\",\n", + " temperature=0.2,\n", + " do_sample=True,\n", + " repetition_penalty=1.1,\n", + " return_full_text=False,\n", + " max_new_tokens=200,\n", + " eos_token_id=terminators,\n", + ")\n", + "\n", + "llm = HuggingFacePipeline(pipeline=text_generation_pipeline)\n", + "\n", + "prompt_template = \"\"\"\n", + "<|start_header_id|>user<|end_header_id|>\n", + "You are an assistant for answering questions about IPM.\n", + "You are given the extracted parts of a long document and a question. Provide a conversational answer.\n", + "If you don't know the answer, just say \"I do not know.\" Don't make up an answer.\n", + "Question: {question}\n", + "Context: {context}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n", + "\"\"\"\n", + "\n", + "prompt = PromptTemplate(\n", + " input_variables=[\"context\", \"question\"],\n", + " template=prompt_template,\n", + ")\n", + "\n", + "def format_docs(docs):\n", + " return \"\\n\\n\".join(doc.page_content for doc in docs)\n", + "\n", + "rag_chain = (\n", + " {\"context\": retriever | format_docs, \"question\": RunnablePassthrough()}\n", + " | prompt\n", + " | llm\n", + " | StrOutputParser()\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "question = \"What is best practices for RAG ?\"\n", + "rag_chain.invoke(question)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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 +}