Skip to content

Commit

Permalink
Merge pull request #767 from trueagi-io/experimental
Browse files Browse the repository at this point in the history
metta callers generation in snet sdk
  • Loading branch information
Necr0x0Der authored Sep 6, 2024
2 parents 233de8d + a34670f commit bcd1ab2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 46 deletions.
13 changes: 7 additions & 6 deletions python/sandbox/snet/snet/naint/code-generation.metta
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
;)
)))

(: generate (-> String String))
(= (generate $request)
((code-generation) "generate" "Query"
(Kwargs (request $request))
)
)
; This should be generated automatically
;(: generate (-> String String))
;(= (generate $request)
; ((code-generation) "generate" "Query"
; (Kwargs (request $request))
; )
;)
21 changes: 11 additions & 10 deletions python/sandbox/snet/snet/naint/text-generation.metta
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
(: top_k (-> Number int32))
(: length (-> Number int32))

(: gen_gpt_2 (-> String String Number Number Number String))
(= (gen_gpt_2 $start_text $run_name $temperature $top_k $length)
((text-generation) "gen_gpt_2" "Query"
(Kwargs (start_text $start_text)
(run_name $run_name)
(temperature $temperature)
(top_k $top_k)
(length $length))
)
)
; Should be generated automatically
;(: gen_gpt_2 (-> String String Number Number Number String))
;(= (gen_gpt_2 $start_text $run_name $temperature $top_k $length)
; ((text-generation) "gen_gpt_2" "Query"
; (Kwargs (start_text $start_text)
; (run_name $run_name)
; (temperature $temperature)
; (top_k $top_k)
; (length $length))
; )
;)

; A sketch of ontological relations
(:<proto AI:Text string)
Expand Down
69 changes: 42 additions & 27 deletions python/sandbox/snet/snet_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from hyperon.ext import register_atoms
import os
from snet import sdk
from hyperon import *

class SNetSDKWrapper:

Expand Down Expand Up @@ -52,8 +53,6 @@ def _unwrap_atom(self, atom):
return atom.get_object().content
return repr(atom)



def __call__(self, command_a, *args_a):
command = self._unwrap_atom(command_a)
args = []
Expand All @@ -75,6 +74,8 @@ def __call__(self, command_a, *args_a):
return [E()]
if self.snet_sdk is None:
self.init_sdk()
if command == 'get_service_callers':
return args[0].generate_callers()
if command == 'organization_list':
return list(map(lambda x: ValueAtom(x), self.organization_list()))
if command == 'service_list':
Expand All @@ -93,6 +94,7 @@ def __init__(self, service_client):
self.outputs = []
self.keys = []
self.func_name = ""
self.service_details = self.service_client.get_service_details()
for key in self.message_info[0]:
val = self.message_info[0][key]
self.func_name = val[0][0]
Expand All @@ -103,41 +105,54 @@ def __init__(self, service_client):

def __call__(self, method, input_type, **kwargs):
service_result = self.service_client.call_rpc(method, input_type, **kwargs)
res_tuple = ()
res_list = []
for output in self.outputs:
res_tuple+=getattr(service_result, output[1])
if (len(res_tuple) > 1):
return res_tuple
return res_tuple[0]
res_list += [getattr(service_result, output[1])]
if (len(res_list) > 1):
return res_list
return res_list[0]

def get_service_details(self):
return self.service_client.get_service_details()
return self.service_details

def get_service_messages(self):
return self.message_info

def generate_metta_launch_code(self):
service_id = self.get_service_details()[1]
metta_fun_type = f'(: {self.func_name} (-> '
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',
'int32': 'Number',
'float': 'Number'}
return type_map[t] if t in type_map else t

def generate_callers(self):
service_id = self.service_details[1]
metta_fun_type = []
metta_fun_type.extend([S(':'), S(f'{self.func_name}')])
type_symbols_in = []
type_symbols_out = []
fun_header = [S(f'{self.func_name}')]
kwargs = [S('Kwargs')]
for var_tuple in self.inputs:
metta_fun_type += f'{var_tuple[0].capitalize()} '
if (len(self.outputs) > 1):
metta_fun_type += "("
type_symbols_in.append(S(self._map_type(var_tuple[0])))
fun_header.append(V(f'{var_tuple[1]}'))
kwargs.append(E(S(f'{var_tuple[1]}'), V(f'{var_tuple[1]}')))
for var_tuple in self.outputs:
metta_fun_type += f'{var_tuple[0].capitalize()} '
type_symbols_out.append(S(self._map_type(var_tuple[0])))
if (len(self.outputs) > 1):
metta_fun_type = metta_fun_type[:-1] + ")"
metta_fun_type = metta_fun_type[:-1] + '))'
fun_header = f'({self.func_name} '
kwargs = '(Kwargs '
for input in self.inputs:
fun_header += f'${input[1]} '
kwargs += f'({input[1]} ${input[1]}) '
kwargs = kwargs[:-1] + ')'
fun_header = fun_header[:-1] + ')'
result = f'''\n{metta_fun_type}\n( = {fun_header}\n\t(({service_id}) "{self.func_name}" "{self.keys[0]}"\n\t{kwargs}\n\t)\n)'''
return result

metta_fun_type.extend([E(S('->'), *(type_symbols_in), E(*(type_symbols_out)))])
else:
metta_fun_type.extend([E(S('->'), *(type_symbols_in), *(type_symbols_out))])
metta_fun_type = E(*(metta_fun_type))
kwargs = E(*kwargs)
fun_header = E(*fun_header)
function_expr = E(S('='), fun_header,
E(E(S(service_id)), ValueAtom(self.func_name), ValueAtom(self.keys[0]), kwargs))
return [metta_fun_type, function_expr]

@register_atoms()
def snet_atoms():
Expand Down
9 changes: 7 additions & 2 deletions python/sandbox/snet/test_snet_call.metta
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

(include snet:naint:code-generation)
((py-dot (code-generation) get_service_messages))
; add a wrapper
(add-reduct &self
(snet-sdk get_service_callers (code-generation)))
; code-generation requires passing data in json format,
; so it will return a parse error
;!(generate "Write Hello World in C#")
Expand All @@ -23,5 +26,7 @@
((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)
!(include snet:naint:text-generation)
!(add-reduct &self
(snet-sdk get_service_callers (text-generation)))
!(gen_gpt_2 "What was the largest dinosaur?" "universal" 0.5 2 100)
3 changes: 2 additions & 1 deletion python/sandbox/snet/test_snet_meta.metta
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
; 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))

; turn service description in textual metta code
!((py-dot (image-generation) generate_callers_text))

0 comments on commit bcd1ab2

Please sign in to comment.