Skip to content

Commit

Permalink
Add generateText function and judgment check
Browse files Browse the repository at this point in the history
  • Loading branch information
miurla committed Nov 7, 2023
1 parent d0b4af0 commit 0ac36ba
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
47 changes: 46 additions & 1 deletion src/lib/agents/babyelfagi/tools/utils/textCompletion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AgentMessage, AgentTask } from '@/types';
import { AgentMessage, AgentTask, LLMParams } from '@/types';
import { ChatOpenAI } from 'langchain/chat_models/openai';
import { HumanChatMessage } from 'langchain/schema';

Expand Down Expand Up @@ -65,3 +65,48 @@ export const textCompletion = async (
return 'Failed to generate text.';
}
};

export const generateText = async (
prompt: string,
params: LLMParams = {},
userKey?: string,
signal?: AbortSignal,
) => {
const modelName = 'gpt-3.5-turbo';
const temperature = 0.7;
const maxTokens = 1500;
const topP = 1;
const frequencyPenalty = 0;
const presencePenalty = 0;
const streaming = false;
const callbacks = undefined;

const llmParams = {
...params,
modelName,
temperature,
maxTokens,
topP,
frequencyPenalty,
presencePenalty,
streaming,
callbacks,
};

const llm = new ChatOpenAI({
...llmParams,
openAIApiKey: userKey,
verbose: false,
});

try {
const response = await llm.call([new HumanChatMessage(prompt)]);
return response.text;
} catch (error: any) {
if (error.name === 'AbortError') {
return null;
}
console.log('error: ', error);
return 'Failed to generate text.';
}
};
27 changes: 24 additions & 3 deletions src/lib/agents/babyelfagi/tools/webBrowsing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { simplifySearchResults } from '@/lib/agents/babyelfagi/tools/webSearch';
import { AgentTask, AgentMessage } from '@/types';
import { analystPrompt, searchQueryPrompt } from '../../../../utils/prompt';
import { textCompletion } from './utils/textCompletion';
import { textCompletion, generateText } from './utils/textCompletion';
import { largeTextExtract } from './utils/largeTextExtract';
import { v4 as uuidv4 } from 'uuid';
import { webSearch } from './webSearch';
Expand Down Expand Up @@ -47,11 +47,13 @@ export const webBrowsing = async (
let results = '';
let index = 1;
let completedCount = 0;
const MaxCompletedCount = 3;
const MaxRetryCount = 3;
let sufficientResultsObtained = false;

// Loop through search results
for (const searchResult of simplifiedSearchResults) {
if (signal?.aborted) return '';
if (completedCount >= MaxCompletedCount) break;
if (completedCount >= MaxRetryCount || sufficientResultsObtained) break;

// Extract the URL from the search result
const url = searchResult.link;
Expand Down Expand Up @@ -107,6 +109,25 @@ export const webBrowsing = async (
title = `${index}. Relevant info...`;
callbackSearchStatus(id, message, task, messageCallback, verbose);

const reflect = `You are expart task manager.
Your Mission: You'll be provided with a goal for the team to accomplish and one task from the team's task list and its results.
You decide if the results of the task are satisfactory.
Your judgment will determine whether the task is complete or not.
RULE: If you judge the task to be complete, then OK. If it needs to be redone, then just reply with NG.
Do not include anything else in your response.
Objective: ${objective}
TASK: ${task.task}
Result: ${info}
`;

const judgment = await generateText(
reflect,
{ temperature: 0.0 },
userApiKey,
signal,
);
sufficientResultsObtained = judgment === 'OK';

results += `${info}. `;
index += 1;
completedCount += 1;
Expand Down

0 comments on commit 0ac36ba

Please sign in to comment.