Skip to content

Commit

Permalink
fuck it up
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Dec 10, 2024
1 parent 60a1255 commit cd1165d
Show file tree
Hide file tree
Showing 21 changed files with 822 additions and 426 deletions.
8 changes: 2 additions & 6 deletions commune/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ def determine_type(x):
except ValueError:
pass
return x
def forward(argv = None,
sep = '--',
fn_splitters = [':', '/', '//', '::'],
base = 'module',
helper_fns = ['code', 'schema', 'fn_schema', 'help', 'fn_info', 'fn_hash'],
default_fn = 'vs'):

def forward(argv = None, sep = '--', fn_splitters = [':', '/', '//', '::'], base = 'module', helper_fns = ['code', 'schema', 'fn_schema', 'help', 'fn_info', 'fn_hash'], default_fn = 'vs'):

t0 = time.time()
argv = argv or sys.argv[1:]
Expand Down
144 changes: 48 additions & 96 deletions commune/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,38 @@
import asyncio
import json
import requests
import os
import commune as c

class Client(c.Module):
network2namespace = {}
stream_prefix = 'data: '

def __init__(
self,
module : str = 'module',
def __init__( self, module : str = 'module',
network: Optional[bool] = 'local',
mode: Optional[str] = 'http',
key : Optional[str]= None ,
serializer: Optional[c.Module] = 'serializer',
**kwargs
):
self.serializer = c.module('serializer')()
self.network = network
self.loop = c.get_event_loop()
self.serializer = c.module(serializer)()
self.key = c.get_key(key, create_if_not_exists=True)
self.module = module
self.address = self.resolve_module_address(module)
self.session = requests.Session()
self.set_address(module, network=network, mode=mode)

def set_address(self, module, network='local', mode='http'):
if c.is_address(module):
address = module
else:
namespace = c.namespace(network=network)
if module in namespace:
address = namespace[module]
else:
raise Exception(f'Module {module} not found in namespace {namespace}')
prefix = f'{mode}://'
self.network = network
self.mode = mode
self.address = prefix + address if not address.startswith(prefix) else address
self.session = requests.Session()

@classmethod
def call(cls,
Expand Down Expand Up @@ -65,26 +76,7 @@ def test(self, module='module::test_client'):
key = c.get_key(module)
assert info['key'] == key.ss58_address
return {'info': info, 'key': str(key)}

def __str__ ( self ):
return "Client(address={})".format(self.address)
def __repr__ ( self ):
return self.__str__()

def __repr__(self) -> str:
return super().__repr__()

def resolve_module_address(self, module, mode='http'):
if c.is_address(module):
url = module
else:
namespace = c.namespace(network=self.network)
if module in namespace:
url = namespace[module]
else:
raise Exception(f'Module {module} not found in namespace {namespace}')
url = f'{mode}://' + url if not url.startswith(f'{mode}://') else url
return url


def get_url(self, fn, mode='http'):
if '/' in str(fn):
Expand Down Expand Up @@ -133,32 +125,13 @@ def get_data(self, args=[], kwargs={}, **extra_kwargs):
data = self.serializer.serialize(data)
return data

def forward(self,
fn = 'info',
args : str = [],
kwargs : str = {},
timeout:int=2,
key : str = None,
mode: str = 'http',
headers = None,
data = None,
**extra_kwargs):
def forward(self, fn = 'info', args : str = [], kwargs : str = {},
timeout:int=2, key : str = None, mode: str = 'http', data=None, headers = None, **extra_kwargs):
key = self.resolve_key(key)
url = self.get_url(fn=fn, mode=mode)
data = data or self.get_data(args=args, kwargs=kwargs,**extra_kwargs)
headers = {
'Content-Type': 'application/json',
'key': key.ss58_address,
'hash': c.hash(data),
'crypto_type': str(key.crypto_type),
'time': str(c.time())
}

headers['signature'] = key.sign({'data': headers['hash'], 'time': headers['time']}).hex()
return self.request(url=url,
data=data,
headers=headers,
timeout=timeout)
data = data or self.get_data(args=args, kwargs=kwargs,**extra_kwargs)
headers = headers or self.get_header(data=data, key=key)
return self.request(url=url, data=data,headers=headers, timeout=timeout)

def __del__(self):
try:
Expand Down Expand Up @@ -225,7 +198,19 @@ def __getattr__(self, key):
return getattr(self, key)
else:
return lambda *args, **kwargs : self.remote_call(*args, remote_fn=key, **kwargs)




def get_header(self, data, key):
headers = {
'Content-Type': 'application/json',
'key': key.ss58_address,
'crypto_type': str(key.crypto_type),
'time': str(c.time()),
}
headers['signature'] = key.sign({'data': data, 'time': headers['time']}).hex()

return headers

def forcurl(self,
fn: str = 'info',
Expand All @@ -234,47 +219,19 @@ def forcurl(self,
timeout: int = 2,
key: str = None,
**extra_kwargs) -> str:
"""
Generate a cURL command for the equivalent HTTP request
Args:
fn (str): Function name to call
args (list): Arguments list
kwargs (dict): Keyword arguments
timeout (int): Request timeout in seconds
key (str): Key for authentication
**extra_kwargs: Additional keyword arguments
Returns:
str: cURL command string
"""
# Resolve the key and URL
key = self.resolve_key(key)
url = self.get_url(fn=fn)

# Prepare the data
data = self.get_data(args=args or [], kwargs=kwargs or {}, **extra_kwargs)

headers = self.get_header(data=data, key=key)
# Prepare headers
headers = {
'Content-Type': 'application/json',
'key': key.ss58_address,
'hash': c.hash(data),
'crypto_type': str(key.crypto_type),
'time': str(c.time())
}

# Add signature
headers['signature'] = key.sign({
'data': headers['hash'],
'time': headers['time']
}).hex()


# Build curl command
curl_cmd = ['curl']

# Add method
curl_cmd.append('-X POST')
curl_cmd = ['curl', '-X POST']

# Add headers
for header_name, header_value in headers.items():
Expand All @@ -286,18 +243,13 @@ def forcurl(self,
else:
data_str = json.dumps(data)
curl_cmd.append(f"-d '{data_str}'")

# Add URL
curl_cmd.append(f"'{url}'")

# Add timeout
curl_cmd.append(f'--max-time {timeout}')

# now get the dict of the response and return it
# make the request in the os and return the response
import os
response = os.popen(' '.join(curl_cmd)).read()


return response



def __str__ ( self ):
return "Client(address={})".format(self.address)
def __repr__ ( self ):
return self.__str__()
41 changes: 29 additions & 12 deletions commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
nest_asyncio.apply()

class c:
free = False
libname = lib = __file__.split('/')[-2]# the name of the library
endpoints = ['ask', 'generate', 'forward']
core_features = ['module_name', 'module_class', 'filepath', 'dirpath', 'tree']
Expand Down Expand Up @@ -208,6 +209,12 @@ def resolve_object(cls, obj:str = None, **kwargs):
def pwd(cls):
pwd = os.getcwd() # the current wor king directory from the process starts
return pwd

def help(self, module, *question):
code = c.code(module)
question = ' '.join(question)
prompt = f" {code} {question}"
return c.ask(prompt)

@classmethod
def argparse(cls):
Expand Down Expand Up @@ -437,6 +444,15 @@ def utils(cls, search=None):
utils = [u for u in utils if search in u]
return sorted(utils)



@classmethod
def util2code(cls, search=None):
utils = cls.utils()
util2code = {}
for f in utils:
util2code[f] = c.code(f)
return len(str(util2code))
@classmethod
def get_utils(cls, search=None):
utils = c.find_functions(c.rootpath + '/utils')
Expand All @@ -450,15 +466,7 @@ def num_utils(cls, search=None):
return len(cls.utils(search))

cache = {}
@classmethod
def util2code(cls, search=None):
utils = cls.utils()
util2code = {}
for f in utils:
if search != None:
if search in f:
util2code[f] = c.fn_code(f)
return util2code


@classmethod
def util2path(cls, search=None):
Expand Down Expand Up @@ -1111,7 +1119,7 @@ def get_parents(cls, obj = None,recursive=True, avoid_classes=['object']) -> Lis
def schema(cls, fn:str = '__init__', **kwargs)->dict:
'''
Get function schema of function in cls
'''
'''
schema = {}
fn = cls.get_fn(fn)
for k,v in dict(inspect.signature(fn)._parameters).items():
Expand Down Expand Up @@ -1313,6 +1321,13 @@ def is_fn(cls, fn, splitters = [':', '/', '.']):
print('Error in is_fn:', e, fn)
return False
return callable(fn)

def fn(self, fn:str):
if '/' in fn:
module , fn = fn.split('/')
module = c.module(module)
return getattr(module, fn)
return self.get_fn(fn)(*args, **kwargs)

@classmethod
def get_fn(cls, fn:str, splitters=[":", "/"]) -> 'Callable':
Expand Down Expand Up @@ -1759,7 +1774,7 @@ def has_app(cls, module:str, **kwargs) -> bool:

@classmethod
def get_path(cls, module:str, **kwargs) -> bool:
return c.module(module).filepath()
return c.filepath(module, **kwargs)

@classmethod
def objectpath2name(cls, p,
Expand Down Expand Up @@ -2333,7 +2348,9 @@ def epoch(self, *args, **kwargs):
"generate",
"models"
],
"chat": ["ask", "models", "pricing", "model2info"]
"chat": ["ask", "models", "pricing", "model2info", "reduce"],
"builder": ["build"],
"summary": ["reduce"]
}
c.add_routes()
Module = c # Module is alias of c
Expand Down
7 changes: 5 additions & 2 deletions commune/network/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def modules(self,
return modules

def namespace(self, search=None, max_age:int = tempo, update:bool = False, **kwargs) -> dict:
return {m['name']: m['address'] for m in self.modules(search=search, max_age=max_age, update=update)}
return {m['name']: '0.0.0.0' + ':' + m['address'].split(':')[-1] for m in self.modules(search=search, max_age=max_age, update=update)}

def register_server(self, name:str, address:str, key:str) -> None:
data = {'name': name, 'address': address, 'key': key}
Expand Down Expand Up @@ -103,7 +103,10 @@ def networks(self, module_prefix:str='network') -> List[str]:
networks.append(network)
networks = sorted(list(set(networks)))
return networks


def infos(self, *args, **kwargs) -> Dict:
return [c.call(address+'/info') for name, address in self.namespace(*args, **kwargs).items()]

Network.run(__name__)


8 changes: 7 additions & 1 deletion commune/network/subspace/subspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ def set_network(self,
self.network = network
self.set_connections(num_connections)
self.connection_latency = c.time() - t0
c.print(f'Network(name={self.network} url={self.url} connections={self.num_connections} latency={c.round(self.connection_latency, 2)})', color='blue')
network_state = {
"network": self.network,
"url": self.url,
"connections": self.num_connections,
"latency": self.connection_latency,
}
c.print(f'NETWORK(name={self.network} url={self.url} cons={self.num_connections} lat={c.round(self.connection_latency, 2)}s)', color='blue')

def set_connections(self, num_connections: int):
self.connections_queue = queue.Queue(num_connections)
Expand Down
Loading

0 comments on commit cd1165d

Please sign in to comment.