mirror of
https://github.com/jina-ai/node-DeepResearch.git
synced 2025-12-26 06:28:56 +08:00
chore: first commit
This commit is contained in:
parent
8fb4f8dbb6
commit
cd7b9a6095
67
src/agent.ts
67
src/agent.ts
@ -131,7 +131,8 @@ function getPrompt(
|
|||||||
allowSearch: boolean = true,
|
allowSearch: boolean = true,
|
||||||
badContext?: { question: string, answer: string, evaluation: string, recap: string; blame: string; improvement: string; }[],
|
badContext?: { question: string, answer: string, evaluation: string, recap: string; blame: string; improvement: string; }[],
|
||||||
knowledge?: { question: string; answer: string; }[],
|
knowledge?: { question: string; answer: string; }[],
|
||||||
allURLs?: Record<string, string>
|
allURLs?: Record<string, string>,
|
||||||
|
beastMode?: boolean
|
||||||
): string {
|
): string {
|
||||||
const sections: string[] = [];
|
const sections: string[] = [];
|
||||||
|
|
||||||
@ -214,6 +215,14 @@ ${urlList}
|
|||||||
- Responses must be definitive (no ambiguity, uncertainty, or disclaimers)${allowReflect ? '\n- If doubts remain, use "reflect" instead' : ''}`);
|
- Responses must be definitive (no ambiguity, uncertainty, or disclaimers)${allowReflect ? '\n- If doubts remain, use "reflect" instead' : ''}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (beastMode) {
|
||||||
|
actions.push(`**answer**:
|
||||||
|
- You have gathered enough information to answer the question; they may not be perfect, but this is your very last chance to answer the question.
|
||||||
|
- Try the best of the best reasoning ability, investigate every details in the context and provide the best answer you can think of.
|
||||||
|
- When uncertain, educated guess is allowed and encouraged, but make sure it is based on the context and knowledge you have gathered.
|
||||||
|
- Responses must be definitive (no ambiguity, uncertainty, or disclaimers`);
|
||||||
|
}
|
||||||
|
|
||||||
if (allowReflect) {
|
if (allowReflect) {
|
||||||
actions.push(`**reflect**:
|
actions.push(`**reflect**:
|
||||||
- Perform critical analysis through hypothetical scenarios or systematic breakdowns
|
- Perform critical analysis through hypothetical scenarios or systematic breakdowns
|
||||||
@ -268,6 +277,8 @@ async function getResponse(question: string, tokenBudget: number = 1000000, maxB
|
|||||||
let allowRead = true;
|
let allowRead = true;
|
||||||
let allowReflect = true;
|
let allowReflect = true;
|
||||||
let prompt = '';
|
let prompt = '';
|
||||||
|
let thisStep: StepAction = {action: 'answer', answer: '', references: [], thoughts: ''};
|
||||||
|
let isAnswered = false;
|
||||||
|
|
||||||
const allURLs: Record<string, string> = {};
|
const allURLs: Record<string, string> = {};
|
||||||
const visitedURLs: string[] = [];
|
const visitedURLs: string[] = [];
|
||||||
@ -295,7 +306,9 @@ async function getResponse(question: string, tokenBudget: number = 1000000, maxB
|
|||||||
allowSearch,
|
allowSearch,
|
||||||
badContext,
|
badContext,
|
||||||
allKnowledge,
|
allKnowledge,
|
||||||
allURLs);
|
allURLs,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
const model = genAI.getGenerativeModel({
|
const model = genAI.getGenerativeModel({
|
||||||
model: MODEL_NAME,
|
model: MODEL_NAME,
|
||||||
@ -312,7 +325,7 @@ async function getResponse(question: string, tokenBudget: number = 1000000, maxB
|
|||||||
tokenTracker.trackUsage('agent', usage?.totalTokenCount || 0);
|
tokenTracker.trackUsage('agent', usage?.totalTokenCount || 0);
|
||||||
|
|
||||||
|
|
||||||
const thisStep = JSON.parse(response.text());
|
thisStep = JSON.parse(response.text());
|
||||||
// print allowed and chose action
|
// print allowed and chose action
|
||||||
const actionsStr = [allowSearch, allowRead, allowAnswer, allowReflect].map((a, i) => a ? ['search', 'read', 'answer', 'reflect'][i] : null).filter(a => a).join(', ');
|
const actionsStr = [allowSearch, allowRead, allowAnswer, allowReflect].map((a, i) => a ? ['search', 'read', 'answer', 'reflect'][i] : null).filter(a => a).join(', ');
|
||||||
console.log(`${thisStep.action} <- [${actionsStr}]`);
|
console.log(`${thisStep.action} <- [${actionsStr}]`);
|
||||||
@ -352,10 +365,11 @@ ${evaluation.reasoning}
|
|||||||
|
|
||||||
Your journey ends here.
|
Your journey ends here.
|
||||||
`);
|
`);
|
||||||
return thisStep;
|
isAnswered = false;
|
||||||
|
break
|
||||||
}
|
}
|
||||||
if (evaluation.is_definitive) {
|
if (evaluation.is_definitive) {
|
||||||
if (thisStep.references.length > 0 || Object.keys(allURLs).length === 0) {
|
if (thisStep.references?.length > 0 || Object.keys(allURLs).length === 0) {
|
||||||
// EXIT POINT OF THE PROGRAM!!!!
|
// EXIT POINT OF THE PROGRAM!!!!
|
||||||
diaryContext.push(`
|
diaryContext.push(`
|
||||||
At step ${step}, you took **answer** action and finally found the answer to the original question:
|
At step ${step}, you took **answer** action and finally found the answer to the original question:
|
||||||
@ -371,7 +385,8 @@ ${evaluation.reasoning}
|
|||||||
|
|
||||||
Your journey ends here. You have successfully answered the original question. Congratulations! 🎉
|
Your journey ends here. You have successfully answered the original question. Congratulations! 🎉
|
||||||
`);
|
`);
|
||||||
return thisStep;
|
isAnswered = true;
|
||||||
|
break
|
||||||
} else {
|
} else {
|
||||||
diaryContext.push(`
|
diaryContext.push(`
|
||||||
At step ${step}, you took **answer** action and finally found the answer to the original question:
|
At step ${step}, you took **answer** action and finally found the answer to the original question:
|
||||||
@ -386,8 +401,8 @@ Unfortunately, you did not provide any references to support your answer.
|
|||||||
You need to find more URL references to support your answer.`);
|
You need to find more URL references to support your answer.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
await storeContext(prompt, [allContext, allKeywords, allQuestions, allKnowledge], totalStep);
|
isAnswered = true;
|
||||||
return thisStep;
|
break
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
diaryContext.push(`
|
diaryContext.push(`
|
||||||
@ -593,6 +608,42 @@ You decided to think out of the box or cut from a completely different angle.`);
|
|||||||
|
|
||||||
await storeContext(prompt, [allContext, allKeywords, allQuestions, allKnowledge], totalStep);
|
await storeContext(prompt, [allContext, allKeywords, allQuestions, allKnowledge], totalStep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await storeContext(prompt, [allContext, allKeywords, allQuestions, allKnowledge], totalStep);
|
||||||
|
if (isAnswered) {
|
||||||
|
return thisStep;
|
||||||
|
} else {
|
||||||
|
const prompt = getPrompt(
|
||||||
|
question,
|
||||||
|
diaryContext,
|
||||||
|
allQuestions,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
badContext,
|
||||||
|
allKnowledge,
|
||||||
|
allURLs,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
const model = genAI.getGenerativeModel({
|
||||||
|
model: MODEL_NAME,
|
||||||
|
generationConfig: {
|
||||||
|
temperature: 0.7,
|
||||||
|
responseMimeType: "application/json",
|
||||||
|
responseSchema: getSchema(false, false, allowAnswer, false)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await model.generateContent(prompt);
|
||||||
|
const response = await result.response;
|
||||||
|
const usage = response.usageMetadata;
|
||||||
|
tokenTracker.trackUsage('agent', usage?.totalTokenCount || 0);
|
||||||
|
|
||||||
|
await storeContext(prompt, [allContext, allKeywords, allQuestions, allKnowledge], totalStep);
|
||||||
|
return JSON.parse(response.text());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function storeContext(prompt: string, memory: any[][], step: number) {
|
async function storeContext(prompt: string, memory: any[][], step: number) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user