Skip to content

Commit 721314a

Browse files
authored
Merge pull request #11 from explodinggradients/fix/get-by-name
feat: added get resources by name
2 parents 9bfa15f + 208d438 commit 721314a

File tree

9 files changed

+1003
-175
lines changed

9 files changed

+1003
-175
lines changed

nbs/backends/ragas_api_client.ipynb

Lines changed: 380 additions & 75 deletions
Large diffs are not rendered by default.

nbs/exceptions.ipynb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818
"# | default_exp exceptions"
1919
]
2020
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"#| export\n",
28+
"class RagasError(Exception):\n",
29+
" \"\"\"Base class for all Ragas-related exceptions.\"\"\"\n",
30+
" pass"
31+
]
32+
},
2133
{
2234
"cell_type": "code",
2335
"execution_count": null,
@@ -42,6 +54,56 @@
4254
"\n",
4355
" pass"
4456
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"#| export\n",
65+
"\n",
66+
"class ResourceNotFoundError(RagasError):\n",
67+
" \"\"\"Exception raised when a requested resource doesn't exist.\"\"\"\n",
68+
" pass\n",
69+
"\n",
70+
"class ProjectNotFoundError(ResourceNotFoundError):\n",
71+
" \"\"\"Exception raised when a project doesn't exist.\"\"\"\n",
72+
" pass\n",
73+
"\n",
74+
"class DatasetNotFoundError(ResourceNotFoundError):\n",
75+
" \"\"\"Exception raised when a dataset doesn't exist.\"\"\"\n",
76+
" pass\n",
77+
"\n",
78+
"class ExperimentNotFoundError(ResourceNotFoundError):\n",
79+
" \"\"\"Exception raised when an experiment doesn't exist.\"\"\"\n",
80+
" pass"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"metadata": {},
87+
"outputs": [],
88+
"source": [
89+
"#| export\n",
90+
"\n",
91+
"class DuplicateResourceError(RagasError):\n",
92+
" \"\"\"Exception raised when multiple resources exist with the same identifier.\"\"\"\n",
93+
" pass\n",
94+
"\n",
95+
"class DuplicateProjectError(DuplicateResourceError):\n",
96+
" \"\"\"Exception raised when multiple projects exist with the same name.\"\"\"\n",
97+
" pass\n",
98+
"\n",
99+
"class DuplicateDatasetError(DuplicateResourceError):\n",
100+
" \"\"\"Exception raised when multiple datasets exist with the same name.\"\"\"\n",
101+
" pass\n",
102+
"\n",
103+
"class DuplicateExperimentError(DuplicateResourceError):\n",
104+
" \"\"\"Exception raised when multiple experiments exist with the same name.\"\"\"\n",
105+
" pass"
106+
]
45107
}
46108
],
47109
"metadata": {

nbs/project/core.ipynb

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,53 @@
144144
"project"
145145
]
146146
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"# | export\n",
154+
"@patch(cls_method=True)\n",
155+
"def get(cls: Project, name: str, ragas_app_client: t.Optional[RagasApiClient] = None) -> Project:\n",
156+
" \"\"\"Get an existing project by name.\"\"\"\n",
157+
" # Search for project with given name\n",
158+
" if ragas_app_client is None:\n",
159+
" ragas_app_client = RagasApiClientFactory.create()\n",
160+
"\n",
161+
" # get the project by name\n",
162+
" sync_version = async_to_sync(ragas_app_client.get_project_by_name)\n",
163+
" project_info = sync_version(\n",
164+
" project_name=name\n",
165+
" )\n",
166+
"\n",
167+
" # Return Project instance\n",
168+
" return Project(\n",
169+
" project_id=project_info[\"id\"],\n",
170+
" ragas_app_client=ragas_app_client,\n",
171+
" )"
172+
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": null,
177+
"metadata": {},
178+
"outputs": [
179+
{
180+
"data": {
181+
"text/plain": [
182+
"Project(name='SuperMe')"
183+
]
184+
},
185+
"execution_count": null,
186+
"metadata": {},
187+
"output_type": "execute_result"
188+
}
189+
],
190+
"source": [
191+
"Project.get(\"SuperMe\")"
192+
]
193+
},
147194
{
148195
"cell_type": "code",
149196
"execution_count": null,
@@ -268,7 +315,7 @@
268315
{
269316
"data": {
270317
"text/plain": [
271-
"'bbe45632-3268-43a6-9694-b020b3f5226f'"
318+
"'3d9b529b-c23f-4e87-8a26-dd1923749aa7'"
272319
]
273320
},
274321
"execution_count": null,
@@ -288,7 +335,7 @@
288335
{
289336
"data": {
290337
"text/plain": [
291-
"'0fee5330-9f6e-44a9-a85c-e3b947b697de'"
338+
"'5f7839e7-f4f9-4ada-9082-bc1c5528c259'"
292339
]
293340
},
294341
"execution_count": null,
@@ -308,7 +355,7 @@
308355
"source": [
309356
"# | export\n",
310357
"@patch\n",
311-
"def get_dataset(self: Project, dataset_id: str, model) -> Dataset:\n",
358+
"def get_dataset_by_id(self: Project, dataset_id: str, model) -> Dataset:\n",
312359
" \"\"\"Get an existing dataset by name.\"\"\"\n",
313360
" # Search for database with given name\n",
314361
" sync_version = async_to_sync(self._ragas_api_client.get_dataset)\n",
@@ -344,7 +391,34 @@
344391
}
345392
],
346393
"source": [
347-
"project.get_dataset(test_dataset.dataset_id, TestModel)"
394+
"project.get_dataset_by_id(test_dataset.dataset_id, TestModel)"
395+
]
396+
},
397+
{
398+
"cell_type": "code",
399+
"execution_count": null,
400+
"metadata": {},
401+
"outputs": [],
402+
"source": [
403+
"# | export\n",
404+
"@patch\n",
405+
"def get_dataset(self: Project, dataset_name: str, model) -> Dataset:\n",
406+
" \"\"\"Get an existing dataset by name.\"\"\"\n",
407+
" # Search for dataset with given name\n",
408+
" sync_version = async_to_sync(self._ragas_api_client.get_dataset_by_name)\n",
409+
" dataset_info = sync_version(\n",
410+
" project_id=self.project_id,\n",
411+
" dataset_name=dataset_name\n",
412+
" )\n",
413+
"\n",
414+
" # Return Dataset instance\n",
415+
" return Dataset(\n",
416+
" name=dataset_info[\"name\"],\n",
417+
" model=model,\n",
418+
" project_id=self.project_id,\n",
419+
" dataset_id=dataset_info[\"id\"],\n",
420+
" ragas_api_client=self._ragas_api_client,\n",
421+
" )"
348422
]
349423
},
350424
{
@@ -355,7 +429,7 @@
355429
{
356430
"data": {
357431
"text/plain": [
358-
"'0a7c4ecb-b313-4bb0-81c0-852c9634ce03'"
432+
"Dataset(name=TestModel, model=TestModel, len=0)"
359433
]
360434
},
361435
"execution_count": null,
@@ -364,15 +438,8 @@
364438
}
365439
],
366440
"source": [
367-
"project.project_id"
441+
"project.get_dataset(\"TestModel\", TestModel)"
368442
]
369-
},
370-
{
371-
"cell_type": "code",
372-
"execution_count": null,
373-
"metadata": {},
374-
"outputs": [],
375-
"source": []
376443
}
377444
],
378445
"metadata": {

0 commit comments

Comments
 (0)