Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Feb 2, 2025
1 parent b1579cc commit f514cd1
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 116 deletions.
2 changes: 1 addition & 1 deletion commune/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def get_fn(self, argv:list, init_kwargs:dict={}, default_fn:str='forward', defau
print(f'Calling({module}/{fn}, path={filepath})', color='yellow')
module = c.module(module)
if not hasattr(module, fn):
return {'error': f'module::{fn} does not exist', 'success': False}
return {'error': f'module/{fn} does not exist', 'success': False}
fn_obj = getattr(module, fn)
initialize_module_class = isinstance(fn, property) or 'self' in c.get_args(fn_obj)
module = module(**init_kwargs) if initialize_module_class else module
Expand Down
4 changes: 4 additions & 0 deletions commune/key/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ def register_key(cls, key):
def n(cls, search=None, **kwargs):
return len(cls.key2address(search, **kwargs))

@property
def addy(self):
return self.address

@classmethod
def address2key(cls, search:Optional[str]=None, update:bool=False):
address2key = { v: k for k,v in cls.key2address(update=update).items()}
Expand Down
125 changes: 94 additions & 31 deletions commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def obj2module(cls, obj:'Object', verbose=False):
module.code = lambda *args, **kwargs : c.code(module)
module.code_hash = lambda *args, **kwargs : c.code_hash(module)
module.schema = lambda *args, **kwargs : c.schema(module)
module.functions = module.fns = lambda *args, **kwargs : c.fns(module)
module.fns = module.fns = lambda *args, **kwargs : c.fns(module)
module.fn2code = lambda *args, **kwargs : c.fn2code(module)
module.fn2hash = lambda *args, **kwargs : c.fn2hash(module)
module.config = lambda *args, **kwargs : c.config(module)
Expand Down Expand Up @@ -195,11 +195,10 @@ def resolve_module(cls, obj:str = None, default=None, fn_splitter='/', **kwargs)
if fn_splitter in obj:
fn = obj.split(fn_splitter)[-1]
obj = fn_splitter.join(obj.split(fn_splitter)[:-1])
obj = getattr(obj(), fn)
else:
fn = None
obj = c.module(obj)
if fn != None:
return getattr(obj(), fn)
assert obj != None, f'Object {obj} does not exist'
return obj

Expand Down Expand Up @@ -281,19 +280,30 @@ def argv(cls, include_script:bool = False):
return args[1:]

@classmethod
def is_module_file(cls, module = None) -> bool:
if module != None:
cls = c.module(module)
dirpath = cls.dirpath()
filepath = cls.filepath()
def is_module_file(cls, module = None, exts=['py', 'rs', 'ts'], folder_filenames=['module', 'agent']) -> bool:
dirpath = c.dirpath(module)
filepath = c.filepath(module)
for ext in exts:
for fn in folder_filenames:
if filepath.endswith(f'/{fn}.{ext}'):
return False
return bool(dirpath.split('/')[-1] != filepath.split('/')[-1].split('.')[0])

@classmethod
def module2isfolder(cls):
module2isfolder = {}
for m in c.modules():
try:
module2isfolder[m] = c.is_module_folder(m)
except Exception as e:
pass
return module2isfolder



@classmethod
def is_module_folder(cls, module = None) -> bool:
if module != None:
cls = c.module(module)
return not cls.is_file_module()
return not c.is_module_file(module)

is_folder_module = is_module_folder

Expand Down Expand Up @@ -386,11 +396,7 @@ def resolve_key(self, key: str = None) -> str:
def is_pwd(cls, module:str = None):
module = c.module(module) if module != None else cls
return module.dirpath() == c.pwd()

def __repr__(self) -> str:
return f'<{self.class_name()}'
def __str__(self) -> str:
return f'<{self.class_name()}'


# local update
@classmethod
Expand Down Expand Up @@ -482,6 +488,7 @@ def fn2route(cls):
splitter = '/' if is_module else '/'
for fn in fns:
fn2route[fn] = module + splitter + fn
fn2route = dict(sorted({k: v for k, v in fn2route.items() if v != ''}.items(), key=lambda x: x[0]))
return fn2route

@classmethod
Expand Down Expand Up @@ -874,15 +881,17 @@ def get_parents(cls, obj = None,recursive=True, avoid_classes=['object']) -> Lis
def module_schema(cls, module = None):
module = cls.resolve_module(module)
module_schema = {}
for fn in c.functions(module):
for fn in c.fns(module):
schema = c.schema(getattr(module, fn))
module_schema[fn] = schema
return module_schema

fn2cost = {}



@classmethod
def schema(cls, fn:str = '__init__', **kwargs)->dict:
def fn_schema(cls, fn:str = '__init__', **kwargs)->dict:
'''
Get function schema of function in cls
'''
Expand All @@ -894,19 +903,30 @@ def schema(cls, fn:str = '__init__', **kwargs)->dict:

for k,v in dict(inspect.signature(fn)._parameters).items():
schema[k] = {
'default': "_empty" if v.default == inspect._empty else v.default,
'type': str(type(v.default)).split("'")[1] if v.default == inspect._empty and v.default != None else v.annotation.__name__
'value': "_empty" if v.default == inspect._empty else v.default,
'type': '_empty' if v.default == inspect._empty else str(type(v.default)).split("'")[1]
}
return schema


@classmethod
def schema(cls, fn:str = '__init__', **kwargs)->dict:
'''
Get function schema of function in cls
'''
fn2schema = {}
fns = c.fns(cls)
for fn in fns:
fn2schema[fn] = c.fn_schema(getattr(cls, fn))
return fn2schema

@classmethod
def code(cls, module = None, search=None, *args, **kwargs):
if module != None:
util2path = cls.util2path()
if module in util2path:
module = util2path[module]
obj = cls.resolve_module(module)

return inspect.getsource(obj)

pycode = code
Expand Down Expand Up @@ -955,7 +975,7 @@ def static_functions(cls: Union[str, type], obj=None):
Gets the self methods in a class
'''
obj = obj or cls
functions = c.functions(obj)
functions = c.fns(obj)
signature_map = {f:c.get_args(getattr(obj, f)) for f in functions}
return [k for k, v in signature_map.items() if not ('self' in v or 'cls' in v)]

Expand Down Expand Up @@ -1002,14 +1022,27 @@ def fns(cls,
functions = [f for f in functions if not f.startswith('__') and not f.startswith('_')]
return functions

@classmethod
def functions(cls, obj=None, search = None, include_parents = True):
obj = cls.resolve_module(obj)
return c.fns(obj=obj, search=search, include_parents=include_parents)
# @classmethod
# def fns(cls, obj=None, search = None, include_parents = True):
# obj = cls.resolve_module(obj)
# return c.fns(obj=obj, search=search, include_parents=include_parents)

def n_fns(self, search = None):
return len(self.fns(search=search))



@classmethod
def info(cls, obj=None, lite=False, key=None):
obj = cls.resolve_module(obj)
code = c.code(obj)
schema = c.schema(obj)
name = c.module_name(obj)
founder = c.founder().address
key = c.get_key(name).address
code_hash = c.hash(code)
info = {'code': code, 'schema': schema, 'name': name, 'key': key, 'founder': founder, 'code_hash': code_hash}

return info
fn_n = n_fns
@classmethod
def is_property(cls, fn: 'Callable') -> bool:
Expand Down Expand Up @@ -1427,6 +1460,8 @@ def module_exists(cls, module:str, **kwargs) -> bool:
'''
try:
module = c.shortcuts.get(module, module)
path = c.name2path(module)
print(path)
module_exists = os.path.exists(c.name2path(module))
if not module_exists:
module_exists = bool(c.object_exists(path))
Expand Down Expand Up @@ -1619,6 +1654,10 @@ def run_util(cls, util:str, *args, **kwargs):
def root_key(cls):
return cls.get_key()

@classmethod
def founder(cls):
return c.get_key()

def repo2path(self, search=None):
repo2path = {}
for p in c.ls('~/'):
Expand All @@ -1645,6 +1684,19 @@ def clone(self, repo:str = 'commune-ai/commune', path:str=None, **kwargs):
path = os.path.abspath(path or '~/'+repo.split('/')[-1])
cmd = f'git clone {repo} {path}'
return c.cmd(cmd, verbose=True)


def clone_modules(self, repo:str = 'commune-ai/modules', path:str=None, **kwargs):
c.clone(repo, path=path, **kwargs)
# remove the .git folder
c.rm(path + '/.git')
return c.cmd(cmd, verbose=True)

def save_modules(self, repo:str = 'commune-ai/modules', path:str=None, **kwargs):
c.clone(repo, path=path, **kwargs)
# remove the .git folder
c.rm(path + '/.git')
return c.cmd(cmd, verbose=True)

def copy_module(self,module:str, path:str):
code = c.code(module)
Expand All @@ -1667,18 +1719,29 @@ def has_module(self, path:str):

@classmethod
def module2fns(cls, path=None):
path = path or cls.dirpath()
tree = c.get_tree(path)

tree = c.get_tree(path or cls.libpath)
module2fns = {}
for m,m_path in tree.items():
if '.modules.' in m_path:
continue
try:
module2fns[m] = c.module(m).fns()
except Exception as e:
pass
return module2fns


@classmethod
def module2schema(cls, path=None):
tree = c.get_tree(path or cls.libpath)
module2fns = {}
for m,m_path in tree.items():
try:
module2fns[m] = c.schema(m)
except Exception as e:
print(e)
pass
return module2fns

def module2code(self, search=None, update=False, max_age=60, **kwargs):
module2code = {}
module2code = c.get('module2code', None, max_age=max_age, update=update)
Expand Down
2 changes: 1 addition & 1 deletion commune/server/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def forward(self,
if self.module.free:
return True
stake = 0
assert fn in self.module.functions , f"Function {fn} not in endpoints={self.module.functions}"
assert fn in self.module.fns , f"Function {fn} not in endpoints={self.module.fns}"
request_staleness = c.time() - float(headers['time'])
assert request_staleness < max_request_staleness, f"Request is too old ({request_staleness}s > {max_request_staleness}s (MAX)"
auth = {'params': params, 'time': str(headers['time'])}
Expand Down
8 changes: 4 additions & 4 deletions commune/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,19 @@ def set_functions(self, functions:Optional[List[str]] ):
functions[i] = fn.__name__
function_attributes = [fa for fa in self.function_attributes if hasattr(self.module, fa) and isinstance(getattr(self.module, fa), list)]
assert len(function_attributes) == 1 , f'{function_attributes} is too many funcitonal attributes, choose one dog'
functions = getattr(self.module, function_attributes[0])
fns = getattr(self.module, function_attributes[0])
self.module.schema = {fn: c.schema(getattr(self.module, fn )) for fn in functions if hasattr(self.module, fn)}
self.module.free = self.free
self.module.functions = sorted(list(set(functions + self.helper_functions)))
self.module.fns = sorted(list(set(functions + self.helper_functions)))
self.module.fn2cost = self.module.fn2cost if hasattr(self.module, 'fn2cost') else {}
c.print(f'Functions({self.module.functions} fn2cost={self.module.fn2cost} free={self.free})')
c.print(f'Functions({self.module.fns} fn2cost={self.module.fn2cost} free={self.free})')
assert isinstance(self.module.fn2cost, dict), f'fn2cost must be a dict, not {type(self.module.fn2cost)}'
self.module.info = {
"name": self.module.name,
"url": self.module.url,
"key": self.module.key.ss58_address,
"time": c.time(),
"functions": self.module.functions,
"fns": self.module.fns,
"schema": self.module.schema,
}
return {'success':True, 'message':f'Set functions to {functions}'}
Expand Down
47 changes: 38 additions & 9 deletions modules/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def __init__(self,
self.prompt = prompt
self.model = c.module('model.openrouter')(model=model, **kwargs)

def generate(self, text = 'whats 2+2?' , model= 'anthropic/claude-3.5-sonnet', temperature= 0.5, max_tokens= 1000000, stream=True ):
text = self.process_text(text)
def generate(self, text = 'whats 2+2?' , model= 'c s', temperature= 0.5, max_tokens= 1000000, stream=True , process_text=True):
text = self.process_text(text) if process_text else text
return self.model.generate(text, stream=stream, model=model, max_tokens=max_tokens,temperature=temperature )

forward = generate
Expand All @@ -35,7 +35,7 @@ def edit(self, *args, file='./',**kwargs):
"""
return self.ask(prompt, **kwargs)

def executor(self, *text, path='./', **kwargs):
def exe(self, *text, path='./', **kwargs):
text = ' '.join(list(map(str, text)))
prompt = f"""
GOAL
Expand Down Expand Up @@ -192,9 +192,8 @@ def score(self, module:str, **kwargs):
you suggest in the feedback
CODE:
{code}
OUTPUT FORMAT ONLY BETWEEN THE TAGS SO WE CAN PARSE
<OUTPUT>INT(score=INT, feedback:STR, suggestions=List[dict(improvement:STR, delta:INT)]])</OUTPUT>
OUTPUT_FORMAT:
<OUTPUT>DICT(score:int, feedback:str, suggestions=List[dict(improvement:str, delta:int)]])</OUTPUT>
"""
output = ''
for ch in self.generate(prompt, **kwargs):
Expand All @@ -203,7 +202,37 @@ def score(self, module:str, **kwargs):
if '</OUTPUT>' in output:
break
return json.loads(output.split('<OUTPUT>')[1].split('</OUTPUT>')[0])

# def find_fns(self):
# fns = []

def resolve_path(self, path):
return os.path.abspath(c.storage_dir() + '/' + 'agent/' + path)



def addkey(self, module, key):
return c.module('apikey')().add(key)


def desc(self, module, max_age=0):
code= c.code(module)
code_hash = c.hash(code)
path = self.resolve_path(f'summary/{module}.json')
output = c.get(path, max_age=max_age)
if output != None:
return output

prompt = {
"task": "summarize the following into tupples and make sure you compress as much as oyu can",
"code": code,
"hash": code_hash,
}
output = ''
for ch in self.generate(str(prompt), process_text=False):
output += ch
print(ch, end='')
c.put(path, output)
print('Writing to path -->', path)
return output


def api_key(self, module):
return c.module('apikey')(module=module).get_key()
2 changes: 1 addition & 1 deletion modules/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def start(self,
module = module + '.app'
module_class = c.module(module)
cmd = cmd or f'streamlit run {module_class.filepath()} --server.port {port}'
return cmd
return c.cmd(cmd)

start_app = app = start

Expand Down
Loading

0 comments on commit f514cd1

Please sign in to comment.