|
15 | 15 | "- context: the context retrieved by the retrieval system\n", |
16 | 16 | "- answer: the output of the model that tries to answer the question\n", |
17 | 17 | "\n", |
18 | | - "The presence of these 3 elements allows us to simulate a full RAG system without actually setting up the system.\n" |
| 18 | + "The presence of these 3 elements allows us to simulate a full RAG system without actually setting up the system." |
19 | 19 | ] |
20 | 20 | }, |
21 | 21 | { |
|
34 | 34 | "\n", |
35 | 35 | "These are the dependencies your Python environment is required to have in order to properly run this notebook.\n", |
36 | 36 | "```\n", |
37 | | - "ml3-platform-sdk>=0.0.17\n", |
| 37 | + "ml3-platform-sdk>=0.0.22\n", |
38 | 38 | "torch==2.2.0\n", |
39 | 39 | "datasets==2.15.0\n", |
40 | 40 | "sentence-transformers==3.0.1\n", |
|
54 | 54 | }, |
55 | 55 | { |
56 | 56 | "cell_type": "code", |
57 | | - "execution_count": null, |
| 57 | + "execution_count": 1, |
58 | 58 | "metadata": {}, |
59 | 59 | "outputs": [], |
60 | 60 | "source": [ |
|
77 | 77 | "User Inputs" |
78 | 78 | ] |
79 | 79 | }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": 2, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "URL = 'https://api.platform.mlcube.com'\n", |
| 87 | + "API_KEY = \"\"\n", |
| 88 | + "PROJECT_ID = ''\n", |
| 89 | + "model_name = 'mymodel'\n", |
| 90 | + "model_version = 'v0.0.1'" |
| 91 | + ] |
| 92 | + }, |
80 | 93 | { |
81 | 94 | "cell_type": "markdown", |
82 | 95 | "metadata": {}, |
83 | 96 | "source": [ |
84 | 97 | "## Dataset, model and predictions\n", |
85 | | - "Download dataset and model using Huggingface api.\n", |
86 | | - "After the dataset and the model are downloaded we run the model to get predictions." |
| 98 | + "Download dataset and model using Huggingface api." |
87 | 99 | ] |
88 | 100 | }, |
89 | 101 | { |
|
95 | 107 | }, |
96 | 108 | { |
97 | 109 | "cell_type": "code", |
98 | | - "execution_count": null, |
| 110 | + "execution_count": 3, |
99 | 111 | "metadata": {}, |
100 | 112 | "outputs": [], |
101 | 113 | "source": [ |
102 | | - "complete_dataset = load_dataset(\"neural-bridge/rag-dataset-12000\")\n", |
103 | | - "\n", |
104 | 114 | "USER_INPUT_COL_NAME = 'question'\n", |
105 | 115 | "CONTEXT_COL_NAME = 'context'\n", |
106 | | - "ANSWER_COL_NAME = 'answer'" |
| 116 | + "ANSWER_COL_NAME = 'answer'\n", |
| 117 | + "\n", |
| 118 | + "complete_dataset = load_dataset(\"neural-bridge/rag-dataset-12000\", split=\"train[:10%]\").filter(lambda x: all(x[col] is not None for col in [USER_INPUT_COL_NAME, CONTEXT_COL_NAME, ANSWER_COL_NAME]))" |
107 | 119 | ] |
108 | 120 | }, |
109 | 121 | { |
110 | 122 | "cell_type": "code", |
111 | | - "execution_count": null, |
| 123 | + "execution_count": 4, |
112 | 124 | "metadata": {}, |
113 | 125 | "outputs": [], |
114 | 126 | "source": [ |
115 | | - "def sample_dataset(dataset, fraction=0.1, seed=42):\n", |
| 127 | + "def sample_dataset(dataset, reference_portion=0.5, first_production_portion=0.5, seed=42):\n", |
116 | 128 | " sampled_dataset = DatasetDict()\n", |
117 | 129 | " \n", |
118 | | - " # Split train data\n", |
119 | | - " train_split = dataset['train'].train_test_split(test_size=0.5, seed=seed)\n", |
120 | | - " \n", |
121 | | - " sampled_dataset['train'] = train_split['train'].train_test_split(test_size=fraction, seed=seed)['test']\n", |
122 | | - " sampled_dataset['validation'] = train_split['test'].train_test_split(test_size=fraction, seed=seed)['test']\n", |
123 | | - " \n", |
124 | | - " # Split test data\n", |
125 | | - " sampled_dataset['test'] = dataset['test'].train_test_split(test_size=fraction, seed=seed)['test']\n", |
| 130 | + " # Split the dataset into reference and production\n", |
| 131 | + "\n", |
| 132 | + " split = dataset.train_test_split(test_size=reference_portion, seed=seed)\n", |
| 133 | + "\n", |
| 134 | + " sampled_dataset['reference'] = split['train']\n", |
| 135 | + "\n", |
| 136 | + " split_2 = split['test'].train_test_split(test_size=first_production_portion, seed=seed)\n", |
| 137 | + "\n", |
| 138 | + " sampled_dataset['first_production'] = split_2['train']\n", |
| 139 | + " sampled_dataset['second_production'] = split_2['test']\n", |
| 140 | + "\n", |
126 | 141 | " return sampled_dataset\n", |
127 | 142 | "\n", |
128 | 143 | "# Perform the sampling\n", |
|
135 | 150 | "metadata": {}, |
136 | 151 | "outputs": [], |
137 | 152 | "source": [ |
138 | | - "len(dataset['train']['context']), len(dataset['validation']['context']), len(dataset['test']['context'])" |
| 153 | + "len(dataset[\"reference\"]), len(dataset[\"first_production\"]), len(dataset[\"second_production\"])" |
139 | 154 | ] |
140 | 155 | }, |
141 | 156 | { |
|
156 | 171 | "\n", |
157 | 172 | "Uploading data coming from a RAG system is equivalent to uploading text data (refer to this [notebook](https://colab.research.google.com/github/ml-cube/ml3-platform-docs/blob/main/notebooks/text_classification.ipynb) for further information). \n", |
158 | 173 | "\n", |
159 | | - "Data needs to be stored in a json file as a list of objects. Each object must contain two mandatory fields, namely the timestamp and the sample-id, along with other the other fields that represent the data (e.g. question and context for input data, answer for predcition data).\n", |
| 174 | + "Data needs to be stored in a json file as a list of objects. Each object must contain two mandatory fields, namely the timestamp and the sample-id, along with other the other fields that represent the data (e.g. question and context for input data, answer for prediction data).\n", |
160 | 175 | "\n", |
161 | 176 | "When dealing with unstructured data like text it is possible to send them in three ways:\n", |
162 | 177 | "1. By sending only embeddings i.e., a numerical representation of the text sample as a vector, using `EmbeddingData`;\n", |
|
177 | 192 | }, |
178 | 193 | { |
179 | 194 | "cell_type": "code", |
180 | | - "execution_count": null, |
| 195 | + "execution_count": 7, |
181 | 196 | "metadata": {}, |
182 | 197 | "outputs": [], |
183 | 198 | "source": [ |
|
333 | 348 | " starting_id,\n", |
334 | 349 | " starting_timestamp,\n", |
335 | 350 | ") = build_data_objects(\n", |
336 | | - " dataset['train'],\n", |
| 351 | + " dataset['reference'],\n", |
337 | 352 | " embedder,\n", |
338 | 353 | " model_name,\n", |
339 | 354 | " model_version,\n", |
340 | 355 | " historical_initial_sample_id,\n", |
341 | 356 | " historical_initial_timestamp,\n", |
342 | | - " prefix='train',\n", |
| 357 | + " prefix='reference',\n", |
343 | 358 | " with_prediction=True\n", |
344 | 359 | ")\n", |
345 | 360 | "historical_end_timestamp = starting_timestamp - 120" |
|
357 | 372 | " starting_id,\n", |
358 | 373 | " starting_timestamp,\n", |
359 | 374 | ") = build_data_objects(\n", |
360 | | - " dataset['validation'],\n", |
| 375 | + " dataset['first_production'],\n", |
361 | 376 | " embedder,\n", |
362 | 377 | " model_name,\n", |
363 | 378 | " model_version,\n", |
|
379 | 394 | " starting_id,\n", |
380 | 395 | " starting_timestamp,\n", |
381 | 396 | ") = build_data_objects(\n", |
382 | | - " dataset['test'],\n", |
| 397 | + " dataset['second_production'],\n", |
383 | 398 | " embedder,\n", |
384 | 399 | " model_name,\n", |
385 | 400 | " model_version,\n", |
|
407 | 422 | }, |
408 | 423 | { |
409 | 424 | "cell_type": "code", |
410 | | - "execution_count": null, |
| 425 | + "execution_count": 11, |
411 | 426 | "metadata": {}, |
412 | 427 | "outputs": [], |
413 | 428 | "source": [ |
|
485 | 500 | "- **task_type:** artificial intelligence task type. In this case it is a RAG task.\n", |
486 | 501 | "- **data_structure:** the type of input data. Since we are dealing with text data, we set it to TEXT.\n", |
487 | 502 | "- **optional_target:** rag tasks don't have a target, hence it must be set to True\n", |
488 | | - "- **text_language:** it is mandatory to specify the language used in the task." |
| 503 | + "- **text_language:** it is mandatory to specify the language used in the task.\n", |
| 504 | + "- **rag_context_separator**: the string used to separate different contexts. In this case it is None as the context is composed of a single sentence." |
489 | 505 | ] |
490 | 506 | }, |
491 | 507 | { |
492 | 508 | "cell_type": "code", |
493 | | - "execution_count": null, |
| 509 | + "execution_count": 13, |
494 | 510 | "metadata": {}, |
495 | 511 | "outputs": [], |
496 | 512 | "source": [ |
|
501 | 517 | " task_type=ml3_enums.TaskType.RAG,\n", |
502 | 518 | " data_structure=ml3_enums.DataStructure.TEXT,\n", |
503 | 519 | " optional_target=True, # Must be True in RAG tasks\n", |
504 | | - " text_language=ml3_enums.TextLanguage.ENGLISH\n", |
| 520 | + " text_language=ml3_enums.TextLanguage.ENGLISH,\n", |
| 521 | + " rag_contexts_separator=None,\n", |
505 | 522 | ")" |
506 | 523 | ] |
507 | 524 | }, |
508 | 525 | { |
509 | 526 | "cell_type": "code", |
510 | | - "execution_count": null, |
| 527 | + "execution_count": 14, |
511 | 528 | "metadata": {}, |
512 | 529 | "outputs": [], |
513 | 530 | "source": [ |
|
525 | 542 | }, |
526 | 543 | { |
527 | 544 | "cell_type": "code", |
528 | | - "execution_count": null, |
| 545 | + "execution_count": 15, |
529 | 546 | "metadata": {}, |
530 | 547 | "outputs": [], |
531 | 548 | "source": [ |
|
543 | 560 | "cell_type": "markdown", |
544 | 561 | "metadata": {}, |
545 | 562 | "source": [ |
546 | | - "Historical data are available data that don't come from the production environment. We need to add them in order to set up the reference data of our model, which is needed by internal algorithms.\n", |
| 563 | + "Reference data are uploaded as historical data i.e., any data that do not come from production.\n", |
| 564 | + "Reference data are defined by their initial and final timestamps and they are used to configure the detection algorithms.\n", |
547 | 565 | "\n", |
548 | 566 | "Note that it is possible to add other historical data in any time." |
549 | 567 | ] |
|
0 commit comments