STORY
Nostrizing Auto-GPT
AUTHOR
Joined 2023.07.25
PROJECT
DATE
VOTES
sats
COMMENTS

Nostrizing Auto-GPT

Intro

Hey! Since this is my first post, i'll introduce myself. My name is Sebas Arriola, part of the [PhotoBolt Team](https://youtu.be/xex9rEsrU5I)..)

In this post, we will write a plugin that gives Auto-GPT the ability to post to the Nostr Network. Part of the motivation for this post is that we found writing a plugin a little confusing, so we want to document our findings here.

Pre-Requisites

- Install [Auto-GPT](https://github.com/Significant-Gravitas/Auto-GPT),,) follow the instructions in the repo.

1 - Clone the Auto-GPT Plugin Template Repo

[Auto-GPT Plugin Template](https://github.com/Significant-Gravitas/Auto-GPT-Plugin-Template))

git clone [[email protected]](mailto:[email protected]):Significant-Gravitas/Auto-GPT-Plugin-Template.git

2 - Create Nostr Plugin Folder

Navigate to Auto-GPT plugins folder and create a new folder:

cd /path/to/Auto-GPT/plugins
mkdir AutoGPTNostr

3 - Copy Required Files

Copy the __init__.py and requirements.txt files from the plugin template:

cp /path/to/Auto-GPT-Plugin-Template/src/auto\_gpt\_plugin\_template/\_\_init\_\_.py /path/to/Auto-GPT/plugins/AutoGPTNostr
cp /path/to/Auto-GPT-Plugin-Template/requirements.txt /path/to/Auto-GPT/plugins/AutoGPTNostr

4 - Code simple function to post to Nostr

Run pip install nostr to add the dependency that helps interact with nostr in python.

Add nostr dependency to requirements.txt:

black
isort
flake8
pylint
abstract-singleton
wheel
setuptools
build
twine
nostr

Create a file nostr.py in /path/to/Auto-GPT/plugins/AutoGPTNostr and place the following content:

from __future__ import annotations
from nostr.key import PrivateKey
from nostr.relay_manager import RelayManager
from nostr.event import Event
import time
import ssl

private_key = PrivateKey()

def publish(content: str, kind: int = 1) -> Event:
"""
connect to a relay
- relay url
construct an event
publish
close connection
"""

public_key = private_key.public_key
print(f"Public key: {public_key.bech32()}")

relay_manager = RelayManager()
relay_manager.add_relay("wss://nostr-pub.wellorder.net")
relay_manager.open_connections({"cert_reqs": ssl.CERT_NONE})

time.sleep(1.25)

event = Event(public_key.hex(), content)
private_key.sign_event(event)
relay_manager.publish_event(event)

time.sleep(1) # allow the messages to send
relay_manager.close_connections()

return event

### 5 - Plugin Code

We need to update the __init__.py file. Add a couple imports at the top:

```

from auto_gpt_plugin_template import AutoGPTPluginTemplate

from .nostr import publish

```

Rename the class:

```

class AutoGPTNostr(AutoGPTPluginTemplate):

```

Update plugin name, etc:

```

def __init__(self):

super().__init__()

self._name = "autogpt-nostr"

self._version = "0.0.1"

self._description = "Give the ability to post to Nostr!"

```

Remove all "abstract methods" annotations: @abc.abstractmethod

```

@abc.abstractmethod # remove this line

def can_handle_on_response(self) -> bool:

"""This method is called to check that the plugin can

handle the on_response method.

Returns:

bool: True if the plugin can handle the on_response method."""

return False

```

We want to handle the post_prompt hook, so we need to return True:

```

def can_handle_post_prompt(self) -> bool:

"""This method is called to check that the plugin can

handle the post_prompt method.

Returns:

bool: True if the plugin can handle the post_prompt method."""

return True

```

Modify the post_prompt handler:

```

def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator:

"""This method is called just after the generate_prompt is called,

but actually before the prompt is generated.

Args:

prompt (PromptGenerator): The prompt generator.

Returns:

Prompt

Generator: The prompt generator.

"""

prompt.add_command(

"nostr",

"Nostr Stuff",

{"content": ""},

publish,

)

return prompt

```

### 6 - Enable Plugin

Modify the /path/to/Auto-GPT/plugins_config.yaml file to look like this:

```

AutoGPTNostr:

enabled: true

```

### 7 - Testing Time

Navigate to Auto-GPT root folder: /path/to/Auto-GPT and run:

```

./run.sh --install-plugin-deps

```

Try the following prompt:

```

generate a random though that is obviously created by an AI and post it to the Nostr network

```