mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-03-26 17:41:43 +01:00
add example duckduckai in generic_dvm
This commit is contained in:
parent
b270b1b9da
commit
e51e6506b3
@ -38,8 +38,15 @@ class GenericDVM(DVMTaskInterface):
|
||||
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)
|
||||
prompt = ""
|
||||
for tag in event.tags():
|
||||
if tag.as_vec()[0] == 'i':
|
||||
input_type = tag.as_vec()[2]
|
||||
if input_type == "text":
|
||||
prompt = tag.as_vec()[1]
|
||||
|
||||
request_form = {"jobID": event.id().to_hex()}
|
||||
self.options["input"] = prompt
|
||||
request_form['options'] = json.dumps(self.options)
|
||||
return request_form
|
||||
|
||||
|
@ -716,10 +716,6 @@ def playground():
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
env_path = Path('.env')
|
||||
if not env_path.is_file():
|
||||
|
74
tests/generic_dvm_duck_chat.py
Normal file
74
tests/generic_dvm_duck_chat.py
Normal file
@ -0,0 +1,74 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import dotenv
|
||||
from duck_chat import ModelType
|
||||
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 = "DuckChat"
|
||||
identifier = "duckduckchat" # 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 briding DuckDuckAI'",
|
||||
"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 = {
|
||||
"input": "How do you call a noisy ostrich?",
|
||||
}
|
||||
|
||||
dvm = GenericDVM(name=name, dvm_config=dvm_config, nip89config=nip89config,
|
||||
admin_config=admin_config, options=options)
|
||||
|
||||
async def process(request_form):
|
||||
#pip install -U https://github.com/mrgick/duckduckgo-chat-ai/archive/master.zip
|
||||
|
||||
from duck_chat import DuckChat
|
||||
options = dvm.set_options(request_form)
|
||||
async with DuckChat(model=ModelType.GPT4o) as chat:
|
||||
query = options["input"]
|
||||
result = await chat.ask_question(query)
|
||||
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)
|
@ -267,6 +267,35 @@ async def nostr_client_generic_test(ptag):
|
||||
return event.as_json()
|
||||
|
||||
|
||||
async def nostr_client_duckduck_test(ptag, query):
|
||||
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])
|
||||
iTag = Tag.parse(["i", query, "text"])
|
||||
|
||||
tags = [relaysTag, alttag, pTag, iTag]
|
||||
|
||||
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"))
|
||||
@ -390,7 +419,7 @@ async def nostr_client():
|
||||
#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_generic_test("da1a5e31dec2d34e09da02974f832d3e4df81d9f254a8035e91da615dcb53920")
|
||||
await nostr_client_duckduck_test("aa8ab5b774d47e7b29a985dd739cfdcccf93451678bf7977ba1b2e094ecd8b30", "Tell me a joke")
|
||||
|
||||
# await nostr_client_test_search_profile("dontbelieve")
|
||||
#wot = ["99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64"]
|
||||
|
Loading…
x
Reference in New Issue
Block a user