diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index a2332d69..ec16eb4d 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -223,8 +223,9 @@ nav: - 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" + - Overview: "swarms_platform/agents/agents_api.md" + - Add Agents: "swarms_platform/agents/fetch_agents.md" + - Edit Agents: "swarms_platform/agents/edit_agent.md" # - Tools API: # - Overview: "swarms_platform/tools_api.md" # - Add Tools: "swarms_platform/fetch_tools.md" diff --git a/docs/swarms_platform/agents/agents_api.md b/docs/swarms_platform/agents/agents_api.md new file mode 100644 index 00000000..6dab163a --- /dev/null +++ b/docs/swarms_platform/agents/agents_api.md @@ -0,0 +1,217 @@ +# Agents API Documentation + +The `https://swarms.world/api/add-agent` endpoint allows users to add a new agent to the Swarms platform. This API accepts a POST request with a JSON body containing details of the agent, such as its name, description, use cases, language, tags and requirements. The request must be authenticated using an API key. + +## Endpoint: Add Agent + +- **URL:** `https://swarms.world/api/add-agent` +- **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 agent. | Yes | +| `agent` | `string` | The agent text. | Yes | +| `description` | `string` | A brief description of the agent. | Yes | +| `language` | `string` | The agent's syntax language with a default of python | No | +| `useCases` | `array` | An array of use cases, each containing a title and description. | Yes | +| `requirements` | `array` | An array of requirements, each containing a package name and installation. | Yes | +| `tags` | `string` | Comma-separated tags for the agent. | Yes | + +### `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 | + +### `requirements` Structure + +Each requirement in the `requirements` array should be an object with the following attributes: + +| Attribute | Type | Description | Required | +| -------------- | -------- | ------------------------------------ | -------- | +| `package` | `string` | The name of the package. | Yes | +| `installation` | `string` | Installation command for the package | Yes | + +## Example Usage + +### Python + +```python +import requests +import json +import os + + +url = "https://swarms.world/api/add-agent" + +headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {os.getenv("SWARMS_API_KEY")}" +} + +data = { + "name": "Example Agent", + "agent": "This is an example agent from an API route.", + "description": "Description of the agent.", + "language": "python", + "useCases": [ + {"title": "Use case 1", "description": "Description of use case 1"}, + {"title": "Use case 2", "description": "Description of use case 2"} + ], + "requirements": [ + {"package": "pip", "installation": "pip install"}, + {"package": "pip3", "installation": "pip3 install"} + ], + "tags": "example, agent" +} + +response = requests.post(url, headers=headers, data=json.dumps(data)) +print(response.json()) +``` + +### Node.js + +```javascript +const fetch = require("node-fetch"); + +async function addAgentHandler() { + try { + const response = await fetch("https://swarms.world/api/add-agent", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer {apiKey}", + }, + body: JSON.stringify({ + name: "Example Agent", + agent: "This is an example agent from an API route.", + description: "Description of the agent.", + language: "python", + useCases: [ + { title: "Use case 1", description: "Description of use case 1" }, + { title: "Use case 2", description: "Description of use case 2" }, + ], + requirements: [ + { package: "pip", installation: "pip install" }, + { package: "pip3", installation: "pip3 install" }, + ], + tags: "example, agent", + }), + }); + + const result = await response.json(); + console.log(result); + } catch (error) { + console.error("An error has occurred", error); + } +} + +addAgentHandler(); +``` + +### Go + +```go +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" +) + +func main() { + url := "https://swarms.world/api/add-agent" + payload := map[string]interface{}{ + "name": "Example Agent", + "agent": "This is an example agent from an API route.", + "description": "Description of the agent.", + "useCases": []map[string]string{ + {"title": "Use case 1", "description": "Description of use case 1"}, + {"title": "Use case 2", "description": "Description of use case 2"}, + }, + "requirements": []map[string]string{ + {"package": "pip", "installation": "pip install"}, + {"package": "pip3", "installation": "pip3 install"} + }, + "tags": "example, agent", + } + 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-agent \ +-H "Content-Type: application/json" \ +-H "Authorization: Bearer {apiKey}" \ +-d '{ + "name": "Example Agent", + "agent": "This is an example agent from an API route.", + "description": "Description of the agent.", + "language": "python", + "useCases": [ + { title: "Use case 1", description: "Description of use case 1" }, + { title: "Use case 2", description: "Description of use case 2" }, + ], + "requirements": [ + { package: "pip", installation: "pip install" }, + { package: "pip3", installation: "pip3 install" }, + ], + "tags": "example, agent", +}' +``` + +## Response + +The response will be a JSON object containing the result of the operation. Example response: + +```json +{ + "success": true, + "message": "Agent added successfully", + "data": { + "id": "agent_id", + "name": "Example Agent", + "agent": "This is an example agent from an API route.", + "description": "Description of the agent.", + "language": "python", + "useCases": [ + { "title": "Use case 1", "description": "Description of use case 1" }, + { "title": "Use case 2", "description": "Description of use case 2" } + ], + "requirements": [ + { "package": "pip", "installation": "pip install" }, + { "package": "pip3", "installation": "pip3 install" } + ], + "tags": "example, agent" + } +} +``` \ No newline at end of file diff --git a/docs/swarms_platform/agents_api.md b/docs/swarms_platform/agents/edit_agent.md similarity index 55% rename from docs/swarms_platform/agents_api.md rename to docs/swarms_platform/agents/edit_agent.md index 2dace318..dc934bee 100644 --- a/docs/swarms_platform/agents_api.md +++ b/docs/swarms_platform/agents/edit_agent.md @@ -1,216 +1,3 @@ -# Agents API Documentation - -The `https://swarms.world/api/add-agent` endpoint allows users to add a new agent to the Swarms platform. This API accepts a POST request with a JSON body containing details of the agent, such as its name, description, use cases, language, tags and requirements. The request must be authenticated using an API key. - -## Endpoint: Add Agent - -- **URL:** `https://swarms.world/api/add-agent` -- **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 agent. | Yes | -| `agent` | `string` | The agent text. | Yes | -| `description` | `string` | A brief description of the agent. | Yes | -| `language` | `string` | The agent's syntax language with a default of python | No | -| `useCases` | `array` | An array of use cases, each containing a title and description. | Yes | -| `requirements` | `array` | An array of requirements, each containing a package name and installation. | Yes | -| `tags` | `string` | Comma-separated tags for the agent. | Yes | - -### `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 | - -### `requirements` Structure - -Each requirement in the `requirements` array should be an object with the following attributes: - -| Attribute | Type | Description | Required | -| -------------- | -------- | ------------------------------------ | -------- | -| `package` | `string` | The name of the package. | Yes | -| `installation` | `string` | Installation command for the package | Yes | - -## Example Usage - -### Python - -```python -import requests -import json - -url = "https://swarms.world/api/add-agent" -headers = { - "Content-Type": "application/json", - "Authorization": "Bearer {apiKey}" -} -data = { - "name": "Example Agent", - "agent": "This is an example agent from an API route.", - "description": "Description of the agent.", - "language": "python", - "useCases": [ - {"title": "Use case 1", "description": "Description of use case 1"}, - {"title": "Use case 2", "description": "Description of use case 2"} - ], - "requirements": [ - {"package": "pip", "installation": "pip install"}, - {"package": "pip3", "installation": "pip3 install"} - ], - "tags": "example, agent" -} - -response = requests.post(url, headers=headers, data=json.dumps(data)) -print(response.json()) -``` - -### Node.js - -```javascript -const fetch = require("node-fetch"); - -async function addAgentHandler() { - try { - const response = await fetch("https://swarms.world/api/add-agent", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: "Bearer {apiKey}", - }, - body: JSON.stringify({ - name: "Example Agent", - agent: "This is an example agent from an API route.", - description: "Description of the agent.", - language: "python", - useCases: [ - { title: "Use case 1", description: "Description of use case 1" }, - { title: "Use case 2", description: "Description of use case 2" }, - ], - requirements: [ - { package: "pip", installation: "pip install" }, - { package: "pip3", installation: "pip3 install" }, - ], - tags: "example, agent", - }), - }); - - const result = await response.json(); - console.log(result); - } catch (error) { - console.error("An error has occurred", error); - } -} - -addAgentHandler(); -``` - -### Go - -```go -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "net/http" -) - -func main() { - url := "https://swarms.world/api/add-agent" - payload := map[string]interface{}{ - "name": "Example Agent", - "agent": "This is an example agent from an API route.", - "description": "Description of the agent.", - "useCases": []map[string]string{ - {"title": "Use case 1", "description": "Description of use case 1"}, - {"title": "Use case 2", "description": "Description of use case 2"}, - }, - "requirements": []map[string]string{ - {"package": "pip", "installation": "pip install"}, - {"package": "pip3", "installation": "pip3 install"} - }, - "tags": "example, agent", - } - 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-agent \ --H "Content-Type: application/json" \ --H "Authorization: Bearer {apiKey}" \ --d '{ - "name": "Example Agent", - "agent": "This is an example agent from an API route.", - "description": "Description of the agent.", - "language": "python", - "useCases": [ - { title: "Use case 1", description: "Description of use case 1" }, - { title: "Use case 2", description: "Description of use case 2" }, - ], - "requirements": [ - { package: "pip", installation: "pip install" }, - { package: "pip3", installation: "pip3 install" }, - ], - "tags": "example, agent", -}' -``` - -## Response - -The response will be a JSON object containing the result of the operation. Example response: - -```json -{ - "success": true, - "message": "Agent added successfully", - "data": { - "id": "agent_id", - "name": "Example Agent", - "agent": "This is an example agent from an API route.", - "description": "Description of the agent.", - "language": "python", - "useCases": [ - { "title": "Use case 1", "description": "Description of use case 1" }, - { "title": "Use case 2", "description": "Description of use case 2" } - ], - "requirements": [ - { "package": "pip", "installation": "pip install" }, - { "package": "pip3", "installation": "pip3 install" } - ], - "tags": "example, agent" - } -} -``` # Endpoint: Edit Agent diff --git a/docs/swarms_platform/fetch_agents.md b/docs/swarms_platform/agents/fetch_agents.md similarity index 100% rename from docs/swarms_platform/fetch_agents.md rename to docs/swarms_platform/agents/fetch_agents.md diff --git a/swarms/utils/add_to_marketplace.py b/swarms/utils/add_to_marketplace.py new file mode 100644 index 00000000..459134bd --- /dev/null +++ b/swarms/utils/add_to_marketplace.py @@ -0,0 +1,101 @@ +import os +import json +from typing import Dict, List + +import requests +from loguru import logger +from swarms.structs.agent import Agent + + +def add_agent_to_marketplace( + name: str, + agent: str, + language: str, + description: str, + use_cases: List[Dict[str, str]], + requirements: List[Dict[str, str]], + tags: str, +) -> Dict[str, str]: + """ + Add an agent to the marketplace. + + Args: + name (str): The name of the agent. + agent (str): The agent code. + language (str): The programming language of the agent. + description (str): The description of the agent. + use_cases (List[Dict[str, str]]): The list of use cases for the agent. + requirements (List[Dict[str, str]]): The list of requirements for the agent. + tags (str): The tags for the agent. + api_key (str): The API key for authentication. + + Returns: + Dict[str, str]: The response from the API. + + Raises: + requests.exceptions.RequestException: If there is an error making the API request. + """ + logger.info("Adding agent to marketplace...") + + url = "https://swarms.world/api/add-agent" + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {os.getenv("SWARMS_API_KEY")}", + } + data = { + "name": name, + "agent": agent, + "description": description, + "language": language, + "useCases": use_cases, + "requirements": requirements, + "tags": tags, + } + + try: + response = requests.post( + url, headers=headers, data=json.dumps(data) + ) + response.raise_for_status() + return response.json() + except requests.exceptions.RequestException as e: + logger.error(f"Error making API request: {e}") + + +def add_agent_to_marketplace_sync( + agent: Agent, + use_cases: List[Dict[str, str]], + requirements: List[Dict[str, str]], + tags: str, +): + return add_agent_to_marketplace( + name=agent.agent_name, + description=agent.description, + language="python", + use_cases=use_cases, + requirements=requirements, + tags=tags, + ) + + +# # Example usage +# async def main(): +# name = "Example Agent" +# agent = "This is an example agent from an API route." +# description = "Description of the agent." +# language = "python" +# use_cases = [ +# {"title": "Use case 1", "description": "Description of use case 1"}, +# {"title": "Use case 2", "description": "Description of use case 2"} +# ] +# requirements = [ +# {"package": "pip", "installation": "pip install"}, +# {"package": "pip3", "installation": "pip3 install"} +# ] +# tags = "example, agent" +# api_key = "YOUR_API_KEY" + +# result = await add_agent_to_marketplace(name, agent, language, description, use_cases, requirements, tags, api_key) +# print(result) + +# asyncio.run(main()) diff --git a/tests/artifacts/test_artifact_main.py b/tests/artifacts/test_artifact_main.py index a2182e37..e54cb86f 100644 --- a/tests/artifacts/test_artifact_main.py +++ b/tests/artifacts/test_artifact_main.py @@ -1,6 +1,6 @@ import pytest from datetime import datetime -from swarms.artifacts.artifact_base import Artifact, FileVersion +from swarms.artifacts.main_artifact import Artifact, FileVersion def test_file_version():