Skip to content

Commit

Permalink
Merge pull request #190 from miurla/gpt-4-turbo
Browse files Browse the repository at this point in the history
Support for OpenAI GPT-4 Turbo model
  • Loading branch information
miurla committed Nov 7, 2023
2 parents 85232d8 + 6b257c0 commit 604f50b
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This is a port of [babyagi](https://github.com/yoheinakajima/babyagi) with [Lang
- [x] Skills Class allows for easy skill creation ([🧝 BabyElfAGI](https://twitter.com/yoheinakajima/status/1678443482866933760))
- [x] Aggregate the logic of the agent in the backend.
- [x] Add hooks to make it easier to handle the agent on the frontend.
- [ ] Support the OpenAI GPT-4 Turbo model
- [ ] Support the Llama2 model 🦙

and more ...
Expand Down
2 changes: 1 addition & 1 deletion public/locales/au/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
"EXAMPLE_OBJECTIVE_4": "Write a report on what BabyAGI is. Include a Youtube link in the report",
"EXAMPLE_OBJECTIVE_5": "Based on skillSaver.ts, write a new skill called airtableSaver.ts which use the Airtable API to insert data into a specific table",
"EXAMPLE_OBJECTIVE_6": "Summarise the page"
}
}
2 changes: 1 addition & 1 deletion public/locales/en/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"ERROR_CREATING_TASKS": "Error creating tasks",
"EXAMPLES": "Examples",
"EXAMPLE_OBJECTIVE_1": "Write a weather report for the next 3 days in Paris",
"EXAMPLE_OBJECTIVE_2": "Research the gpt-3.5-turbo-0613 model",
"EXAMPLE_OBJECTIVE_2": "Research the GPT-4 Turbo",
"EXAMPLE_OBJECTIVE_3": "Report the latest news in the user’s favorite category",
"EXAMPLE_OBJECTIVE_4": "Write a report on what BabyAGI is. Include a Youtube link in the report",
"EXAMPLE_OBJECTIVE_5": "Based on skillSaver.ts, write a new skill called airtableSaver.ts which use the Airtable API to insert data into a specific table",
Expand Down
4 changes: 2 additions & 2 deletions public/locales/gb/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"USER_INPUT_PLACEHOLDER": "Enter your answer here...",
"ERROR_CREATING_TASKS": "Error creating tasks",
"EXAMPLE_OBJECTIVE_1": "Write a weather report for the next 3 days in Paris.",
"EXAMPLE_OBJECTIVE_2": "Research the gpt-3.5-turbo-0613 model",
"EXAMPLE_OBJECTIVE_2": "Research the GPT-4 Turbo",
"EXAMPLE_OBJECTIVE_3": "Report the latest news in the user's favorite category.",
"EXAMPLES": "Examples",
"EXAMPLE_OBJECTIVE_4": "Write a report on what BabyAGI is. Include a Youtube link in the report",
"EXAMPLE_OBJECTIVE_5": "Based on skillSaver.ts, write a new skill called airtableSaver.ts which uses the Airtable API to insert data into a specific table.",
"EXAMPLE_OBJECTIVE_6": "Summarise the page."
}
}
3 changes: 1 addition & 2 deletions src/components/Agent/AgentParameter.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { FC, use, useEffect, useState } from 'react';
import { FC, useEffect, useState } from 'react';
import { Select } from './Select';
import { SelectItem } from '@/types';
import { AGENT, ITERATIONS, MODELS } from '@/utils/constants';
import { translate } from '../../utils/translate';
import { getUserApiKey } from '@/utils/settings';

