Skip to content

Commit

Permalink
agent
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Nov 6, 2024
1 parent 7952890 commit d4984a0
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 137 deletions.
11 changes: 8 additions & 3 deletions commune/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ def __init__(self,
argv = None,
base = 'module',
fn_splitters = [':', '/', '//', '::'],
helper_fns = ['code', 'schema', 'fn_schema', 'help', 'fn_info', 'fn_hash'],
helper_fns = ['code',
'schema',
'fn_schema',
'help',
'fn_info',
'fn_hash'],
sep = '--'
):

Expand All @@ -34,9 +39,9 @@ def forward(self, argv=None):
if arg.startswith(self.sep):
key = arg[len(self.sep):].split('=')[0]
if key in self.helper_fns:
# is it a helper function
return self.forward([key , argv[0]])
else:

value = arg.split('=')[-1] if '=' in arg else True
argv.remove(arg)
init_kwargs[key] = self.determine_type(value)
Expand Down Expand Up @@ -67,7 +72,7 @@ def forward(self, argv=None):
fn2module = module.fn2module()
if not fn in fn2module:
functions = c.get_functions(module)
return c.print(f'Function {fn} not found in {module}. Available functions are {functions}')
return c.print(f'FN({fn}) not found {module}', color='red')
module = c.module(fn2module[fn])


Expand Down
18 changes: 12 additions & 6 deletions commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class c:
lib_path = libpath = os.path.dirname(root_path) # the path to the library
repo_path = repopath = os.path.dirname(root_path) # the path to the repo
modules_path = os.path.dirname(__file__) + '/modules'
tests_path = f'{root_path}/tests'


cache = {} # cache for module objects
home = os.path.expanduser('~') # the home directory
Expand Down Expand Up @@ -306,9 +306,10 @@ def from_json(cls, json_str:str) -> 'Module':
@classmethod
def test_fns(cls, *args, **kwargs):
return [f for f in cls.functions(*args, **kwargs) if f.startswith('test_')]

tests_path = f'{libpath}/tests'
@classmethod
def pytest(cls, *args, **kwargs):

return c.cmd(f'pytest {c.tests_path}', stream=1, *args, **kwargs)

@classmethod
Expand Down Expand Up @@ -451,6 +452,11 @@ def utils(cls, search=None):
utils = [u for u in utils if search in u]
return sorted(utils)

@classmethod
def num_utils(cls, search=None):
return len(cls.utils(search))

cache = {}
@classmethod
def util2code(cls, search=None):
utils = cls.utils()
Expand Down Expand Up @@ -1417,7 +1423,7 @@ def get_fn(cls, fn:str, init_kwargs = None, splitters=splitters):
return getattr(module, fn_name)
if callable(fn):
return fn
raise ValueError(f'{fn} is not a function')
return fn

@classmethod
def self_functions(cls, search = None):
Expand Down Expand Up @@ -1882,9 +1888,9 @@ def lib_tree(cls, depth=10, **kwargs):
return c.get_tree(c.libpath, depth=depth, **kwargs)

@classmethod
def core_tree(cls, depth=10, **kwargs):
tree = c.get_tree(c.libpath, depth=depth, **kwargs)
return {k:v for k,v in tree.items() if '.modules.' not in v}
def core_tree(cls, **kwargs):
tree = c.get_tree(c.libpath, **kwargs)
return {k:v for k,v in tree.items() if 'modules.' not in v}
@classmethod
def local_tree(cls , depth=4, **kwargs):
return c.get_tree(c.pwd(), depth=depth, **kwargs)
Expand Down
109 changes: 109 additions & 0 deletions commune/modules/agent/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import commune as c
import json
import os
class Agent:
anchor="OUTPUT"

def ask(self, *args, path='./'):
text = self.args2text(args)
context = self.summarize(query=text, path=path)
prompt = f"""
{context}
AD START FINISH THE OUTPUT WITH THE ANCHOR TAGS
if you write a file so i can easily process it back
<{self.anchor}(path=filepath)></{self.anchor}(path=wherethefilepathwillbe)>
you are totally fine using ./ if you are refering to the pwd for brevity
"""
return c.ask(prompt)

def args2text(self, args):
return ' '.join(list(map(str, args)))

def get_context(self,
path='./',
query='what are the required packages',
instruction= "Which files are relevant to you?",
output_format="DICT(files:list[str])"):

c.print('FINDING RELEVANT FILES IN THE PATH {}'.format(path), color='green')
files = c.files(path)
prompt = f"""
QUERY \n {query} \n INSTRUCTION \n {instruction} \n CONTEXT {files} {query }
OUTPUT FORMAT
USE THE FOLLOWING FORMAT FOR OUTPUT WITH A JSON STRING IN THE CENTER
<{self.anchor}>{output_format})</{self.anchor}>
"""

output = ''
for ch in c.ask(prompt):
print(ch, end='')
output += ch
if ch == f'</{self.anchor}>':
break

files = json.loads(output.split('<' +self.anchor + '>')[1].split('</'+self.anchor + '>')[0])['files']
file2text = {c.get_text(f) for f in files}
return file2text

def score(self, *args, path='./'):
text = self.args2text(args)
context = self.get_context(text, path=path)
return c.ask(self.prompt.format(context=context, text=text))


def summary(self, path='./',
query = "get all of the important objects and a description",
anchor = 'OUTPUT',
max_ = 100000,
):

self.batch_context(path=path)
context = c.file2text(path)

