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

Develop to #54

Merged
merged 4 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions lib/agi/main.agi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ export class MainAGI<T extends ActionType> {
this.loggerUtil = new LoggerUtil(this.consolidationId, this.logPath);
}

public isOneMinuteExceeded(previousDate: Date, currentDate: Date): boolean {
const timeDifference = currentDate.getTime() - previousDate.getTime();
const oneMinuteInMillis = 60 * 1000; // One minute in milliseconds

return timeDifference > oneMinuteInMillis;
}

/**
* Starts an AGI (Artificial General Intelligence) action by initializing necessary components,
* clearing previous folders, and generating prompts for user input based on previous responses.
Expand All @@ -98,10 +105,14 @@ export class MainAGI<T extends ActionType> {

await this.fileUtil.createFolder(this.taskDir);

this.loggerUtil.log('Action started at ' + new Date().toISOString());
let startDate = new Date();

this.loggerUtil.log('Action started at ' + startDate.toISOString());

this.openAIProvider.initialize(this.loggerUtil);

const TPM = 60000;

const memoryUtil = new MemoryUtil(this.fileUtil, this.ltmPath);
await memoryUtil.resetLTM();

Expand Down Expand Up @@ -177,6 +188,8 @@ export class MainAGI<T extends ActionType> {

let iteration = 0;

let currentToken = 0;

while (!parsed.completed && content.maxAttempt >= attemptCount) {
const stepName = 'Step ' + attemptCount.toString() + ': ' + parsed.step;

Expand Down Expand Up @@ -226,16 +239,30 @@ export class MainAGI<T extends ActionType> {
}
}

// 20 seconds delay between each request to avoid exceeding rate limit
this.loggerUtil.log('Waiting 20 seconds to do not exceed rate limit.');
await this.delay(20000);
// Rate Limit Fix
const now = new Date();

if (this.isOneMinuteExceeded(startDate, now)) {
currentToken = 0;
} else if (TPM <= currentToken) {
const delayInterval = now.getTime() - startDate.getTime();
const delaySeconds = delayInterval / 1000;

this.loggerUtil.log(
`Waiting ${delaySeconds} seconds to avoid exceeding the rate limit.`
);
await this.delay(delayInterval);
startDate = new Date();
}

try {
res = await this.openAIProvider.generateCompletion(
nextPrompt,
max_tokens
);

currentToken += this.openAIProvider.getDefaultMaxToken();

this.loggerUtil.log('Response is captured. Processing..');

parsed = await this.processGpt4ApiResponse(
Expand Down
4 changes: 4 additions & 0 deletions lib/provider/open-ai.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class OpenAIAzureProvider {
return this.MAX_TOKEN_COUNT - this.countTokens(prompt) - 10;
};

getDefaultMaxToken = () => {
return this.MAX_TOKEN_COUNT;
};

/**
* Sends a completion request to OpenAI API.
*
Expand Down