Skip to content

Commit

Permalink
add example for very generic dvm that can be controlled from a single…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
believethehype committed Aug 27, 2024
1 parent 608c5fa commit 0c78e3e
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 2 deletions.
103 changes: 103 additions & 0 deletions nostr_dvm/tasks/generic_dvm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import json
import os
from datetime import timedelta
from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKey, NostrSigner, Kind, RelayOptions

from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv
from nostr_dvm.utils.admin_utils import AdminConfig
from nostr_dvm.utils.definitions import EventDefinitions
from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config
from nostr_dvm.utils.nip88_utils import NIP88Config
from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag
from nostr_dvm.utils.output_utils import post_process_list_to_events

"""
This File contains a Generic DVM that can be overwritten by the user
Accepted Inputs: None
Outputs: Text
Params: None
"""


class GenericDVM(DVMTaskInterface):
KIND: Kind = Kind(5000)
TASK: str = "generic"
FIX_COST: float = 0
dvm_config: DVMConfig
options = {}

async def init_dvm(self, name, dvm_config: DVMConfig, nip89config: NIP89Config, nip88config: NIP88Config = None,
admin_config: AdminConfig = None, options=None):
dvm_config.SCRIPT = os.path.abspath(__file__)
if dvm_config.KIND is not None:
self.KIND = dvm_config.KIND

async def is_input_supported(self, tags, client=None, dvm_config=None):
return True

async def create_request_from_nostr_event(self, event, client=None, dvm_config=None):
self.dvm_config = dvm_config
print(self.dvm_config.PRIVATE_KEY)

request_form = {"jobID": event.id().to_hex()}
request_form['options'] = json.dumps(self.options)
return request_form

async def process(self, request_form):
options = self.set_options(request_form)
result = "I'm manipulating the DVM from my inside function\n"
result += options["some_option"]
print(result)
return result


# We build an example here that we can call by either calling this file directly from the main directory,
# or by adding it to our playground. You can call the example and adjust it to your needs or redefine it in the
# playground or elsewhere
def build_example(name, identifier, admin_config, announce = False):

admin_config = AdminConfig()
admin_config.REBROADCAST_NIP89 = announce
admin_config.REBROADCAST_NIP65_RELAY_LIST = announce
admin_config.UPDATE_PROFILE = announce

name = "Generic DVM"
identifier = "a_very_generic_dvm" # Chose a unique identifier in order to get a lnaddress
dvm_config = build_default_config(identifier)
dvm_config.KIND = Kind(5050) # Manually set the Kind Number (see data-vending-machines.org)

# Add NIP89
nip89info = {
"name": name,
"image": "https://image.nostr.build/28da676a19841dcfa7dcf7124be6816842d14b84f6046462d2a3f1268fe58d03.png",
"about": "I'm an all purpose DVM'",
"encryptionSupported": True,
"cashuAccepted": True,
"nip90Params": {
}
}

nip89config = NIP89Config()
nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["image"])
nip89config.CONTENT = json.dumps(nip89info)

options = {
"some_option": "#RunDVM",
}

dvm = GenericDVM(name=name, dvm_config=dvm_config, nip89config=nip89config,
admin_config=admin_config, options=options)

async def process(request_form):
options = dvm.set_options(request_form)
result = "I'm manipulating the DVM from outside\n"
result += options["some_option"]
print(result)
return result

dvm.process = process # overwrite the process function with the above one
return dvm


if __name__ == '__main__':
process_venv(GenericDVM)
1 change: 1 addition & 0 deletions nostr_dvm/utils/dvmconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class DVMConfig:
UPDATE_DATABASE = True # DVMs that use a db manage their db by default. If a dvm should use the same db as another DVM, deactive it for those who do.
CUSTOM_PROCESSING_MESSAGE = None
LOGLEVEL = LogLevel.DEBUG
KIND = None

# Make sure you have the cashu library installed and built correctly on your system, before enableing nutzaps for a DVM
# this is not installed by default
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = '0.8.7'
VERSION = '0.8.8'
DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines'
LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information')

Expand Down
69 changes: 69 additions & 0 deletions tests/generic_dvm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import json
from pathlib import Path

import dotenv
from nostr_sdk import Kind

from nostr_dvm.tasks.generic_dvm import GenericDVM
from nostr_dvm.utils.admin_utils import AdminConfig
from nostr_dvm.utils.dvmconfig import build_default_config
from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag


def playground(announce=False):
admin_config = AdminConfig()
admin_config.REBROADCAST_NIP89 = announce
admin_config.REBROADCAST_NIP65_RELAY_LIST = announce
admin_config.UPDATE_PROFILE = announce

name = "Generic DVM"
identifier = "a_very_generic_dvm" # Chose a unique identifier in order to get a lnaddress
dvm_config = build_default_config(identifier)
dvm_config.KIND = Kind(5050) # Manually set the Kind Number (see data-vending-machines.org)

# Add NIP89
nip89info = {
"name": name,
"image": "https://image.nostr.build/28da676a19841dcfa7dcf7124be6816842d14b84f6046462d2a3f1268fe58d03.png",
"about": "I'm an all purpose DVM'",
"encryptionSupported": True,
"cashuAccepted": True,
"nip90Params": {
}
}

nip89config = NIP89Config()
nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["image"])
nip89config.CONTENT = json.dumps(nip89info)

options = {
"some_option": "#RunDVM",
}

dvm = GenericDVM(name=name, dvm_config=dvm_config, nip89config=nip89config,
admin_config=admin_config, options=options)

async def process(request_form):
options = dvm.set_options(request_form)
result = "I'm manipulating the DVM from outside\n"
result += options["some_option"]
print(result)
return result

dvm.process = process # overwrite the process function with the above one
dvm.run(True)


if __name__ == '__main__':
env_path = Path('.env')
if not env_path.is_file():
with open('.env', 'w') as f:
print("Writing new .env file")
f.write('')
if env_path.is_file():
print(f'loading environment from {env_path.resolve()}')
dotenv.load_dotenv(env_path, verbose=True, override=True)
else:
raise FileNotFoundError(f'.env file not found at {env_path} ')

playground(announce=False)
31 changes: 30 additions & 1 deletion tests/test_dvm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,34 @@ async def nostr_client_custom_discovery(user, ptag):
return event.as_json()


async def nostr_client_generic_test(ptag):
keys = Keys.parse(check_and_set_private_key("test_client"))

relay_list = ["wss://nostr.oxtr.dev", "wss://relay.primal.net",
]

relaysTag = Tag.parse(relay_list)
alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task"])

pTag = Tag.parse(["p", ptag])

tags = [relaysTag, alttag, pTag]

event = EventBuilder(Kind(5050), str("Give me content"),
tags).to_event(keys)

signer = NostrSigner.keys(keys)
client = Client(signer)
for relay in relay_list:
await client.add_relay(relay)
ropts = RelayOptions().ping(False)
await client.connect()
config = DVMConfig
await send_event(event, client=client, dvm_config=config)
return event.as_json()



async def nostr_client_test_discovery_user(user, ptag):
keys = Keys.parse(check_and_set_private_key("test_client"))

Expand Down Expand Up @@ -360,8 +388,9 @@ async def nostr_client():
# await nostr_client_test_translation("44a0a8b395ade39d46b9d20038b3f0c8a11168e67c442e3ece95e4a1703e2beb", "event", "zh", 20, 20)

#await nostr_client_test_image("a beautiful purple ostrich watching the sunset, eating a cashew nut")
await nostr_client_custom_discovery("99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64", "8e998d62eb20ec892acf9d5e8efa58050ccd951cae15a64eabbc5c0a7c74d185")
# await nostr_client_custom_discovery("99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64", "8e998d62eb20ec892acf9d5e8efa58050ccd951cae15a64eabbc5c0a7c74d185")

await nostr_client_generic_test("da1a5e31dec2d34e09da02974f832d3e4df81d9f254a8035e91da615dcb53920")

# await nostr_client_test_search_profile("dontbelieve")
#wot = ["99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64"]
Expand Down

0 comments on commit 0c78e3e

Please sign in to comment.