Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry agent fails better #160

Open
hatkyinc2 opened this issue Apr 30, 2023 · 1 comment
Open

Retry agent fails better #160

hatkyinc2 opened this issue Apr 30, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@hatkyinc2
Copy link
Collaborator

Background story explaining the current situation:
We can see failed to parse errors in an agent.
We try to recover using the langchain function to fix the output.

Desired new situation:
We should retry the original agent call several times hoping one of them passes as we know it's likely.

Implementation details:
Catch an agent fail in the generic agent call function, and retry the call until a hardcoded number of retries before totally failing.

@hatkyinc2 hatkyinc2 added the enhancement New feature or request label Apr 30, 2023
@codeautopilot
Copy link

codeautopilot bot commented Apr 30, 2023

🤖 This is my suggestion:

File: agents/genericAgent.js
To implement the retry mechanism in the callAgent function, you can follow these steps:

  1. Define a constant for the maximum number of retries, for example, const MAX_RETRIES = 3;.
  2. Create a loop that will iterate until the maximum number of retries is reached.
  3. Inside the loop, call the GPT model using callGPT(prompt, model) and store the result in the reply variable.
  4. Check if the reply is successful (i.e., it doesn't contain a failed to parse error or any other error condition). If it is successful, return the reply immediately.
  5. If the reply is not successful and the maximum number of retries has been reached, throw an error or return an appropriate error message.

Here's the modified callAgent function with the retry mechanism:

const { PromptTemplate } = require("langchain/prompts");
const { callGPT } = require('../modules/gpt');

const MAX_RETRIES = 3;

async function callAgent(template, values, model) {
    const promptTemplate = PromptTemplate.fromTemplate(template);
    const prompt = await promptTemplate.format(values);

    for (let retryCount = 0; retryCount < MAX_RETRIES; retryCount++) {
        const reply = await callGPT(prompt, model);

        // Check if the reply is successful, and return it if it is.
        if (isReplySuccessful(reply)) {
            return reply;
        }
    }

    // If the maximum number of retries has been reached, throw an error or return an appropriate error message.
    throw new Error("Failed to get a successful reply after multiple retries.");
}

function isReplySuccessful(reply) {
    // Implement the logic to check if the reply is successful (e.g., it doesn't contain a failed to parse error).
    // For example:
    return !reply.includes("failed to parse");
}

module.exports = {
    callAgent
}

Make sure to replace the isReplySuccessful function with the appropriate logic to check if the reply is successful based on your specific use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant