Created using Colaboratory
Browse files
notebooks/15-Use_OpenSource_Models.ipynb
CHANGED
@@ -4,7 +4,7 @@
|
|
4 |
"metadata": {
|
5 |
"colab": {
|
6 |
"provenance": [],
|
7 |
-
"authorship_tag": "
|
8 |
"include_colab_link": true
|
9 |
},
|
10 |
"kernelspec": {
|
@@ -4829,7 +4829,7 @@
|
|
4829 |
},
|
4830 |
{
|
4831 |
"cell_type": "code",
|
4832 |
-
"execution_count":
|
4833 |
"metadata": {
|
4834 |
"id": "QPJzr-I9XQ7l"
|
4835 |
},
|
@@ -4843,14 +4843,14 @@
|
|
4843 |
"source": [
|
4844 |
"import os\n",
|
4845 |
"\n",
|
4846 |
-
"# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
|
4847 |
"os.environ[\"OPENAI_API_KEY\"] = \"<YOUR_OPENAI_KEY>\"\n",
|
4848 |
"os.environ[\"REPLICATE_API_TOKEN\"] = \"<YOUR_REPLICATE_KEY>\""
|
4849 |
],
|
4850 |
"metadata": {
|
4851 |
"id": "riuXwpSPcvWC"
|
4852 |
},
|
4853 |
-
"execution_count":
|
4854 |
"outputs": []
|
4855 |
},
|
4856 |
{
|
@@ -4863,7 +4863,7 @@
|
|
4863 |
"metadata": {
|
4864 |
"id": "jIEeZzqLbz0J"
|
4865 |
},
|
4866 |
-
"execution_count":
|
4867 |
"outputs": []
|
4868 |
},
|
4869 |
{
|
@@ -4881,7 +4881,7 @@
|
|
4881 |
"from llama_index.prompts import PromptTemplate\n",
|
4882 |
"from llama_index.llms import Replicate\n",
|
4883 |
"\n",
|
4884 |
-
"\n",
|
4885 |
"llm = Replicate(\n",
|
4886 |
" model=\"meta/llama-2-70b-chat:2796ee9483c3fd7aa2e171d38f4ca12251a30609463dcfd4cd76703f22e96cdf\",\n",
|
4887 |
" is_chat_model=True,\n",
|
@@ -4891,7 +4891,7 @@
|
|
4891 |
"metadata": {
|
4892 |
"id": "A1yVgic9DeJ6"
|
4893 |
},
|
4894 |
-
"execution_count":
|
4895 |
"outputs": []
|
4896 |
},
|
4897 |
{
|
@@ -4916,7 +4916,7 @@
|
|
4916 |
"metadata": {
|
4917 |
"id": "SQP87lHczHKc"
|
4918 |
},
|
4919 |
-
"execution_count":
|
4920 |
"outputs": []
|
4921 |
},
|
4922 |
{
|
@@ -4930,7 +4930,7 @@
|
|
4930 |
"metadata": {
|
4931 |
"id": "zAaGcYMJzHAN"
|
4932 |
},
|
4933 |
-
"execution_count":
|
4934 |
"outputs": []
|
4935 |
},
|
4936 |
{
|
@@ -4972,7 +4972,7 @@
|
|
4972 |
"id": "wl_pbPvMlv1h",
|
4973 |
"outputId": "244bb539-f023-46f9-bc25-a05d0e526c14"
|
4974 |
},
|
4975 |
-
"execution_count":
|
4976 |
"outputs": [
|
4977 |
{
|
4978 |
"output_type": "stream",
|
@@ -5027,7 +5027,7 @@
|
|
5027 |
},
|
5028 |
"outputId": "8b475ed3-23b1-43e1-c8fc-027fc26455c3"
|
5029 |
},
|
5030 |
-
"execution_count":
|
5031 |
"outputs": [
|
5032 |
{
|
5033 |
"output_type": "execute_result",
|
@@ -5061,7 +5061,7 @@
|
|
5061 |
"metadata": {
|
5062 |
"id": "YizvmXPejkJE"
|
5063 |
},
|
5064 |
-
"execution_count":
|
5065 |
"outputs": []
|
5066 |
},
|
5067 |
{
|
@@ -5078,6 +5078,8 @@
|
|
5078 |
"source": [
|
5079 |
"from llama_index.text_splitter import TokenTextSplitter\n",
|
5080 |
"\n",
|
|
|
|
|
5081 |
"text_splitter = TokenTextSplitter(\n",
|
5082 |
" separator=\" \", chunk_size=512, chunk_overlap=128\n",
|
5083 |
")"
|
@@ -5085,7 +5087,7 @@
|
|
5085 |
"metadata": {
|
5086 |
"id": "9z3t70DGWsjO"
|
5087 |
},
|
5088 |
-
"execution_count":
|
5089 |
"outputs": []
|
5090 |
},
|
5091 |
{
|
@@ -5099,6 +5101,8 @@
|
|
5099 |
"from llama_index.embeddings.huggingface import HuggingFaceEmbedding\n",
|
5100 |
"from llama_index.ingestion import IngestionPipeline\n",
|
5101 |
"\n",
|
|
|
|
|
5102 |
"pipeline = IngestionPipeline(\n",
|
5103 |
" transformations=[\n",
|
5104 |
" text_splitter,\n",
|
@@ -5207,7 +5211,7 @@
|
|
5207 |
"id": "P9LDJ7o-Wsc-",
|
5208 |
"outputId": "08a795a9-53e3-4a2b-89d2-c0a8912d66b9"
|
5209 |
},
|
5210 |
-
"execution_count":
|
5211 |
"outputs": [
|
5212 |
{
|
5213 |
"output_type": "stream",
|
@@ -5367,7 +5371,7 @@
|
|
5367 |
"id": "mPGa85hM2P3P",
|
5368 |
"outputId": "56c3980a-38a4-40e7-abdd-84ec1f26cb95"
|
5369 |
},
|
5370 |
-
"execution_count":
|
5371 |
"outputs": [
|
5372 |
{
|
5373 |
"output_type": "execute_result",
|
@@ -5384,6 +5388,7 @@
|
|
5384 |
{
|
5385 |
"cell_type": "code",
|
5386 |
"source": [
|
|
|
5387 |
"!zip -r vectorstore-bge-embedding.zip mini-llama-articles"
|
5388 |
],
|
5389 |
"metadata": {
|
@@ -5393,7 +5398,7 @@
|
|
5393 |
"id": "OeeG3jxT0taW",
|
5394 |
"outputId": "d1938534-9a12-4f5e-b7e1-5fd58d687d60"
|
5395 |
},
|
5396 |
-
"execution_count":
|
5397 |
"outputs": [
|
5398 |
{
|
5399 |
"output_type": "stream",
|
@@ -5419,10 +5424,19 @@
|
|
5419 |
"id": "OWaT6rL7ksp8"
|
5420 |
}
|
5421 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5422 |
{
|
5423 |
"cell_type": "code",
|
5424 |
"source": [
|
5425 |
-
"!unzip vectorstore.zip"
|
5426 |
],
|
5427 |
"metadata": {
|
5428 |
"colab": {
|
@@ -5452,7 +5466,7 @@
|
|
5452 |
{
|
5453 |
"cell_type": "code",
|
5454 |
"source": [
|
5455 |
-
"#
|
5456 |
"db = chromadb.PersistentClient(path=\"./mini-llama-articles\")\n",
|
5457 |
"chroma_collection = db.get_or_create_collection(\"mini-llama-articles\")\n",
|
5458 |
"vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
|
@@ -5460,7 +5474,7 @@
|
|
5460 |
"metadata": {
|
5461 |
"id": "mXi56KTXk2sp"
|
5462 |
},
|
5463 |
-
"execution_count":
|
5464 |
"outputs": []
|
5465 |
},
|
5466 |
{
|
@@ -5468,6 +5482,8 @@
|
|
5468 |
"source": [
|
5469 |
"from llama_index import ServiceContext\n",
|
5470 |
"\n",
|
|
|
|
|
5471 |
"service_context = ServiceContext.from_defaults(llm=llm, embed_model=\"local:BAAI/bge-small-en-v1.5\")"
|
5472 |
],
|
5473 |
"metadata": {
|
@@ -5546,7 +5562,7 @@
|
|
5546 |
"id": "RZ5iQ_KkJufJ",
|
5547 |
"outputId": "dd6029ee-10ed-4bf8-95d1-88ac5c636c47"
|
5548 |
},
|
5549 |
-
"execution_count":
|
5550 |
"outputs": [
|
5551 |
{
|
5552 |
"output_type": "display_data",
|
@@ -5637,15 +5653,15 @@
|
|
5637 |
{
|
5638 |
"cell_type": "code",
|
5639 |
"source": [
|
5640 |
-
"# Create your index\n",
|
5641 |
"from llama_index import VectorStoreIndex\n",
|
5642 |
"\n",
|
|
|
5643 |
"index = VectorStoreIndex.from_vector_store(vector_store, service_context=service_context)"
|
5644 |
],
|
5645 |
"metadata": {
|
5646 |
"id": "jKXURvLtkuTS"
|
5647 |
},
|
5648 |
-
"execution_count":
|
5649 |
"outputs": []
|
5650 |
},
|
5651 |
{
|
@@ -5660,12 +5676,14 @@
|
|
5660 |
{
|
5661 |
"cell_type": "code",
|
5662 |
"source": [
|
|
|
|
|
5663 |
"query_engine = index.as_query_engine()"
|
5664 |
],
|
5665 |
"metadata": {
|
5666 |
"id": "8lBu8V7tJ2_8"
|
5667 |
},
|
5668 |
-
"execution_count":
|
5669 |
"outputs": []
|
5670 |
},
|
5671 |
{
|
@@ -5676,7 +5694,7 @@
|
|
5676 |
"metadata": {
|
5677 |
"id": "rWAI0jUhJ7qH"
|
5678 |
},
|
5679 |
-
"execution_count":
|
5680 |
"outputs": []
|
5681 |
},
|
5682 |
{
|
@@ -5692,7 +5710,7 @@
|
|
5692 |
"id": "VKK3jMprctre",
|
5693 |
"outputId": "06d62444-5e04-4e99-abf0-0ff1c76c0a82"
|
5694 |
},
|
5695 |
-
"execution_count":
|
5696 |
"outputs": [
|
5697 |
{
|
5698 |
"output_type": "execute_result",
|
@@ -5726,7 +5744,7 @@
|
|
5726 |
"id": "nvSmOtqBoCY2",
|
5727 |
"outputId": "21a60031-4f39-4d1a-fbfa-ddba7d267936"
|
5728 |
},
|
5729 |
-
"execution_count":
|
5730 |
"outputs": [
|
5731 |
{
|
5732 |
"output_type": "stream",
|
@@ -5761,6 +5779,9 @@
|
|
5761 |
"from llama_index.evaluation import generate_question_context_pairs\n",
|
5762 |
"from llama_index.llms import OpenAI\n",
|
5763 |
"\n",
|
|
|
|
|
|
|
5764 |
"llm = OpenAI(model=\"gpt-3.5-turbo\")\n",
|
5765 |
"rag_eval_dataset = generate_question_context_pairs(\n",
|
5766 |
" nodes,\n",
|
@@ -5768,7 +5789,7 @@
|
|
5768 |
" num_questions_per_chunk=1\n",
|
5769 |
")\n",
|
5770 |
"\n",
|
5771 |
-
"# We can save the dataset as a json file for later use.\n",
|
5772 |
"rag_eval_dataset.save_json(\"./rag_eval_dataset_bge_embedding.json\")"
|
5773 |
],
|
5774 |
"metadata": {
|
@@ -5778,7 +5799,7 @@
|
|
5778 |
},
|
5779 |
"outputId": "a0e0b170-a49e-4c83-fb5a-579046056af5"
|
5780 |
},
|
5781 |
-
"execution_count":
|
5782 |
"outputs": [
|
5783 |
{
|
5784 |
"output_type": "stream",
|
@@ -5789,20 +5810,29 @@
|
|
5789 |
}
|
5790 |
]
|
5791 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5792 |
{
|
5793 |
"cell_type": "code",
|
5794 |
"source": [
|
5795 |
-
"from llama_index.finetuning.embeddings.common import (\n",
|
5796 |
-
"
|
5797 |
-
")\n",
|
5798 |
-
"rag_eval_dataset = EmbeddingQAFinetuneDataset.from_json(\n",
|
5799 |
-
"
|
5800 |
-
")"
|
5801 |
],
|
5802 |
"metadata": {
|
5803 |
"id": "3sA1K84U254o"
|
5804 |
},
|
5805 |
-
"execution_count":
|
5806 |
"outputs": []
|
5807 |
},
|
5808 |
{
|
@@ -5810,6 +5840,7 @@
|
|
5810 |
"source": [
|
5811 |
"import pandas as pd\n",
|
5812 |
"\n",
|
|
|
5813 |
"def display_results_retriever(name, eval_results):\n",
|
5814 |
" \"\"\"Display results from evaluate.\"\"\"\n",
|
5815 |
"\n",
|
@@ -5832,7 +5863,7 @@
|
|
5832 |
"metadata": {
|
5833 |
"id": "H7ubvcbk27vr"
|
5834 |
},
|
5835 |
-
"execution_count":
|
5836 |
"outputs": []
|
5837 |
},
|
5838 |
{
|
@@ -5856,7 +5887,7 @@
|
|
5856 |
"id": "uNLxDxoc2-Ac",
|
5857 |
"outputId": "f5762021-37d3-423e-b4d6-23b1066a0e97"
|
5858 |
},
|
5859 |
-
"execution_count":
|
5860 |
"outputs": [
|
5861 |
{
|
5862 |
"output_type": "stream",
|
|
|
4 |
"metadata": {
|
5 |
"colab": {
|
6 |
"provenance": [],
|
7 |
+
"authorship_tag": "ABX9TyNglgWONCP8jqJ/WIk0fS+z",
|
8 |
"include_colab_link": true
|
9 |
},
|
10 |
"kernelspec": {
|
|
|
4829 |
},
|
4830 |
{
|
4831 |
"cell_type": "code",
|
4832 |
+
"execution_count": null,
|
4833 |
"metadata": {
|
4834 |
"id": "QPJzr-I9XQ7l"
|
4835 |
},
|
|
|
4843 |
"source": [
|
4844 |
"import os\n",
|
4845 |
"\n",
|
4846 |
+
"# Set the \"OPENAI_API_KEY\" and \"REPLICATE_API_TOKEN\" in the Python environment. Will be used by OpenAI client later.\n",
|
4847 |
"os.environ[\"OPENAI_API_KEY\"] = \"<YOUR_OPENAI_KEY>\"\n",
|
4848 |
"os.environ[\"REPLICATE_API_TOKEN\"] = \"<YOUR_REPLICATE_KEY>\""
|
4849 |
],
|
4850 |
"metadata": {
|
4851 |
"id": "riuXwpSPcvWC"
|
4852 |
},
|
4853 |
+
"execution_count": null,
|
4854 |
"outputs": []
|
4855 |
},
|
4856 |
{
|
|
|
4863 |
"metadata": {
|
4864 |
"id": "jIEeZzqLbz0J"
|
4865 |
},
|
4866 |
+
"execution_count": null,
|
4867 |
"outputs": []
|
4868 |
},
|
4869 |
{
|
|
|
4881 |
"from llama_index.prompts import PromptTemplate\n",
|
4882 |
"from llama_index.llms import Replicate\n",
|
4883 |
"\n",
|
4884 |
+
"# Use the repicate service to access the LLaMA2-70B chat model\n",
|
4885 |
"llm = Replicate(\n",
|
4886 |
" model=\"meta/llama-2-70b-chat:2796ee9483c3fd7aa2e171d38f4ca12251a30609463dcfd4cd76703f22e96cdf\",\n",
|
4887 |
" is_chat_model=True,\n",
|
|
|
4891 |
"metadata": {
|
4892 |
"id": "A1yVgic9DeJ6"
|
4893 |
},
|
4894 |
+
"execution_count": null,
|
4895 |
"outputs": []
|
4896 |
},
|
4897 |
{
|
|
|
4916 |
"metadata": {
|
4917 |
"id": "SQP87lHczHKc"
|
4918 |
},
|
4919 |
+
"execution_count": null,
|
4920 |
"outputs": []
|
4921 |
},
|
4922 |
{
|
|
|
4930 |
"metadata": {
|
4931 |
"id": "zAaGcYMJzHAN"
|
4932 |
},
|
4933 |
+
"execution_count": null,
|
4934 |
"outputs": []
|
4935 |
},
|
4936 |
{
|
|
|
4972 |
"id": "wl_pbPvMlv1h",
|
4973 |
"outputId": "244bb539-f023-46f9-bc25-a05d0e526c14"
|
4974 |
},
|
4975 |
+
"execution_count": null,
|
4976 |
"outputs": [
|
4977 |
{
|
4978 |
"output_type": "stream",
|
|
|
5027 |
},
|
5028 |
"outputId": "8b475ed3-23b1-43e1-c8fc-027fc26455c3"
|
5029 |
},
|
5030 |
+
"execution_count": null,
|
5031 |
"outputs": [
|
5032 |
{
|
5033 |
"output_type": "execute_result",
|
|
|
5061 |
"metadata": {
|
5062 |
"id": "YizvmXPejkJE"
|
5063 |
},
|
5064 |
+
"execution_count": null,
|
5065 |
"outputs": []
|
5066 |
},
|
5067 |
{
|
|
|
5078 |
"source": [
|
5079 |
"from llama_index.text_splitter import TokenTextSplitter\n",
|
5080 |
"\n",
|
5081 |
+
"# Define the splitter object that split the text into segments with 512 tokens,\n",
|
5082 |
+
"# with a 128 overlap between the segments.\n",
|
5083 |
"text_splitter = TokenTextSplitter(\n",
|
5084 |
" separator=\" \", chunk_size=512, chunk_overlap=128\n",
|
5085 |
")"
|
|
|
5087 |
"metadata": {
|
5088 |
"id": "9z3t70DGWsjO"
|
5089 |
},
|
5090 |
+
"execution_count": null,
|
5091 |
"outputs": []
|
5092 |
},
|
5093 |
{
|
|
|
5101 |
"from llama_index.embeddings.huggingface import HuggingFaceEmbedding\n",
|
5102 |
"from llama_index.ingestion import IngestionPipeline\n",
|
5103 |
"\n",
|
5104 |
+
"# Create the pipeline to apply the transformation on each chunk,\n",
|
5105 |
+
"# and store the transformed text in the chroma vector store.\n",
|
5106 |
"pipeline = IngestionPipeline(\n",
|
5107 |
" transformations=[\n",
|
5108 |
" text_splitter,\n",
|
|
|
5211 |
"id": "P9LDJ7o-Wsc-",
|
5212 |
"outputId": "08a795a9-53e3-4a2b-89d2-c0a8912d66b9"
|
5213 |
},
|
5214 |
+
"execution_count": null,
|
5215 |
"outputs": [
|
5216 |
{
|
5217 |
"output_type": "stream",
|
|
|
5371 |
"id": "mPGa85hM2P3P",
|
5372 |
"outputId": "56c3980a-38a4-40e7-abdd-84ec1f26cb95"
|
5373 |
},
|
5374 |
+
"execution_count": null,
|
5375 |
"outputs": [
|
5376 |
{
|
5377 |
"output_type": "execute_result",
|
|
|
5388 |
{
|
5389 |
"cell_type": "code",
|
5390 |
"source": [
|
5391 |
+
"# Compress the vector store directory to a zip file to be able to download and use later.\n",
|
5392 |
"!zip -r vectorstore-bge-embedding.zip mini-llama-articles"
|
5393 |
],
|
5394 |
"metadata": {
|
|
|
5398 |
"id": "OeeG3jxT0taW",
|
5399 |
"outputId": "d1938534-9a12-4f5e-b7e1-5fd58d687d60"
|
5400 |
},
|
5401 |
+
"execution_count": null,
|
5402 |
"outputs": [
|
5403 |
{
|
5404 |
"output_type": "stream",
|
|
|
5424 |
"id": "OWaT6rL7ksp8"
|
5425 |
}
|
5426 |
},
|
5427 |
+
{
|
5428 |
+
"cell_type": "markdown",
|
5429 |
+
"source": [
|
5430 |
+
"If you have already uploaded the zip file for the vector store checkpoint, please uncomment the code in the following cell block to extract its contents. After doing so, you will be able to load the dataset from local storage."
|
5431 |
+
],
|
5432 |
+
"metadata": {
|
5433 |
+
"id": "RF4U62oMr-iW"
|
5434 |
+
}
|
5435 |
+
},
|
5436 |
{
|
5437 |
"cell_type": "code",
|
5438 |
"source": [
|
5439 |
+
"# !unzip vectorstore.zip"
|
5440 |
],
|
5441 |
"metadata": {
|
5442 |
"colab": {
|
|
|
5466 |
{
|
5467 |
"cell_type": "code",
|
5468 |
"source": [
|
5469 |
+
"# Load the vector store from the local storage.\n",
|
5470 |
"db = chromadb.PersistentClient(path=\"./mini-llama-articles\")\n",
|
5471 |
"chroma_collection = db.get_or_create_collection(\"mini-llama-articles\")\n",
|
5472 |
"vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
|
|
|
5474 |
"metadata": {
|
5475 |
"id": "mXi56KTXk2sp"
|
5476 |
},
|
5477 |
+
"execution_count": null,
|
5478 |
"outputs": []
|
5479 |
},
|
5480 |
{
|
|
|
5482 |
"source": [
|
5483 |
"from llama_index import ServiceContext\n",
|
5484 |
"\n",
|
5485 |
+
"# Define a ServiceContext that uses the BGE model for embedding which will be loads from Huggingface.\n",
|
5486 |
+
"# The model will be downloaded in your local machine.\n",
|
5487 |
"service_context = ServiceContext.from_defaults(llm=llm, embed_model=\"local:BAAI/bge-small-en-v1.5\")"
|
5488 |
],
|
5489 |
"metadata": {
|
|
|
5562 |
"id": "RZ5iQ_KkJufJ",
|
5563 |
"outputId": "dd6029ee-10ed-4bf8-95d1-88ac5c636c47"
|
5564 |
},
|
5565 |
+
"execution_count": null,
|
5566 |
"outputs": [
|
5567 |
{
|
5568 |
"output_type": "display_data",
|
|
|
5653 |
{
|
5654 |
"cell_type": "code",
|
5655 |
"source": [
|
|
|
5656 |
"from llama_index import VectorStoreIndex\n",
|
5657 |
"\n",
|
5658 |
+
"# Create the index based on the vector store.\n",
|
5659 |
"index = VectorStoreIndex.from_vector_store(vector_store, service_context=service_context)"
|
5660 |
],
|
5661 |
"metadata": {
|
5662 |
"id": "jKXURvLtkuTS"
|
5663 |
},
|
5664 |
+
"execution_count": null,
|
5665 |
"outputs": []
|
5666 |
},
|
5667 |
{
|
|
|
5676 |
{
|
5677 |
"cell_type": "code",
|
5678 |
"source": [
|
5679 |
+
"# Define a query engine that is responsible for retrieving related pieces of text,\n",
|
5680 |
+
"# and using a LLM to formulate the final answer.\n",
|
5681 |
"query_engine = index.as_query_engine()"
|
5682 |
],
|
5683 |
"metadata": {
|
5684 |
"id": "8lBu8V7tJ2_8"
|
5685 |
},
|
5686 |
+
"execution_count": null,
|
5687 |
"outputs": []
|
5688 |
},
|
5689 |
{
|
|
|
5694 |
"metadata": {
|
5695 |
"id": "rWAI0jUhJ7qH"
|
5696 |
},
|
5697 |
+
"execution_count": null,
|
5698 |
"outputs": []
|
5699 |
},
|
5700 |
{
|
|
|
5710 |
"id": "VKK3jMprctre",
|
5711 |
"outputId": "06d62444-5e04-4e99-abf0-0ff1c76c0a82"
|
5712 |
},
|
5713 |
+
"execution_count": null,
|
5714 |
"outputs": [
|
5715 |
{
|
5716 |
"output_type": "execute_result",
|
|
|
5744 |
"id": "nvSmOtqBoCY2",
|
5745 |
"outputId": "21a60031-4f39-4d1a-fbfa-ddba7d267936"
|
5746 |
},
|
5747 |
+
"execution_count": null,
|
5748 |
"outputs": [
|
5749 |
{
|
5750 |
"output_type": "stream",
|
|
|
5779 |
"from llama_index.evaluation import generate_question_context_pairs\n",
|
5780 |
"from llama_index.llms import OpenAI\n",
|
5781 |
"\n",
|
5782 |
+
"# Create questions for each segment. These questions will be used to\n",
|
5783 |
+
"# assess whether the retriever can accurately identify and return the\n",
|
5784 |
+
"# corresponding segment when queried.\n",
|
5785 |
"llm = OpenAI(model=\"gpt-3.5-turbo\")\n",
|
5786 |
"rag_eval_dataset = generate_question_context_pairs(\n",
|
5787 |
" nodes,\n",
|
|
|
5789 |
" num_questions_per_chunk=1\n",
|
5790 |
")\n",
|
5791 |
"\n",
|
5792 |
+
"# We can save the evaluation dataset as a json file for later use.\n",
|
5793 |
"rag_eval_dataset.save_json(\"./rag_eval_dataset_bge_embedding.json\")"
|
5794 |
],
|
5795 |
"metadata": {
|
|
|
5799 |
},
|
5800 |
"outputId": "a0e0b170-a49e-4c83-fb5a-579046056af5"
|
5801 |
},
|
5802 |
+
"execution_count": null,
|
5803 |
"outputs": [
|
5804 |
{
|
5805 |
"output_type": "stream",
|
|
|
5810 |
}
|
5811 |
]
|
5812 |
},
|
5813 |
+
{
|
5814 |
+
"cell_type": "markdown",
|
5815 |
+
"source": [
|
5816 |
+
"If you have uploaded the generated question JSON file, please uncomment the code in the next cell block. This will avoid the need to generate the questions manually, saving you time and effort."
|
5817 |
+
],
|
5818 |
+
"metadata": {
|
5819 |
+
"id": "JjM95B9Zs29W"
|
5820 |
+
}
|
5821 |
+
},
|
5822 |
{
|
5823 |
"cell_type": "code",
|
5824 |
"source": [
|
5825 |
+
"# from llama_index.finetuning.embeddings.common import (\n",
|
5826 |
+
"# EmbeddingQAFinetuneDataset,\n",
|
5827 |
+
"# )\n",
|
5828 |
+
"# rag_eval_dataset = EmbeddingQAFinetuneDataset.from_json(\n",
|
5829 |
+
"# \"./rag_eval_dataset_bge_embedding.json\"\n",
|
5830 |
+
"# )"
|
5831 |
],
|
5832 |
"metadata": {
|
5833 |
"id": "3sA1K84U254o"
|
5834 |
},
|
5835 |
+
"execution_count": null,
|
5836 |
"outputs": []
|
5837 |
},
|
5838 |
{
|
|
|
5840 |
"source": [
|
5841 |
"import pandas as pd\n",
|
5842 |
"\n",
|
5843 |
+
"# A simple function to show the evaluation result.\n",
|
5844 |
"def display_results_retriever(name, eval_results):\n",
|
5845 |
" \"\"\"Display results from evaluate.\"\"\"\n",
|
5846 |
"\n",
|
|
|
5863 |
"metadata": {
|
5864 |
"id": "H7ubvcbk27vr"
|
5865 |
},
|
5866 |
+
"execution_count": null,
|
5867 |
"outputs": []
|
5868 |
},
|
5869 |
{
|
|
|
5887 |
"id": "uNLxDxoc2-Ac",
|
5888 |
"outputId": "f5762021-37d3-423e-b4d6-23b1066a0e97"
|
5889 |
},
|
5890 |
+
"execution_count": null,
|
5891 |
"outputs": [
|
5892 |
{
|
5893 |
"output_type": "stream",
|