madaan / self-refine

LLMs can generate feedback on their work, use it to improve the output, and repeat this process iteratively.
https://selfrefine.info
Apache License 2.0
596 stars 48 forks source link

Pseudocode from https://selfrefine.info #18

Open Mr-Ruben opened 5 months ago

Mr-Ruben commented 5 months ago

In https://selfrefine.info/ I found the following pseudocode


def self_refine(prompt: str) -> str:
    def is_refinement_sufficient(prompt, feedback, initial, refined) -> bool:
        # Define stopping criteria here
        pass

    answer = ChatGPT(prompt)

    while True:
        feedback = ChatGPT(feedback_prompt, answer)
        refined = ChatGPT(refiner_prompt, feedback, answer)

        if is_refinement_sufficient(prompt, feedback, answer, refined):
            break

        answer = refined

    return refined

It seemed a bit weird to me, so I had this conversation

====================================================

User there is one function enclosing it all, but I think it doesn't have all the required parameters. What do you think?

====================================================

ChatGPT You're correct. The self_refine function is the outermost enclosing function, but it seems to be missing some necessary parameters for the nested functions (is_refinement_sufficient, feedback_prompt, and refiner_prompt) to work properly.

In its current state, the self_refine function only accepts a single parameter (prompt). However, the nested functions and the loop inside self_refine rely on additional parameters such as feedback_prompt and refiner_prompt, which are not provided as arguments to the self_refine function.

To fix this issue, the self_refine function should be modified to accept these additional parameters or they should be defined globally outside the function. Alternatively, the nested functions could be modified to access these parameters from the global scope.

====================================================

User improve the code

====================================================

ChatGPT To improve the code, we need to ensure that all necessary parameters are provided to the functions and that the stopping criteria function (is_refinement_sufficient) is properly implemented. Here's an improved version of the code:

python Copy code


def self_refine(prompt: str, feedback_prompt: str, refiner_prompt: str) -> str:
    def is_refinement_sufficient(prompt: str, feedback: str, initial: str, refined: str) -> bool:
        # Define stopping criteria here
        # For example, you could compare the similarity between refined and initial responses
        if len(refined) > len(initial):
            return True
        else:
            return False

    answer = ChatGPT(prompt)

    while True:
        feedback = ChatGPT(feedback_prompt, answer)
        refined = ChatGPT(refiner_prompt, feedback, answer)

        if is_refinement_sufficient(prompt, feedback, answer, refined):
            break

        answer = refined

    return refined

In this improved version:

The self_refine function now accepts three additional parameters: feedback_prompt and refiner_prompt, which are required for generating feedback and refining the answer, respectively. The is_refinement_sufficient function now only takes four parameters (prompt, feedback, initial, and refined). I've also added a basic stopping criterion example that checks whether the length of the refined response is greater than the length of the initial response. You can adjust this criterion based on your specific requirements. The function now returns the refined response. This improved version should work provided that the ChatGPT function is properly implemented and accessible within the scope of this code.

====================================================

User should the is_refinement_sufficient be defined outside to make the self_refine fully adaptable to multiple cases?

====================================================

ChatGPT Defining the is_refinement_sufficient function outside of the self_refine function could indeed enhance adaptability and reusability, especially if you anticipate using the refinement logic in multiple contexts. By defining it outside, you can easily reuse it across different refinement scenarios without duplicating code.

Here's how you can refactor the code to define is_refinement_sufficient outside of self_refine:

python Copy code

def is_refinement_sufficient(prompt: str, feedback: str, initial: str, refined: str) -> bool:
    # Define stopping criteria here
    # For example, you could compare the similarity between refined and initial responses
    if len(refined) > len(initial):
        return True
    else:
        return False

def self_refine(prompt: str, feedback_prompt: str, refiner_prompt: str) -> str:
    answer = ChatGPT(prompt)

    while True:
        feedback = ChatGPT(feedback_prompt, answer)
        refined = ChatGPT(refiner_prompt, feedback, answer)

        if is_refinement_sufficient(prompt, feedback, answer, refined):
            break

        answer = refined

    return refined

With this refactoring:

The is_refinement_sufficient function is defined separately, making it reusable across different contexts. The self_refine function now only focuses on the iterative refinement process without the need to define the stopping criteria, thus making it more modular and adaptable. This approach enhances code organization and facilitates maintenance and reuse. Now, if you need to adjust the stopping criteria or use it elsewhere, you can do so without modifying the self_refine function.

====================================================

User but if we provide a parameter to pass a function as is_refinement_sufficient function to self_refine we would not be locked with the "name" of the equivalent is_refinement_sufficient function.

====================================================

ChatGPT Absolutely, passing the refinement criterion function as a parameter to self_refine adds even more flexibility and decouples the refinement process from any specific stopping criteria function. Here's how you can modify the self_refine function to accept a custom refinement criterion function:

python Copy code

def self_refine(prompt: str, feedback_prompt: str, refiner_prompt: str, refinement_criteria_func) -> str:
    answer = ChatGPT(prompt)

    while True:
        feedback = ChatGPT(feedback_prompt, answer)
        refined = ChatGPT(refiner_prompt, feedback, answer)

        if refinement_criteria_func(prompt, feedback, answer, refined):
            break

        answer = refined

    return refined

Now, when calling self_refine, you can pass any function that meets the criteria for refinement. For example:

python Copy code

def is_refinement_sufficient(prompt: str, feedback: str, initial: str, refined: str) -> bool:
    # Define stopping criteria here
    # For example, you could compare the similarity between refined and initial responses
    if len(refined) > len(initial):
        return True
    else:
        return False

# Example call to self_refine with custom refinement criteria function
refined_response = self_refine(prompt, feedback_prompt, refiner_prompt, is_refinement_sufficient)

This way, the self_refine function becomes more adaptable and can be customized to different refinement scenarios by simply passing the appropriate refinement criteria function.

====================================================

User An infinite loop seems too much. Change it to have a maximum amount of loops defined by a constant with predefined value

====================================================

ChatGPT Certainly! Let's incorporate a maximum loop count into the self_refine function to prevent infinite loops. We'll define a constant with a predefined value for the maximum number of iterations. Here's the updated code:

python Copy code

MAX_ITERATIONS = 10

def self_refine(prompt: str, feedback_prompt: str, refiner_prompt: str, refinement_criteria_func) -> str:
    answer = ChatGPT(prompt)

    for _ in range(MAX_ITERATIONS):
        feedback = ChatGPT(feedback_prompt, answer)
        refined = ChatGPT(refiner_prompt, feedback, answer)

        if refinement_criteria_func(prompt, feedback, answer, refined):
            break

        answer = refined

    return refined

In this updated version:

We introduced a constant MAX_ITERATIONS with a predefined value, indicating the maximum number of iterations allowed. Instead of a while True loop, we now use a for loop with a range of MAX_ITERATIONS iterations. The loop breaks if the refinement criteria function returns True or if the maximum number of iterations is reached. This modification ensures that the refinement process doesn't run indefinitely and provides a safety mechanism in case the refinement criteria are not met within a reasonable number of iterations. Adjust the value of MAX_ITERATIONS as needed based on your specific requirements and computational constraints.

====================================================