mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-03-17 13:21:48 +01:00
adding unleashed.chat example
This commit is contained in:
parent
857be9f368
commit
06c3a6617a
18
examples/unleashed_dvm/.env_example
Normal file
18
examples/unleashed_dvm/.env_example
Normal file
@ -0,0 +1,18 @@
|
||||
#Create an account with a lnbits instance of your choice, add the admin key and id here. This account will be used to create a new lnbits wallet for each dvm/bot
|
||||
LNBITS_ADMIN_KEY = ""
|
||||
LNBITS_ADMIN_ID = ""
|
||||
LNBITS_HOST = "https://lnbits.com" #Use your own/a trusted instance ideally.
|
||||
# In order to create a zappable lightning address, host nostdress on your domain or use this preinstalled domain.
|
||||
# We will use the api to create and manage zapable lightning addresses
|
||||
NOSTDRESS_DOMAIN = "nostrdvm.com"
|
||||
|
||||
|
||||
UNLEASHED_API_KEY = ""
|
||||
|
||||
|
||||
# We will automatically create dtags and private keys based on the identifier variable in main.
|
||||
# If your DVM already has a dtag and private key you can replace it here before publishing the DTAG to not create a new one.
|
||||
# The name and NIP90 info of the DVM can be changed but the identifier must stay the same in order to not create a different dtag.
|
||||
|
||||
# We will also create new wallets on the given lnbits instance for each dvm. If you want to use an existing wallet, you can replace the parameters here as well.
|
||||
# Make sure you backup this file to keep access to your wallets
|
26
examples/unleashed_dvm/README.md
Normal file
26
examples/unleashed_dvm/README.md
Normal file
@ -0,0 +1,26 @@
|
||||
# NostrAI: Nostr NIP90 Data Vending Machine Framework Example
|
||||
|
||||
Projects in this folder contain ready-to-use DVMs. To tun the DVM following the next steps:
|
||||
|
||||
## To get started:
|
||||
- Install Python (tested on 3.10/3.11)
|
||||
|
||||
|
||||
Create a new venv in this directory by opening the terminal here, or navigate to this directory and type: `"python -m venv venv"`
|
||||
- Place .env file (based on .env_example) in this folder.
|
||||
- Recommended but optional:
|
||||
- Create a `LNbits` account on an accessible instance of your choice, enter one account's id and admin key (this account will create other accounts for the dvms) Open the .env file and enter this info to `LNBITS_ADMIN_KEY`, `LNBITS_ADMIN_ID`, `LNBITS_HOST`.
|
||||
- If you are running an own instance of `Nostdress` enter `NOSTDRESS_DOMAIN` or use the default one.
|
||||
- Activate the venv with
|
||||
- MacOS/Linux: source ./venv/bin/activate
|
||||
- Windows: .\venv\Scripts\activate
|
||||
- Type: `pip install nostr-dvm`
|
||||
- Run `python3 main.py` (or python main.py)
|
||||
- The framework will then automatically create keys, nip89 tags and zapable NIP57 `lightning addresses` for your dvms in this file.
|
||||
- Check the .env file if these values look correct.
|
||||
- Check the `main.py` file. You can update the image/description/name of your DVM before announcing it.
|
||||
- You can then in main.py set `admin_config.REBROADCAST_NIP89` and
|
||||
`admin_config.UPDATE_PROFILE` to `True` to announce the NIP89 info and update the npubs profile automatically.
|
||||
- After this was successful you can set these back to False until the next time you want to update the NIP89 or profile.
|
||||
|
||||
You are now running your own DVM.
|
55
examples/unleashed_dvm/main.py
Normal file
55
examples/unleashed_dvm/main.py
Normal file
@ -0,0 +1,55 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
import dotenv
|
||||
|
||||
from nostr_dvm.tasks.textgeneration_unleashed_chat import TextGenerationUnleashedChat
|
||||
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 main():
|
||||
identifier = "unleashed"
|
||||
name = "Unleashed Chat"
|
||||
dvm_config = build_default_config(identifier)
|
||||
dvm_config.SEND_FEEDBACK_EVENTS = False
|
||||
dvm_config.USE_OWN_VENV = False
|
||||
dvm_config.FIX_COST = 0
|
||||
admin_config = AdminConfig()
|
||||
admin_config.LUD16 = dvm_config.LN_ADDRESS
|
||||
admin_config.REBROADCAST_NIP89 = False
|
||||
|
||||
|
||||
nip89info = {
|
||||
"name": name,
|
||||
"image": "https://unleashed.chat/_app/immutable/assets/hero.pehsu4x_.jpeg",
|
||||
"about": "I generate Text with Unleashed.chat",
|
||||
"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)
|
||||
|
||||
|
||||
|
||||
unleashed = TextGenerationUnleashedChat(name=name, dvm_config=dvm_config, nip89config=nip89config, admin_config=admin_config)
|
||||
unleashed.run()
|
||||
|
||||
|
||||
|
||||
|
||||
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} ')
|
||||
main()
|
103
examples/unleashed_dvm/test_client.py
Normal file
103
examples/unleashed_dvm/test_client.py
Normal file
@ -0,0 +1,103 @@
|
||||
import json
|
||||
import time
|
||||
from datetime import timedelta
|
||||
from pathlib import Path
|
||||
from threading import Thread
|
||||
|
||||
import dotenv
|
||||
from nostr_sdk import Keys, Client, Tag, EventBuilder, Filter, HandleNotification, Timestamp, nip04_decrypt, \
|
||||
NostrSigner, Options
|
||||
|
||||
from nostr_dvm.utils.dvmconfig import DVMConfig
|
||||
from nostr_dvm.utils.nostr_utils import send_event, check_and_set_private_key
|
||||
from nostr_dvm.utils.definitions import EventDefinitions
|
||||
|
||||
|
||||
def nostr_client_test(prompt):
|
||||
keys = Keys.parse(check_and_set_private_key("test_client"))
|
||||
|
||||
iTag = Tag.parse(["i", prompt, "text"])
|
||||
|
||||
|
||||
relaysTag = Tag.parse(['relays', "wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
|
||||
"wss://nostr-pub.wellorder.net"])
|
||||
alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task to generate TTS"])
|
||||
event = EventBuilder(EventDefinitions.KIND_NIP90_GENERATE_TEXT, str("Answer to prompt"),
|
||||
[iTag, relaysTag, alttag]).to_event(keys)
|
||||
|
||||
relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
|
||||
"wss://nostr-pub.wellorder.net"]
|
||||
|
||||
opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=5)))
|
||||
signer = NostrSigner.keys(keys)
|
||||
client = Client.with_opts(signer,opts)
|
||||
for relay in relay_list:
|
||||
client.add_relay(relay)
|
||||
client.connect()
|
||||
config = DVMConfig
|
||||
send_event(event, client=client, dvm_config=config)
|
||||
return event.as_json()
|
||||
|
||||
def nostr_client():
|
||||
keys = Keys.parse(check_and_set_private_key("test_client"))
|
||||
sk = keys.secret_key()
|
||||
pk = keys.public_key()
|
||||
print(f"Nostr Test Client public key: {pk.to_bech32()}, Hex: {pk.to_hex()} ")
|
||||
signer = NostrSigner.keys(keys)
|
||||
client = Client(signer)
|
||||
|
||||
dvmconfig = DVMConfig()
|
||||
for relay in dvmconfig.RELAY_LIST:
|
||||
client.add_relay(relay)
|
||||
client.connect()
|
||||
|
||||
dm_zap_filter = Filter().pubkey(pk).kinds([EventDefinitions.KIND_DM,
|
||||
EventDefinitions.KIND_ZAP]).since(
|
||||
Timestamp.now()) # events to us specific
|
||||
dvm_filter = (Filter().kinds([EventDefinitions.KIND_NIP90_RESULT_GENERATE_TEXT,
|
||||
EventDefinitions.KIND_FEEDBACK]).since(Timestamp.now())) # public events
|
||||
client.subscribe([dm_zap_filter, dvm_filter])
|
||||
|
||||
|
||||
#nostr_client_test("What has Pablo been up to?")
|
||||
nostr_client_test("What is Gigi talking about recently?")
|
||||
print("Sending Job Request")
|
||||
|
||||
|
||||
class NotificationHandler(HandleNotification):
|
||||
def handle(self, relay_url, event):
|
||||
print(f"Received new event from {relay_url}: {event.as_json()}")
|
||||
if event.kind() == 7000:
|
||||
print("[Nostr Client]: " + event.as_json())
|
||||
elif 6000 < event.kind() < 6999:
|
||||
print("[Nostr Client " + event.author().to_bech32() + "]: " + event.as_json())
|
||||
print("[Nostr Client " + event.author().to_bech32() + "]: " + event.content())
|
||||
|
||||
|
||||
elif event.kind() == 4:
|
||||
dec_text = nip04_decrypt(sk, event.author(), event.content())
|
||||
print("[Nostr Client]: " + f"Received new msg: {dec_text}")
|
||||
|
||||
elif event.kind() == 9735:
|
||||
print("[Nostr Client]: " + f"Received new zap:")
|
||||
print(event.as_json())
|
||||
|
||||
def handle_msg(self, relay_url, msg):
|
||||
return
|
||||
|
||||
client.handle_notifications(NotificationHandler())
|
||||
while True:
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
env_path = Path('.env')
|
||||
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} ')
|
||||
|
||||
nostr_dvm_thread = Thread(target=nostr_client())
|
||||
nostr_dvm_thread.start()
|
Loading…
x
Reference in New Issue
Block a user