File size: 9,608 Bytes
4b1fcb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generate synthetic dataset of questions + doc ids\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# DEFAULT_QA_GENERATE_PROMPT_TMPL = \"\"\"\\\n",
    "# Context information is below.\n",
    "\n",
    "# ---------------------\n",
    "# {context_str}\n",
    "# ---------------------\n",
    "\n",
    "# Given the context information and not prior knowledge.\n",
    "# generate only questions based on the below query.\n",
    "\n",
    "# You are a Teacher/ Professor with expertise in the field of AI. Your task is to setup \\\n",
    "# {num_questions_per_chunk} questions for an upcoming \\\n",
    "# quiz/examination. The questions should be diverse in nature \\\n",
    "# across the document. Restrict the questions to the \\\n",
    "# context information provided.\"\n",
    "# \"\"\"\n",
    "\n",
    "STUDENT_AI_QUESTIONS_GENERATE_PROMPT_TMPL = \"\"\"\\\n",
    "Context information is below.\n",
    "\n",
    "---------------------\n",
    "{context_str}\n",
    "---------------------\n",
    "\n",
    "Given the context information above, generate {num_questions_per_chunk} questions that a student might ask about AI, specifically related to the information provided in the context, \n",
    "but without the questions mentioning the context information.\n",
    "\n",
    "You are simulating curious students who are learning about AI. Your task is to create questions that:\n",
    "1. Reflect genuine curiosity about the topics covered in the context.\n",
    "2. Vary in complexity, from basic clarifications to more advanced inquiries.\n",
    "3. Demonstrate a student's desire to understand AI concepts better.\n",
    "4. Can be answered using the information provided in the context.\n",
    "\n",
    "The questions should be diverse and cover different aspects of the content. Do not use any prior knowledge beyond what's given in the context. Ensure that each question you generate can be answered using the information provided in the context.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.openai import OpenAI\n",
    "from llama_index.llms.gemini import Gemini\n",
    "\n",
    "# llm = Gemini(model=\"models/gemini-1.5-flash-latest\", temperature=1, max_tokens=1000)\n",
    "llm = OpenAI(\n",
    "    api_key=\"\",\n",
    "    model=\"gpt-4o-mini\",\n",
    "    temperature=1,\n",
    "    max_tokens=1000,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import re\n",
    "import uuid\n",
    "import warnings\n",
    "import asyncio\n",
    "from typing import Dict, List, Tuple\n",
    "\n",
    "from llama_index.core.llms.llm import LLM\n",
    "from llama_index.core.schema import MetadataMode, TextNode\n",
    "from tqdm.asyncio import tqdm as async_tqdm\n",
    "from llama_index.core.llama_dataset.legacy.embedding import EmbeddingQAFinetuneDataset\n",
    "from llama_index.core import Document\n",
    "\n",
    "\n",
    "async def generate_qa_embedding_pairs(\n",
    "    nodes: List[TextNode],\n",
    "    llm: LLM,\n",
    "    qa_generate_prompt_tmpl: str = STUDENT_AI_QUESTIONS_GENERATE_PROMPT_TMPL,\n",
    "    num_questions_per_chunk: int = 1,\n",
    "    max_concurrent: int = 10,\n",
    "    delay: float = 0.5,\n",
    ") -> EmbeddingQAFinetuneDataset:\n",
    "    \"\"\"Generate examples given a set of nodes.\"\"\"\n",
    "    node_dict = {\n",
    "        node.node_id: node.get_content(metadata_mode=MetadataMode.NONE)\n",
    "        for node in nodes\n",
    "    }\n",
    "\n",
    "    queries = {}\n",
    "    relevant_docs = {}\n",
    "    semaphore = asyncio.Semaphore(max_concurrent)\n",
    "\n",
    "    async def process_node(node_id: str, text: str):\n",
    "        async with semaphore:\n",
    "            query = qa_generate_prompt_tmpl.format(\n",
    "                context_str=text, num_questions_per_chunk=num_questions_per_chunk\n",
    "            )\n",
    "            response = await llm.acomplete(\n",
    "                query\n",
    "            )  # Assuming the LLM has an async method\n",
    "\n",
    "            result = str(response).strip().split(\"\\n\")\n",
    "            questions = [\n",
    "                re.sub(r\"^\\d+[\\).\\s]\", \"\", question).strip() for question in result\n",
    "            ]\n",
    "            questions = [question for question in questions if len(question) > 0][\n",
    "                :num_questions_per_chunk\n",
    "            ]\n",
    "\n",
    "            num_questions_generated = len(questions)\n",
    "            if num_questions_generated < num_questions_per_chunk:\n",
    "                warnings.warn(\n",
    "                    f\"Fewer questions generated ({num_questions_generated}) \"\n",
    "                    f\"than requested ({num_questions_per_chunk}).\"\n",
    "                )\n",
    "\n",
    "            for question in questions:\n",
    "                question_id = str(uuid.uuid4())\n",
    "                queries[question_id] = question\n",
    "                relevant_docs[question_id] = [node_id]\n",
    "\n",
    "            await asyncio.sleep(delay)\n",
    "\n",
    "    # Use asyncio.gather to process nodes concurrently\n",
    "    await async_tqdm.gather(\n",
    "        *[process_node(node_id, text) for node_id, text in node_dict.items()]\n",
    "    )\n",
    "\n",
    "    # construct dataset\n",
    "    return EmbeddingQAFinetuneDataset(\n",
    "        queries=queries, corpus=node_dict, relevant_docs=relevant_docs\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "\n",
    "async def generate_questions(path, source_name):\n",
    "    nodes = []\n",
    "    with open(path, \"rb\") as f:\n",
    "        document_dict = pickle.load(f)\n",
    "        for doc_id in document_dict.keys():\n",
    "            doc: Document = document_dict[doc_id]\n",
    "            if doc.metadata[\"tokens\"] >= 100_000:\n",
    "                print(\"skipping\", doc.metadata[\"tokens\"])\n",
    "                continue\n",
    "            node = TextNode(text=doc.text, metadata=doc.metadata, id_=doc_id)\n",
    "            nodes.append(node)\n",
    "\n",
    "    rag_eval_dataset: EmbeddingQAFinetuneDataset = await generate_qa_embedding_pairs(\n",
    "        nodes,\n",
    "        llm=llm,\n",
    "        num_questions_per_chunk=1,\n",
    "        qa_generate_prompt_tmpl=STUDENT_AI_QUESTIONS_GENERATE_PROMPT_TMPL,\n",
    "        max_concurrent=20,  # Adjust this to control concurrency\n",
    "        delay=0.5,  # Adjust this to add delay between API calls\n",
    "    )\n",
    "    rag_eval_dataset.save_json(f\"./rag_eval_{source_name}.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# await generate_questions(\n",
    "#     \"../data/chroma-db-langchain/document_dict_langchain.pkl\", \"langchain\"\n",
    "# )\n",
    "# await generate_questions(\n",
    "#     \"../data/chroma-db-llama_index/document_dict_llama_index.pkl\", \"llama_index\"\n",
    "# )\n",
    "# await generate_questions(\n",
    "#     \"../data/chroma-db-openai_cookbooks/document_dict_openai_cookbooks.pkl\",\n",
    "#     \"openai_cookbooks\",\n",
    "# )\n",
    "# await generate_questions(\"../data/chroma-db-peft/document_dict_peft.pkl\", \"peft\")\n",
    "# await generate_questions(\"../data/chroma-db-trl/document_dict_trl.pkl\", \"trl\")\n",
    "\n",
    "await generate_questions(\n",
    "    \"../data/chroma-db-tai_blog/document_dict_tai_blog.pkl\", \"tai_blog\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # We can also load the dataset from a previously saved json file.\n",
    "# from llama_index.core.evaluation import EmbeddingQAFinetuneDataset\n",
    "\n",
    "# rag_eval_dataset = EmbeddingQAFinetuneDataset.from_json(\"./rag_eval_transformers.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env",
   "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}