Skip to content

Commit

Permalink
fuck
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Sep 16, 2024
1 parent e8103ba commit 360c825
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 30 deletions.
11 changes: 6 additions & 5 deletions commune/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,18 @@ def forward(self, argv=None):
module_name = module.module_name()
fn_path = f'{module_name}/{fn}'
fn_obj = getattr(module, fn)
fn_class = c.classify_fn(fn_obj)
print(f'fn_class: {fn_class}')
if fn_class == 'self':
fn_obj = getattr(module(**init_kwargs), fn)


input_msg = f'[bold]fn[/bold]: {fn_path}'
if callable(fn_obj):
args, kwargs = self.parse_args(argv)
if len(args) > 0 or len(kwargs) > 0:
input_msg += ' ' + f'[purple][bold]params:[/bold] args:{args} kwargs:{kwargs}[/purple]'
try:
output = fn_obj(*args, **kwargs)
except Exception as e:
print('Error:, TRYING THE INITIALIZE THE MODULE AND CALL THE FUNCTION')
output = getattr(module(**init_kwargs), fn)(*args, **kwargs)
output = fn_obj(*args, **kwargs)
else:
output = fn_obj
self.input_msg = input_msg
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 22 additions & 4 deletions commune/docs/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@
import commune as c

class Docs(c.Module):
def generate(self, path=c.libpath):
def __init__(self,
model='model.openrouter',
system = 'Answer the question in a readme vibe with examples from the cli and python for user understandability',

):
self.model = c.module(model)()
self.system = system
print(f'Loaded model: {model}')


def generate(self, text='what is a module and how can I use it?',
path=c.libpath, stream=True):
context = self.get_context(path)
return self.generate(f'{context}')
len_context = len(context)
print(f'Found {len_context} files in {path}')
return self.model.generate(f'SYSTEM: {self.system} CONTEXT : {context} USER: {text}', stream=stream)

def get_context(self, path):
file2text = c.file2text(path)

file2text = c.file2text(path, avoid_folders=c.avoid_folders + [c.rootpath + '/modules'])
return file2text

def ask(self, *text, **kwargs):
text = ' '.join(map(str,text))
return self.generate(text, **kwargs)
41 changes: 20 additions & 21 deletions commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ def dirpath(cls) -> str:
dir_path = dirpath

@classmethod
def module_name(cls, obj=None):
obj = obj or cls
module_file = inspect.getfile(obj)
def module_name(cls):
module_file = inspect.getfile(cls)
return c.path2simple(module_file).lower()

@classmethod
Expand Down Expand Up @@ -1773,9 +1772,10 @@ def fn_code(cls,fn:str,
fn = cls.get_fn(fn)
code_text = inspect.getsource(fn)
text_lines = code_text.split('\n')
if 'classmethod' in text_lines[0] or 'staticmethod' in text_lines[0] or '@' in text_lines[0]:
if '@classmethod' in text_lines[0] or '@staticmethod' in text_lines[0] or '@' in text_lines[0]:
text_lines.pop(0)
fn_code = '\n'.join([l[len(' '):] for l in code_text.split('\n')])
print(fn_code)
assert 'def' in text_lines[0], 'Function not found in code'

if detail:
Expand All @@ -1786,7 +1786,7 @@ def fn_code(cls,fn:str,
'end_line': start_line + len(text_lines)
}
except Exception as e:
print(f'Error: {e}')
raise e
fn_code = None

return fn_code
Expand Down Expand Up @@ -2915,8 +2915,6 @@ def get_module(cls,
# ensure module
if verbose:
c.print(f'Loaded {path} in {c.time() - t0} seconds', color='green')
if not c.is_module(module):
setattr(module, 'module_name', lambda : module.__name__)
if init_kwargs != None:
module = module(**init_kwargs)
if cache:
Expand Down Expand Up @@ -3659,17 +3657,18 @@ def hash_modes(cls):
def set_api_key(self, api_key:str, cache:bool = True):
api_key = os.getenv(str(api_key), None)
if api_key == None:
api_key = self.get_api_key()
api_key = cls.get_api_key()
self.api_key = api_key
if cache:
self.add_api_key(api_key)
assert isinstance(api_key, str)
assert isinstance(api_key, str), f'api_key must be a string not {api_key}'

def get_api_keys(self):
return self.get(self.resolve_path('api_keys'), [])

def add_api_key(self, api_key:str, path=None):
@classmethod
def add_api_key(cls, api_key:str, path=None):
assert isinstance(api_key, str)
path = self.resolve_path(path or 'api_keys')
api_keys = self.get(path, [])
path = cls.resolve_path(path or 'api_keys')
api_keys = cls.get(path, [])
api_keys.append(api_key)
api_keys = list(set(api_keys))
self.put(path, api_keys)
Expand All @@ -3693,19 +3692,19 @@ def rm_api_key(self, api_key:str):

def get_api_key(self, module=None):
if module != None:
self = self.module(module)
self = c.module(module)
api_keys = self.api_keys()
if len(api_keys) == 0:
raise
else:
return self.choice(api_keys)

def api_keys(self):
return self.get(self.resolve_path('api_keys'), [])


def rm_api_keys(self):
self.put(self.resolve_path('api_keys'), [])
@classmethod
def api_keys(cls):
return cls.get(cls.resolve_path('api_keys'), [])
@classmethod
def rm_api_keys(cls):
cls.put(cls.resolve_path('api_keys'), [])
return {'api_keys': []}

thread_map = {}
Expand Down

0 comments on commit 360c825

Please sign in to comment.