From 775c5f82fecee85d459779fbe54ab0feeff06910 Mon Sep 17 00:00:00 2001 From: latentvector Date: Sat, 21 Sep 2024 14:53:10 -0400 Subject: [PATCH] refactor --- commune/cli.py | 90 +++--- commune/client.py | 3 + commune/key.py | 84 +----- commune/module.py | 67 +---- commune/modules/chat/chat.py | 259 ------------------ commune/modules/chat/history.py | 73 ----- commune/namespace.py | 254 +++++------------ commune/pm2.py | 2 - commune/routes.yaml | 7 +- .../serializer/{__init__.py => serializer.py} | 20 +- commune/server.py | 35 +-- commune/subspace.py | 27 +- commune/vali.py | 16 +- {commune/docs => docs}/0_install_commune.md | 0 {commune/docs => docs}/1_what_is_a_module.md | 0 .../docs => docs}/2_key_identity_system.md | 0 {commune/docs => docs}/3_pythonic_cli.md | 0 {commune/docs => docs}/4_servering_modules.md | 0 {commune/docs => docs}/5_making_apps.md | 0 {commune/docs => docs}/6_offchain_networks.md | 0 .../docs => docs}/8_avoiding_recursion.md | 0 {commune/docs => docs}/docs.py | 19 +- {commune/docs => docs}/intro.md | 0 {commune/docs => docs}/modules/app.md | 0 {commune/docs => docs}/modules/key.md | 0 {commune/docs => docs}/modules/serializer.md | 0 .../client.md => docs/modules/server.md | 0 {commune/docs => docs}/modules/vali.md | 0 {commune/modules => modules}/agent/agent.py | 0 {commune/modules => modules}/agent/app.py | 0 {commune/modules => modules}/agent/child.py | 0 .../agent/data/agent_data.py | 0 .../agent/factory/agent_factory.py | 0 {commune/modules => modules}/agent/judge.py | 0 .../agent/maker/agent_maker.py | 0 .../agent/sumarizer/agent_sumarizer.py | 0 {commune/modules => modules}/api/api.py | 0 {commune/modules => modules}/api/app.py | 0 {commune => modules/app}/app.py | 0 {commune/modules => modules}/base/base.py | 0 {commune/modules => modules}/chat/app.py | 0 modules/chat/chat.py | 52 ++++ modules/chat/history.py | 61 +++++ {commune/modules => modules}/code/code.py | 0 {commune/modules => modules}/data/__init__.py | 0 {commune/modules => modules}/data/data.py | 0 {commune/modules => modules}/data/data.yaml | 0 .../data/diffusion/dream/dream_dataset.py | 0 .../data/diffusion/dream/prompt_dataset.py | 0 .../modules => modules}/data/hf/data_hf.py | 0 .../data/hf/data_hf_docs.md | 0 .../data/image/globe/data_image_globe.py | 0 .../data/image/globe/data_image_globe.yaml | 0 .../data/text/code/data_text_code.py | 0 .../data/text/code/data_text_code.yaml | 0 .../data/text/folder/data_text_folder.py | 0 .../data/text/folder/data_text_folder.yaml | 0 .../folder/docs/data_text_realfake_docs.md | 0 .../data/text/math/data_text_math.py | 0 .../data/text/math/data_text_math.yaml | 0 .../data/text/pile/pile.py | 0 .../data/text/pile/pile.yaml | 0 .../data/text/realfake/data_text_realfake.py | 0 .../text/realfake/data_text_realfake.yaml | 0 .../realfake/docs/data_text_realfake_docs.md | 0 .../modules => modules}/data/text/squad.py | 0 .../data/text/truthqa/data_text_truthqa.py | 0 .../data/text/truthqa/data_text_truthqa.yaml | 0 {commune/modules => modules}/docker/docker.py | 0 {commune/modules => modules}/emoji/emoji.py | 0 {commune/modules => modules}/git/git.py | 0 {commune/modules => modules}/key/app/app.py | 0 .../modules => modules}/key/app/style.css | 0 .../modules => modules}/model/__init__.py | 0 .../modules => modules}/model/diffusion.py | 0 {commune/modules => modules}/model/layer.py | 0 {commune/modules => modules}/model/model.py | 0 {commune/modules => modules}/model/openai.py | 0 .../modules => modules}/model/openrouter.py | 23 +- .../modules => modules}/model/sentence.py | 0 .../modules => modules}/model/transformer.py | 0 {commune/modules => modules}/model/utils.py | 0 {commune/modules => modules}/plotly/plotly.py | 0 {commune/modules => modules}/process/pipe.py | 0 {commune/modules => modules}/process/pool.py | 0 .../modules => modules}/process/process.py | 0 {commune/modules => modules}/process/utils.py | 0 .../ray/__init__.py => modules/pyenv/pyenv.py | 0 .../modules/tool => modules/ray}/__init__.py | 0 .../modules => modules}/ray/actor_pool.py | 0 .../ray/client/ray_client.py | 0 .../ray/client/ray_client.yaml | 0 {commune/modules => modules}/ray/queue.py | 0 {commune/modules => modules}/ray/ray.py | 0 .../ray/server/object/ray_object_server.py | 0 .../ray/server/object/ray_object_server.yaml | 0 .../ray/server/queue/ray_queue_server.py | 0 .../ray/server/queue/ray_queue_server.yaml | 0 .../ray/server/redis/ray_redis_server.py | 0 .../ray/server/redis/ray_redis_server.yaml | 0 {commune/modules => modules}/ray/utils.py | 0 .../modules => modules}/remote/.gitignore | 0 {commune/modules => modules}/remote/README.md | 0 {commune/modules => modules}/remote/app.py | 0 .../modules => modules}/remote/data/.gitkeep | 0 {commune/modules => modules}/remote/remote.py | 0 {commune/modules => modules}/remote/ssh.py | 0 {commune/modules => modules}/repo/.gitignore | 0 {commune/modules => modules}/repo/repo.py | 0 .../router/dashboard/router_dashboard.py | 0 {commune/modules => modules}/router/ex.py | 0 {commune/modules => modules}/router/router.py | 0 .../modules => modules}/router/router_v0.py | 0 {commune/modules => modules}/router/task.py | 0 {commune/modules => modules}/router/worker.py | 0 {commune/modules => modules}/sandbox.py | 0 {commune/modules => modules}/ssh.py | 0 .../modules => modules}/storage/storage.py | 0 {commune/modules => modules}/storage/vali.py | 0 .../storage/vector/vector_store.py | 0 .../storage/vector/vector_store.yaml | 0 .../modules => modules}/streamlit/__init__.py | 0 .../streamlit/auth/config_template.yaml | 0 .../streamlit/auth/streamlit_auth.py | 0 .../modules => modules}/streamlit/plot.py | 0 .../streamlit/streamlit.py | 0 .../streamlit/styles/commune.css | 0 .../modules => modules}/streamlit/utils.py | 0 .../streamlit/watchdog/streamlit_watchdog.py | 0 .../watchdog/streamlit_watchdog.yaml | 0 modules/template.py | 22 ++ {commune/modules => modules}/test.py | 0 .../tool/defi => modules/tool}/__init__.py | 0 .../tool/compare_token_price.py | 0 {commune/modules => modules}/tool/compound.py | 0 .../tool/defi/__init__.py | 0 .../modules => modules}/tool/defi/aave.py | 0 .../tool/defi/compare_token_price.py | 0 .../modules => modules}/tool/defi/compound.py | 0 .../tool/defi/defillama/aave.py | 0 .../tool/defi/defillama/defillama.py | 0 .../tool/defi/defillama/lido.py | 0 .../tool/defi/defillama/rocketpool.py | 0 .../tool/defi/get_best_apy.py | 0 .../tool/defi/inch/balances.py | 0 .../tool/defi/inch/gasprice.py | 0 .../tool/defi/inch/inch.py | 0 .../tool/defi/inch/prices.py | 0 .../tool/defi/openai_helper.py | 0 .../tool/defi/read_file.py | 0 .../modules => modules}/tool/defi/swap.py | 0 .../modules => modules}/tool/defi/tool.py | 0 .../tool/defi/write_file.py | 0 .../modules => modules}/tool/get_best_apy.py | 0 .../modules => modules}/tool/openai_helper.py | 0 .../modules => modules}/tool/read_file.py | 0 {commune/modules => modules}/tool/registry.py | 0 .../tool/search/tool_search.py | 0 {commune/modules => modules}/tool/swap.py | 0 {commune/modules => modules}/tool/tool.py | 0 {commune/modules => modules}/tool/web/web.py | 0 .../modules => modules}/tool/web/web.yaml | 0 .../modules => modules}/tool/write_file.py | 0 .../modules => modules}/trainer/__init__.py | 0 .../modules => modules}/trainer/trainer.py | 0 {commune/modules => modules}/web/web.py | 0 {commune/modules => modules}/web/web.yaml | 0 {commune/tests => tests}/README.md | 0 {commune/tests => tests}/test_key.py | 0 {commune/tests => tests}/test_serializer.py | 0 {commune/tests => tests}/test_server.py | 21 +- {commune/tests => tests}/test_subspace.py | 0 {commune/tests => tests}/test_user.py | 0 {commune/tests => tests}/test_validator.py | 0 174 files changed, 320 insertions(+), 815 deletions(-) delete mode 100644 commune/modules/chat/chat.py delete mode 100644 commune/modules/chat/history.py rename commune/serializer/{__init__.py => serializer.py} (84%) rename {commune/docs => docs}/0_install_commune.md (100%) rename {commune/docs => docs}/1_what_is_a_module.md (100%) rename {commune/docs => docs}/2_key_identity_system.md (100%) rename {commune/docs => docs}/3_pythonic_cli.md (100%) rename {commune/docs => docs}/4_servering_modules.md (100%) rename {commune/docs => docs}/5_making_apps.md (100%) rename {commune/docs => docs}/6_offchain_networks.md (100%) rename {commune/docs => docs}/8_avoiding_recursion.md (100%) rename {commune/docs => docs}/docs.py (56%) rename {commune/docs => docs}/intro.md (100%) rename {commune/docs => docs}/modules/app.md (100%) rename {commune/docs => docs}/modules/key.md (100%) rename {commune/docs => docs}/modules/serializer.md (100%) rename commune/docs/modules/client.md => docs/modules/server.md (100%) rename {commune/docs => docs}/modules/vali.md (100%) rename {commune/modules => modules}/agent/agent.py (100%) rename {commune/modules => modules}/agent/app.py (100%) rename {commune/modules => modules}/agent/child.py (100%) rename {commune/modules => modules}/agent/data/agent_data.py (100%) rename {commune/modules => modules}/agent/factory/agent_factory.py (100%) rename {commune/modules => modules}/agent/judge.py (100%) rename {commune/modules => modules}/agent/maker/agent_maker.py (100%) rename {commune/modules => modules}/agent/sumarizer/agent_sumarizer.py (100%) rename {commune/modules => modules}/api/api.py (100%) rename {commune/modules => modules}/api/app.py (100%) rename {commune => modules/app}/app.py (100%) rename {commune/modules => modules}/base/base.py (100%) rename {commune/modules => modules}/chat/app.py (100%) create mode 100644 modules/chat/chat.py create mode 100644 modules/chat/history.py rename {commune/modules => modules}/code/code.py (100%) rename {commune/modules => modules}/data/__init__.py (100%) rename {commune/modules => modules}/data/data.py (100%) rename {commune/modules => modules}/data/data.yaml (100%) rename {commune/modules => modules}/data/diffusion/dream/dream_dataset.py (100%) rename {commune/modules => modules}/data/diffusion/dream/prompt_dataset.py (100%) rename {commune/modules => modules}/data/hf/data_hf.py (100%) rename {commune/modules => modules}/data/hf/data_hf_docs.md (100%) rename {commune/modules => modules}/data/image/globe/data_image_globe.py (100%) rename {commune/modules => modules}/data/image/globe/data_image_globe.yaml (100%) rename {commune/modules => modules}/data/text/code/data_text_code.py (100%) rename {commune/modules => modules}/data/text/code/data_text_code.yaml (100%) rename {commune/modules => modules}/data/text/folder/data_text_folder.py (100%) rename {commune/modules => modules}/data/text/folder/data_text_folder.yaml (100%) rename {commune/modules => modules}/data/text/folder/docs/data_text_realfake_docs.md (100%) rename {commune/modules => modules}/data/text/math/data_text_math.py (100%) rename {commune/modules => modules}/data/text/math/data_text_math.yaml (100%) rename {commune/modules => modules}/data/text/pile/pile.py (100%) rename {commune/modules => modules}/data/text/pile/pile.yaml (100%) rename {commune/modules => modules}/data/text/realfake/data_text_realfake.py (100%) rename {commune/modules => modules}/data/text/realfake/data_text_realfake.yaml (100%) rename {commune/modules => modules}/data/text/realfake/docs/data_text_realfake_docs.md (100%) rename {commune/modules => modules}/data/text/squad.py (100%) rename {commune/modules => modules}/data/text/truthqa/data_text_truthqa.py (100%) rename {commune/modules => modules}/data/text/truthqa/data_text_truthqa.yaml (100%) rename {commune/modules => modules}/docker/docker.py (100%) rename {commune/modules => modules}/emoji/emoji.py (100%) rename {commune/modules => modules}/git/git.py (100%) rename {commune/modules => modules}/key/app/app.py (100%) rename {commune/modules => modules}/key/app/style.css (100%) rename {commune/modules => modules}/model/__init__.py (100%) rename {commune/modules => modules}/model/diffusion.py (100%) rename {commune/modules => modules}/model/layer.py (100%) rename {commune/modules => modules}/model/model.py (100%) rename {commune/modules => modules}/model/openai.py (100%) rename {commune/modules => modules}/model/openrouter.py (86%) rename {commune/modules => modules}/model/sentence.py (100%) rename {commune/modules => modules}/model/transformer.py (100%) rename {commune/modules => modules}/model/utils.py (100%) rename {commune/modules => modules}/plotly/plotly.py (100%) rename {commune/modules => modules}/process/pipe.py (100%) rename {commune/modules => modules}/process/pool.py (100%) rename {commune/modules => modules}/process/process.py (100%) rename {commune/modules => modules}/process/utils.py (100%) rename commune/modules/ray/__init__.py => modules/pyenv/pyenv.py (100%) mode change 100755 => 100644 rename {commune/modules/tool => modules/ray}/__init__.py (100%) mode change 100644 => 100755 rename {commune/modules => modules}/ray/actor_pool.py (100%) rename {commune/modules => modules}/ray/client/ray_client.py (100%) rename {commune/modules => modules}/ray/client/ray_client.yaml (100%) rename {commune/modules => modules}/ray/queue.py (100%) rename {commune/modules => modules}/ray/ray.py (100%) rename {commune/modules => modules}/ray/server/object/ray_object_server.py (100%) rename {commune/modules => modules}/ray/server/object/ray_object_server.yaml (100%) rename {commune/modules => modules}/ray/server/queue/ray_queue_server.py (100%) rename {commune/modules => modules}/ray/server/queue/ray_queue_server.yaml (100%) rename {commune/modules => modules}/ray/server/redis/ray_redis_server.py (100%) rename {commune/modules => modules}/ray/server/redis/ray_redis_server.yaml (100%) rename {commune/modules => modules}/ray/utils.py (100%) rename {commune/modules => modules}/remote/.gitignore (100%) rename {commune/modules => modules}/remote/README.md (100%) rename {commune/modules => modules}/remote/app.py (100%) rename {commune/modules => modules}/remote/data/.gitkeep (100%) rename {commune/modules => modules}/remote/remote.py (100%) rename {commune/modules => modules}/remote/ssh.py (100%) rename {commune/modules => modules}/repo/.gitignore (100%) rename {commune/modules => modules}/repo/repo.py (100%) rename {commune/modules => modules}/router/dashboard/router_dashboard.py (100%) rename {commune/modules => modules}/router/ex.py (100%) rename {commune/modules => modules}/router/router.py (100%) rename {commune/modules => modules}/router/router_v0.py (100%) rename {commune/modules => modules}/router/task.py (100%) rename {commune/modules => modules}/router/worker.py (100%) rename {commune/modules => modules}/sandbox.py (100%) rename {commune/modules => modules}/ssh.py (100%) rename {commune/modules => modules}/storage/storage.py (100%) rename {commune/modules => modules}/storage/vali.py (100%) rename {commune/modules => modules}/storage/vector/vector_store.py (100%) rename {commune/modules => modules}/storage/vector/vector_store.yaml (100%) rename {commune/modules => modules}/streamlit/__init__.py (100%) rename {commune/modules => modules}/streamlit/auth/config_template.yaml (100%) rename {commune/modules => modules}/streamlit/auth/streamlit_auth.py (100%) rename {commune/modules => modules}/streamlit/plot.py (100%) rename {commune/modules => modules}/streamlit/streamlit.py (100%) rename {commune/modules => modules}/streamlit/styles/commune.css (100%) rename {commune/modules => modules}/streamlit/utils.py (100%) rename {commune/modules => modules}/streamlit/watchdog/streamlit_watchdog.py (100%) rename {commune/modules => modules}/streamlit/watchdog/streamlit_watchdog.yaml (100%) create mode 100644 modules/template.py rename {commune/modules => modules}/test.py (100%) rename {commune/modules/tool/defi => modules/tool}/__init__.py (100%) rename {commune/modules => modules}/tool/compare_token_price.py (100%) rename {commune/modules => modules}/tool/compound.py (100%) rename commune/urls.yaml => modules/tool/defi/__init__.py (100%) rename {commune/modules => modules}/tool/defi/aave.py (100%) rename {commune/modules => modules}/tool/defi/compare_token_price.py (100%) rename {commune/modules => modules}/tool/defi/compound.py (100%) rename {commune/modules => modules}/tool/defi/defillama/aave.py (100%) rename {commune/modules => modules}/tool/defi/defillama/defillama.py (100%) rename {commune/modules => modules}/tool/defi/defillama/lido.py (100%) rename {commune/modules => modules}/tool/defi/defillama/rocketpool.py (100%) rename {commune/modules => modules}/tool/defi/get_best_apy.py (100%) rename {commune/modules => modules}/tool/defi/inch/balances.py (100%) rename {commune/modules => modules}/tool/defi/inch/gasprice.py (100%) rename {commune/modules => modules}/tool/defi/inch/inch.py (100%) rename {commune/modules => modules}/tool/defi/inch/prices.py (100%) rename {commune/modules => modules}/tool/defi/openai_helper.py (100%) rename {commune/modules => modules}/tool/defi/read_file.py (100%) rename {commune/modules => modules}/tool/defi/swap.py (100%) rename {commune/modules => modules}/tool/defi/tool.py (100%) rename {commune/modules => modules}/tool/defi/write_file.py (100%) rename {commune/modules => modules}/tool/get_best_apy.py (100%) rename {commune/modules => modules}/tool/openai_helper.py (100%) rename {commune/modules => modules}/tool/read_file.py (100%) rename {commune/modules => modules}/tool/registry.py (100%) rename {commune/modules => modules}/tool/search/tool_search.py (100%) rename {commune/modules => modules}/tool/swap.py (100%) rename {commune/modules => modules}/tool/tool.py (100%) rename {commune/modules => modules}/tool/web/web.py (100%) rename {commune/modules => modules}/tool/web/web.yaml (100%) rename {commune/modules => modules}/tool/write_file.py (100%) rename {commune/modules => modules}/trainer/__init__.py (100%) rename {commune/modules => modules}/trainer/trainer.py (100%) rename {commune/modules => modules}/web/web.py (100%) rename {commune/modules => modules}/web/web.yaml (100%) rename {commune/tests => tests}/README.md (100%) rename {commune/tests => tests}/test_key.py (100%) rename {commune/tests => tests}/test_serializer.py (100%) rename {commune/tests => tests}/test_server.py (76%) rename {commune/tests => tests}/test_subspace.py (100%) rename {commune/tests => tests}/test_user.py (100%) rename {commune/tests => tests}/test_validator.py (100%) diff --git a/commune/cli.py b/commune/cli.py index c8f660da..8abc1c5b 100644 --- a/commune/cli.py +++ b/commune/cli.py @@ -1,33 +1,15 @@ import commune as c -import json import sys import time -import os -import threading import sys - -class cli: +class Cli: """ Create and init the CLI class, which handles the coldkey, hotkey and tao transfer """ - # - - def __init__(self, - args = None, - module = 'module', - verbose = True, - forget_fns = ['module.key_info', 'module.save_keys'], - seperator = ' ', - buffer_size=4, - save: bool = False): + def __init__(self, module = 'module', args = None): - self.seperator = seperator - self.buffer_size = buffer_size - self.verbose = verbose - self.save = save - self.forget_fns = forget_fns self.base_module = c.module(module) if isinstance(module, str) else module self.base_module_attributes = list(set(self.base_module.functions() + self.base_module.attributes())) self.forward(args) @@ -35,7 +17,7 @@ def __init__(self, def forward(self, argv=None): t0 = time.time() argv = argv or self.argv() - self.input_msg = 'c ' + ' '.join(argv) + input_msg =' '.join(argv) output = None init_kwargs = {} if any([arg.startswith('--') for arg in argv]): @@ -51,14 +33,12 @@ def forward(self, argv=None): if '=' not in arg: value = True value = arg.split('=')[1] - init_kwargs[key] = self.determine_type(value) + init_kwargs[key] = self.get_value(value) + # any of the --flags are init kwargs - if argv[0].endswith('.py'): - argv[0] = argv[0][:-3] - if ':' in argv[0]: - # {module}:{fn} arg1 arg2 arg3 ... argn + if ':' in argv[0]: # {module}:{fn} arg1 arg2 arg3 ... argn argv[0] = argv[0].replace(':', '/') - if '/' in argv[0]: + if '/' in argv[0]: # {module}/{fn} arg1 arg2 arg3 ... argn # prioritize the module over the function module = '.'.join(argv[0].split('/')[:-1]) fn = argv[0].split('/')[-1] @@ -66,24 +46,29 @@ def forward(self, argv=None): is_fn = False else: is_fn = argv[0] in self.base_module_attributes + if is_fn: module = self.base_module fn = argv.pop(0) else: module = argv.pop(0) fn = argv.pop(0) + if isinstance(module, str): module = c.get_module(module) + 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: @@ -91,7 +76,7 @@ def forward(self, argv=None): output = fn_obj(*args, **kwargs) else: output = fn_obj - self.input_msg = input_msg + buffer = '⚡️'*4 c.print(buffer+input_msg+buffer, color='yellow') latency = time.time() - t0 @@ -105,16 +90,13 @@ def forward(self, argv=None): msg = f'Result(latency={latency:.3f})' print(buffer + msg + buffer) - - num_spacers = max(0, len(self.input_msg) - len(msg) ) + num_spacers = max(0, len(input_msg) - len(msg) ) left_spacers = num_spacers//2 right_spacers = num_spacers - left_spacers - msg = self.seperator*left_spacers + msg + self.seperator*right_spacers - buffer = self.buffer_size * buffer + seperator = ' ' + msg = seperator*left_spacers + msg + seperator*right_spacers is_generator = c.is_generator(output) - if is_generator: - # print the items side by side instead of vertically for item in output: if isinstance(item, dict): c.print(item) @@ -122,17 +104,11 @@ def forward(self, argv=None): c.print(item, end='') else: c.print(output) - return output - - # c.print( f'Result ✅ (latency={self.latency:.2f}) seconds ✅') - - @classmethod def is_property(cls, obj): return isinstance(obj, property) - @classmethod def parse_args(cls, argv = None): @@ -145,22 +121,16 @@ def parse_args(cls, argv = None): if '=' in arg: parsing_kwargs = True key, value = arg.split('=') - kwargs[key] = cls.determine_type(value) - + kwargs[key] = cls.get_value(value) else: assert parsing_kwargs is False, 'Cannot mix positional and keyword arguments' - args.append(cls.determine_type(arg)) + args.append(cls.get_value(arg)) return args, kwargs @classmethod - def determine_type(cls, x): + def get_value(cls, x): - if x.startswith('py(') and x.endswith(')'): - try: - return eval(x[3:-1]) - except: - return x - if x.lower() in ['null'] or x == 'None': # convert 'null' or 'None' to None + if x.lower() in ['null', 'None']: # convert 'null' or 'None' to None return None elif x.lower() in ['true', 'false']: # convert 'true' or 'false' to bool return bool(x.lower() == 'true') @@ -168,7 +138,7 @@ def determine_type(cls, x): try: list_items = x[1:-1].split(',') # try to convert each item to its actual type - x = [cls.determine_type(item.strip()) for item in list_items] + x = [cls.get_value(item.strip()) for item in list_items] if len(x) == 1 and x[0] == '': x = [] return x @@ -183,7 +153,7 @@ def determine_type(cls, x): try: dict_items = x[1:-1].split(',') # try to convert each item to a key-value pair - return {key.strip(): cls.determine_type(value.strip()) for key, value in [item.split(':', 1) for item in dict_items]} + return {key.strip(): cls.get_value(value.strip()) for key, value in [item.split(':', 1) for item in dict_items]} except: # if conversion fails, return as string return x @@ -192,14 +162,16 @@ def determine_type(cls, x): try: return int(x) except ValueError: - try: - return float(x) - except ValueError: - return x - + pass + try: + return float(x) + except ValueError: + pass + return x + def argv(self): return sys.argv[1:] def main(): - cli() \ No newline at end of file + Cli() \ No newline at end of file diff --git a/commune/client.py b/commune/client.py index 8240b261..5b796ab2 100644 --- a/commune/client.py +++ b/commune/client.py @@ -134,6 +134,9 @@ def resolve_module_address(self, else: url = module + ip = c.ip() + if ip in url: + url = url.replace(ip, '0.0.0.0') url = f'{mode}://' + url if not url.startswith(f'{mode}://') else url return url diff --git a/commune/key.py b/commune/key.py index 0853cd42..7bd4ebf8 100644 --- a/commune/key.py +++ b/commune/key.py @@ -234,41 +234,31 @@ def get_key(cls, print(path) raise ValueError(f'key does not exist at --> {path}') key_json = cls.get(path) - # if key is encrypted, decrypt it if cls.is_encrypted(key_json): key_json = c.decrypt(data=key_json, password=password) if key_json == None: c.print({'status': 'error', 'message': f'key is encrypted, please {path} provide password'}, color='red') return None - - if isinstance(key_json, str): key_json = c.jload(key_json) - - if json: key_json['path'] = path return key_json else: return cls.from_json(key_json) - - @classmethod - def get_keys(cls, search=None, clean_failed_keys=False): + def get_keys(cls, search=None): keys = {} for key in cls.keys(): if str(search) in key or search == None: try: - keys[key] = cls.get_key(key) + key_obj = cls.get_key(key) + if hasattr(key_obj, 'ss58_address'): + keys[key] = key_obj except Exception as e: - c.print(f'failed to get key {key} due to {e}', color='red') - continue - if keys[key] == None: - if clean_failed_keys: - cls.rm_key(key) - keys.pop(key) + print('Key error') return keys @@ -854,8 +844,6 @@ def verify(self, True if data is signed with this Key, otherwise False """ data = c.copy(data) - if isinstance(data, str) and seperator in data: - data, signature = data.split(seperator) if isinstance(data, dict): if self.is_ticket(data): @@ -870,9 +858,7 @@ def verify(self, assert address != None, 'address not found in data' if max_age != None: - if isinstance(data, int): - staleness = c.timestamp() - int(data) - elif 'timestamp' in data or 'time' in data: + if 'timestamp' in data or 'time' in data: timestamp = data.get('timestamp', data.get('time')) staleness = c.timestamp() - int(timestamp) else: @@ -890,7 +876,6 @@ def verify(self, public_key = self.ss58_decode(public_key) if isinstance(public_key, str): public_key = bytes.fromhex(public_key.replace('0x', '')) - if type(data) is ScaleBytes: data = bytes(data.data) elif data[0:2] == '0x': @@ -911,9 +896,7 @@ def verify(self, crypto_verify_fn = ecdsa_verify else: raise ConfigurationError("Crypto type not supported") - verified = crypto_verify_fn(signature, data, public_key) - if not verified: # Another attempt with the data wrapped, as discussed in https://github.com/polkadot-js/extension/pull/743 # Note: As Python apps are trusted sources on its own, no need to wrap data when signing from this lib @@ -933,7 +916,6 @@ def resolve_encryption_password(self, password:str): def resolve_encryption_data(self, data): if not isinstance(data, str): data = str(data) - return data def encrypt(self, data, password=None): @@ -1067,58 +1049,12 @@ def state_dict(self): return self.__dict__ to_dict = state_dict - @classmethod - def dashboard(cls): - import streamlit as st - self = cls.new_key() - keys = self.keys() - selected_keys = st.multiselect('Keys', keys) - buttons = {} - for key_name in selected_keys: - key = cls.get_key(key_name) - with st.expander('Key Info'): - st.write(key.to_dict()) - buttons[key_name] = {} - buttons[key_name]['sign'] = st.button('Sign', key_name) - st.write(self.keys()) @classmethod - def key2type(cls): - keys = cls.keys(object=True) - return {k.path: k.key_type for k in keys} - @classmethod def key2mem(cls, search=None): keys = cls.keys(search, object=True) key2mem = {k.path: k.mnemonic for k in keys} return key2mem - - @classmethod - def type2keys(cls): - type2keys = {} - key2type = cls.key2type() - for k,t in key2type.items(): - type2keys[t] = type2keys.get(t, []) + [k] - return type2keys - - @classmethod - def pubkey2multihash(cls, pk:bytes) -> str: - import multihash - hashed_public_key = multihash.encode(pk, code=multihash.SHA2_256) - return hashed_public_key.hex() - - @classmethod - def duplicate_keys(cls) -> dict: - - key2address = cls.key2address() - duplicate_keys = {} - - for k,a in key2address.items(): - if a not in duplicate_keys: - duplicate_keys[a] = [] - - duplicate_keys[a] += [k] - - return {k:v for k,v in duplicate_keys.items() if len(v) > 1} @classmethod def from_private_key(cls, private_key:str): @@ -1226,7 +1162,7 @@ def is_ss58(address): return False return True - + @classmethod def is_encrypted(cls, data, prefix=encrypted_prefix): if isinstance(data, str): @@ -1282,12 +1218,6 @@ def resolve_key_address(cls, key): else: address = key return address - - - -if __name__ == "__main__": - Key.run() - diff --git a/commune/module.py b/commune/module.py index 6710ab82..d91ba5a1 100755 --- a/commune/module.py +++ b/commune/module.py @@ -129,7 +129,6 @@ def __str__(cls): @classmethod def is_module(cls, obj=None) -> bool: - if obj is None: obj = cls if all([hasattr(obj, k) for k in ['info', 'schema', 'set_config', 'config']]): @@ -1709,7 +1708,7 @@ def get_schema(cls, if search != None and search not in fn: continue if callable(getattr(module, fn )): - schema[fn] = cls.get_function_schema(fn, defaults=defaults,docs=docs) + schema[fn] = cls.get_function_schema(getattr(module, fn ), defaults=defaults,docs=docs) # sort by keys schema = dict(sorted(schema.items())) return schema @@ -1880,49 +1879,6 @@ def chash(cls, *args, **kwargs): return c.hash(code) - @classmethod - def fn_info(cls, fn:str='test_fn') -> dict: - r = {} - code = cls.fn_code(fn) - lines = code.split('\n') - mode = 'self' - if '@classmethod' in lines[0]: - mode = 'class' - elif '@staticmethod' in lines[0]: - mode = 'static' - module_code = cls.code() - in_fn = False - start_line = 0 - end_line = 0 - fn_code_lines = [] - for i, line in enumerate(module_code.split('\n')): - if f'def {fn}('.replace(' ', '') in line.replace(' ', ''): - in_fn = True - start_line = i + 1 - if in_fn: - fn_code_lines.append(line) - if ('def ' in line or '' == line) and len(fn_code_lines) > 1: - end_line = i - 1 - break - - if not in_fn: - end_line = start_line + len(fn_code_lines) # find the endline - - for i, line in enumerate(lines): - is_end = bool(')' in line and ':' in line) - if is_end: - start_code_line = i - break - return { - 'start_line': start_line, - 'end_line': end_line, - 'code': code, - 'n_lines': len(lines), - 'hash': cls.hash(code), - 'start_code_line': start_code_line + start_line , - 'mode': mode - } - @classmethod def get_function_parameters(cls, fn): """ @@ -2052,8 +2008,9 @@ def n_fns(self, search = None): fn_n = n_fns @classmethod - def fns(self, search = None, include_parents = True): + def functions(self, search = None, include_parents = True): return self.get_functions(search=search, include_parents=include_parents) + fns = functions @classmethod def is_property(cls, fn: 'Callable') -> bool: ''' @@ -2280,7 +2237,8 @@ def resolve_extension(cls, filename:str, extension = '.py') -> str: def simple2path(cls, simple:str, extension = '.py', - avoid_dirnames = ['', 'src', + preset_prefixes = ['', + 'src', 'commune', 'commune/module', 'commune/modules', @@ -2314,9 +2272,9 @@ def simple2path(cls, path_options = [] simple = simple.replace('/', '.') - # create all of the possible paths by combining the avoid_dirnames with the simple path - dir_paths = list([pwd+ '/' + x for x in avoid_dirnames]) # local first - dir_paths += list([cls.libpath + '/' + x for x in avoid_dirnames]) # add libpath stuff + # create all of the possible paths by combining the preset_prefixes with the simple path + dir_paths = list([pwd+ '/' + x for x in preset_prefixes]) # local first + dir_paths += list([cls.libpath + '/' + x for x in preset_prefixes]) # add libpath stuff for dir_path in dir_paths: if dir_path.endswith('/'): @@ -2325,7 +2283,7 @@ def simple2path(cls, module_dirpath = dir_path + '/' + simple.replace('.', '/') if os.path.isdir(module_dirpath): simple_filename = simple.replace('.', '_') - filename_options = [simple_filename, simple_filename + '_module', 'module_'+ simple_filename] + ['module'] + simple.split('.') + ['__init__'] + filename_options = [simple_filename, simple_filename + '_module', 'module_'+ simple_filename, 'module', '__init__', 'main', 'block', 'base'] path_options += [module_dirpath + '/' + f for f in filename_options] else: module_filepath = dir_path + '/' + simple.replace('.', '/') @@ -2589,11 +2547,12 @@ def simple2objectpath(cls, simple_path:str, **kwargs) -> str: @classmethod def simple2object(cls, path:str = None, **kwargs) -> str: path = path or 'module' - path = c.simple2objectpath(path, **kwargs) try: + path = c.simple2objectpath(path, **kwargs) return c.import_object(path) except: - path = cls.tree().get(path) + tree = c.tree() + path = tree.get(path, path) return c.import_object(path) included_pwd_in_path = False @@ -2654,10 +2613,8 @@ def import_object(cls, key:str, verbose: bool = 0, trials=3)-> Any: pwd = c.pwd() - if sys.path != None and pwd not in sys.path: sys.path.append(pwd) - # sys.path.append('~/eth') assert key != None, key module = '.'.join(key.split('.')[:-1]) diff --git a/commune/modules/chat/chat.py b/commune/modules/chat/chat.py deleted file mode 100644 index 49a17cb5..00000000 --- a/commune/modules/chat/chat.py +++ /dev/null @@ -1,259 +0,0 @@ -import commune as c -import streamlit as st - - -class Chat(c.Module): - - def __init__(self, - max_tokens=420000, - password = None, - text = 'Hello whaduop fam', - system_prompt = 'The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.', - name='chat', - model = None, - history_path='history', - **kwargs): - - self.max_tokens = max_tokens - self.text = text - - self.set_module(model, - password = password, - name = name, - history_path=history_path, - system_prompt=system_prompt, - **kwargs) - - def set_module(self, - model, - history_path='history', - name='chat', - password=None, - system_prompt = 'The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.', - key=None, - **kwargs): - self.system_prompt = system_prompt - self.admin_key = c.pwd2key(password) if password else self.key - self.model = c.module('model.openrouter')(model=model, **kwargs) - self.models = self.model.models() - self.history_path = self.resolve_path(history_path) - return {'success':True, 'msg':'set_module passed'} - - def add_files(self, files): - cwd = st.text_input('cwd', './') - files = c.glob(cwd) - files = st.multi_select(files, 'files') - file_options = [f.name for f in files] - - - def call(self, - input = 'whats 2+2?' , - temperature= 0.5, - max_tokens= 1000000, - model= 'anthropic/claude-3.5-sonnet', - system_prompt= 'make this shit work', - key = None, - stream=True, - ): - # key = self.resolve_key(key) - data = c.locals2kwargs(locals()) - signature = self.key.ticket(c.hash(data)) - return signature - - @c.endpoint() - def generate(self, - input = 'whats 2+2?' , - temperature= 0.5, - max_tokens= 1000000, - model= 'anthropic/claude-3.5-sonnet', - system_prompt= None, - context = None, - stream=True, - headers = None, - ): - # c.verify_ticket(ticket) - text = input - if context: - print(f'GETTING CONTEXT --> {context}') - context = c.file2text(c.resolve_path(context)) - text = f"{context}" + text - if system_prompt: - text = f'{system_prompt}' + text - num_tokens = len(text) - id = c.hash(text) - print(f'SENDING {id} --> {num_tokens} characters' ) - output = self.model.generate( text,stream=stream, model=model, max_tokens=max_tokens, temperature=temperature ) - for token in output: - yield token - - def ask(self, *text, **kwargs): - return self.generate(' '.join(map(str, text)), **kwargs) - - def save_data(self, data): - path = self.data2path(data) - return c.put(path, data) - - - def get_params(self): - model = st.selectbox('Model', self.models) - temperature = st.slider('Temperature', 0.0, 1.0, 0.5) - if hasattr(self.model, 'get_model_info'): - model_info = self.model.get_model_info(model) - max_tokens = min(int(model_info['context_length']*0.9), self.max_tokens) - else: - model_info = {} - max_tokens = self.max_tokens - max_tokens = st.number_input('Max Tokens', 1, max_tokens, max_tokens) - system_prompt = st.text_area('System Prompt',self.system_prompt, height=200) - input = st.text_area('Text',self.text, height=100) - - params = { - 'model': model, - 'temperature': temperature, - 'max_tokens': max_tokens, - 'system_prompt': system_prompt, - 'input': input - } - - return params - - def sidebar(self, user='user', password='password', seperator='::'): - with st.sidebar: - st.title('Just Chat') - # assert self.key.verify_ticket(ticket) - - user_name = st.text_input('User', user) - pwd = st.text_input('Password', password, type='password') - seed = c.hash(user_name + seperator + pwd) - self.key = c.pwd2key(seed) - self.data = c.dict2munch({ - 'user': user_name, - 'path': self.resolve_path('history', self.key.ss58_address ), - 'history': self.history(self.key.ss58_address) - }) - - def search_history(self): - search = st.text_input('Search') - # if the search is in any of the columns - history = c.copy(self.data.history) - - history = [h for h in history if search in str(h)] - df = c.df(history) - st.write(df) - - def app(self): - self.sidebar() - - tab_names = ['Chat', 'History'] - tabs = st.tabs(tab_names) - - with tabs[0]: - self.chat_page() - with tabs[1]: - self.history_page() - - def chat_page(self): - with st.sidebar.expander('Params', expanded=True): - params = self.get_params() - data = c.ticket(params, key=self.key) - - # make the buttons cover the whole page - cols = st.columns([1,1]) - send_button = cols[0].button('Send', key='send', use_container_width=True) - stop_button = cols[1].button('Stop', key='stop', use_container_width=True) - if send_button and not stop_button: - r = self.generate(data) - # dank emojis to give it that extra flair - emojis = '✅🤖💻🔍🧠🔧⌨️' - reverse_emojis = emojis[::-1] - with st.spinner(f'{emojis} Generating {reverse_emojis}'): - st.write_stream(r) - - self.post_processing(data) - - - def post_processing(self, data): - lambda_string = st.text_area('fn(x={model_output})', 'x', height=100) - prefix = 'lambda x: ' - lambda_string = prefix + lambda_string if not lambda_string.startswith(prefix) else lambda_string - lambda_fn = eval(lambda_string) - try: - output = data['data']['output'] - output = lambda_fn(output) - except Exception as e: - st.error(e) - - def history_page(self): - history = self.data.history - if len(history) == 0: - st.error('No History') - return - else: - cols = history[0].keys() - selected_columns = st.multiselect('Columns', cols, cols) - df = c.df(history)[selected_columns] - st.write(df) - def user_files(self): - return c.get(self.data['path']) - - - - def save_data(self, address, data): - return c.put(self.history_path + '/' + address +'/data.json', data) - - def get_data(self, address): - return c.get(self.history_path + '/' + address +'/data.json') - - - def clear_history(self, address): - return c.rm(self.history_path + '/'+ address) - - def history_paths(self, address:str=None): - paths = [] - if address == None: - for user_address in self.user_addresses(): - paths += self.history_paths(user_address) - else: - paths = c.ls(self.history_path + '/'+ address) - return paths - - def save_data(self, data): - path = self.history_path + '/'+ data['address'] + '/' + str(data['time']) + '.json' - return c.put(path, data) - - def history(self, address:str=None, columns=['datetime', - 'input', - 'output', - 'system_prompt', - 'model', - 'temperature', - 'max_tokens'], df=False): - paths = self.history_paths(address) - history = [] - for i, path in enumerate(paths): - try: - print(paths) - h = c.get(path) - h.update(h.pop('data')) - h['datetime'] = c.time2datetime(h.pop('time')) - h = {k:v for k,v in h.items() if k in columns} - history.append(h) - except Exception as e: - print(e) - # sort by time - - history = sorted(history, key=lambda x: x['datetime'], reverse=True) - if df: - history = c.df(history) - return history - - def user_addresses(self, display_name=False): - users = [u.split('/')[-1] for u in c.ls(self.history_path)] - return users - - - def models(self): - return self.model.models() - - -Chat.run(__name__) \ No newline at end of file diff --git a/commune/modules/chat/history.py b/commune/modules/chat/history.py deleted file mode 100644 index 81998c5e..00000000 --- a/commune/modules/chat/history.py +++ /dev/null @@ -1,73 +0,0 @@ - -import commune as c -from typing import * -import pandas as pd - -class History(c.Module): - def __init__(self, - path='history', - max_age=100000, - **kwargs): - self.max_age = max_age - self.set_history_path(path) - # HISTORY - - - def check_item(self, item, required_fields=['address', 'timestamp']): - assert all([field in item for field in required_fields]), f'Missing required fields: {required_fields}' - assert c.valid_ss58_address(item['address']), f'Invalid address: {item["address"]}' - - def get_user_directory(self, key): - key_address = c.resolve_key_address(key) - return self.history_path + '/' + key_address - - def get_user_path(self, key_address): - if not c.valid_ss58_address(key_address): - key_address = c.get_key(key_address).ss58_address - path = self.history_path +f'/{key_address}/{c.time()}.json' - return path - - def refresh_history(self): - path = self.history_path - self.rm(path) - return self.ls(path) - - def add_history(self, item): - self.check_item(item) - path = self.get_user_path(item['address']) - if 'path' in item: - path = item['path'] - self.put(path, item) - return {'path': path, 'item': item} - - def rm_history(self, key): - path = self.get_user_directory(key) - self.rm(path) - return {'path': path} - - def history_size(self, key): - path = self.get_user_directory(key) - return len(self.ls(path)) - - def history_exists(self, key): - path = self.get_user_directory(key) - return self.exists(path) and self.history_size(key) > 0 - - def user_history(self, key): - path = self.get_user_directory(key) - return self.ls(path) - def set_history_path(self, path): - self.history_path = self.resolve_path(path) - return {'history_path': self.history_path} - - - def test_history(self): - key = c.new_key() - item = {'address': key.ss58_address, 'timestamp': c.time()} - self.add_history(item) - assert self.history_exists(key.ss58_address) - self.user_history(key.ss58_address) - self.rm_history(key.ss58_address) - assert not self.history_exists(key.ss58_address) - return {'key': key.ss58_address, 'item': item} - diff --git a/commune/namespace.py b/commune/namespace.py index 3a34fab4..e92c262d 100644 --- a/commune/namespace.py +++ b/commune/namespace.py @@ -5,37 +5,19 @@ # THIS IS WHAT THE INTERNET IS, A BUNCH OF NAMESPACES, AND A BUNCH OF SERVERS, AND A BUNCH OF MODULES. # THIS IS THE INTERNET OF INTERNETS. class Namespace(c.Module): - # the default - network : str = 'local' - @classmethod - def resolve_network_path(cls, network:str, netuid:str=None): - if netuid != None: - if network not in ['subspace']: - network = f'subspace' - network = f'subspace/{netuid}' - return cls.resolve_path(network + '.json') + incentive_networks = ['subspace', 'bittensor'] + def __init__(self, network='local' ): + self.network = network - @classmethod - def namespace(cls, search=None, - network:str = 'local', - update:bool = False, - netuid=None, - max_age:int = 60, - timeout=6, - verbose=False) -> dict: - network = network or 'local' - path = cls.resolve_network_path(network) - namespace = cls.get(path, None, max_age=max_age) + def resolve_network(self, network=None): + return network or self.network + + def namespace(self, search=None, network:str = None, update:bool = False, max_age:int = 60,timeout=6,verbose=False) -> dict: + network = self.resolve_network(network) + namespace = self.get(network, None, max_age=max_age, update=update) if namespace == None: - namespace = cls.update_namespace(network=network, - netuid=netuid, - timeout=timeout, - verbose=verbose) - cls.put(path,namespace) - if search != None: - namespace = {k:v for k,v in namespace.items() if search in k} - print(namespace) - namespace = namespace or {} + namespace = self.build_namespace(network=network, timeout=timeout) + self.put(network,namespace) if 'null' in namespace: del namespace['null'] namespace = {k:':'.join(v.split(':')[:-1]) + ':'+ str(v.split(':')[-1]) for k,v in namespace.items()} @@ -43,190 +25,94 @@ def namespace(cls, search=None, ip = c.ip() namespace = {k: v.replace(ip, '0.0.0.0') for k,v in namespace.items() } namespace = { k.replace('"', ''): v for k,v in namespace.items() } + if search != None: + namespace = {k:v for k,v in namespace.items() if search in k} return namespace - - @classmethod - def update_namespace(cls, network, netuid=None, timeout=1, search=None, verbose=False): - c.print(f'UPDATING --> NETWORK(network={network} netuid={netuid})', color='blue') - if 'subspace' in network: - if '.' in network: - network, netuid = network.split('.') - else: - netuid = netuid or 0 - if c.is_int(netuid): - netuid = int(netuid) - namespace = c.module(network)().namespace(search=search, max_age=1, netuid=netuid) - return namespace - elif 'local' == network: - namespace = {} - addresses = ['0.0.0.0'+':'+str(p) for p in c.used_ports()] - future2address = {} - for address in addresses: - f = c.submit(c.call, [address+'/server_name'], timeout=timeout) - future2address[f] = address - futures = list(future2address.keys()) - try: - for f in c.as_completed(futures, timeout=timeout): - address = future2address[f] - name = f.result() - if isinstance(name, str): - namespace[name] = address - else: - print(f'Error: {name}') - except Exception as e: - print(e) - namespace = {k:v for k,v in namespace.items() if 'Error' not in k} - namespace = {k: '0.0.0.0:' + str(v.split(':')[-1]) for k,v in namespace.items() } - else: - return {} + def build_namespace(self, network=None, timeout=1, search=None): + network = self.resolve_network(network) + c.print(f'UPDATING --> NETWORK(network={network})', color='blue') + namespace = {} + used_ports = c.used_ports() + addresses = ['0.0.0.0'+':'+str(p) for p in used_ports] + future2address = {} + for address in addresses: + f = c.submit(c.call, [address+'/server_name'], timeout=timeout) + future2address[f] = address + futures = list(future2address.keys()) + try: + for f in c.as_completed(futures, timeout=timeout): + address = future2address[f] + name = f.result() + if isinstance(name, str): + namespace[name] = address + else: + print(f'Error: {name}') + except Exception as e: + print(f'Error: {name} --> {e}') + namespace = {k:v for k,v in namespace.items() if 'Error' not in k} + namespace = {k: '0.0.0.0:' + str(v.split(':')[-1]) for k,v in namespace.items() } return namespace get_namespace = _namespace = namespace - @classmethod - def register_server(cls, name:str, address:str, network=network) -> None: - namespace = cls.namespace(network=network) + def register_server(self, name:str, address:str) -> None: + namespace = self.namespace(network=self.network) namespace[name] = address - cls.put_namespace(network, namespace) - return {'success': True, 'msg': f'Block {name} registered to {network}.'} + self.put(self.network, namespace) + return {'success': True, 'msg': f'Block {name} registered to {self.network}.'} - @classmethod - def deregister_server(cls, name:str, network=network) -> Dict: - namespace = cls.namespace(network=network) + def deregister_server(self, name:str) -> Dict: + namespace = self.namespace() address2name = {v: k for k, v in namespace.items()} if name in address2name: name = address2name[name] if name in namespace: del namespace[name] - cls.put_namespace(network, namespace) + self.put(self.network, namespace) return {'status': 'success', 'msg': f'Block {name} deregistered.'} else: return {'success': False, 'msg': f'Block {name} not found.'} + rm_server = remve_server = deregister_server - @classmethod - def rm_server(self, name:str, network=network): - return self.deregister_server(name, network=network) - - @classmethod - def get_address(cls, name:str, network:str=network, external:bool = True) -> dict: - namespace = cls.namespace(network=network) + def get_address(self, name:str, external:bool = True) -> dict: + namespace = self.namespace() address = namespace.get(name, None) if external and address != None: address = address.replace(c.default_ip, c.ip()) return address - @classmethod - def put_namespace(cls, network:str, namespace:dict) -> None: - assert isinstance(namespace, dict), 'Namespace must be a dict.' - return cls.put(network, namespace) - - add_namespace = put_namespace - - @classmethod - def rm_namespace(cls,network:str) -> None: - if cls.namespace_exists(network): - cls.rm(network) - return {'success': True, 'msg': f'Namespace {network} removed.'} - else: - return {'success': False, 'msg': f'Namespace {network} not found.'} - - @classmethod - def networks(cls) -> dict: - return [p.split('/')[-1].split('.')[0] for p in cls.ls()] - - @classmethod - def namespace_exists(cls, network:str) -> bool: - path = cls.resolve_network_path( network) - return os.path.exists(path) - - @classmethod - def modules(cls, network:List=network) -> List[str]: - return list(cls.namespace(network=network).keys()) + def namespace_exists(self, network:str) -> bool: + return os.path.exists(self.resolve_path(network)) - @classmethod - def addresses(cls, network:str=network, **kwargs) -> List[str]: - return list(cls.namespace(network=network, **kwargs).values()) - - @classmethod - def check_servers(self, *args, **kwargs): - servers = c.pm2ls() - namespace = c.get_namespace(*args, **kwargs) - c.print('Checking servers', color='blue') - for server in servers: - if server in namespace: - c.print(c.pm2_restart(server)) - - return {'success': True, 'msg': 'Servers checked.'} - - @classmethod - def add_server(cls, address:str, name=None, network:str = 'local',timeout:int=4, **kwargs): - """ - Add a server to the namespace. - """ - module = c.connect(address) - info = module.info(timeout=timeout) - name = info['name'] if name == None else name - # check if name exists - address = info['address'] - module_ip = address.split(':')[0] - is_remote = bool(module_ip != c.ip()) - namespace = cls.namespace(network=network) - if is_remote: - name = name + '_' + str(module_ip) - addresses = list(namespace.values()) - if address not in addresses: - return {'success': False, 'msg': f'{address} not in {addresses}'} - namespace[name] = address - cls.put_namespace(network, namespace) + def addresses(self,**kwargs) -> List[str]: + return list(self.namespace(**kwargs).values()) - return {'success': True, 'msg': f'Added {address} to {network} modules', 'remote_modules': cls.servers(network=network), 'network': network} - - @classmethod - def rm_server(cls, name, network:str = 'local', **kwargs): - namespace = cls.namespace(network=network) - if name in namespace.values(): - for k, v in c.copy(list(namespace.items())): - if v == name: - name = k - break + def rm_server(self, name, network:str = None, **kwargs): + network = self.resolve_network(network) + namespace = self.namespace(network=network) if name in namespace: - # reregister - address = cls.get_address(name, network=network) - cls.deregister_server(name, network=network) - servers = cls.servers(network=network) - assert cls.server_exists(name, network=network) == False, f'{name} still exists' - return {'success': True, 'msg': f'removed {address} to remote modules', 'servers': servers, 'network': network} - else: - return {'success': False, 'msg': f'{name} does not exist'} + namespace.pop(name) + self.put(network, namespace) + return {'msg': f'{name} removed from the server'} - @classmethod - def servers(cls, search=None, network:str = 'local', **kwargs): - namespace = cls.namespace(search=search, network=network, **kwargs) + def servers(self, search=None, network=None, **kwargs): + namespace = self.namespace(search=search, network=network, **kwargs) return list(namespace.keys()) - @classmethod - def server_exists(cls, name:str, network:str = None, prefix_match:bool=False, **kwargs) -> bool: - servers = cls.servers(network=network, **kwargs) - if prefix_match: - server_exists = any([s for s in servers if s.startswith(name)]) - - else: - server_exists = bool(name in servers) - - return server_exists - + def server_exists(self, name:str, **kwargs) -> bool: + servers = self.servers(network=self.network, **kwargs) + return bool(name in servers) - @classmethod - def server_exists(cls, name:str, network:str = None, prefix_match:bool=False, **kwargs) -> bool: - servers = cls.servers(network=network, **kwargs) - if prefix_match: - server_exists = any([s for s in servers if s.startswith(name)]) - - else: - server_exists = bool(name in servers) - - return server_exists + def rm_namespace(self,network:str) -> None: + if not self.namespace_exists(network): + return {'success': False, 'msg': f'Namespace {network} not removed.'} + self.rm(network) + return {'success': True, 'msg': f'Namespace {network} removed.'} + + def namespaces(self) -> dict: + return [p.split('/')[-1].split('.')[0] for p in self.ls()] + networks = namespaces Namespace.run(__name__) diff --git a/commune/pm2.py b/commune/pm2.py index bb74cd9f..2f8a1675 100644 --- a/commune/pm2.py +++ b/commune/pm2.py @@ -1,6 +1,4 @@ import commune as c - - import os from typing import * import json diff --git a/commune/routes.yaml b/commune/routes.yaml index 79a6a812..ad7c4662 100644 --- a/commune/routes.yaml +++ b/commune/routes.yaml @@ -62,9 +62,7 @@ namespace: [ register_server, deregister_server, server_exists, - add_server, has_server, - add_servers, rm_servers, rm_server, remote_servers, @@ -82,7 +80,8 @@ namespace: [ servers, name2address, namespace, - get_address] + get_address + ] app : - start_app - app @@ -127,6 +126,8 @@ vali: - setup_vali - from_module subspace: + - register_subnet + - storage_names - key2balance - my_keys - key2value diff --git a/commune/serializer/__init__.py b/commune/serializer/serializer.py similarity index 84% rename from commune/serializer/__init__.py rename to commune/serializer/serializer.py index bb67f884..314706c5 100644 --- a/commune/serializer/__init__.py +++ b/commune/serializer/serializer.py @@ -29,7 +29,7 @@ def serialize(self,x:dict, mode = 'dict', copy_value = True): data_type = self.get_data_type_string(x) serializer = self.get_serializer(data_type) result = {'data': serializer.serialize(x), - 'data_type': serializer.date_type, + 'data_type': serializer.data_type, 'serialized': True} return self.process_output(result, mode=mode) @@ -94,20 +94,28 @@ def deserialize(self, x) -> object: def serializer_map(self): type_path = self.dirpath() module_paths = c.find_objects(type_path) - return {p.split('.')[-2]: c.obj(p)() for p in module_paths if '__init__' not in p} + print('FAMMMMM', module_paths) + serializer_map = {p.split('.')[-2]: p for p in module_paths if 'serializer' not in p.split('.')[-2]} + print('FAMMMMM', serializer_map) + + return c.copy(serializer_map) def types(self): return list(self.serializer_map().keys()) + serializer_map_cache = {} def get_serializer(self, data_type): serializer_map = self.serializer_map() if data_type in serializer_map: - serializer = serializer_map[data_type] - if not hasattr(serializer, 'date_type'): - setattr(serializer, 'date_type', data_type) - serializer_map[data_type] = serializer + if data_type in self.serializer_map_cache: + serializer = self.serializer_map_cache[data_type] + setattr(serializer, 'data_type', data_type) + else: + serializer = c.import_object(serializer_map[data_type])() + serializer.data_type = data_type + self.serializer_map_cache[data_type] = serializer else: raise TypeError(f'Type Not supported for serializeation ({data_type})') return serializer diff --git a/commune/server.py b/commune/server.py index df1ab38a..d9eb7daf 100644 --- a/commune/server.py +++ b/commune/server.py @@ -9,7 +9,6 @@ from starlette.middleware.base import BaseHTTPMiddleware from sse_starlette.sse import EventSourceResponse - class ServerMiddleware(BaseHTTPMiddleware): def __init__(self, app, max_bytes: int): super().__init__(app) @@ -66,10 +65,10 @@ def __init__( print(module) module.server_functions = functions or self.get_server_functions(module) module.helper_functions = helper_functions - module.name = module.server_name = name or module.server_name + module.server_name = module.name = name or module.server_name module.server_port = port if port not in ['None', None] else c.free_port() module.server_address = f"{c.ip()}:{module.server_port}" - module.key = c.get_key(key or module.server_name, create_if_not_exists=True) + module.server_key = module.key = c.get_key(key or module.server_name, create_if_not_exists=True) module.server_info = module.info = self.get_info(module) self.module = module self.network = network @@ -87,11 +86,7 @@ def __init__( self.set_api(max_bytes=max_bytes, loop=loop, nest_asyncio=nest_asyncio, **kwargs) def get_server_functions(self, module:c.Module=None, - functions_attributes = [ 'public_functions', - 'server_functions', - 'server_fns', - 'whitelist', - 'endpoints']): + functions_attributes = [ 'public_functions','server_functions','server_fns', 'whitelist', 'endpoints']): module = module or self.module functions = [] for k in functions_attributes: @@ -114,7 +109,7 @@ def get_server_functions(self, module:c.Module=None, except Exception as e: print(f'Error in get_endpoints: {e} for {f}') return sorted(list(set(functions))) - + @classmethod def endpoint(cls, cost=1, # cost per call @@ -425,14 +420,12 @@ def kill(cls, else: delete_modules.append(killed_module) # update modules - c.deregister_server(module, network=network) - - assert c.server_exists(module, network=network) == False, f'module {module} still exists' - + c.deregister_server(module) + assert c.server_exists(module) == False, f'module {module} still exists' servers = c.servers() for m in delete_modules: if m in servers: - c.deregister_server(m, network=network) + c.deregister_server(m) return {'server_killed': delete_modules, 'update': update} @@ -514,8 +507,9 @@ def wait_for_server(cls, time_waiting = 0 t0 = c.time() while (c.time() - t0) < timeout: + print('CHECKING' ,name) try: - assert name == c.call(f'{name}/server_name', network=network) + assert name == c.call(f'{name}/server_name') return True except Exception as e: c.sleep(sleep_interval) @@ -578,16 +572,5 @@ def get_info(self , info['key_type'] = module.key.key_type return info - def metadata(self, to_string=False): - if hasattr(self, '_metadata'): - return self._metadata - metadata = {} - metadata['schema'] = self.get_schema() - metadata['description'] = self.description - if to_string: - return self.python2str(metadata) - self._metadata = metadata - return metadata - if __name__ == '__main__': Server.run(__name__) \ No newline at end of file diff --git a/commune/subspace.py b/commune/subspace.py index c77c3f1a..6f7c8fd3 100644 --- a/commune/subspace.py +++ b/commune/subspace.py @@ -921,10 +921,8 @@ def transfer( ) return response - - - + send = transfer def add_profit_shares( self, @@ -2440,7 +2438,7 @@ def get_module(self, module = self.keys(netuid=netuid, update=update, max_age=max_age)[0] c.print(f'No module specified, using {module}') - module = c.key2address().get(module, module) + module = module if c.valid_ss58_address(module) else c.get_key(module).ss58_address url = self.get_url( mode=mode) module_key = module is_valid_key = c.valid_ss58_address(module) @@ -2812,7 +2810,6 @@ def format_module(self, module: 'ModuleInfo', fmt:str='j') -> 'ModuleInfo': return module def subnet_state(self, netuid=0, update=False, **kwargs): - modules = self.get_modules(netuid=netuid, update=update, **kwargs) return { @@ -2822,22 +2819,8 @@ def subnet_state(self, netuid=0, update=False, **kwargs): 'modules': modules } - def register_subnet(self, key: 'Keypair', name: str, metadata: str | None = None) -> 'c': - """ - Registers a new subnet in the network. - - Args: - key (Keypair): The keypair used for registering the subnet. - name (str): The name of the subnet to be registered. - metadata (str | None, optional): Additional metadata for the subnet. Defaults to None. - - Returns: - ExtrinsicReceipt: A receipt of the subnet registration transaction. - - Raises: - ChainTransactionError: If the transaction fails. - """ - + def register_subnet(self, name: str, metadata: str | None = None, key: 'Keypair'=None) -> Dict: + key = self.resolve_key(key) params = { "name": name, "metadata": metadata, @@ -2846,6 +2829,8 @@ def register_subnet(self, key: 'Keypair', name: str, metadata: str | None = None response = self.compose_call("register_subnet", params=params, key=key) return response + + Subspace.run(__name__) diff --git a/commune/vali.py b/commune/vali.py index f3807d63..0dbb52d7 100644 --- a/commune/vali.py +++ b/commune/vali.py @@ -32,6 +32,7 @@ def __init__(self, timeout_info= 4, # (OPTIONAL) the timeout for the info worker update=False, **kwargs): + self.last_start_time = c.time() max_workers = max_workers or batch_size config = self.set_config(locals()) config = c.dict2munch({**Vali.config(), **config}) @@ -234,7 +235,7 @@ def sync(self, # RESOLVE THE VOTING NETWORKS if 'local' in network: # local network does not need to be updated as it is atomically updated - namespace = c.get_namespace(search=search, update=1, max_age=max_network_staleness) + namespace = c.get_namespace(search=search,network=network) elif 'subspace' in network: # the network is a voting network self.subspace = c.module('subspace')(network=network, netuid=netuid) @@ -347,7 +348,7 @@ def vote_info(self): 'msg': 'Not a voting network' , 'network': self.config.network , 'voting_networks': self.voting_networks} - votes = self.calculate_votes() + votes = self.votes() except Exception as e: votes = {'uids': [], 'weights': []} c.print(c.detailed_error(e)) @@ -358,7 +359,7 @@ def vote_info(self): 'network': self.config.network, } - def calculate_votes(self, **kwargs): + def votes(self, **kwargs): leaderboard = self.leaderboard(keys=['name', 'w', 'staleness','latency', 'key'], to_dict=True) assert len(leaderboard) > 0 votes = {'keys' : [],'weights' : [],'uids': [], 'timestamp' : c.time() } @@ -371,17 +372,10 @@ def calculate_votes(self, **kwargs): votes['weights'] += [info['w']] votes['uids'] += [key2uid.get(info['key'], -1)] assert len(votes['uids']) == len(votes['weights']), f'Length of uids and weights must be the same, got {len(votes["uids"])} uids and {len(votes["weights"])} weights' - return votes - votes = calculate_votes - - @property - def votes_path(self): - return self.path + f'/votes' - def vote(self,**kwargs): - votes =self.calculate_votes() + votes =self.votes() return self.subspace.set_weights(uids=votes['uids'], # passing names as uids, to avoid slot conflicts weights=votes['weights'], key=self.key, diff --git a/commune/docs/0_install_commune.md b/docs/0_install_commune.md similarity index 100% rename from commune/docs/0_install_commune.md rename to docs/0_install_commune.md diff --git a/commune/docs/1_what_is_a_module.md b/docs/1_what_is_a_module.md similarity index 100% rename from commune/docs/1_what_is_a_module.md rename to docs/1_what_is_a_module.md diff --git a/commune/docs/2_key_identity_system.md b/docs/2_key_identity_system.md similarity index 100% rename from commune/docs/2_key_identity_system.md rename to docs/2_key_identity_system.md diff --git a/commune/docs/3_pythonic_cli.md b/docs/3_pythonic_cli.md similarity index 100% rename from commune/docs/3_pythonic_cli.md rename to docs/3_pythonic_cli.md diff --git a/commune/docs/4_servering_modules.md b/docs/4_servering_modules.md similarity index 100% rename from commune/docs/4_servering_modules.md rename to docs/4_servering_modules.md diff --git a/commune/docs/5_making_apps.md b/docs/5_making_apps.md similarity index 100% rename from commune/docs/5_making_apps.md rename to docs/5_making_apps.md diff --git a/commune/docs/6_offchain_networks.md b/docs/6_offchain_networks.md similarity index 100% rename from commune/docs/6_offchain_networks.md rename to docs/6_offchain_networks.md diff --git a/commune/docs/8_avoiding_recursion.md b/docs/8_avoiding_recursion.md similarity index 100% rename from commune/docs/8_avoiding_recursion.md rename to docs/8_avoiding_recursion.md diff --git a/commune/docs/docs.py b/docs/docs.py similarity index 56% rename from commune/docs/docs.py rename to docs/docs.py index afa0ccad..01fcf97d 100644 --- a/commune/docs/docs.py +++ b/docs/docs.py @@ -12,16 +12,17 @@ def __init__(self, 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) - 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 generate(self, text='what is a module and how can I use it?', stream=True): + text = self.process_text(text) + return self.model.generate(text, stream=stream) - def get_context(self, path): - file2text = c.file2text(path, avoid_folders=c.avoid_folders + [c.rootpath + '/modules']) - return file2text + def process_text(self, text): + path=c.libpath + code_context = c.file2text(path+ '/commune') + docs_context = c.file2text(path + '/docs') + text = f'SYSTEM: {self.system} CONTEXT : {code_context} {docs_context} USER: {text}' + print(len(text)) + return text def ask(self, *text, **kwargs): text = ' '.join(map(str,text)) diff --git a/commune/docs/intro.md b/docs/intro.md similarity index 100% rename from commune/docs/intro.md rename to docs/intro.md diff --git a/commune/docs/modules/app.md b/docs/modules/app.md similarity index 100% rename from commune/docs/modules/app.md rename to docs/modules/app.md diff --git a/commune/docs/modules/key.md b/docs/modules/key.md similarity index 100% rename from commune/docs/modules/key.md rename to docs/modules/key.md diff --git a/commune/docs/modules/serializer.md b/docs/modules/serializer.md similarity index 100% rename from commune/docs/modules/serializer.md rename to docs/modules/serializer.md diff --git a/commune/docs/modules/client.md b/docs/modules/server.md similarity index 100% rename from commune/docs/modules/client.md rename to docs/modules/server.md diff --git a/commune/docs/modules/vali.md b/docs/modules/vali.md similarity index 100% rename from commune/docs/modules/vali.md rename to docs/modules/vali.md diff --git a/commune/modules/agent/agent.py b/modules/agent/agent.py similarity index 100% rename from commune/modules/agent/agent.py rename to modules/agent/agent.py diff --git a/commune/modules/agent/app.py b/modules/agent/app.py similarity index 100% rename from commune/modules/agent/app.py rename to modules/agent/app.py diff --git a/commune/modules/agent/child.py b/modules/agent/child.py similarity index 100% rename from commune/modules/agent/child.py rename to modules/agent/child.py diff --git a/commune/modules/agent/data/agent_data.py b/modules/agent/data/agent_data.py similarity index 100% rename from commune/modules/agent/data/agent_data.py rename to modules/agent/data/agent_data.py diff --git a/commune/modules/agent/factory/agent_factory.py b/modules/agent/factory/agent_factory.py similarity index 100% rename from commune/modules/agent/factory/agent_factory.py rename to modules/agent/factory/agent_factory.py diff --git a/commune/modules/agent/judge.py b/modules/agent/judge.py similarity index 100% rename from commune/modules/agent/judge.py rename to modules/agent/judge.py diff --git a/commune/modules/agent/maker/agent_maker.py b/modules/agent/maker/agent_maker.py similarity index 100% rename from commune/modules/agent/maker/agent_maker.py rename to modules/agent/maker/agent_maker.py diff --git a/commune/modules/agent/sumarizer/agent_sumarizer.py b/modules/agent/sumarizer/agent_sumarizer.py similarity index 100% rename from commune/modules/agent/sumarizer/agent_sumarizer.py rename to modules/agent/sumarizer/agent_sumarizer.py diff --git a/commune/modules/api/api.py b/modules/api/api.py similarity index 100% rename from commune/modules/api/api.py rename to modules/api/api.py diff --git a/commune/modules/api/app.py b/modules/api/app.py similarity index 100% rename from commune/modules/api/app.py rename to modules/api/app.py diff --git a/commune/app.py b/modules/app/app.py similarity index 100% rename from commune/app.py rename to modules/app/app.py diff --git a/commune/modules/base/base.py b/modules/base/base.py similarity index 100% rename from commune/modules/base/base.py rename to modules/base/base.py diff --git a/commune/modules/chat/app.py b/modules/chat/app.py similarity index 100% rename from commune/modules/chat/app.py rename to modules/chat/app.py diff --git a/modules/chat/chat.py b/modules/chat/chat.py new file mode 100644 index 00000000..636e800b --- /dev/null +++ b/modules/chat/chat.py @@ -0,0 +1,52 @@ +import commune as c +import streamlit as st + +class Chat(c.Module): + + prompt = 'The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.', + + def __init__(self, + prompt = prompt, + model = None, + path='/.chat', + **kwargs): + + self.history = c.module('chat.history')(path=path) + self.prompt = prompt + self.model = c.module('model.openrouter')(model=model, **kwargs) + self.models = self.model.models() + + def get_model(self, model): + if model not in self.models: + c.print(f'Model Not Specified, Searching for {model} out of n={len(self.models)} models', color='yellow') + for m in self.models: + if model in m: + return model + return model + + def generate(self, + text = 'whats 2+2?' , + temperature= 0.5, + max_tokens= 1000000, + model= 'anthropic/claude-3.5-sonnet', + prompt= None, + stream=True, + headers = None, + ): + # text = self.process_text(text, prompt=prompt) + output = self.model.generate( text, stream=stream, model=model, + max_tokens=max_tokens, temperature=temperature ) + for token in output: + yield token + + def process_text(self, text, prompt=None): + if prompt: + text = prompt + text + num_tokens = len(text) + c.print(f'Sending {num_tokens} chars' ) + # find all of the @ and + print(text) + return text + + def ask(self, *text, **kwargs): + return self.generate(' '.join(map(str, text)), **kwargs) \ No newline at end of file diff --git a/modules/chat/history.py b/modules/chat/history.py new file mode 100644 index 00000000..0ffab61c --- /dev/null +++ b/modules/chat/history.py @@ -0,0 +1,61 @@ + +import commune as c +from typing import * +import os + +class History(c.Module): + + def __init__(self,path='history', **kwargs): + self.path = self.resolve_path(path) + + def check_data(self, data): + assert isinstance(data, dict) + + def get_path(self, user): + return f'{self.path}/{user}/{c.time()}.json' + + def get_path_dir(self, user): + return os.path.dirname(self.get_path(user)) + + def add_data(self, user, data): + self.check_data(data) + path = self.get_path(user) + return c.put_json(path, data) + + def get_data(self, user, search=None): + data = [c.get_json(f) for f in self.get_data_paths(user)] + if search: + data = [d for d in self.get_data(user) if search in str(d)] + return data + + def get_data_paths(self, user, reverse=True): + return sorted(c.ls(self.get_path_dir(user)), reverse=reverse) + + def pop_data(self, user, reverse=True): + return c.rm(self.get_data_paths(user, reverse=reverse)[-1]) + + def rm_data(self, user): + path = self.get_path_dir(user) + return c.rm(path) + + def get_n(self, user): + return len(self.get_data(user)) + + def rm_all_history(self): + return c.rm(self.path) + + def test(self, n=2, user = 'billy', data = {'data': 'my search history'}): + self.rm_data(user) + assert len(self.get_data(user)) == 0 + [self.add_data(user, data) for i in range(n)] + assert len(self.get_data(user)) == n + assert len(self.get_data(user, data['data'] )) == n + assert len(self.get_data(user, data['data'] * 2 )) == 0 + self.pop_data(user) + assert len(self.get_data(user)) == n - 1 + self.rm_data(user) + assert self.get_n(user) == 0 + return True + + + diff --git a/commune/modules/code/code.py b/modules/code/code.py similarity index 100% rename from commune/modules/code/code.py rename to modules/code/code.py diff --git a/commune/modules/data/__init__.py b/modules/data/__init__.py similarity index 100% rename from commune/modules/data/__init__.py rename to modules/data/__init__.py diff --git a/commune/modules/data/data.py b/modules/data/data.py similarity index 100% rename from commune/modules/data/data.py rename to modules/data/data.py diff --git a/commune/modules/data/data.yaml b/modules/data/data.yaml similarity index 100% rename from commune/modules/data/data.yaml rename to modules/data/data.yaml diff --git a/commune/modules/data/diffusion/dream/dream_dataset.py b/modules/data/diffusion/dream/dream_dataset.py similarity index 100% rename from commune/modules/data/diffusion/dream/dream_dataset.py rename to modules/data/diffusion/dream/dream_dataset.py diff --git a/commune/modules/data/diffusion/dream/prompt_dataset.py b/modules/data/diffusion/dream/prompt_dataset.py similarity index 100% rename from commune/modules/data/diffusion/dream/prompt_dataset.py rename to modules/data/diffusion/dream/prompt_dataset.py diff --git a/commune/modules/data/hf/data_hf.py b/modules/data/hf/data_hf.py similarity index 100% rename from commune/modules/data/hf/data_hf.py rename to modules/data/hf/data_hf.py diff --git a/commune/modules/data/hf/data_hf_docs.md b/modules/data/hf/data_hf_docs.md similarity index 100% rename from commune/modules/data/hf/data_hf_docs.md rename to modules/data/hf/data_hf_docs.md diff --git a/commune/modules/data/image/globe/data_image_globe.py b/modules/data/image/globe/data_image_globe.py similarity index 100% rename from commune/modules/data/image/globe/data_image_globe.py rename to modules/data/image/globe/data_image_globe.py diff --git a/commune/modules/data/image/globe/data_image_globe.yaml b/modules/data/image/globe/data_image_globe.yaml similarity index 100% rename from commune/modules/data/image/globe/data_image_globe.yaml rename to modules/data/image/globe/data_image_globe.yaml diff --git a/commune/modules/data/text/code/data_text_code.py b/modules/data/text/code/data_text_code.py similarity index 100% rename from commune/modules/data/text/code/data_text_code.py rename to modules/data/text/code/data_text_code.py diff --git a/commune/modules/data/text/code/data_text_code.yaml b/modules/data/text/code/data_text_code.yaml similarity index 100% rename from commune/modules/data/text/code/data_text_code.yaml rename to modules/data/text/code/data_text_code.yaml diff --git a/commune/modules/data/text/folder/data_text_folder.py b/modules/data/text/folder/data_text_folder.py similarity index 100% rename from commune/modules/data/text/folder/data_text_folder.py rename to modules/data/text/folder/data_text_folder.py diff --git a/commune/modules/data/text/folder/data_text_folder.yaml b/modules/data/text/folder/data_text_folder.yaml similarity index 100% rename from commune/modules/data/text/folder/data_text_folder.yaml rename to modules/data/text/folder/data_text_folder.yaml diff --git a/commune/modules/data/text/folder/docs/data_text_realfake_docs.md b/modules/data/text/folder/docs/data_text_realfake_docs.md similarity index 100% rename from commune/modules/data/text/folder/docs/data_text_realfake_docs.md rename to modules/data/text/folder/docs/data_text_realfake_docs.md diff --git a/commune/modules/data/text/math/data_text_math.py b/modules/data/text/math/data_text_math.py similarity index 100% rename from commune/modules/data/text/math/data_text_math.py rename to modules/data/text/math/data_text_math.py diff --git a/commune/modules/data/text/math/data_text_math.yaml b/modules/data/text/math/data_text_math.yaml similarity index 100% rename from commune/modules/data/text/math/data_text_math.yaml rename to modules/data/text/math/data_text_math.yaml diff --git a/commune/modules/data/text/pile/pile.py b/modules/data/text/pile/pile.py similarity index 100% rename from commune/modules/data/text/pile/pile.py rename to modules/data/text/pile/pile.py diff --git a/commune/modules/data/text/pile/pile.yaml b/modules/data/text/pile/pile.yaml similarity index 100% rename from commune/modules/data/text/pile/pile.yaml rename to modules/data/text/pile/pile.yaml diff --git a/commune/modules/data/text/realfake/data_text_realfake.py b/modules/data/text/realfake/data_text_realfake.py similarity index 100% rename from commune/modules/data/text/realfake/data_text_realfake.py rename to modules/data/text/realfake/data_text_realfake.py diff --git a/commune/modules/data/text/realfake/data_text_realfake.yaml b/modules/data/text/realfake/data_text_realfake.yaml similarity index 100% rename from commune/modules/data/text/realfake/data_text_realfake.yaml rename to modules/data/text/realfake/data_text_realfake.yaml diff --git a/commune/modules/data/text/realfake/docs/data_text_realfake_docs.md b/modules/data/text/realfake/docs/data_text_realfake_docs.md similarity index 100% rename from commune/modules/data/text/realfake/docs/data_text_realfake_docs.md rename to modules/data/text/realfake/docs/data_text_realfake_docs.md diff --git a/commune/modules/data/text/squad.py b/modules/data/text/squad.py similarity index 100% rename from commune/modules/data/text/squad.py rename to modules/data/text/squad.py diff --git a/commune/modules/data/text/truthqa/data_text_truthqa.py b/modules/data/text/truthqa/data_text_truthqa.py similarity index 100% rename from commune/modules/data/text/truthqa/data_text_truthqa.py rename to modules/data/text/truthqa/data_text_truthqa.py diff --git a/commune/modules/data/text/truthqa/data_text_truthqa.yaml b/modules/data/text/truthqa/data_text_truthqa.yaml similarity index 100% rename from commune/modules/data/text/truthqa/data_text_truthqa.yaml rename to modules/data/text/truthqa/data_text_truthqa.yaml diff --git a/commune/modules/docker/docker.py b/modules/docker/docker.py similarity index 100% rename from commune/modules/docker/docker.py rename to modules/docker/docker.py diff --git a/commune/modules/emoji/emoji.py b/modules/emoji/emoji.py similarity index 100% rename from commune/modules/emoji/emoji.py rename to modules/emoji/emoji.py diff --git a/commune/modules/git/git.py b/modules/git/git.py similarity index 100% rename from commune/modules/git/git.py rename to modules/git/git.py diff --git a/commune/modules/key/app/app.py b/modules/key/app/app.py similarity index 100% rename from commune/modules/key/app/app.py rename to modules/key/app/app.py diff --git a/commune/modules/key/app/style.css b/modules/key/app/style.css similarity index 100% rename from commune/modules/key/app/style.css rename to modules/key/app/style.css diff --git a/commune/modules/model/__init__.py b/modules/model/__init__.py similarity index 100% rename from commune/modules/model/__init__.py rename to modules/model/__init__.py diff --git a/commune/modules/model/diffusion.py b/modules/model/diffusion.py similarity index 100% rename from commune/modules/model/diffusion.py rename to modules/model/diffusion.py diff --git a/commune/modules/model/layer.py b/modules/model/layer.py similarity index 100% rename from commune/modules/model/layer.py rename to modules/model/layer.py diff --git a/commune/modules/model/model.py b/modules/model/model.py similarity index 100% rename from commune/modules/model/model.py rename to modules/model/model.py diff --git a/commune/modules/model/openai.py b/modules/model/openai.py similarity index 100% rename from commune/modules/model/openai.py rename to modules/model/openai.py diff --git a/commune/modules/model/openrouter.py b/modules/model/openrouter.py similarity index 86% rename from commune/modules/model/openrouter.py rename to modules/model/openrouter.py index ca691570..58a4afc9 100644 --- a/commune/modules/model/openrouter.py +++ b/modules/model/openrouter.py @@ -5,29 +5,16 @@ import commune as c class OpenRouter(c.Module): - system_prompt = r"""""" - + prompt = r"""""" + def __init__( self, model: str = 'anthropic/claude-3-haiku', - search = None, api_key = None, base_url: str | None = 'https://openrouter.ai/api/v1', timeout: float | None = None, max_retries: int = 10, ): - """ - Initialize the OpenAI with the specified model, API key, timeout, and max retries. - - Args: - model (OPENAI_MODES): The OpenAI model to use. - api_key (API_KEY): The API key for authentication. - base_url (str | None, optional): can be used for openrouter api calls - timeout (float | None, optional): The timeout value for the client. Defaults to None. - max_retries (int | None, optional): The maximum number of retries for the client. Defaults to None. - """ - super().__init__() - if api_key == None: api_key = self.get_api_key() @@ -46,7 +33,7 @@ def generate( message: str, *extra_text , history = None, - system_prompt: str = None, + prompt: str = None, stream: bool = False, model:str = None, max_tokens: int = 100000, @@ -66,8 +53,8 @@ def generate( if len(extra_text) > 0: message = message + ' '.join(extra_text) history = history or [] - system_prompt = system_prompt or self.system_prompt - message = message + system_prompt + prompt = prompt or self.prompt + message = message + prompt model = model or self.model c.print('Using Model --> ', model, color='cyan') messages = history.copy() diff --git a/commune/modules/model/sentence.py b/modules/model/sentence.py similarity index 100% rename from commune/modules/model/sentence.py rename to modules/model/sentence.py diff --git a/commune/modules/model/transformer.py b/modules/model/transformer.py similarity index 100% rename from commune/modules/model/transformer.py rename to modules/model/transformer.py diff --git a/commune/modules/model/utils.py b/modules/model/utils.py similarity index 100% rename from commune/modules/model/utils.py rename to modules/model/utils.py diff --git a/commune/modules/plotly/plotly.py b/modules/plotly/plotly.py similarity index 100% rename from commune/modules/plotly/plotly.py rename to modules/plotly/plotly.py diff --git a/commune/modules/process/pipe.py b/modules/process/pipe.py similarity index 100% rename from commune/modules/process/pipe.py rename to modules/process/pipe.py diff --git a/commune/modules/process/pool.py b/modules/process/pool.py similarity index 100% rename from commune/modules/process/pool.py rename to modules/process/pool.py diff --git a/commune/modules/process/process.py b/modules/process/process.py similarity index 100% rename from commune/modules/process/process.py rename to modules/process/process.py diff --git a/commune/modules/process/utils.py b/modules/process/utils.py similarity index 100% rename from commune/modules/process/utils.py rename to modules/process/utils.py diff --git a/commune/modules/ray/__init__.py b/modules/pyenv/pyenv.py old mode 100755 new mode 100644 similarity index 100% rename from commune/modules/ray/__init__.py rename to modules/pyenv/pyenv.py diff --git a/commune/modules/tool/__init__.py b/modules/ray/__init__.py old mode 100644 new mode 100755 similarity index 100% rename from commune/modules/tool/__init__.py rename to modules/ray/__init__.py diff --git a/commune/modules/ray/actor_pool.py b/modules/ray/actor_pool.py similarity index 100% rename from commune/modules/ray/actor_pool.py rename to modules/ray/actor_pool.py diff --git a/commune/modules/ray/client/ray_client.py b/modules/ray/client/ray_client.py similarity index 100% rename from commune/modules/ray/client/ray_client.py rename to modules/ray/client/ray_client.py diff --git a/commune/modules/ray/client/ray_client.yaml b/modules/ray/client/ray_client.yaml similarity index 100% rename from commune/modules/ray/client/ray_client.yaml rename to modules/ray/client/ray_client.yaml diff --git a/commune/modules/ray/queue.py b/modules/ray/queue.py similarity index 100% rename from commune/modules/ray/queue.py rename to modules/ray/queue.py diff --git a/commune/modules/ray/ray.py b/modules/ray/ray.py similarity index 100% rename from commune/modules/ray/ray.py rename to modules/ray/ray.py diff --git a/commune/modules/ray/server/object/ray_object_server.py b/modules/ray/server/object/ray_object_server.py similarity index 100% rename from commune/modules/ray/server/object/ray_object_server.py rename to modules/ray/server/object/ray_object_server.py diff --git a/commune/modules/ray/server/object/ray_object_server.yaml b/modules/ray/server/object/ray_object_server.yaml similarity index 100% rename from commune/modules/ray/server/object/ray_object_server.yaml rename to modules/ray/server/object/ray_object_server.yaml diff --git a/commune/modules/ray/server/queue/ray_queue_server.py b/modules/ray/server/queue/ray_queue_server.py similarity index 100% rename from commune/modules/ray/server/queue/ray_queue_server.py rename to modules/ray/server/queue/ray_queue_server.py diff --git a/commune/modules/ray/server/queue/ray_queue_server.yaml b/modules/ray/server/queue/ray_queue_server.yaml similarity index 100% rename from commune/modules/ray/server/queue/ray_queue_server.yaml rename to modules/ray/server/queue/ray_queue_server.yaml diff --git a/commune/modules/ray/server/redis/ray_redis_server.py b/modules/ray/server/redis/ray_redis_server.py similarity index 100% rename from commune/modules/ray/server/redis/ray_redis_server.py rename to modules/ray/server/redis/ray_redis_server.py diff --git a/commune/modules/ray/server/redis/ray_redis_server.yaml b/modules/ray/server/redis/ray_redis_server.yaml similarity index 100% rename from commune/modules/ray/server/redis/ray_redis_server.yaml rename to modules/ray/server/redis/ray_redis_server.yaml diff --git a/commune/modules/ray/utils.py b/modules/ray/utils.py similarity index 100% rename from commune/modules/ray/utils.py rename to modules/ray/utils.py diff --git a/commune/modules/remote/.gitignore b/modules/remote/.gitignore similarity index 100% rename from commune/modules/remote/.gitignore rename to modules/remote/.gitignore diff --git a/commune/modules/remote/README.md b/modules/remote/README.md similarity index 100% rename from commune/modules/remote/README.md rename to modules/remote/README.md diff --git a/commune/modules/remote/app.py b/modules/remote/app.py similarity index 100% rename from commune/modules/remote/app.py rename to modules/remote/app.py diff --git a/commune/modules/remote/data/.gitkeep b/modules/remote/data/.gitkeep similarity index 100% rename from commune/modules/remote/data/.gitkeep rename to modules/remote/data/.gitkeep diff --git a/commune/modules/remote/remote.py b/modules/remote/remote.py similarity index 100% rename from commune/modules/remote/remote.py rename to modules/remote/remote.py diff --git a/commune/modules/remote/ssh.py b/modules/remote/ssh.py similarity index 100% rename from commune/modules/remote/ssh.py rename to modules/remote/ssh.py diff --git a/commune/modules/repo/.gitignore b/modules/repo/.gitignore similarity index 100% rename from commune/modules/repo/.gitignore rename to modules/repo/.gitignore diff --git a/commune/modules/repo/repo.py b/modules/repo/repo.py similarity index 100% rename from commune/modules/repo/repo.py rename to modules/repo/repo.py diff --git a/commune/modules/router/dashboard/router_dashboard.py b/modules/router/dashboard/router_dashboard.py similarity index 100% rename from commune/modules/router/dashboard/router_dashboard.py rename to modules/router/dashboard/router_dashboard.py diff --git a/commune/modules/router/ex.py b/modules/router/ex.py similarity index 100% rename from commune/modules/router/ex.py rename to modules/router/ex.py diff --git a/commune/modules/router/router.py b/modules/router/router.py similarity index 100% rename from commune/modules/router/router.py rename to modules/router/router.py diff --git a/commune/modules/router/router_v0.py b/modules/router/router_v0.py similarity index 100% rename from commune/modules/router/router_v0.py rename to modules/router/router_v0.py diff --git a/commune/modules/router/task.py b/modules/router/task.py similarity index 100% rename from commune/modules/router/task.py rename to modules/router/task.py diff --git a/commune/modules/router/worker.py b/modules/router/worker.py similarity index 100% rename from commune/modules/router/worker.py rename to modules/router/worker.py diff --git a/commune/modules/sandbox.py b/modules/sandbox.py similarity index 100% rename from commune/modules/sandbox.py rename to modules/sandbox.py diff --git a/commune/modules/ssh.py b/modules/ssh.py similarity index 100% rename from commune/modules/ssh.py rename to modules/ssh.py diff --git a/commune/modules/storage/storage.py b/modules/storage/storage.py similarity index 100% rename from commune/modules/storage/storage.py rename to modules/storage/storage.py diff --git a/commune/modules/storage/vali.py b/modules/storage/vali.py similarity index 100% rename from commune/modules/storage/vali.py rename to modules/storage/vali.py diff --git a/commune/modules/storage/vector/vector_store.py b/modules/storage/vector/vector_store.py similarity index 100% rename from commune/modules/storage/vector/vector_store.py rename to modules/storage/vector/vector_store.py diff --git a/commune/modules/storage/vector/vector_store.yaml b/modules/storage/vector/vector_store.yaml similarity index 100% rename from commune/modules/storage/vector/vector_store.yaml rename to modules/storage/vector/vector_store.yaml diff --git a/commune/modules/streamlit/__init__.py b/modules/streamlit/__init__.py similarity index 100% rename from commune/modules/streamlit/__init__.py rename to modules/streamlit/__init__.py diff --git a/commune/modules/streamlit/auth/config_template.yaml b/modules/streamlit/auth/config_template.yaml similarity index 100% rename from commune/modules/streamlit/auth/config_template.yaml rename to modules/streamlit/auth/config_template.yaml diff --git a/commune/modules/streamlit/auth/streamlit_auth.py b/modules/streamlit/auth/streamlit_auth.py similarity index 100% rename from commune/modules/streamlit/auth/streamlit_auth.py rename to modules/streamlit/auth/streamlit_auth.py diff --git a/commune/modules/streamlit/plot.py b/modules/streamlit/plot.py similarity index 100% rename from commune/modules/streamlit/plot.py rename to modules/streamlit/plot.py diff --git a/commune/modules/streamlit/streamlit.py b/modules/streamlit/streamlit.py similarity index 100% rename from commune/modules/streamlit/streamlit.py rename to modules/streamlit/streamlit.py diff --git a/commune/modules/streamlit/styles/commune.css b/modules/streamlit/styles/commune.css similarity index 100% rename from commune/modules/streamlit/styles/commune.css rename to modules/streamlit/styles/commune.css diff --git a/commune/modules/streamlit/utils.py b/modules/streamlit/utils.py similarity index 100% rename from commune/modules/streamlit/utils.py rename to modules/streamlit/utils.py diff --git a/commune/modules/streamlit/watchdog/streamlit_watchdog.py b/modules/streamlit/watchdog/streamlit_watchdog.py similarity index 100% rename from commune/modules/streamlit/watchdog/streamlit_watchdog.py rename to modules/streamlit/watchdog/streamlit_watchdog.py diff --git a/commune/modules/streamlit/watchdog/streamlit_watchdog.yaml b/modules/streamlit/watchdog/streamlit_watchdog.yaml similarity index 100% rename from commune/modules/streamlit/watchdog/streamlit_watchdog.yaml rename to modules/streamlit/watchdog/streamlit_watchdog.yaml diff --git a/modules/template.py b/modules/template.py new file mode 100644 index 00000000..d276fe61 --- /dev/null +++ b/modules/template.py @@ -0,0 +1,22 @@ +import commune as c + +class Custom: + def __init__(self,fam=1): + pass + def testfjdjfhh(self, a=1, b=2): + return a+b + +class Template(c.Module): + @classmethod + def from_object(cls, obj=Custom): + if isinstance(obj, str): + obj = c.import_object(obj) + module_name = obj.__name__.lower() + cls.module_name = lambda *args, **kwargs : module_name + cls._obj = obj + assert not c.module_exists(obj), f'{obj} already exists' + cls.__name__ = obj.__name__ + cls.__doc__ = obj.__doc__ + cls.__str__ = lambda *args, **kwargs : obj.__str__() + return str(cls) + diff --git a/commune/modules/test.py b/modules/test.py similarity index 100% rename from commune/modules/test.py rename to modules/test.py diff --git a/commune/modules/tool/defi/__init__.py b/modules/tool/__init__.py similarity index 100% rename from commune/modules/tool/defi/__init__.py rename to modules/tool/__init__.py diff --git a/commune/modules/tool/compare_token_price.py b/modules/tool/compare_token_price.py similarity index 100% rename from commune/modules/tool/compare_token_price.py rename to modules/tool/compare_token_price.py diff --git a/commune/modules/tool/compound.py b/modules/tool/compound.py similarity index 100% rename from commune/modules/tool/compound.py rename to modules/tool/compound.py diff --git a/commune/urls.yaml b/modules/tool/defi/__init__.py similarity index 100% rename from commune/urls.yaml rename to modules/tool/defi/__init__.py diff --git a/commune/modules/tool/defi/aave.py b/modules/tool/defi/aave.py similarity index 100% rename from commune/modules/tool/defi/aave.py rename to modules/tool/defi/aave.py diff --git a/commune/modules/tool/defi/compare_token_price.py b/modules/tool/defi/compare_token_price.py similarity index 100% rename from commune/modules/tool/defi/compare_token_price.py rename to modules/tool/defi/compare_token_price.py diff --git a/commune/modules/tool/defi/compound.py b/modules/tool/defi/compound.py similarity index 100% rename from commune/modules/tool/defi/compound.py rename to modules/tool/defi/compound.py diff --git a/commune/modules/tool/defi/defillama/aave.py b/modules/tool/defi/defillama/aave.py similarity index 100% rename from commune/modules/tool/defi/defillama/aave.py rename to modules/tool/defi/defillama/aave.py diff --git a/commune/modules/tool/defi/defillama/defillama.py b/modules/tool/defi/defillama/defillama.py similarity index 100% rename from commune/modules/tool/defi/defillama/defillama.py rename to modules/tool/defi/defillama/defillama.py diff --git a/commune/modules/tool/defi/defillama/lido.py b/modules/tool/defi/defillama/lido.py similarity index 100% rename from commune/modules/tool/defi/defillama/lido.py rename to modules/tool/defi/defillama/lido.py diff --git a/commune/modules/tool/defi/defillama/rocketpool.py b/modules/tool/defi/defillama/rocketpool.py similarity index 100% rename from commune/modules/tool/defi/defillama/rocketpool.py rename to modules/tool/defi/defillama/rocketpool.py diff --git a/commune/modules/tool/defi/get_best_apy.py b/modules/tool/defi/get_best_apy.py similarity index 100% rename from commune/modules/tool/defi/get_best_apy.py rename to modules/tool/defi/get_best_apy.py diff --git a/commune/modules/tool/defi/inch/balances.py b/modules/tool/defi/inch/balances.py similarity index 100% rename from commune/modules/tool/defi/inch/balances.py rename to modules/tool/defi/inch/balances.py diff --git a/commune/modules/tool/defi/inch/gasprice.py b/modules/tool/defi/inch/gasprice.py similarity index 100% rename from commune/modules/tool/defi/inch/gasprice.py rename to modules/tool/defi/inch/gasprice.py diff --git a/commune/modules/tool/defi/inch/inch.py b/modules/tool/defi/inch/inch.py similarity index 100% rename from commune/modules/tool/defi/inch/inch.py rename to modules/tool/defi/inch/inch.py diff --git a/commune/modules/tool/defi/inch/prices.py b/modules/tool/defi/inch/prices.py similarity index 100% rename from commune/modules/tool/defi/inch/prices.py rename to modules/tool/defi/inch/prices.py diff --git a/commune/modules/tool/defi/openai_helper.py b/modules/tool/defi/openai_helper.py similarity index 100% rename from commune/modules/tool/defi/openai_helper.py rename to modules/tool/defi/openai_helper.py diff --git a/commune/modules/tool/defi/read_file.py b/modules/tool/defi/read_file.py similarity index 100% rename from commune/modules/tool/defi/read_file.py rename to modules/tool/defi/read_file.py diff --git a/commune/modules/tool/defi/swap.py b/modules/tool/defi/swap.py similarity index 100% rename from commune/modules/tool/defi/swap.py rename to modules/tool/defi/swap.py diff --git a/commune/modules/tool/defi/tool.py b/modules/tool/defi/tool.py similarity index 100% rename from commune/modules/tool/defi/tool.py rename to modules/tool/defi/tool.py diff --git a/commune/modules/tool/defi/write_file.py b/modules/tool/defi/write_file.py similarity index 100% rename from commune/modules/tool/defi/write_file.py rename to modules/tool/defi/write_file.py diff --git a/commune/modules/tool/get_best_apy.py b/modules/tool/get_best_apy.py similarity index 100% rename from commune/modules/tool/get_best_apy.py rename to modules/tool/get_best_apy.py diff --git a/commune/modules/tool/openai_helper.py b/modules/tool/openai_helper.py similarity index 100% rename from commune/modules/tool/openai_helper.py rename to modules/tool/openai_helper.py diff --git a/commune/modules/tool/read_file.py b/modules/tool/read_file.py similarity index 100% rename from commune/modules/tool/read_file.py rename to modules/tool/read_file.py diff --git a/commune/modules/tool/registry.py b/modules/tool/registry.py similarity index 100% rename from commune/modules/tool/registry.py rename to modules/tool/registry.py diff --git a/commune/modules/tool/search/tool_search.py b/modules/tool/search/tool_search.py similarity index 100% rename from commune/modules/tool/search/tool_search.py rename to modules/tool/search/tool_search.py diff --git a/commune/modules/tool/swap.py b/modules/tool/swap.py similarity index 100% rename from commune/modules/tool/swap.py rename to modules/tool/swap.py diff --git a/commune/modules/tool/tool.py b/modules/tool/tool.py similarity index 100% rename from commune/modules/tool/tool.py rename to modules/tool/tool.py diff --git a/commune/modules/tool/web/web.py b/modules/tool/web/web.py similarity index 100% rename from commune/modules/tool/web/web.py rename to modules/tool/web/web.py diff --git a/commune/modules/tool/web/web.yaml b/modules/tool/web/web.yaml similarity index 100% rename from commune/modules/tool/web/web.yaml rename to modules/tool/web/web.yaml diff --git a/commune/modules/tool/write_file.py b/modules/tool/write_file.py similarity index 100% rename from commune/modules/tool/write_file.py rename to modules/tool/write_file.py diff --git a/commune/modules/trainer/__init__.py b/modules/trainer/__init__.py similarity index 100% rename from commune/modules/trainer/__init__.py rename to modules/trainer/__init__.py diff --git a/commune/modules/trainer/trainer.py b/modules/trainer/trainer.py similarity index 100% rename from commune/modules/trainer/trainer.py rename to modules/trainer/trainer.py diff --git a/commune/modules/web/web.py b/modules/web/web.py similarity index 100% rename from commune/modules/web/web.py rename to modules/web/web.py diff --git a/commune/modules/web/web.yaml b/modules/web/web.yaml similarity index 100% rename from commune/modules/web/web.yaml rename to modules/web/web.yaml diff --git a/commune/tests/README.md b/tests/README.md similarity index 100% rename from commune/tests/README.md rename to tests/README.md diff --git a/commune/tests/test_key.py b/tests/test_key.py similarity index 100% rename from commune/tests/test_key.py rename to tests/test_key.py diff --git a/commune/tests/test_serializer.py b/tests/test_serializer.py similarity index 100% rename from commune/tests/test_serializer.py rename to tests/test_serializer.py diff --git a/commune/tests/test_server.py b/tests/test_server.py similarity index 76% rename from commune/tests/test_server.py rename to tests/test_server.py index 48ab1747..0b4da89e 100644 --- a/commune/tests/test_server.py +++ b/tests/test_server.py @@ -17,11 +17,13 @@ def test_basics() -> dict: def test_serving(server_name = 'module::test'): if server_name in c.servers(): c.kill(server_name) + module = c.serve(server_name) c.wait_for_server(server_name) module = c.connect(server_name) r = module.info() - assert 'name' in r, f"get failed {v}" + print(r, 'FAM') + assert 'name' in r, f"get failed {r}" c.kill(server_name) assert server_name not in c.servers() return {'success': True, 'msg': 'server test passed'} @@ -42,6 +44,7 @@ def test_serving_with_different_key(module = 'module'): c.sleep(1) c.print('waiting for server {}'.format(module_name)) info = c.connect(module_name).info() + print(info) assert info['key'] == key.ss58_address, f"key failed {key.ss58_address} != {info['key']}" c.kill(module_name) c.rm_key(key_name) @@ -51,18 +54,12 @@ def test_serving_with_different_key(module = 'module'): def test_namespace(): - cls = c.module('namespace') - network = 'test_namespace' - cls.rm_namespace(network) - namespace = cls.namespace(network=network) - assert cls.namespace(network=network) == {}, f'Namespace not empty., {namespace}' + cls = c.module('namespace')() name = 'test' address = '0.0.0.0:8888' - cls.register_server(name=name, address=address, network=network) - namespace = cls.namespace(network=network) + c.register_server(name=name, address=address) + namespace = c.get_namespace() assert namespace[name] == address, f'Namespace not updated. {namespace}' - cls.deregister_server(name, network=network) - assert cls.namespace(network=network) == {} - cls.rm_namespace(network) - assert cls.namespace_exists(network) == False + c.deregister_server(name=name) + assert not c.server_exists(name) return {'success': True, 'msg': 'Namespace tests passed.'} \ No newline at end of file diff --git a/commune/tests/test_subspace.py b/tests/test_subspace.py similarity index 100% rename from commune/tests/test_subspace.py rename to tests/test_subspace.py diff --git a/commune/tests/test_user.py b/tests/test_user.py similarity index 100% rename from commune/tests/test_user.py rename to tests/test_user.py diff --git a/commune/tests/test_validator.py b/tests/test_validator.py similarity index 100% rename from commune/tests/test_validator.py rename to tests/test_validator.py