Skip to content

Commit

Permalink
snet_gate updated since new verion of snet-sdk released
Browse files Browse the repository at this point in the history
  • Loading branch information
Innokenty committed Oct 24, 2024
1 parent 1f0633d commit d4912b6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 79 deletions.
83 changes: 11 additions & 72 deletions python/hyperon/exts/snet_io/snet_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,13 @@ 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.email = email
config = sdk.config.Config(private_key=private_key,
eth_rpc_endpoint=eth_rpc_endpoint,
concurrency=concurrency,
force_update=force_update)
self.snet_sdk = sdk.SnetSDK(config)

def organization_list(self):
Expand All @@ -44,7 +36,7 @@ def create_service_client(self, org_id, service_id,
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",
email=self.email,
free_call_auth_token_bin=free_call_auth_token_bin,
free_call_token_expiry_block=free_call_token_expiry_block)
return ServiceCall(service_client)
Expand Down Expand Up @@ -103,60 +95,6 @@ def __call__(self, command_a, *args_a):
return [E(S('Error'), E(S('snet-sdk'), command_a, *args_a),
ValueAtom(f'unknown command {repr(command_a)}'))]

len_threshold = 50
current_len = 0
def pretty_print_atoms(input_atoms):

global len_threshold
global current_len
def process_svg_atom(atom):
global len_threshold
global current_len
repr_atom = repr(atom)
current_len += len(repr_atom)
return repr_atom

def check_len(depth):
global len_threshold
global current_len
if current_len > len_threshold:
current_len = 0
return "\n" + "\t" * (depth - 1)
else:
return ""

def process_atom(atom, depth):
global len_threshold
global current_len
process_res = ""
metatype = atom.get_metatype()
if metatype == AtomKind.EXPR:
len_to_last_eol_flag = current_len > 5
current_len *= (depth <= 1) * (not len_to_last_eol_flag)
process_res += ("\n" + "\t" * depth) * (
depth > 0) * len_to_last_eol_flag + f"({process_expr_atom(atom, depth + 1)})"
elif (metatype == AtomKind.SYMBOL) or (metatype == AtomKind.VARIABLE) or (metatype == AtomKind.GROUNDED):
process_res += process_svg_atom(atom) + check_len(depth)
else:
raise Exception(f"Unexpected type of the Atom: {str(metatype)}")
return process_res

def process_expr_atom(expr_atom, depth):
sub_atoms = expr_atom.get_children()
process_res = ""
for sub_atom in sub_atoms:
process_atom_res = process_atom(sub_atom, depth)
process_res += process_atom_res + check_len(depth)
process_res += " "
return process_res[:-1]

res_string = "(" * (not (input_atoms[0].get_metatype() == AtomKind.EXPR))
current_len = 0
for atom in input_atoms:
res_string += process_atom(atom, 0)
res_string += "\n\n"
current_len = 0
return res_string

class ServiceCall:

Expand Down Expand Up @@ -195,13 +133,14 @@ def get_service_messages(self):
def get_operation_atom(self):
return OperationAtom(self.service_details[1], self)

def generate_callers_text(self):
return pretty_print_atoms(self.generate_callers())

def open_channel_and_deposit(self, amount, expiration):
self.service_client.deposit_and_open_channel(amount, expiration)
return [E()]

def generate_callers_text(self):
# TODO: pretty print
return "\n".join([repr(e) for e in self.generate_callers()])

def _map_type(self, t):
type_map = {'bool': 'Bool',
'string': 'String',
Expand Down Expand Up @@ -245,4 +184,4 @@ def snet_atoms():
# TODO: new-sdk-atom
return {
'snet-sdk': defaultSDKAtom,
}
}
7 changes: 2 additions & 5 deletions python/hyperon/exts/snet_io/test_snet_call.metta
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(&image-generation
"Gen" "Text" (Kwargs (sentence "Hello World") (type False)))

(bind! &generative-lms
!(bind! &generative-lms
(snet-sdk create_service_client "naint" "generative-lms"))
; we can add a wrapper for the service
(add-reduct &self
Expand All @@ -45,7 +45,4 @@
(gen_gpt_2 "What was the largest dinosaur?" "universal" 0.5 2 100)

; possible usage of open_channel_and_deposit function. 100 is the amount of AGIX and 10000 is a payment channel's TTL in blocks
(snet-sdk open_channel_and_deposit &generative-lms 100 10000)

; usage of generate_callers_text method
((py-dot &generative-lms generate_callers_text))
(snet-sdk open_channel_and_deposit &generative-lms 100 10000)
3 changes: 1 addition & 2 deletions python/hyperon/exts/snet_io/test_snet_meta.metta
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@
; (naint punctuation-restoration) returns a service caller now
!((py-dot (naint punctuation-restoration) get_service_messages))
; we can also get a list of all service methods as typed metta functions
!(naint punctuation-restoration methods)

!(naint punctuation-restoration methods)

0 comments on commit d4912b6

Please sign in to comment.