-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #763 from trueagi-io/snet_upd2
Snet upd2
- Loading branch information
Showing
10 changed files
with
161 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
;) | ||
))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
;) | ||
))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|