Skip to content

Commit

Permalink
Merge pull request #763 from trueagi-io/snet_upd2
Browse files Browse the repository at this point in the history
Snet upd2
  • Loading branch information
Necr0x0Der authored Aug 23, 2024
2 parents 0a28f60 + bc21c7b commit 0ef9fa9
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 87 deletions.
10 changes: 9 additions & 1 deletion python/hyperon/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ class NoReduceError(Exception):
"""Custom exception; raised when a reduction operation cannot be performed."""
pass

class MettaError(Exception):
"""Custom exception; raised when a error should be returned from OperationAtom,
, but we don't want to output Python error stack."""
pass

class OperationObject(GroundedObject):
"""
An OperationObject represents an operation as a grounded object, allowing for more
Expand Down Expand Up @@ -406,7 +411,10 @@ def execute(self, *atoms, res_typ=AtomType.UNDEFINED):
# so a MeTTa program can catch and analyze it.
# raise RuntimeError("Grounded operation " + self.name + " with unwrap=True expects only grounded arguments")
raise NoReduceError()
result = self.op(*args, **kwargs)
try:
result = self.op(*args, **kwargs)
except MettaError as e:
return [E(S('Error'), *e.args)]
if result is None:
return [Atoms.UNIT]
if callable(result):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

!(add-reduct &self
(= (abstractive-summarisation)
(snet-service "naint" "abstractive-summarisation"
(snet-sdk create_service_client "naint" "abstractive-summarisation"
; Put your data here or use environment variables
;(Kwargs
; (private_key SNET_PRIVATE_KEY)
; (eth_rpc_endpoint ETH_RPC_ENDPOINT)
; (email SNET_EMAIL)
; (free_call_auth_token_bin FREE_CALL_AUTH_TOKEN_BIN)
; (free_call_token_expiry_block FREE_CALL_TOKEN_EXPIRE_BLOCK)
;)
)))
8 changes: 2 additions & 6 deletions python/sandbox/snet/snet/naint/code-generation.metta
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
!(import! &self snet_io)

!(add-reduct &self
(= (code-generation)
(snet-service "naint" "code-generation"
(snet-sdk create_service_client "naint" "code-generation"
; Put your data here or use environment variables
;(Kwargs
; (private_key SNET_PRIVATE_KEY)
; (eth_rpc_endpoint ETH_RPC_ENDPOINT)
; (email SNET_EMAIL)
; (free_call_auth_token_bin FREE_CALL_AUTH_TOKEN_BIN)
; (free_call_token_expiry_block FREE_CALL_TOKEN_EXPIRE_BLOCK)
;)
)))

Expand Down
8 changes: 2 additions & 6 deletions python/sandbox/snet/snet/naint/generative-lms.metta
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
!(import! &self snet_io)

!(add-reduct &self
(= (generative-lms)
(snet-service "naint" "generative-lms"
(snet-sdk create_service_client "naint" "generative-lms"
; Put your data here or use environment variables
;(Kwargs
; (private_key SNET_PRIVATE_KEY)
; (eth_rpc_endpoint ETH_RPC_ENDPOINT)
; (email SNET_EMAIL)
; (free_call_auth_token_bin FREE_CALL_AUTH_TOKEN_BIN)
; (free_call_token_expiry_block FREE_CALL_TOKEN_EXPIRE_BLOCK)
;)
)))
8 changes: 2 additions & 6 deletions python/sandbox/snet/snet/naint/image-generation.metta
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
!(import! &self snet_io)

!(add-reduct &self
(= (image-generation)
(snet-service "naint" "image-generation"
(snet-sdk create_service_client "naint" "image-generation"
; Put your data here or use environment variables
;(Kwargs
; (private_key SNET_PRIVATE_KEY)
; (eth_rpc_endpoint ETH_RPC_ENDPOINT)
; (email SNET_EMAIL)
; (free_call_auth_token_bin FREE_CALL_AUTH_TOKEN_BIN)
; (free_call_token_expiry_block FREE_CALL_TOKEN_EXPIRE_BLOCK)
;)
)))
8 changes: 2 additions & 6 deletions python/sandbox/snet/snet/naint/text-generation.metta
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
!(import! &self snet_io)

!(add-reduct &self
(= (text-generation)
(snet-service "naint" "text-generation"
(snet-sdk create_service_client "naint" "text-generation"
; Put your data here or use environment variables
;(Kwargs
; (private_key SNET_PRIVATE_KEY)
; (eth_rpc_endpoint ETH_RPC_ENDPOINT)
; (email SNET_EMAIL)
; (free_call_auth_token_bin FREE_CALL_AUTH_TOKEN_BIN)
; (free_call_token_expiry_block FREE_CALL_TOKEN_EXPIRE_BLOCK)
;)
)))

Expand Down
120 changes: 90 additions & 30 deletions python/sandbox/snet/snet_io.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,103 @@
from hyperon.atoms import *
from hyperon import *
from hyperon.ext import register_atoms
import os
from snet import sdk

class SNetSDKWrapper:

def __init__(self):
self.snet_sdk = None

def init_sdk(self,
private_key=os.getenv("SNET_PRIVATE_KEY", '0'*32),
eth_rpc_endpoint=os.getenv("ETH_RPC_ENDPOINT"),
email=os.getenv("SNET_EMAIL"),
identity_name="hyperon",
network="mainnet",
identity_type="key",
concurrency=False,
force_update=False):
config = {
"private_key": private_key,
"eth_rpc_endpoint": eth_rpc_endpoint,
"email": email,
"concurrency": concurrency,
"identity_name": identity_name,
"network": network,
"identity_type": identity_type,
"force_update": force_update
}
self.snet_sdk = sdk.SnetSDK(config)

def organization_list(self):
return self.snet_sdk.get_organization_list()

def service_list(self, org_id):
return self.snet_sdk.get_services_list(org_id)

def create_service_client(self, org_id, service_id,
free_call_auth_token_bin=os.getenv("FREE_CALL_AUTH_TOKEN_BIN", None),
free_call_token_expiry_block=os.getenv("FREE_CALL_TOKEN_EXPIRE_BLOCK", None)):
if free_call_token_expiry_block is not None:
free_call_token_expiry_block = int(free_call_token_expiry_block)
service_client = self.snet_sdk.create_service_client(
org_id=org_id, service_id=service_id,
#group_name="default_group",
free_call_auth_token_bin=free_call_auth_token_bin,
free_call_token_expiry_block=free_call_token_expiry_block)
return ServiceCall(service_client)

def _unwrap_atom(self, atom):
if isinstance(atom, GroundedAtom):
return atom.get_object().content
return repr(atom)

def __call__(self, command_a, *args_a):
command = self._unwrap_atom(command_a)
args = []
kwargs = {}
try:
for arg_a in args_a:
if isinstance(arg_a, ExpressionAtom):
ch = arg_a.get_children()
k = ch[0].get_name()
v = self._unwrap_atom(ch[1])
kwargs[k] = v
else:
args += [self._unwrap_atom(arg_a)]
except:
return [E(S('Error'), E(S('snet-sdk'), command_a, *args_a),
ValueAtom(f'argument error'))]
if command == 'init':
self.init_sdk(*args, **kwargs)
return [E()]
if self.snet_sdk is None:
self.init_sdk()
if command == 'organization_list':
return list(map(lambda x: ValueAtom(x), self.organization_list()))
if command == 'service_list':
return list(map(lambda x: ValueAtom(x), self.service_list(*args, **kwargs)))
if command == 'create_service_client':
service_client = self.create_service_client(*args, **kwargs)
return [OperationAtom(service_client.get_service_details()[1], service_client)]
return [E(S('Error'), E(S('snet-sdk'), command_a, *args_a),
ValueAtom(f'unknown command {repr(command_a)}'))]

class ServiceCall:
def __init__(self, service_client):
self.service_client = service_client
def __call__(self, method, input_type, **kwargs):
return self.service_client.call_rpc(method, input_type, **kwargs)
def get_service_details(self):
return self.service_client.get_service_details()
def get_service_messages(self):
return self.service_client.get_services_and_messages_info()

def import_service(org_id, service_id,
private_key=os.getenv("SNET_PRIVATE_KEY"),
eth_rpc_endpoint=os.getenv("ETH_RPC_ENDPOINT"),
email=os.getenv("SNET_EMAIL", None),
free_call_auth_token_bin=os.getenv("FREE_CALL_AUTH_TOKEN_BIN", None),
free_call_token_expiry_block=os.getenv("FREE_CALL_TOKEN_EXPIRE_BLOCK", None)
):
if free_call_token_expiry_block is not None:
free_call_token_expiry_block = int(free_call_token_expiry_block)
config = {
"private_key": private_key,
"eth_rpc_endpoint": eth_rpc_endpoint,
"email": email,
"concurrency": False,
"identity_name": "hyperon",
"network": "mainnet",
"identity_type": "key",
"force_update": False
}
snet_sdk = sdk.SnetSDK(config)

service_client = snet_sdk.create_service_client(
org_id=org_id, service_id=service_id,
#group_name="default_group",
free_call_auth_token_bin=free_call_auth_token_bin,
free_call_token_expiry_block=free_call_token_expiry_block)
return ServiceCall(service_client)

@register_atoms()
def snet_atoms():
serviceAtom = OperationAtom("snet-service", import_service)
return { 'snet-service': serviceAtom }
defaultSDKAtom = OperationAtom("snet-sdk", SNetSDKWrapper(), unwrap=False)
# TODO: new-sdk-atom
return {
'snet-sdk': defaultSDKAtom,
}
28 changes: 0 additions & 28 deletions python/sandbox/snet/test_snet.metta

This file was deleted.

27 changes: 27 additions & 0 deletions python/sandbox/snet/test_snet_call.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
!(import! &self snet_io)

(include snet:naint:code-generation)
((py-dot (code-generation) get_service_messages))
; code-generation requires passing data in json format,
; so it will return a parse error
;!(generate "Write Hello World in C#")
; FIXME: the following is converted to a Symbol, not String
;!(generate "{\"query\": \"Write Hello World in C#\"}"))
; A hacky way, which works for some reason atm
(generate (repr {"query":"Write_Hello_World_in_C#"}))

; Doesn't work atm
(include snet:naint:image-generation)
((image-generation)
"Gen" "Text" (Kwargs (sentence "Hello World") (type False)))

(include snet:naint:generative-lms)
((generative-lms)
"generate" "Query" (Kwargs (request "Write Hello World in C#")))

(include snet:naint:abstractive-summarisation)
((abstractive-summarisation)
"neural_summarisation" "Query" (Kwargs (text "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ullamcorper vehicula augue. Curabitur maximus aliquet ex sed fringilla. Mauris id erat dolor.")))

(include snet:naint:text-generation)
(gen_gpt_2 "What was the largest dinosaur?" "universal" 0.5 2 100)
25 changes: 25 additions & 0 deletions python/sandbox/snet/test_snet_meta.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
!(import! &self snet_io)

; Only etherium endpoint is needed for retrieving metadata
; It should be either provided via init parameters or
; in the environment variable (ETH_RPC_ENDPOINT).
; In the latter case, it is not necessary to call `init`
; explicitly.
; !(snet-sdk init (eth_rpc_endpoint "YOUR ETH ENDPOINT"))

!(snet-sdk organization_list)

; Both positional and named arguments can be passed to snet-sdk
!(snet-sdk service_list (org_id "snet"))
!(snet-sdk service_list "naint")

; We can create a service client and get its details without
; using a secret key, opening a channel, or using free calls
!(let $service
(snet-sdk create_service_client "naint" "question-answering-long-seq")
((py-dot $service get_service_messages)))

; we use `include` here, so `snet-sdk` is accessible in the included file
!(include snet:naint:image-generation)
!((py-dot (image-generation) get_service_messages))

0 comments on commit 0ef9fa9

Please sign in to comment.