ch3njust1n / smart

Self-modifying code at runtime with Large Language Models
MIT License
4 stars 0 forks source link

[Metaprogramming] Metapython LLM #65

Open ch3njust1n opened 1 year ago

ch3njust1n commented 1 year ago

Factory function that given a data structure, it returns a new generative data structure e.g.

from collections import deque
from generative.structures import convert

q = convert(deque)

Under the hood it could be implemented as follows:

import builtins
from generative import adapt

vars().update({k: adapt.adapt(v) if callable(v) else v for k, v in vars(builtins).items() if not k.startswith("__")})

Import all python modules:

import os
import importlib
import sys

# Get the path of Python's lib directory
lib_path = os.path.dirname(os.__file__)

# Get a list of all .py files in lib directory
module_files = [f for f in os.listdir(lib_path) if f.endswith('.py')]

# Remove the .py extension to get the module names
module_names = [f[:-3] for f in module_files]

# Import the modules
native_modules = []
for name in module_names:
    try:
        module = importlib.import_module(name)
        native_modules.append(module)
    except:
        pass  # If a module fails to import, just skip it

print(native_modules)  # This prints the list of imported native modules

Then can modify all native functionality with generative.adapt(<place functionality here>), but would need to modify the comprehension so that it recursively applies @adapt to functions of objects or use metaclasses.generate().