From b9088d5f4974ee0bdf45dc1dd51a194cc21e1e0f Mon Sep 17 00:00:00 2001 From: Kye Gomez Date: Thu, 1 Aug 2024 20:22:06 -0400 Subject: [PATCH] [SWARMS AMRKETPLACE][PROMPTS] --- .env.example | 1 + docs/mkdocs.yml | 14 +- docs/swarms_platform/prompts/add_prompt.md | 178 +++++++++++++++++ .../edit_prompt.md} | 179 ------------------ .../{ => prompts}/fetch_prompts.md | 0 .../swarms_marketplace/agents/create_agent.py | 41 ++++ .../prompts_api/add_prompt.py | 23 +++ .../prompts_api/edit_prompt.py | 25 +++ .../prompts_api/query_prompts.py | 25 +++ 9 files changed, 302 insertions(+), 184 deletions(-) create mode 100644 docs/swarms_platform/prompts/add_prompt.md rename docs/swarms_platform/{prompts_api.md => prompts/edit_prompt.md} (56%) rename docs/swarms_platform/{ => prompts}/fetch_prompts.md (100%) create mode 100644 playground/swarms_marketplace/agents/create_agent.py create mode 100644 playground/swarms_marketplace/prompts_api/add_prompt.py create mode 100644 playground/swarms_marketplace/prompts_api/edit_prompt.py create mode 100644 playground/swarms_marketplace/prompts_api/query_prompts.py diff --git a/.env.example b/.env.example index b80bf6c9..dfe4c75b 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,7 @@ AI21_API_KEY="your_api_key_here" COHERE_API_KEY="your_api_key_here" ALEPHALPHA_API_KEY="your_api_key_here" HUGGINFACEHUB_API_KEY="your_api_key_here" +SWARMS_API_KEY="" EVAL_PORT=8000 MODEL_NAME="gpt-4" diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 624ca954..98725948 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -212,11 +212,15 @@ nav: - Overview: "swarms_platform/index.md" - Share & Discover Prompts, Agents, Tools, and more: "swarms_platform/share_discover.md" - Prompts API: - - Overview: "swarms_platform/prompts_api.md" - - Fetch Prompts: "swarms_platform/fetch_prompts.md" - # - Agents API: - # - Overview: "swarms_platform/agents_api.md" - # - Fetch Agents: "swarms_platform/fetch_agents.md" + - Add Prompts: "swarms_platform/prompts/add_prompt.md" + - Edit Prompts: "swarms_platform/prompts/edit_prompt.md" + - Query Prompts: "swarms_platform/prompts/fetch_prompts.md" + - Agents API: + - Overview: "swarms_platform/agents_api.md" + - Add Agents: "swarms_platform/fetch_agents.md" + # - Tools API: + # - Overview: "swarms_platform/tools_api.md" + # - Add Tools: "swarms_platform/fetch_tools.md" # - Guides: # - Comparing LLM Provider Pricing A Guide for Enterprises: "guides/pricing.md" - References: diff --git a/docs/swarms_platform/prompts/add_prompt.md b/docs/swarms_platform/prompts/add_prompt.md new file mode 100644 index 00000000..7812eec6 --- /dev/null +++ b/docs/swarms_platform/prompts/add_prompt.md @@ -0,0 +1,178 @@ +# Prompts API Documentation + +The `https://swarms.world/api/add-prompt` endpoint allows users to add a new prompt to the Swarms platform. This API accepts a POST request with a JSON body containing details of the prompt, such as its name, description, use cases, and tags. The request must be authenticated using an API key. + +## Endpoint: Add Prompt + +- **URL:** `https://swarms.world/api/add-prompt` +- **Method:** POST +- **Content-Type:** `application/json` +- **Authorization:** Bearer token required in the header + +## Request Parameters + +The request body should be a JSON object with the following attributes: + +| Attribute | Type | Description | Required | +| ------------- | -------- | --------------------------------------------------------------- | -------- | +| `name` | `string` | The name of the prompt. | Yes | +| `prompt` | `string` | The prompt text. | Yes | +| `description` | `string` | A brief description of the prompt. | Yes | +| `useCases` | `array` | An array of use cases, each containing a title and description. | Yes | +| `tags` | `string` | Comma-separated tags for the prompt. | No | + +### `useCases` Structure + +Each use case in the `useCases` array should be an object with the following attributes: + +| Attribute | Type | Description | Required | +| ------------- | -------- | ------------------------------------ | -------- | +| `title` | `string` | The title of the use case. | Yes | +| `description` | `string` | A brief description of the use case. | Yes | + +## Example Usage + +### Python + +```python +import requests +import json + +url = "https://swarms.world/api/add-prompt" +headers = { + "Content-Type": "application/json", + "Authorization": "Bearer {apiKey}" +} +data = { + "name": "Example Prompt", + "prompt": "This is an example prompt from an API route.", + "description": "Description of the prompt.", + "useCases": [ + {"title": "Use case 1", "description": "Description of use case 1"}, + {"title": "Use case 2", "description": "Description of use case 2"} + ], + "tags": "example, prompt" +} + +response = requests.post(url, headers=headers, data=json.dumps(data)) +print(response.json()) +``` + +### Node.js + +```javascript +const fetch = require("node-fetch"); + +async function addPromptsHandler() { + try { + const response = await fetch("https://swarms.world/api/add-prompt", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer {apiKey}", + }, + body: JSON.stringify({ + name: "Example Prompt", + prompt: "This is an example prompt from an API route.", + description: "Description of the prompt.", + useCases: [ + { title: "Use case 1", description: "Description of use case 1" }, + { title: "Use case 2", description: "Description of use case 2" }, + ], + tags: "example, prompt", + }), + }); + + const result = await response.json(); + console.log(result); + } catch (error) { + console.error("An error has occurred", error); + } +} + +addPromptsHandler(); +``` + +### Go + +```go +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" +) + +func main() { + url := "https://swarms.world/api/add-prompt" + payload := map[string]interface{}{ + "name": "Example Prompt", + "prompt": "This is an example prompt from an API route.", + "description": "Description of the prompt.", + "useCases": []map[string]string{ + {"title": "Use case 1", "description": "Description of use case 1"}, + {"title": "Use case 2", "description": "Description of use case 2"}, + }, + "tags": "example, prompt", + } + jsonPayload, _ := json.Marshal(payload) + + req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer {apiKey}") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + fmt.Println("An error has occurred", err) + return + } + defer resp.Body.Close() + + var result map[string]interface{} + json.NewDecoder(resp.Body).Decode(&result) + fmt.Println(result) +} +``` + +### cURL + +```bash +curl -X POST https://swarms.world/api/add-prompt \ +-H "Content-Type: application/json" \ +-H "Authorization: Bearer {apiKey}" \ +-d '{ + "name": "Example Prompt", + "prompt": "This is an example prompt from an API route.", + "description": "Description of the prompt.", + "useCases": [ + { "title": "Use case 1", "description": "Description of use case 1" }, + { "title": "Use case 2", "description": "Description of use case 2" } + ], + "tags": "example, prompt" +}' +``` + +## Response + +The response will be a JSON object containing the result of the operation. Example response: + +```json +{ + "success": true, + "message": "Prompt added successfully", + "data": { + "id": "prompt_id", + "name": "Example Prompt", + "prompt": "This is an example prompt from an API route.", + "description": "Description of the prompt.", + "useCases": [ + { "title": "Use case 1", "description": "Description of use case 1" }, + { "title": "Use case 2", "description": "Description of use case 2" } + ], + "tags": "example, prompt" + } +} +``` \ No newline at end of file diff --git a/docs/swarms_platform/prompts_api.md b/docs/swarms_platform/prompts/edit_prompt.md similarity index 56% rename from docs/swarms_platform/prompts_api.md rename to docs/swarms_platform/prompts/edit_prompt.md index f61d0b7a..ebb01cde 100644 --- a/docs/swarms_platform/prompts_api.md +++ b/docs/swarms_platform/prompts/edit_prompt.md @@ -1,182 +1,3 @@ -# Prompts API Documentation - -The `https://swarms.world/api/add-prompt` endpoint allows users to add a new prompt to the Swarms platform. This API accepts a POST request with a JSON body containing details of the prompt, such as its name, description, use cases, and tags. The request must be authenticated using an API key. - -## Endpoint: Add Prompt - -- **URL:** `https://swarms.world/api/add-prompt` -- **Method:** POST -- **Content-Type:** `application/json` -- **Authorization:** Bearer token required in the header - -## Request Parameters - -The request body should be a JSON object with the following attributes: - -| Attribute | Type | Description | Required | -| ------------- | -------- | --------------------------------------------------------------- | -------- | -| `name` | `string` | The name of the prompt. | Yes | -| `prompt` | `string` | The prompt text. | Yes | -| `description` | `string` | A brief description of the prompt. | Yes | -| `useCases` | `array` | An array of use cases, each containing a title and description. | Yes | -| `tags` | `string` | Comma-separated tags for the prompt. | No | - -### `useCases` Structure - -Each use case in the `useCases` array should be an object with the following attributes: - -| Attribute | Type | Description | Required | -| ------------- | -------- | ------------------------------------ | -------- | -| `title` | `string` | The title of the use case. | Yes | -| `description` | `string` | A brief description of the use case. | Yes | - -## Example Usage - -### Python - -```python -import requests -import json - -url = "https://swarms.world/api/add-prompt" -headers = { - "Content-Type": "application/json", - "Authorization": "Bearer {apiKey}" -} -data = { - "name": "Example Prompt", - "prompt": "This is an example prompt from an API route.", - "description": "Description of the prompt.", - "useCases": [ - {"title": "Use case 1", "description": "Description of use case 1"}, - {"title": "Use case 2", "description": "Description of use case 2"} - ], - "tags": "example, prompt" -} - -response = requests.post(url, headers=headers, data=json.dumps(data)) -print(response.json()) -``` - -### Node.js - -```javascript -const fetch = require("node-fetch"); - -async function addPromptsHandler() { - try { - const response = await fetch("https://swarms.world/api/add-prompt", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: "Bearer {apiKey}", - }, - body: JSON.stringify({ - name: "Example Prompt", - prompt: "This is an example prompt from an API route.", - description: "Description of the prompt.", - useCases: [ - { title: "Use case 1", description: "Description of use case 1" }, - { title: "Use case 2", description: "Description of use case 2" }, - ], - tags: "example, prompt", - }), - }); - - const result = await response.json(); - console.log(result); - } catch (error) { - console.error("An error has occurred", error); - } -} - -addPromptsHandler(); -``` - -### Go - -```go -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "net/http" -) - -func main() { - url := "https://swarms.world/api/add-prompt" - payload := map[string]interface{}{ - "name": "Example Prompt", - "prompt": "This is an example prompt from an API route.", - "description": "Description of the prompt.", - "useCases": []map[string]string{ - {"title": "Use case 1", "description": "Description of use case 1"}, - {"title": "Use case 2", "description": "Description of use case 2"}, - }, - "tags": "example, prompt", - } - jsonPayload, _ := json.Marshal(payload) - - req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload)) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("Authorization", "Bearer {apiKey}") - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - fmt.Println("An error has occurred", err) - return - } - defer resp.Body.Close() - - var result map[string]interface{} - json.NewDecoder(resp.Body).Decode(&result) - fmt.Println(result) -} -``` - -### cURL - -```bash -curl -X POST https://swarms.world/api/add-prompt \ --H "Content-Type: application/json" \ --H "Authorization: Bearer {apiKey}" \ --d '{ - "name": "Example Prompt", - "prompt": "This is an example prompt from an API route.", - "description": "Description of the prompt.", - "useCases": [ - { "title": "Use case 1", "description": "Description of use case 1" }, - { "title": "Use case 2", "description": "Description of use case 2" } - ], - "tags": "example, prompt" -}' -``` - -## Response - -The response will be a JSON object containing the result of the operation. Example response: - -```json -{ - "success": true, - "message": "Prompt added successfully", - "data": { - "id": "prompt_id", - "name": "Example Prompt", - "prompt": "This is an example prompt from an API route.", - "description": "Description of the prompt.", - "useCases": [ - { "title": "Use case 1", "description": "Description of use case 1" }, - { "title": "Use case 2", "description": "Description of use case 2" } - ], - "tags": "example, prompt" - } -} -``` - # Endpoint: Edit Prompt The `https://swarms.world/api/edit-prompt` endpoint allows users to edit an existing prompt on the Swarms platform. This API accepts a POST request with a JSON body containing the prompt details to be updated, such as its name, description, use cases, and tags. The request must be authenticated using an API key. diff --git a/docs/swarms_platform/fetch_prompts.md b/docs/swarms_platform/prompts/fetch_prompts.md similarity index 100% rename from docs/swarms_platform/fetch_prompts.md rename to docs/swarms_platform/prompts/fetch_prompts.md diff --git a/playground/swarms_marketplace/agents/create_agent.py b/playground/swarms_marketplace/agents/create_agent.py new file mode 100644 index 00000000..ea04c3a3 --- /dev/null +++ b/playground/swarms_marketplace/agents/create_agent.py @@ -0,0 +1,41 @@ +import requests +import os + +# API endpoint +url = "https://swarms.world/api/add-agent" # replace with your actual API endpoint + +# API key +api_key = os.getenv("SWARMS_API_KEY") # replace with your actual API key + +# Agent data +agent_data = { + "name": "Sample Agent", + "agent": "SampleAgent001", + "language": "Python", + "description": "This is a sample agent description.", + "requirements": [ + {"package": "numpy", "installation": "pip install numpy"}, + {"package": "pandas", "installation": "pip install pandas"} + ], + "useCases": [ + {"title": "Data Analysis", "description": "Analyzes data using advanced algorithms."}, + {"title": "Prediction", "description": "Predicts outcomes based on data."} + ], + "tags": "data,analysis,prediction" +} + +# Headers +headers = { + "Authorization": f"Bearer {api_key}", + "Content-Type": "application/json" +} + +# Sending POST request +response = requests.post(url, json=agent_data, headers=headers) + +# Check response +if response.status_code == 200: + print("Agent created successfully!") +else: + print(f"Failed to create agent: {response.status_code}") + print(response.json()) diff --git a/playground/swarms_marketplace/prompts_api/add_prompt.py b/playground/swarms_marketplace/prompts_api/add_prompt.py new file mode 100644 index 00000000..74f222a4 --- /dev/null +++ b/playground/swarms_marketplace/prompts_api/add_prompt.py @@ -0,0 +1,23 @@ +import requests +import json +import os + +url = "https://swarms.world/api/add-prompt" +headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {os.getenv('SWARMS_API_KEY')}" +} + +data = { + "name": "Example Prompt", + "prompt": "This is an example prompt from an API route.", + "description": "Description of the prompt.", + "useCases": [ + {"title": "Use case 1", "description": "Description of use case 1"}, + {"title": "Use case 2", "description": "Description of use case 2"} + ], + "tags": "example, prompt" +} + +response = requests.post(url, headers=headers, data=json.dumps(data)) +print(response.json()) \ No newline at end of file diff --git a/playground/swarms_marketplace/prompts_api/edit_prompt.py b/playground/swarms_marketplace/prompts_api/edit_prompt.py new file mode 100644 index 00000000..7a7e6fca --- /dev/null +++ b/playground/swarms_marketplace/prompts_api/edit_prompt.py @@ -0,0 +1,25 @@ +import requests +import json +import os + +url = "https://swarms.world/api/edit-prompt" + +headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {os.getenv('SWARMS_API_KEY')}" +} + +data = { + "id": "prompt_id", + "name": "Updated Prompt", + "prompt": "This is an updated prompt from an API route.", + "description": "Updated description of the prompt.", + "useCases": [ + {"title": "Updated use case 1", "description": "Updated description of use case 1"}, + {"title": "Updated use case 2", "description": "Updated description of use case 2"} + ], + "tags": "updated, prompt" +} + +response = requests.post(url, headers=headers, data=json.dumps(data)) +print(response.json()) \ No newline at end of file diff --git a/playground/swarms_marketplace/prompts_api/query_prompts.py b/playground/swarms_marketplace/prompts_api/query_prompts.py new file mode 100644 index 00000000..0a9b418a --- /dev/null +++ b/playground/swarms_marketplace/prompts_api/query_prompts.py @@ -0,0 +1,25 @@ +import requests + +# Fetch all prompts with optional filters +def get_prompts(filters): + response = requests.get('https://swarms.world/get-prompts', params=filters) + + if response.status_code != 200: + raise Exception(f'Error: {response.status_code}, {response.text}') + + data = response.json() + print(data) + +# Fetch prompt by ID +def get_prompt_by_id(id): + response = requests.get(f'https://swarms.world/get-prompts/{id}') + + if response.status_code != 200: + raise Exception(f'Error: {response.status_code}, {response.text}') + + data = response.json() + print(data) + +# Example usage +get_prompts({'name': 'example', 'tag': 'tag1,tag2', 'use_case': 'example', 'use_case_description': 'description'}) +get_prompt_by_id('123') \ No newline at end of file