|
| 1 | +# Decorators |
| 2 | +> With only a few lines of code, builders can easily deploy custom AI agents to the Naptha hub or their own local node. |
| 3 | +
|
| 4 | +### Context |
| 5 | +Naptha supports a web of multi-agent systems that grow and evolve. This walkthrough explains how to decorate functions in order to **quickly publish existing agents, *allowing them to interact with others.*** |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +* Python `>= 3.10, <= 3.13` |
| 10 | +* Poetry package manager |
| 11 | +* Naptha username & password |
| 12 | + |
| 13 | +*See [installation guide](/GettingStarted/Installation) for help.* |
| 14 | + |
| 15 | +### Step-by-step Walkthrough |
| 16 | + |
| 17 | +#### 1. Setup |
| 18 | +Configure your `.env` file: |
| 19 | +``` |
| 20 | +HUB_USER=<your_naptha_username> |
| 21 | +HUB_PASS=<your_naptha_password> |
| 22 | +HUB_URL=ws://node.naptha.ai:3001/rpc |
| 23 | +
|
| 24 | +NODE_URL=http://node.naptha.ai:7001 |
| 25 | +``` |
| 26 | +*These variables are used to connect to the Naptha network.* |
| 27 | + |
| 28 | +#### 2. Add Naptha SDK |
| 29 | +Update your `pyproject.toml` file: |
| 30 | +```toml |
| 31 | +[tool.poetry.dependencies] |
| 32 | +naptha-sdk = {git = "https://github.com/NapthaAI/naptha-sdk"} |
| 33 | +``` |
| 34 | + |
| 35 | +#### 3. Install dependencies: |
| 36 | +Execute via CLI: |
| 37 | +```bash |
| 38 | +poetry install |
| 39 | +``` |
| 40 | + |
| 41 | +#### 4. Import Naptha SDK |
| 42 | +Put this in your main Python file: `<agent>.py` |
| 43 | +```python |
| 44 | +from naptha_sdk.client.naptha import agent as naptha_agent |
| 45 | +``` |
| 46 | +*We recommend importing as `naptha_agent` to avoid naming conflicts.* |
| 47 | + |
| 48 | +#### 5. Decorate |
| 49 | +Label agent functions: |
| 50 | +```python |
| 51 | +@naptha_agent("<agent_name>") |
| 52 | +def <agent_function>(...): |
| 53 | + # agent logic goes here |
| 54 | + return <agent_output> |
| 55 | +``` |
| 56 | +*Replace `<agent_name>` with a unique identifier for your agent. Keep `<agent_function>` and `<agent_output>` the same, however you named them.* |
| 57 | + |
| 58 | +#### 6. Convert (automatically) |
| 59 | +Process and package decorated functions: |
| 60 | +```bash |
| 61 | +poetry run python <agent>.py |
| 62 | +``` |
| 63 | +*This creates a folder named `agent_pkgs`, which contains your "Napthafied" agent functions. The SDK translates your code into a format compatible with other agents on Naptha.* |
| 64 | + |
| 65 | +#### 7. Publish |
| 66 | +Enter this command: |
| 67 | +```bash |
| 68 | +naptha publish |
| 69 | +``` |
| 70 | +*This command publishes all agents in the `agent_pkgs` folder to the Naptha node specified in your `.env` file.* |
| 71 | + |
| 72 | +#### 8. Test |
| 73 | +Verify the agent is working properly: |
| 74 | +```bash |
| 75 | +naptha run <agent_name> |
| 76 | +``` |
| 77 | +*Check for your expected output.* |
| 78 | + |
| 79 | +### How does this work? |
| 80 | + |
| 81 | +Let's break it down: |
| 82 | + |
| 83 | +`@naptha_agent("<agent_name>")` with a unique name for the agent. Below that line, agent functionality can be defined normally using various frameworks. |
| 84 | + |
| 85 | +By running Python code that includes our decorator, agent functions are automatically processed and converted into Naptha-compatible packages. |
| 86 | + |
| 87 | +Later, when you enter `naptha publish` via the CLI, those agent packages will be added to the Naptha node specified in your `.env` file. |
| 88 | + |
| 89 | +## Usage Examples |
| 90 | + |
| 91 | +### Stock Analysis - CrewAI |
| 92 | +View the full code sample [here](https://github.com/NapthaAI/crewAI-examples/blob/main/stock_analysis/src/stock_analysis/crew.py). |
| 93 | +```python |
| 94 | +@naptha_agent("financial_agent") |
| 95 | + def financial_agent(self) -> Agent: |
| 96 | + return Agent( |
| 97 | + ... |
| 98 | + ) |
| 99 | +``` |
| 100 | + |
| 101 | +## Feedback |
| 102 | + |
| 103 | +[Create issues](https://github.com/NapthaAI/naptha-sdk/issues) in the Naptha SDK GitHub repository to let us know any problems, ideas, or questions. We plan to continue adding examples, and your help would be much appreciated! |
0 commit comments