bananaml/fructose

Fructose being able to generate classes

Opened this issue · 3 comments

While Fructose can generate functions given a function signature and docstring explanation, presently it seems that it cannot do the same for a class. Understandably, the semantics for generating a function vs. a class are different which would require a different implementation to handle class generation. Thus, this might be out of scope.

Classes are an integral part of Python and as such I think it would be useful to have the @ai decorator be applied to a class.

For example,

@ai
class Point_2D
"""Create a class that can hold integer or float values to represent x and y coordinates."""

Thus, when I try to find the result, result = dir(Point_2D), I should be able to find a list of properties and methods generated for the class stub.

Additionally, to fit within existing classes, could there be functionality for creating new methods within a class? This would ideally work the way Fructose does right now except that it would be directly tied to a class and its instances.

This feels out of scope because it'd generate an unknown set of properties (and maybe even methods) at runtime, so we as the programmer wouldn't have any idea how to interact with the object, what its interface is.

Have you experimented with something like this before?

Yes, I have experimented with something like this before. I've worked with generating a method for an existing class before.

If generating an unknown set of properties or methods at runtime would lead this to being out of scope, perhaps we can specify the methods and properties we want as arguments. This might work if the fields were known beforehand as well, thus we could leverage @dataclass as well.

For example, it could work something along these lines. The following example is fairly clunky, but essentially you would be able to have a way to inject properties or methods with their names, signatures, and docstring as inputs as required.

type Num = int | float

@gen_property([get_x_coord, "get the x_coordinate from a Point_2D", [Point_2D, Num]])
@dataclass 
class Point_2D
"""A class that can hold integer or float values to represent x and y coordinates."""
    x: Num
    y: Num

Thus, the programmer can specify what methods or properties they want their class to have while the LLM can generate the implementation.

0w9 commented

I agree with @erik-dunteman - I don't think that the feature of generating the implementation of a function would be the scope of Fructose.

But what I do think would be great is being able to declare a function in a class and get the parameters for it generated (kind of what OpenAI functions were for initially).

Example

@dataclass
class Application
    job: str
    application: str
    accepted: bool

@dataclass
class Person:
    name: str
    age: int
    
    def apply_for_job(str job, str application) -> Appplication
    
@ai
def generate_fake_person_data() -> Person:
  """
    Generate fake data for a cliche aspiring author that applies to a publisher with their newly written book.
  """

What do you think about that? Would be possible to implement.