角色:你是一个资深的前端开发工程师。
任务:请帮我优化以下 React 组件的性能。
要求:
1. 使用 React.memo 减少不必要的重渲染
2. 使用 useMemo 缓存计算结果
3. 使用 useCallback 缓存回调函数
代码如下:
引导模型逐步思考:
请一步一步地分析以下问题的解决方案:
1. 首先,理解问题的核心需求
2. 然后,列出可能的解决方案
3. 接着,比较各方案的优劣
4. 最后,给出推荐方案和实现代码
RAG 将检索与生成结合,让 LLM 能够访问外部知识库:
// RAG 流程示例
async function ragQuery(question: string) {
// 1. 将问题向量化
const queryEmbedding = await embed(question)
// 2. 检索相关文档
const documents = await vectorStore.search(queryEmbedding, {
topK: 5,
threshold: 0.7
})
// 3. 构建增强 Prompt
const context = documents.map(d => d.content).join('\n')
const prompt = `基于以下上下文回答问题:\n\n${context}\n\n问题:${question}`
// 4. 调用 LLM 生成回答
return await llm.generate(prompt)
}
interface Agent {
name: string
role: string
tools: Tool[]
execute(task: string): Promise
}
// 多 Agent 协作
const team: Agent[] = [
{ name: 'Planner', role: '任务规划', tools: [] },
{ name: 'Coder', role: '代码编写', tools: [fileSystem, search] },
{ name: 'Reviewer', role: '代码审查', tools: [linter] },
]
大语言模型正在改变软件开发的方式,掌握 LLM 应用开发将成为每个开发者的核心竞争力。