prompt = f"""
INSTRUCTION
SUMMARIZE the info as a black hole condenses info
ensure to include all necessary info and discard
useless info. do it as such. use the query to condition
the tuples listed.
[head, relation, tail]
QUERY
{query}
OUTPUT FORMAT
USE THE FOLLOWING FORMAT FOR OUTPUT WITH A JSON STRING IN THE CENTER
store it in a relations
<{anchor}>
DICT(relations:list[str])
</{anchor}>
CONTEXT
{context}
"""
output = ''
for ch in c.ask(prompt):
print(ch, end='')
output += ch
if ch == f'</{self.anchor}>':
break
return json.loads(output.split('<' +self.anchor + '>')[1].split('</'+self.anchor + '>')[0])


def batch_context(self, path='./', batch_size=20000):

file2text = c.file2text(path)
file2size = {k:len(v) for k,v in file2text.items()}
current_size = 0
batch_list = []
files_batch = {}
for f, s in file2size.items():
if (current_size + s) > batch_size:
batch_list += [files_batch]
files_batch = {}
current_size = 0
current_size += s
files_batch[f] = c.get_text(path + f )
return batch_list


9 changes: 7 additions & 2 deletions commune/network/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,13 @@ def registration_signature(self, name='agi', address='0.0.0.0:8888', key=None):
def infos(self, timeout=10):
return c.wait([c.submit(c.call, [s + '/info']) for s in c.servers()], timeout=timeout)

def keys(self, timeout=10):
return c.wait([c.submit(c.call, [s + '/key_address']) for s in c.servers()], timeout=timeout)
def keys(self, max_age=60, update=False, timeout=10):
path = 'network_keys'
keys = c.get(path, max_age=max_age, update=update)
if keys == None:
keys = c.wait([c.submit(c.call, [s + '/key_address']) for s in c.servers()], timeout=timeout)
c.put(path, keys)
return keys
def infos(self, timeout=10):
return c.wait([c.submit(c.call, [s + '/info']) for s in c.servers()], timeout=timeout)

Expand Down
18 changes: 10 additions & 8 deletions commune/network/subspace/subspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1429,23 +1429,21 @@ def update_subnet(
ChainTransactionError: If the transaction fails.
"""
netuid = self.resolve_netuid(subnet)
subnet_params = self.subnet_params(netuid=netuid)
subnet_params = self.subnet_params(netuid=netuid, update=True)
# get subnet_key
address2key = c.address2key()
assert subnet_params['founder'] in address2key, f'No key found for {subnet_params["founder"]}'
key = c.get_key(address2key[subnet_params['founder']])
params = params or {}
params.update(extra_params)
params = {**(params or {}), **extra_params}
if 'founder' in params:
params['founder'] = self.resolve_key_address(params['founder'])

params = {**subnet_params, **params}
assert any([k in subnet_params for k in params.keys()]), f'Invalid params {params.keys()}'
params["netuid"] = netuid
governance_config = params.pop('governance_configuration', None)
params['vote_mode'] = governance_config['vote_mode']
params['vote_mode'] = params.pop('governance_configuration')['vote_mode']
params["metadata"] = params.pop("metadata", None)
response = self.compose_call(fn="update_subnet",params=params,key=key)
return response

return self.compose_call(fn="update_subnet",params=params,key=key)

def metadata(self) -> str:
netuids = self.netuids()
Expand Down Expand Up @@ -2719,6 +2717,7 @@ def subnet_params(self,
("MaxAllowedValidators", params),
("ModuleBurnConfig", params),
("SubnetMetadata", params),
("TrustRatio", params)
],
"GovernanceModule": [
("SubnetGovernanceConfig", params),
Expand Down Expand Up @@ -2749,6 +2748,7 @@ def subnet_params(self,
"min_validator_stake": bulk_query.get("MinValidatorStake", {}),
"max_allowed_validators": bulk_query.get("MaxAllowedValidators", {}),
"module_burn_config": bulk_query.get("ModuleBurnConfig", {}),
'trust_ratio': bulk_query.get("TrustRatio", {}),
"metadata": bulk_query.get("SubnetMetadata", {}),
}

Expand Down Expand Up @@ -2834,6 +2834,8 @@ def global_params(self, max_age=60, update=False) -> NetworkParams:
self.put(path, result)
return result

params = subnet_params

def clean_feature_name(self, x):
new_x = ''
for i, ch in enumerate(x):
Expand Down
5 changes: 1 addition & 4 deletions commune/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(
module.key_address = module.key.ss58_address
module.fn2cost = fn2cost or {}
module.schema = self.get_schema(module)
module.functions = module.server_functions = functions or list(set(helper_functions + list(module.schema.keys())))
module.functions = module.fns = module.server_functions = functions or list(set(helper_functions + list(module.schema.keys())))
module.info = module.server_info = self.get_info(module)
module.network_path = self.resolve_path(f'{self.network}/state.json')
module.users_path = users_path or self.resolve_path(f'{name}/users')
Expand Down Expand Up @@ -707,9 +707,6 @@ def processes(cls, search=None, **kwargs) -> List[str]:
for line in output_string.split('\n')[3:]:
if line.count('│') > 2:
name = line.split('│')[2].strip()
if 'errored' in line:
self.kill(name, verbose=True)
continue
module_list += [name]
if search != None:
module_list = [m for m in module_list if search in m]
Expand Down
Loading

0 comments on commit d4984a0

Please sign in to comment.