fjrdomingues / autopilot

Code Autopilot, a tool that uses GPT to read a codebase, create context and solve tasks.
591 stars 62 forks source link

return number of tokens spent on a task #127

Open fjrdomingues opened 1 year ago

fjrdomingues commented 1 year ago

I need the main function on ui.js to also return the number of tokens that were spent on a task. There are already modules in the project to calculate the number of tokens for all gpt calls, but those counts don't happen on the ui.js file. Use the ui.js on the root of the project. There should be a variable that stores the total tokens, on the ui.js, and that gets incremented each time there's an API call to gpt, which happens on different modules. You also need to make sure that the app gets the value from the module that runs gpt calls and is able to provide that context to the ui.js file.

Present a solution to implement this on the current app.

codeautopilot[bot] commented 1 year ago

🤖 This is my suggestion:

I am moderately confident in the solution provided. Here's a summary of the changes you need to make to implement the requested feature:

  1. Modify the modules/gpt.js file to export the totalTokensUsed variable and create a function to reset it.
  2. In the ui.js file, create a variable totalTokensSpent to store the total tokens spent on a task.
  3. Modify the runAgent function in the modules/interactiveAgent.js file to return the number of tokens spent on the API call.
  4. Update the calls to runAgent in the ui.js file to handle the new return value and increment the totalTokensSpent variable.
  5. Modify the main function in the ui.js file to return the totalTokensSpent variable along with the solutions array.

Here are the code snippets for each step:

modules/gpt.js:

module.exports.totalTokensUsed = totalTokensUsed;

function resetTotalTokensUsed() {
  totalTokensUsed = 0;
}

module.exports.resetTotalTokensUsed = resetTotalTokensUsed;

ui.js:

let totalTokensSpent = 0;

const { result: relevantFilesChunk, tokensSpent } = await runAgent(getRelevantFiles, task, summaries, interactive);
totalTokensSpent += tokensSpent;

return { solutions, totalTokensSpent };

Please note that this solution assumes that the runAgent function in the modules/interactiveAgent.js file is modified to return the number of tokens spent on the API call. If this is not the case, you may need to adjust the solution accordingly.