GithubHelp home page GithubHelp logo

ch3njust1n / smart Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 2.58 MB

Self-modifying code at runtime with Large Language Models

License: MIT License

Python 100.00%
adaptable large-language-models metaprogramming self-modifying-code

smart's Issues

[DevOps] Add mypy

  1. pip install mypy
  2. Add to setup.py dev section
  3. Update pre-commit-config.yaml
  4. Update github workflow dynamic.yaml

[Metaprogramming] _is_generative

_is_generative property in decorators and metaclasses indicate a function or class has access to a generative model.
Currently unclear how this could be used, but it gives the rest of the program information on what is generative and what is now.

Here is an example that could be used so that a LLM knows it can also manipulate other parts of the program:

import inspect
import textwrap
from meta import gpt4

all_methods = inspect.getmembers(
    cls, predicate=inspect.isfunction
)
available_funcs = [
    (
        method[0],
        textwrap.dedent(inspect.getsource(method[1])),
    )
    for method in all_methods if method._is_generative
]

func_source = gpt4(f'Generate code with these generative functions: {available_funcs}')
  • Add unit tests
  • Documentation

[Metaprogramming] Monkey patching

Definition: This involves changing or extending the behavior of code at runtime, typically by adding, replacing, or modifying methods or attributes in a class or object.

Example:

class MyClass:
    pass

def new_method(self):
    print("New method")

MyClass.new_method = new_method
instance = MyClass()
instance.new_method() # Prints: "New method"

Task:

  • LLM generates the new_method and returns a pointer to the function from exec() and assigns to MyClass.new_method
  • Add example to documentation

[Metaprogramming] Dynamic creation of classes

Definition: You can create classes dynamically using the type function.

Example:

def my_method(self):
    return self.x * 2

MyClass = type('MyClass', (object,), {'x': 10, 'my_method': my_method})
instance = MyClass()
print(instance.x)  # Output: 10
print(instance.my_method())  # Output: 20

Task:

  • LLM generates the parameters given to type()
  • Add example to documentation

Refactor out OpenAI api

Allow user to provide their own callback to decorator so they can specify their own LLM solution

[Metaprogramming] Class decorators

Definition: These are similar to function decorators, but they modify or extend classes instead.

Example:

def my_class_decorator(cls):
    cls.decorated = True
    return cls

@my_class_decorator
class MyClass:
    pass

print(MyClass.decorated)  # Output: True

instance = MyClass()
print(instance.decorated)  # Output: True

Task:

  • LLM generates the decorators properties and values given the current class definition. Similar to function decorators.
  • Add example to documentation

[LLM] Check syntax of generated code

Use python's ast package to check the syntax of generated code at runtime.

import ast

def is_code_correct(code):
    try:
        ast.parse(code)
        return True
    except SyntaxError:
        return False

# test
print(is_code_correct('print("Hello, world!")'))  # True
print(is_code_correct('print("Hello, world!'))  # False - missing closing quote
  • Unit tests
  • Documentation

[Metaprogramming] Metapython LLM

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().

[QA] Generate unit tests

Generate a large unit test/ benchmark dataset for paper. Tests should be random and range in complexity.

[Metaprogramming] Metaclasses

Definition: A metaclass is the class of a class, which Python uses to create and control classes just like classes create and control objects. The default metaclass is type, but you can create your own metaclass by inheriting from type.

Example:

class MyMeta(type):
    def __init__(cls, name, bases, dct):
        cls.class_created_by = "MyMeta"
        super().__init__(name, bases, dct)

class MyClass(metaclass=MyMeta):
    pass

print(MyClass.class_created_by)  # Output: MyMeta

Task:

  • LLM generates metaclass properties
  • Add example to documentation

[Paper] Write code injection section

  1. What is code injection
  2. Types of code injection and examples
  3. How to execute a code injection
  4. Code injection with respect to metaprogramming
  5. How to prevent code injections
  6. How we're using code injection
  7. Citations (find papers on machine learning and cybersecurity or related papers)

[LLM] Semantic Checker

Use GPT-4 to generate code and a faster model with more context like claude-instant-v1-100k.
User should be able to define their own semantic checking model and the metaprogramming API should optionally expect a semantic checker as input along with the generating model (model input parameter).

  • Unit test
  • Documentation

[Paper] Outline

Dynamic Metaprogramming with Large Language Models

Sections:

  1. Abstract

  2. Introduction

  • Metaprogramming
    -- What is metaprogramming
    -- Types of metaprogramming
    -- Why use metaprogramming

  • Code injection
    -- What is code injection
    -- What are the different types of code injection
    -- How to mitigate most common attacks
    -- How does this relate to metaprogramming

  1. Related work

  2. Dynamic Metaprogramming with Large Language Models

  3. Results

  4. Considerations

  • Practicality
    -- Latency
    -- Compute & Cost
  • Secure code
  • Best practices
  1. Risks for run-time metaprogramming
  • Difficulty in directing outputs of LLM
  • Difficulty of guaranteeing correctness of LLM outputs
  1. References

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.