Skip to content

Commit

Permalink
Merge pull request #2517 from jupyter-naas/2516-naas-api-workspace-pl…
Browse files Browse the repository at this point in the history
…ugin-snippets

2516 naas api workspace plugin snippets
  • Loading branch information
FlorentLvr committed Apr 26, 2024
2 parents a3d1b32 + b8d612c commit 8a4abe2
Show file tree
Hide file tree
Showing 4 changed files with 900 additions and 15 deletions.
29 changes: 14 additions & 15 deletions Naas API/Naas_API_Create_Workspace.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,21 @@
"outputs": [],
"source": [
"api_key = naas.secret.get('NAAS_API_TOKEN')\n",
"user_id = \"2b9ac8af-4026-4173-8796-xxxxxx\"\n",
"user_id = \"2b9ac8af-4026-4173-8796-xxxxxxx\"\n",
"data = {\n",
" \"user_id\": user_id,\n",
" \"workspace\": {\n",
" 'name': 'Test',\n",
" 'fav_icon': '',\n",
" 'large_logo': '',\n",
" 'small_logo': '',\n",
" 'primary_color': '#0080FF', #highlight green\n",
" 'secondary_color': '#0A165D', #background black left pannel \n",
" 'tertiary_color': '#fff', #background grey main chat\n",
" 'text_primary_color': '#fff',\n",
" 'text_secondary_color': '#747677',\n",
" 'is_personal': False,\n",
" } \n",
" 'name': 'Naas',\n",
" 'fav_icon': '',\n",
" 'large_logo': '/assets/nass-logo-big-Bgh_HbfS.png',\n",
" 'small_logo': 'https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/.github/assets/logos/Naas%20API.png',\n",
" 'primary_color': '#48DD82',\n",
" 'secondary_color': '#181a1c',\n",
" 'tertiary_color': '#3c3f40',\n",
" 'text_primary_color': '#fff',\n",
" 'text_secondary_color': '#747677',\n",
" 'is_personal': False,\n",
" } \n",
"}"
]
},
Expand Down Expand Up @@ -193,7 +193,6 @@
"source": [
"def create_workspace(\n",
" api_key,\n",
" name,\n",
" data,\n",
"):\n",
" url = \"https://api.naas.ai/workspace\"\n",
Expand All @@ -204,7 +203,7 @@
" response = requests.post(url, headers=headers, json=data)\n",
" return response.json()\n",
"\n",
"result = create_workspace(api_key, name, data)\n",
"result = create_workspace(api_key, data)\n",
"print(result)"
]
},
Expand Down Expand Up @@ -293,4 +292,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
340 changes: 340 additions & 0 deletions Naas API/Naas_API_Create_Workspace_Plugin.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,340 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "6a04f99a-e16e-4087-98d1-07631df5be0a",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"8%\" alt=\"Naas API.png\" src=\"https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/.github/assets/logos/Naas%20API.png\" style=\"border-radius: 15%\">"
]
},
{
"cell_type": "markdown",
"id": "040f57d7-baa8-4f5c-8f00-2911825d7a42",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Naas API - Create Workspace Plugin\n",
"<a href=\"https://bit.ly/3JyWIk6\">Give Feedback</a> | <a href=\"https://github.com/jupyter-naas/awesome-notebooks/issues/new?assignees=&labels=bug&template=bug_report.md&title=Naas+API+-+List+Workspace+Users:+Error+short+description\">Bug report</a>"
]
},
{
"cell_type": "markdown",
"id": "9afb388c-60b4-43ec-88a5-46c69cd1aef3",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #naas #api #workspace #create #snippet"
]
},
{
"cell_type": "markdown",
"id": "5e796b36-0f21-49fa-83e2-637df65868dd",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/)"
]
},
{
"cell_type": "markdown",
"id": "7c29836b-87f1-47f4-8f31-dc7b844b8bfa",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2024-04-25 (Created: 2024-03-28)"
]
},
{
"cell_type": "markdown",
"id": "beccc4e5-d064-4b06-94bc-23786b1a0385",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook demonstrates how to create a workspace plugin using the Naas API."
]
},
{
"cell_type": "markdown",
"id": "a3b9a052-36ce-45b8-a776-a090f34a3c04",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n",
"- [Naas API Documentation](https://api.naas.ai/redoc#tag/Workspace-API/operation/create_workspace_plugin_workspace__workspace_id__plugin_post)\n",
"- [Naas Official Website](https://naas.ai/)"
]
},
{
"cell_type": "markdown",
"id": "a2107faa-0d01-45ad-a74d-a7b6937bb296",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "f7957d06-6d17-40d1-b5ec-b4cac0be6831",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c6184bb-dae6-4cb0-9cbe-26753bbb2f69",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"import requests\n",
"import naas\n",
"import json"
]
},
{
"cell_type": "markdown",
"id": "0beb4f2a-8564-48ae-a6c1-f9411ab60150",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n",
"- `api_key`: Naas API key.\n",
"- `workspace_id`: Workspace ID.\n",
"- `personal_workspace`: Boolean to send plugin to Personal workspace.\n",
"- `plugin`: Plugin JSON."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1e100cb-681b-4723-bc4b-864890b082f0",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"api_key = naas.secret.get('NAAS_API_TOKEN')\n",
"workspace_id = None\n",
"personal_workspace = True\n",
"plugin = {\n",
" 'name': 'Content Assistant',\n",
" 'model': 'gpt-4-1106-preview',\n",
" 'temperature': 0.5,\n",
" 'max_tokens': 128000,\n",
" 'prompt': \"Act as a Content Assistant who has access to valuable data and insights about the content strategy.\",\n",
" 'commands': [],\n",
" 'description': \"Streamline the generation and distribution of content that aligns with the user's or business's brand voice and audience engagement goals.\",\n",
" 'avatar': 'https://naasai-public.s3.eu-west-3.amazonaws.com/abi-demo/content_creation.png',\n",
" 'prompt_type': 'system',\n",
" 'slug': 'content-assistant/test',\n",
" 'id': 'content-assistant',\n",
" 'url': ''\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "d88be66d-4656-48dd-9403-4ed1d2723095",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "e952a778-1800-443f-9053-43dd356e38b9",
"metadata": {},
"source": [
"### Get workspace ID"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "810e02eb-5578-4c50-bb53-4ce9d5dec2ee",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def list_workspaces(\n",
" api_key,\n",
"):\n",
" url = \"https://api.naas.ai/workspace/\"\n",
" headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": f\"Bearer {api_key}\"\n",
" }\n",
" response = requests.get(url, headers=headers)\n",
" return response.json()\n",
"\n",
"# If not workspace ID, we get the personal workspace ID\n",
"if workspace_id is None and personal_workspace:\n",
" result = list_workspaces(api_key)\n",
" if len(result) > 0:\n",
" for workspace in result.get(\"workspaces\"):\n",
" is_personal = workspace.get(\"is_personal\")\n",
" workspace_id = workspace.get(\"id\")\n",
" if is_personal:\n",
" break\n",
" \n",
"print(f\"Workspace ID: {workspace_id}\")"
]
},
{
"cell_type": "markdown",
"id": "b9fa7620-5ae2-4f1e-8345-89e5f8141944",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Create Workspace Plugin"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72edcd3b-ab9c-4e39-9d53-f4698404d578",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"def create_workspace_plugin(\n",
" api_key,\n",
" workspace_id,\n",
" plugin,\n",
"):\n",
" url = f\"https://api.naas.ai/workspace/{workspace_id}/plugin\"\n",
" headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": f\"Bearer {api_key}\"\n",
" }\n",
" data = {\n",
" \"workspace_id\": workspace_id,\n",
" \"payload\": json.dumps(plugin),\n",
" }\n",
" response = requests.post(url, headers=headers, json=data)\n",
" return response.json()\n",
"\n",
"result = create_workspace_plugin(api_key, workspace_id, plugin)\n",
"print(result.get('workspace_plugin'))"
]
},
{
"cell_type": "markdown",
"id": "ea210872-cf05-404e-9594-74c0321d2a61",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "0eac2556-b0f0-446c-947d-86931508243b",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7268aac3-8000-4ab8-a1fc-694e2081b08c",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5019b530-3337-4b05-b426-5587b4c2d5f6",
"metadata": {},
"outputs": [],
"source": []
}
],
"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"
},
"naas": {
"notebook_id": "17018fb5c65fbd665ae64d41f5950100a4f95e5f1759728f084901da02cb8ae4",
"notebook_path": "Naas API/Naas_API_List_Workspace_Users.ipynb"
},
"papermill": {
"default_parameters": {},
"environment_variables": {},
"parameters": {},
"version": "2.5.0"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 8a4abe2

Please sign in to comment.