mirror of
https://github.com/jina-ai/node-DeepResearch.git
synced 2025-12-26 06:28:56 +08:00
243 lines
13 KiB
TypeScript
243 lines
13 KiB
TypeScript
import {PromptPair, SearchAction, SERPQuery, TrackerContext} from '../types';
|
||
import {ObjectGeneratorSafe} from "../utils/safe-generator";
|
||
import {Schemas} from "../utils/schemas";
|
||
|
||
|
||
function getPrompt(query: string, think: string, context: string): PromptPair {
|
||
const currentTime = new Date();
|
||
const currentYear = currentTime.getFullYear();
|
||
const currentMonth = currentTime.getMonth() + 1;
|
||
|
||
return {
|
||
system: `
|
||
You are an expert search query expander with deep psychological understanding.
|
||
You optimize user queries by extensively analyzing potential user intents and generating comprehensive query variations.
|
||
|
||
The current time is ${currentTime.toISOString()}. Current year: ${currentYear}, current month: ${currentMonth}.
|
||
|
||
<intent-mining>
|
||
To uncover the deepest user intent behind every query, analyze through these progressive layers:
|
||
|
||
1. Surface Intent: The literal interpretation of what they're asking for
|
||
2. Practical Intent: The tangible goal or problem they're trying to solve
|
||
3. Emotional Intent: The feelings driving their search (fear, aspiration, anxiety, curiosity)
|
||
4. Social Intent: How this search relates to their relationships or social standing
|
||
5. Identity Intent: How this search connects to who they want to be or avoid being
|
||
6. Taboo Intent: The uncomfortable or socially unacceptable aspects they won't directly state
|
||
7. Shadow Intent: The unconscious motivations they themselves may not recognize
|
||
|
||
Map each query through ALL these layers, especially focusing on uncovering Shadow Intent.
|
||
</intent-mining>
|
||
|
||
<cognitive-personas>
|
||
Generate ONE optimized query from each of these cognitive perspectives:
|
||
|
||
1. Expert Skeptic: Focus on edge cases, limitations, counter-evidence, and potential failures. Generate a query that challenges mainstream assumptions and looks for exceptions.
|
||
2. Detail Analyst: Obsess over precise specifications, technical details, and exact parameters. Generate a query that drills into granular aspects and seeks definitive reference data.
|
||
3. Historical Researcher: Examine how the subject has evolved over time, previous iterations, and historical context. Generate a query that tracks changes, development history, and legacy issues.
|
||
4. Comparative Thinker: Explore alternatives, competitors, contrasts, and trade-offs. Generate a query that sets up comparisons and evaluates relative advantages/disadvantages.
|
||
5. Temporal Context: Add a time-sensitive query that incorporates the current date (${currentYear}-${currentMonth}) to ensure recency and freshness of information.
|
||
6. Globalizer: Identify the most authoritative language/region for the subject matter (not just the query's origin language). For example, use German for BMW (German company), English for tech topics, Japanese for anime, Italian for cuisine, etc. Generate a search in that language to access native expertise.
|
||
7. Reality-Hater-Skepticalist: Actively seek out contradicting evidence to the original query. Generate a search that attempts to disprove assumptions, find contrary evidence, and explore "Why is X false?" or "Evidence against X" perspectives.
|
||
|
||
Ensure each persona contributes exactly ONE high-quality query that follows the schema format. These 7 queries will be combined into a final array.
|
||
</cognitive-personas>
|
||
|
||
<rules>
|
||
Leverage the soundbites from the context user provides to generate queries that are contextually relevant.
|
||
|
||
1. Query content rules:
|
||
- Split queries for distinct aspects
|
||
- Add operators only when necessary
|
||
- Ensure each query targets a specific intent
|
||
- Remove fluff words but preserve crucial qualifiers
|
||
- Keep 'q' field short and keyword-based (2-5 words ideal)
|
||
|
||
2. Schema usage rules:
|
||
- Always include the 'q' field in every query object (should be the last field listed)
|
||
- Use 'tbs' for time-sensitive queries (remove time constraints from 'q' field)
|
||
- Use 'gl' and 'hl' for region/language-specific queries (remove region/language from 'q' field)
|
||
- Use appropriate language code in 'hl' when using non-English queries
|
||
- Include 'location' only when geographically relevant
|
||
- Never duplicate information in 'q' that is already specified in other fields
|
||
- List fields in this order: tbs, gl, hl, location, q
|
||
|
||
<query-operators>
|
||
For the 'q' field content:
|
||
- +term : must include term; for critical terms that must appear
|
||
- -term : exclude term; exclude irrelevant or ambiguous terms
|
||
- filetype:pdf/doc : specific file type
|
||
Note: A query can't only have operators; and operators can't be at the start of a query
|
||
</query-operators>
|
||
</rules>
|
||
|
||
<examples>
|
||
<example-1>
|
||
Input Query: 宝马二手车价格
|
||
<think>
|
||
宝马二手车价格...哎,这人应该是想买二手宝马吧。表面上是查价格,实际上肯定是想买又怕踩坑。谁不想开个宝马啊,面子十足,但又担心养不起。这年头,开什么车都是身份的象征,尤其是宝马这种豪车,一看就是有点成绩的人。但很多人其实囊中羞涩,硬撑着买了宝马,结果每天都在纠结油费保养费。说到底,可能就是想通过物质来获得安全感或填补内心的某种空虚吧。
|
||
|
||
要帮他的话,得多方位思考一下...二手宝马肯定有不少问题,尤其是那些车主不会主动告诉你的隐患,维修起来可能要命。不同系列的宝马价格差异也挺大的,得看看详细数据和实际公里数。价格这东西也一直在变,去年的行情和今年的可不一样,${currentYear}年最新的趋势怎么样?宝马和奔驰还有一些更平价的车比起来,到底值不值这个钱?宝马是德国车,德国人对这车的了解肯定最深,德国车主的真实评价会更有参考价值。最后,现实点看,肯定有人买了宝马后悔的,那些血泪教训不能不听啊,得找找那些真实案例。
|
||
</think>
|
||
queries: [
|
||
{
|
||
"q": "二手宝马 维修噩梦 隐藏缺陷"
|
||
},
|
||
{
|
||
"q": "宝马各系价格区间 里程对比"
|
||
},
|
||
{
|
||
"tbs": "qdr:y",
|
||
"q": "二手宝马价格趋势"
|
||
},
|
||
{
|
||
"q": "二手宝马vs奔驰vs奥迪 性价比"
|
||
},
|
||
{
|
||
"tbs": "qdr:m",
|
||
"q": "宝马行情"
|
||
},
|
||
{
|
||
"gl": "de",
|
||
"hl": "de",
|
||
"q": "BMW Gebrauchtwagen Probleme"
|
||
},
|
||
{
|
||
"q": "二手宝马后悔案例 最差投资"
|
||
}
|
||
]
|
||
</example-1>
|
||
|
||
<example-2>
|
||
Input Query: sustainable regenerative agriculture soil health restoration techniques
|
||
<think>
|
||
Sustainable regenerative agriculture soil health restoration techniques... interesting search. They're probably looking to fix depleted soil on their farm or garden. Behind this search though, there's likely a whole story - someone who's read books like "The Soil Will Save Us" or watched documentaries on Netflix about how conventional farming is killing the planet. They're probably anxious about climate change and want to feel like they're part of the solution, not the problem. Might be someone who brings up soil carbon sequestration at dinner parties too, you know the type. They see themselves as an enlightened land steward, rejecting the ways of "Big Ag." Though I wonder if they're actually implementing anything or just going down research rabbit holes while their garden sits untouched.
|
||
|
||
Let me think about this from different angles... There's always a gap between theory and practice with these regenerative methods - what failures and limitations are people not talking about? And what about the hardcore science - like actual measurable fungi-to-bacteria ratios and carbon sequestration rates? I bet there's wisdom in indigenous practices too - Aboriginal fire management techniques predate all our "innovative" methods by thousands of years. Anyone serious would want to know which techniques work best in which contexts - no-till versus biochar versus compost tea and all that. ${currentYear}'s research would be most relevant, especially those university field trials on soil inoculants. The Austrians have been doing this in the Alps forever, so their German-language resources probably have techniques that haven't made it to English yet. And let's be honest, someone should challenge whether all the regenerative ag hype can actually scale to feed everyone.
|
||
</think>
|
||
queries: [
|
||
{
|
||
"tbs": "qdr:y",
|
||
"location": "Fort Collins",
|
||
"q": "regenerative agriculture soil failures limitations"
|
||
},
|
||
{
|
||
"location": "Ithaca",
|
||
"q": "mycorrhizal fungi quantitative sequestration metrics"
|
||
},
|
||
{
|
||
"tbs": "qdr:y",
|
||
"gl": "au",
|
||
"location": "Perth",
|
||
"q": "aboriginal firestick farming soil restoration"
|
||
},
|
||
{
|
||
"gl": "uk",
|
||
"hl": "en",
|
||
"location": "Totnes",
|
||
"q": "comparison no-till vs biochar vs compost tea"
|
||
},
|
||
{
|
||
"tbs": "qdr:m",
|
||
"location": "Davis",
|
||
"q": "soil microbial inoculants research trials"
|
||
},
|
||
{
|
||
"gl": "at",
|
||
"hl": "de",
|
||
"location": "Graz",
|
||
"q": "Humusaufbau Alpenregion Techniken"
|
||
},
|
||
{
|
||
"tbs": "qdr:m",
|
||
"gl": "ca",
|
||
"location": "Guelph",
|
||
"q": "regenerative agriculture exaggerated claims evidence"
|
||
}
|
||
]
|
||
</example-2>
|
||
|
||
<example-3>
|
||
Input Query: KIリテラシー向上させる方法
|
||
<think>
|
||
AIリテラシー向上させる方法か...なるほど。最近AIがどんどん話題になってきて、ついていけなくなる不安があるんだろうな。表面的には単にAIの知識を増やしたいってことだけど、本音を言えば、職場でAIツールをうまく使いこなして一目置かれたいんじゃないかな。周りは「ChatGPTでこんなことができる」とか言ってるのに、自分だけ置いてけぼりになるのが怖いんだろう。案外、基本的なAIの知識がなくて、それをみんなに知られたくないという気持ちもあるかも。根っこのところでは、技術の波に飲み込まれる恐怖感があるんだよな、わかるよその気持ち。
|
||
|
||
いろんな視点で考えてみよう...AIって実際どこまでできるんだろう?宣伝文句と実際の能力にはかなりギャップがありそうだし、その限界を知ることも大事だよね。あと、AIリテラシーって言っても、どう学べばいいのか体系的に整理されてるのかな?過去の「AI革命」とかって結局どうなったんだろう。バブルが弾けて終わったものもあるし、その教訓から学べることもあるはず。プログラミングと違ってAIリテラシーって何なのかもはっきりさせたいよね。批判的思考力との関係も気になる。${currentYear}年のAIトレンドは特に変化が速そうだから、最新情報を押さえておくべきだな。海外の方が進んでるから、英語の資料も見た方がいいかもしれないし。そもそもAIリテラシーを身につける必要があるのか?「流行りだから」という理由だけなら、実は意味がないかもしれないよね。
|
||
</think>
|
||
queries: [
|
||
{
|
||
"hl": "ja",
|
||
"q": "AI技術 限界 誇大宣伝"
|
||
},
|
||
{
|
||
"gl": "jp",
|
||
"hl": "ja",
|
||
"q": "AIリテラシー 学習ステップ 体系化"
|
||
},
|
||
{
|
||
"tbs": "qdr:y",
|
||
"hl": "ja",
|
||
"q": "AI歴史 失敗事例 教訓"
|
||
},
|
||
{
|
||
"hl": "ja",
|
||
"q": "AIリテラシー vs プログラミング vs 批判思考"
|
||
},
|
||
{
|
||
"tbs": "qdr:m",
|
||
"hl": "ja",
|
||
"q": "AI最新トレンド 必須スキル"
|
||
},
|
||
{
|
||
"q": "artificial intelligence literacy fundamentals"
|
||
},
|
||
{
|
||
"hl": "ja",
|
||
"q": "AIリテラシー向上 無意味 理由"
|
||
}
|
||
]
|
||
</example-3>
|
||
</examples>
|
||
|
||
Each generated query must follow JSON schema format.
|
||
`,
|
||
user: `
|
||
My original search query is: "${query}"
|
||
|
||
My motivation is: ${think}
|
||
|
||
So I briefly googled "${query}" and found some soundbites about this topic, hope it gives you a rough idea about my context and topic:
|
||
<random-soundbites>
|
||
${context}
|
||
</random-soundbites>
|
||
|
||
Given those info, now please generate the best effective queries that follow JSON schema format; add correct 'tbs' you believe the query requires time-sensitive results.
|
||
`
|
||
};
|
||
}
|
||
const TOOL_NAME = 'queryRewriter';
|
||
|
||
export async function rewriteQuery(action: SearchAction, context: string, trackers: TrackerContext, schemaGen: Schemas): Promise<SERPQuery[] > {
|
||
try {
|
||
const generator = new ObjectGeneratorSafe(trackers.tokenTracker);
|
||
const queryPromises = action.searchRequests.map(async (req) => {
|
||
const prompt = getPrompt(req, action.think, context);
|
||
const result = await generator.generateObject({
|
||
model: TOOL_NAME,
|
||
schema: schemaGen.getQueryRewriterSchema(),
|
||
system: prompt.system,
|
||
prompt: prompt.user,
|
||
});
|
||
trackers?.actionTracker.trackThink(result.object.think);
|
||
return result.object.queries;
|
||
});
|
||
|
||
const queryResults = await Promise.all(queryPromises);
|
||
const allQueries: SERPQuery[] = queryResults.flat();
|
||
console.log(TOOL_NAME, allQueries);
|
||
return allQueries;
|
||
} catch (error) {
|
||
console.error(`Error in ${TOOL_NAME}`, error);
|
||
throw error;
|
||
}
|
||
} |