interface AgentParameterProps {
model: SelectItem;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Agent/AgentView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const AgentView: FC = () => {
const { i18n } = useTranslation();

// useState hooks
const [model, setModel] = useState<SelectItem>(MODELS[1]);
const [model, setModel] = useState<SelectItem>(MODELS[0]);
const [iterations, setIterations] = useState<SelectItem>(ITERATIONS[0]);
const [objective, setObjective] = useState<string>('');
const [firstTask, setFirstTask] = useState<string>(
Expand Down
10 changes: 8 additions & 2 deletions src/hooks/useAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,15 @@ export function useAgent({
// stream is finished
if (onError && !hadFinishMessage) {
if (!currentMesageType) {
onError(new Error('Error: Invalid OpenAI API key'));
onError(
new Error(
'Error: OpenAI API error occurred or Invalid OpenAI API key',
),
);
reset();
} else {
onError(new Error('Error: No result message'));
onError(new Error('Error: OpenAI API error occurred'));
reset();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/agents/babyelfagi/executer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class BabyElfAGI extends AgentExecuter {
taskOutputs,
this.objective,
this.skillRegistry,
this.modelName,
);
} catch (error) {
console.error(error);
Expand Down
21 changes: 18 additions & 3 deletions src/lib/agents/babyelfagi/registory/taskRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ export class TaskRegistry {
const model = new ChatOpenAI(
{
openAIApiKey: this.userApiKey,
modelName: this.useSpecifiedSkills ? modelName : 'gpt-4',
modelName,
temperature: 0,
maxTokens: 1500,
topP: 1,
verbose: false, // You can set this to true to see the lanchain logs
streaming: true,
maxRetries: 2,
callbacks: [
{
handleLLMNewToken(token: string) {
Expand All @@ -95,6 +96,13 @@ export class TaskRegistry {
try {
const response = await model.call([systemMessage, messages]);
result = response.text;
// markdown is now appended (remove when langchain supports json mode)
if (result.startsWith('```json')) {
result = result.slice(7);
}
if (result.endsWith('```')) {
result = result.slice(0, -3);
}
} catch (error: any) {
if (error.name === 'AbortError') {
console.log('Task creation aborted');
Expand All @@ -103,6 +111,7 @@ export class TaskRegistry {
}

if (result === undefined) {
console.log('error');
return;
}

Expand All @@ -115,13 +124,19 @@ export class TaskRegistry {
taskOutputs: TaskOutputs,
objective: string,
skillRegistry: SkillRegistry,
modelName: string,
): Promise<string> {
const skill = skillRegistry.getSkill(task.skill ?? '');
const dependentTaskOutputs = task.dependentTaskIds
? task.dependentTaskIds.map((id) => taskOutputs[id].output).join('\n')
: '';

return await skill.execute(task, dependentTaskOutputs, objective);
return await skill.execute(
task,
dependentTaskOutputs,
objective,
modelName,
);
}

getTasks(): AgentTask[] {
Expand Down Expand Up @@ -156,7 +171,7 @@ export class TaskRegistry {
objective: string,
taskOutput: string,
skillDescriptions: string,
modelName: string = 'gpt-3.5-turbo-16k',
modelName: string = 'gpt-4-1106-preview',
): Promise<[AgentTask[], number[], AgentTask[]]> {
const example = [
[
Expand Down
4 changes: 3 additions & 1 deletion src/lib/agents/babyelfagi/skills/presets/textCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class TextCompletion extends Skill {
task: AgentTask,
dependentTaskOutputs: string,
objective: string,
modelName: string,
): Promise<string> {
if (!this.valid) return '';

Expand All @@ -27,7 +28,8 @@ export class TextCompletion extends Skill {
return this.generateText(prompt, task, {
temperature: 0.2,
maxTokens: 800,
modelName: 'gpt-3.5-turbo-16k',
modelName:
modelName === 'gpt-4-1106-preview' ? modelName : 'gpt-3.5-turbo-16k',
});
}
}
26 changes: 22 additions & 4 deletions src/lib/agents/babyelfagi/skills/presets/webLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export class WebLoader extends Skill {
'This skill extracts URLs from the task and returns the contents of the web pages of those URLs.';
icon = '🌐';

async execute(task: AgentTask, objective: string): Promise<string> {
async execute(
task: AgentTask,
dependentTaskOutputs: string,
objective: string,
modelName?: string,
): Promise<string> {
if (typeof objective !== 'string') {
throw new Error('Invalid inputs');
}
Expand All @@ -32,10 +37,15 @@ export class WebLoader extends Skill {
});
};

const urlString = await this.extractUrlsFromTask(task, callback);
const urlString = await this.extractUrlsFromTask(task, callback, modelName);
const urls = urlString.split(',').map((url) => url.trim());
const contents = await this.fetchContentsFromUrls(urls, callback);
const info = await this.extractInfoFromContents(contents, objective, task);
const info = await this.extractInfoFromContents(
contents,
objective,
task,
modelName,
);
this.handleMessage({
id: uuidv4(),
taskId: task.id.toString(),
Expand All @@ -52,9 +62,15 @@ export class WebLoader extends Skill {
private async extractUrlsFromTask(
task: AgentTask,
callback: (message: string) => void,
modelName: string = 'gpt-3.5-turbo',
): Promise<string> {
const prompt = `Extracting URLs from the task.\nReturn a comma-separated URL List.\nTASK: ${task.task}\nURLS:`;
const urlString = await this.generateText(prompt, task, undefined, true);
const urlString = await this.generateText(
prompt,
task,
{ modelName },
true,
);
callback(` - URLs: ${urlString}\n`);
return urlString;
}
Expand Down Expand Up @@ -86,6 +102,7 @@ export class WebLoader extends Skill {
contents: { url: string; content: string }[],
objective: string,
task: AgentTask,
modelName: string = 'gpt-3.5-turbo',
): Promise<string[]> {
return await Promise.all(
contents.map(async (item) => {
Expand All @@ -99,6 +116,7 @@ export class WebLoader extends Skill {
this.apiKeys.openai,
this.handleMessage,
this.abortSignal,
modelName,
))
);
}),
Expand Down
3 changes: 2 additions & 1 deletion src/lib/agents/babyelfagi/skills/presets/webSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class WebSearch extends Skill {
task: AgentTask,
dependentTaskOutputs: string,
objective: string,
modelName?: string,
): Promise<string> {
if (!this.valid || this.signal?.aborted) return '';

Expand All @@ -25,7 +26,7 @@ export class WebSearch extends Skill {
dependentTaskOutputs,
this.handleMessage,
this.verbose,
undefined,
modelName,
this.language,
this.apiKeys.openai,
this.signal,
Expand Down
1 change: 1 addition & 0 deletions src/lib/agents/babyelfagi/skills/skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class Skill {
task: AgentTask,
dependentTaskOutputs: string,
objective: string,
modelName?: string,
): Promise<string> {
// This method should be overridden by subclasses
throw new Error("Method 'execute' must be implemented");
Expand Down
5 changes: 4 additions & 1 deletion src/lib/agents/babyelfagi/tools/utils/largeTextExtract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AgentMessage, AgentTask } from '@/types';
import { relevantInfoExtraction } from './relevantInfoExtraction';
import { chunk } from 'lodash';

export const largeTextExtract = async (
id: string,
Expand All @@ -9,8 +10,9 @@ export const largeTextExtract = async (
userApiKey?: string,
callback?: (message: AgentMessage) => void,
signal?: AbortSignal,
model?: string,
) => {
const chunkSize = 15000;
const chunkSize = model === 'gpt-4-1106-preview' ? 127000 : 15000;
const overlap = 500;
let notes = '';

Expand All @@ -36,6 +38,7 @@ export const largeTextExtract = async (
chunk,
userApiKey,
signal,
model,
);
notes += response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export const relevantInfoExtraction = async (
chunk: string,
userApiKey?: string,
signal?: AbortSignal,
model?: string,
) => {
const modelName = 'gpt-3.5-turbo-16k-0613'; // use a fixed model
const modelName =
model === 'gpt-4-1106-preview' ? model : 'gpt-3.5-turbo-16k';
const prompt = relevantInfoExtractionPrompt();
const llm = new OpenAIChat(
{
Expand Down
4 changes: 2 additions & 2 deletions src/lib/agents/babyelfagi/tools/utils/textCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const textCompletion = async (
signal?: AbortSignal,
messageCallnback?: (message: AgentMessage) => void,
) => {
if (prompt.length > 3200) {
modelName = 'gpt-3.5-turbo-16k-0613';
if (modelName !== 'gpt-4-1106-preview' && prompt.length > 3200) {
modelName = 'gpt-3.5-turbo-16k';
}

const llm = new ChatOpenAI(
Expand Down
3 changes: 2 additions & 1 deletion src/lib/agents/babyelfagi/tools/webBrowsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ export const webBrowsing = async (
const info = await largeTextExtract(
id,
objective,
content.slice(0, 20000),
content,
task,
userApiKey,
messageCallback,
signal,
modelName,
);
clearInterval(intervalId);

Expand Down
15 changes: 10 additions & 5 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ export const EXAMPLES_KEY = 'BABYAGIUI_EXAMPLES';

export const MODELS = [
{
id: 'gpt-4-0613',
name: 'OpenAI gpt-4',
message: `GPT_4_WARNING`,
id: 'gpt-4-1106-preview',
name: 'OpenAI GPT-4 Turbo',
icon: 'openai-logo.svg',
badge: 'Preview',
},
{
id: 'gpt-3.5-turbo-0613',
name: 'OpenAI gpt-3.5-turbo',
id: 'gpt-4',
name: 'OpenAI GPT-4',
icon: 'openai-logo.svg',
},
{
id: 'gpt-3.5-turbo',
name: 'OpenAI GPT-3.5 Turbo',
icon: 'openai-logo.svg',
},
];
Expand Down

1 comment on commit 604f50b

@vercel
Copy link

@vercel vercel bot commented on 604f50b Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

babyagi-ui – ./

babyagi-ui-babyagi-ui.vercel.app
babyagi-ui-git-main-babyagi-ui.vercel.app
babyagi-ui.vercel.app

Please sign in to comment.