diff --git a/Untitled-1.ipynb b/Untitled-1.ipynb deleted file mode 100644 index 21aa554..0000000 --- a/Untitled-1.ipynb +++ /dev/null @@ -1,272 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Note: you may need to restart the kernel to use updated packages.\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "ERROR: Could not find a version that satisfies the requirement faiss-gpu (from versions: none)\n", - "ERROR: No matching distribution found for faiss-gpu\n" - ] - } - ], - "source": [ - "%pip install -q unstructured-client unstructured[all-docs] langchain transformers accelerate bitsandbytes sentence-transformers faiss-gpu" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'unstructured_client'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[5], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01munstructured_client\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m UnstructuredClient \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[0;32m 3\u001b[0m unstructured_api_key \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39menviron\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUNSTRUCTURED_API_KEY\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 5\u001b[0m client \u001b[38;5;241m=\u001b[39m UnstructuredClient(\n\u001b[0;32m 6\u001b[0m api_key_auth\u001b[38;5;241m=\u001b[39munstructured_api_key,\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# if using paid API, provide your unique API URL:\u001b[39;00m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;66;03m# server_url=\"YOUR_API_URL\",\u001b[39;00m\n\u001b[0;32m 9\u001b[0m )\n", - "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'unstructured_client'" - ] - } - ], - "source": [ - "from unstructured_client import UnstructuredClient # type: ignore\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": 7, - "metadata": {}, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'unstructured_client'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01munstructured_client\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodels\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m shared \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01munstructured_client\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodels\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01merrors\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m SDKError \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01munstructured\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mstaging\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mbase\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m dict_to_elements \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n", - "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'unstructured_client'" - ] - } - ], - "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": 8, - "metadata": {}, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'langchain_core'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[8], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mlangchain_core\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdocuments\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Document\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mlangchain\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mvectorstores\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m FAISS\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mlangchain\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01membeddings\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m HuggingFaceEmbeddings\n", - "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'langchain_core'" - ] - } - ], - "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": 9, - "metadata": {}, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'huggingface_hub'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[9], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mhuggingface_hub\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mhf_api\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m HfFolder\n\u001b[0;32m 3\u001b[0m HfFolder\u001b[38;5;241m.\u001b[39msave_token(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mYOUR_HF_TOKEN\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", - "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'huggingface_hub'" - ] - } - ], - "source": [ - "from huggingface_hub.hf_api import HfFolder\n", - "\n", - "HfFolder.save_token('YOUR_HF_TOKEN')" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'torch'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[10], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtransformers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n\u001b[0;32m 4\u001b[0m model_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmeta-llama/Meta-Llama-3-8B-Instruct\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", - "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'torch'" - ] - } - ], - "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 -}