96SEO 2026-08-06 03:31 0
话说回来,
Agent 官方给出的定义是一个大模型不断调用工具为了完成一个给定的任务。直到该任务完成,说起来,
这里非常好理解:Tool 就是 Agent 的手脚。Model 是 Agent 的脑子,由脑子控制手脚完成具体的任务,而衔接脑子与手脚的程序叫做 Agent。其实目前各种 AI 落地,说的再花哨就两样东西:一个是 LLM 即大模型。对了一个是 prompt 即提示词!LLM 是基础能力,决定了 AI 的下限。而 prompt 的花样就多了Tool 是提示词,而整个流程叫做 Agent。形象地说明大模型是通过代理完成工具的调用,而那个自动化程序才是真正的 Agent,负责衔接大模型与 Tool。

上面提到与 LLM 交互的一切都是提示词。Tool 是提示词,SKILL 是提示词,RAG 也是提示词,Harness 还是提示词,因为目前 LLM天生只能通过自然语言进行交互,别无他法!当前 AI 圈和币圈比较像,喜欢造热词,今天是 SKILL。明天是龙虾,后天是 Hermes,Loop Engineering…
tool 包装成可以被 LLM 调用的格式。
创建一个 Agent 是比较简单的,createAgent 是一个高度可配置的执行框架:
import { createAgent } from "langchain";const agent = createAgent({
说到model。"openai:gpt-",tools,});
tools,可以参考我的实战四文章。下面给出一个完整可运行的示例:
import dotenv from "dotenv";// 加载环境变量中的模型 API 密钥
import { ChatOpenAI } from "@langchain/openai";dotenv.config;const model = new ChatOpenAI({
model的观点是,"qwen-plus",apiKey: process.env.QWEN_API_KEY,temperature: 0.7。streamUsage: false,logprobs: true,configuration: {
baseURL:
"https这方面,//dashscope.aliyuncs.com/compatible-mode/v1",},});
import { createAgent,tool } from "langchain";import * as z from "zod";const search = tool(
=> `${query} 的查询结果是:杭州今日晴朗,温度25°C。`,{
说到name,"searchWear",description: "Search for wear information"。schema: z.object({
query的观点是,z.string.describe,}),}
);
const agent = createAgent({
model。tools:,systemMessage:
"你是一个有用的智能助手!怎么说呢,你必须信任工具的结果。",}),
const result = await agent.invoke({
messages:,});console.log(
result.messages.content
);
此时你已经掌握了最基本的 Agent 建立方式。
If you need Agent to return JSON‑like structured data,just define a Zod schema and pass it via responseFormat.
import * as z from "zod";const Answer = z.object({
summary: z.string.describe。confidence: z.number.describe,});不过,const agent = createAgent({
model。tools:,responseFormat: Answer,});const result = await agent.invoke({
messages:,});老实说,console.log;// => { summary: "...",confidence: ... }
A long‑running task can be streamed so you see each step:
const stream = await agent.stream(
{
messages:。},{ streamMode: "values" }
);for await {
const latestMessage = chunk.messages.at;if {
console.log;} else if {
const names = latestMessage.tool_calls.map => tc.name);console.log}`);}
}
Agent = Model + Harness . Harness 包含 Prompt、Tools、Middleware 等所有能约束 Model 行为的部件。下面展示如何通过 LangChain 中间件实现细粒度约束。
import { createAgent } from "langchain";import {
FilesystemMiddleware。
StateBackend,} from "deepagents";const backend = new StateBackend;const agent = createAgent({
至于model。"anthropic::claude-sonnet--",tools:,middleware:,});
This gives agent commands like /ls,/read_file,/write_file。/edit_file**.
import {
FilesystemMiddleware。MemoryMiddleware,SkillsMiddleware,SummarizationMiddleware,StateBackend,} from "deepagents";const backend = new StateBackend;const model = "anthropic::claude-sonnet--";const agent = createAgent({
model。tools:,middleware:,});
import {
createAgent。todoListMiddleware,tool
} from "langchain";import {
createFilesystemMiddleware。createSubAgentMiddleware,StateBackend
} from "deepagents";import * as z from "zod";const search = tool(
=> `Search results for ${query}`,{
name的观点是,"search"。description:"Search and return short summary.",schema:z.object })
}
);const backend = new StateBackend;const agent = createAgent({
至于model,"openai:gpt-4o-mini",tools:。middleware:,model:"anthropic::claude-sonnet--",middleware:
}
]
})
]
});
import {
createAgent,modelRetryMiddleware,tool,toolRetryMiddleware
} from "langchain";import * as z from "zod";const search=tool(
=>`Search results for ${query}`,{
说到name,"search"。description:"Search and return short summary.",schema:z.object })
}
);const agent=createAgent({
model这方面。"openai:gpt-4o-mini",tools:,middleware:
});
-
The **modelRetry** middleware automatically retries failed LLM calls .
-
The **toolRetry** middleware does same for external tool failures.
-
No need to sprinkle try/catch everywhere – centralised error handling keeps your workflow tidy.
<\/ul>
安全护栏的观点是。PII Middleware
import {createAgent,piiMiddleware,tool} from "langchain";import * as z from "zod";const search=tool(
=>`Search results for ${query}`,{
再看name,"search"。description:"Search and return short summary.",schema:z.object })
}
);const agent=createAgent({
model这方面。"openai:gpt-4o-mini",tools:,middleware: // 自动脱敏邮箱
});
-
The **piiMiddleware** intercepts any email address before it reaches LLM.
-
You can replace it with masking or full redaction .
<\/ul>
至于人工引导,Human‑in‑‑Loop Middleware
// 当即将写文件时需要人工确认
Agent 官方给出的定义是:a large language model repeatedly calls external tools until a given task is completed.
This definition is easy to grasp:
-
The Tools are hands and feet of agent.
-
The Model is brain that decides when and how to use those tools.
-
The Agent itself is glue that feeds prompts to brain and routes its commands to hands/feet.
-
A modern AI stack essentially reduces to two things:
-
LLM
-
Prompt
\end{ul>
Pain point #1 – You don’t know how to wrap an arbitrary function into an LLM‑callable Tool.
Code example – basic set up
js
import {createA gent}from'langcha in';const ag ent=createA gent({
m odel:'op ena i:g pt -',t oo ls: // <-- your first custom Tool
});
Replace 'wear' with any async function that follows LangChain's `tool` contract.
The `createA gent` factory injects your function into a self‑contained loop where it can be called by GPT every turn until completion.
This eliminates boilerplate such as manual prompt engineering or hand‑crafted loops.
js
import dotenv fro m 'dotenv';import{ChatOpe nAI}from'@lan gcha in/o penai';dotenv .config;按理说,const llm=newChatOpenAI({
mode l:'q wen-p lus',//any supported provider/model
至于apiKey,p rocess.env.Q W EN_API_KEY。temperature :0,//deterministic output
configuration:{baseU RL:'https://dashscope.aliyun cs.c om/compatible-mode/v1'}
});
js
import{cre ateAg ent,to ol }from'langc hain';按理说,import*z od*as z f rom'z od';const wearT ool=to ol=>`\${city}\u7684\u67E5\u8BEF \u7ED9\u51FA\uFF1A\u6D77\u5E02\u5929\u6C14 \u660E \u667A \uFF08\u65F6\u95F4\:2025\uff09`。{
name这方面,'wear',description:'Query current wear by city',schema:z.object({
从city来看,z.string.desc ription
})
});怎么说呢,// Build final Agent using LL M + Wear Tool
const ag ent=createAg ent({
m odel。llm,t ools:,systemPrompt:'You are a helpful assistant who trusts TOOL results.'
});// Invoke,const answer=await ag ent.invoke({
messages:
});console.log,
Pain point #2 – Context windows overflow when Tools spit out massive text chunks.
Solution → Offload big responses into a virtual file system so only pointers stay in memory.
js
// define desired JSON schema using Zod
// will enforce format on LLM output
// --------------------
import*aszfrom'z od';
export const AnswerSchema=z.object({
summary:z.string.desc ribe,confidence:z.number.de sc ribe
});
// attach schema when creating AGENT
// -----------------------------------
c onst ag ent=c reateAg ent({
model。llm,tools:,responseFormat :AnswerS chema // <- forces JSON response!
});
c onst r esult=awaitag ent.invo ke({
messages:
});
c onse le r esult.struct uredRe spo nse;// => {"summary":"....","confidence":85}
{
summary :"Hangzhou today sunny.."。confidence :94
}
Pain point #10 – No real‑time feedback during lengthy tasks .
Solution → Use streaming mode – print each step as soon as it happens.
。
标签:
实战
-
上一篇:
鸿蒙MVVM实战,如何设计登录与埋点系统?
-
下一篇:
JS中this关键字如何正确使用?
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback