generated from jupyter-naas/data-product-framework
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from jupyter-naas/abi-ontologies
feat: add abi ontologies
- Loading branch information
Showing
76 changed files
with
85,385 additions
and
2,413 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
270 changes: 270 additions & 0 deletions
270
models/__main__/core/Naas_Demo_Ontologies_Assistants.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Naas - Demo Ontologies & Assistants" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import naas_data_product\n", | ||
"from abi.naaspython import NaasPython\n", | ||
"naaspython = NaasPython()\n", | ||
"import naas_python\n", | ||
"import pydash as _\n", | ||
"import json\n", | ||
"from datetime import datetime\n", | ||
"from urllib.parse import quote\n", | ||
"from algoliasearch.search.client import SearchClient" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Setup variables" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Input\n", | ||
"api_key = naas_python.utils.domains_base.authorization.NaasSpaceAuthenticatorAdapter().jwt_token()\n", | ||
"workspace_id = naas_python.secret.get(\"WORKSPACE_DEMO_ID\").value\n", | ||
"algolia_app_id = naas_python.secret.get(\"ALGOLIA_APP_ID\").value\n", | ||
"algolia_api_key = naas_python.secret.get(\"ALGOLIA_API_KEY\").value\n", | ||
"data_algolia = []\n", | ||
"\n", | ||
"# Outputs\n", | ||
"output_dir = os.path.join(naas_data_product.OUTPUTS_PATH, \"demo\")\n", | ||
"index_name = \"workspace-search\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Get Ontologies" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"ontologies = naaspython.connect(api_key).ontology.get(workspace_id).get(\"ontologies\")\n", | ||
"print(\"Ontologies:\", len(ontologies))\n", | ||
"\n", | ||
"for x in ontologies:\n", | ||
" label = x.get(\"label\")\n", | ||
" if not \"Old\" in label:\n", | ||
" uid = x.get(\"id\")\n", | ||
" label = x.get(\"label\")\n", | ||
" download_url = x.get(\"download_url\")\n", | ||
" description = x.get(\"description\")\n", | ||
" logo_url = x.get(\"logo_url\")\n", | ||
" level = x.get(\"level\")\n", | ||
" # Get tool\n", | ||
" tool = \"Naas\"\n", | ||
" if \"-\" in label:\n", | ||
" tool = label.split(\"-\")[0].strip()\n", | ||
" # Get ranking & author\n", | ||
" author = \"Naas Core Team\"\n", | ||
" author_url = \"\"\n", | ||
" ranking = 2\n", | ||
" if level == \"TOP_LEVEL\":\n", | ||
" ranking = 5\n", | ||
" author = \"BFO Team (Barry Smith)\"\n", | ||
" elif level == \"MID_LEVEL\":\n", | ||
" ranking = 4\n", | ||
" author = \"CCO Team\"\n", | ||
" elif level == \"DOMAIN\":\n", | ||
" ranking = 2\n", | ||
" \n", | ||
" # Create Search data JSON\n", | ||
" data_algolia.append(\n", | ||
" {\n", | ||
" \"tool\": tool,\n", | ||
" \"notebook\": label,\n", | ||
" \"tags\": [\n", | ||
" \"#ontologies\",\n", | ||
" f\"#{level.replace('_', '').lower()}\"\n", | ||
" ],\n", | ||
" \"author\": author,\n", | ||
" \"author_url\": author_url,\n", | ||
" \"updated_at\": datetime.now().strftime(\"%Y-%m-%d\"),\n", | ||
" \"created_at\": datetime.now().strftime(\"%Y-%m-%d\"),\n", | ||
" \"description\": description,\n", | ||
" \"open_in_lab\": \"\",\n", | ||
" \"open_in_chat\": \"\",\n", | ||
" \"notebook_url\": download_url,\n", | ||
" \"image_url\": logo_url,\n", | ||
" \"ranking\": ranking,\n", | ||
" \"objectID\": uid\n", | ||
" }\n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Get Plugins" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"workspace_plugins = naaspython.connect(api_key, workspace_id).plugin.get().get(\"workspace_plugins\")\n", | ||
"print(\"Workspace Plugins:\", len(workspace_plugins))\n", | ||
"\n", | ||
"for workspace_plugin in workspace_plugins:\n", | ||
" # Init\n", | ||
" uid = workspace_plugin.get(\"id\")\n", | ||
" plugin = json.loads(workspace_plugin.get(\"payload\"))\n", | ||
" \n", | ||
" # Map fields\n", | ||
" ranking = 3\n", | ||
" tool = \"Naas Demo\"\n", | ||
" label = plugin.get(\"name\")\n", | ||
" short_name = label.lower().replace(' ', '')\n", | ||
" image_url = plugin.get(\"avatar\")\n", | ||
" description = plugin.get(\"prompt\")[:300] + \" ...\"\n", | ||
" model = plugin.get(\"model\")\n", | ||
" id = plugin.get(\"id\")\n", | ||
" temperature = plugin.get(\"temperature\")\n", | ||
" author = \"Naas Core Team\"\n", | ||
" author_url = \"\"\n", | ||
" \n", | ||
" # Save plugin json\n", | ||
" file_path = os.path.join(output_dir, \"assistants\", f\"{short_name}.json\")\n", | ||
" with open(file_path, 'w') as json_file:\n", | ||
" json.dump(plugin, json_file, indent=4)\n", | ||
" \n", | ||
" # Create Search data JSON\n", | ||
" data_algolia.append(\n", | ||
" {\n", | ||
" \"tool\": tool,\n", | ||
" \"notebook\": label,\n", | ||
" \"tags\": [\n", | ||
" \"#assistants\",\n", | ||
" f\"#{short_name.split('.')[-1]}\",\n", | ||
" f\"#{model}\",\n", | ||
" f\"#temperature:{temperature}\",\n", | ||
" ],\n", | ||
" \"author\": author,\n", | ||
" \"author_url\": author_url,\n", | ||
" \"updated_at\": datetime.now().strftime(\"%Y-%m-%d\"),\n", | ||
" \"created_at\": datetime.now().strftime(\"%Y-%m-%d\"),\n", | ||
" \"description\": description,\n", | ||
" \"open_in_lab\": \"\",\n", | ||
" \"open_in_chat\": \"\",\n", | ||
" \"notebook_url\": quote(f\"https://raw.githubusercontent.com/jupyter-naas/abi/refs/heads/main/outputs/demo/ontologies/{short_name}.json\", safe=\":/#\"),\n", | ||
" \"image_url\": image_url,\n", | ||
" \"ranking\": ranking,\n", | ||
" \"objectID\": uid\n", | ||
" }\n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Udpate Data Algolia" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Initialize the Algolia client\n", | ||
"_client = SearchClient(algolia_app_id, algolia_api_key)\n", | ||
"\n", | ||
"# Update Algolia\n", | ||
"print(\"Data to update:\", len(data_algolia))\n", | ||
"for d in data_algolia:\n", | ||
" # Add a new record to your Algolia index\n", | ||
" response = await _client.save_object(\n", | ||
" index_name=index_name,\n", | ||
" body=d,\n", | ||
" )\n", | ||
" print(response)" | ||
] | ||
} | ||
], | ||
"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.9.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.