来源:互联网 更新时间:2026-08-26 14:35
前面聊过一次Hugging Face的Agent框架,这次直接上一个完整的实验脚本,顺便看看它的设计逻辑。说实话,这框架确实挺简洁实用,值得再推荐一次。
Agent的规划方式,大体上分两类:一次性输出所有action的规划,和一步步生成的action规划。transformers.agents里主要支持这两种,但实际变体不少,也正好体现了框架本身的灵活性。

多步规划里又有两种典型形态。一种是用codeblock的形式,比如下面这个codeAgent的示例——一次生成一个完整的代码块,把后续所有动作都规划好。下图的system prompt片段展示了这种输出代码块的方式。
另一种则是类似AutoGPT的风格,输出多个step,然后逐个调用执行。
ReAct的system prompt走的是单步规划路线。有趣的是,ReAct还可以跟code组合使用,打出一些混合拳。
这里放一个完整的测试脚本,代码里用的是“一次性规划所有Action”的方式。日志如下:
from zhipuai import ZhipuAI
client = ZhipuAI(api_key=".") # 填写您自己的APIKey
def llm_engine(messages, stop_sequences=None):
response = client.chat.completions.create(
model="glm-4-plus",
messages=messages,
stop=stop_sequences
)
return response.choices[0].message.content
from transformers import Tool
class Text2image(Tool):
name = "text_to_image"
description = (
"这是一个根据文本生成图片的工具,它返回一个生成的图片路径"
)
inputs = {
"prompt": {
"type": "text",
"description": "需要生成图片的描述文本",
}
}
output_type = "text"
def forward(self, prompt):
response = client.images.generations(
model="cogview-3-plus",
prompt=prompt
)
print(response.data[0].url)
return response.data[0].url
class ImageQuestionAnswering(Tool):
description = "这是一个可以回答关于图片问题的工具,它返回一个文本,作为对问题的答案。"
name = "image_qa"
inputs = {
"image_path": {
"type": "text",
"description": "图片路径或url",
},
"question": {"type": "text", "description": "问题"},
}
output_type = "text"
def forward(self, image_path, question):
if 'http' not in image_path:
with open(image_path, 'rb') as img_file:
img_base = base64.b64encode(img_file.read()).decode('utf-8')
else:
img_base = image_path
response = client.chat.completions.create(
model="glm-4v-plus", # 填写需要调用的模型名称
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": img_base
}
},
{
"type": "text",
"text": question
}
]
}
]
)
return response.choices[0].message.content
from transformers import Tool, load_tool, CodeAgent
agent = CodeAgent(tools=[Text2image(),ImageQuestionAnswering()], llm_engine=llm_engine, verbose=1)
agent.run(
"画一张搞笑图片,然后描述一下这张图片为什么搞笑?以及图片内容是否符合你生成的prompt"
) 腾讯ima怎么把微信内容一键导入知识库?
腾讯ima怎么创建共享知识库?
Celestia价格预测2026-2032:TIA币能否引领山寨币上涨行情?历史价格回顾
比特币(BTC)核心周期指标复刻历史走势 价格或跌破5.8万美元关键支撑位
比特币 2025 年价格预测:BTC 的未来走势
新浪互联网热点小时报丨2026年07月26日16时_今日实时互联网热点速递
新浪机器学习热点小时报丨2026年07月25日18时_今日实时机器学习热点速递
WorkBuddy微信版怎么获得积分?
新浪人工智能热点小时报丨2026年07月30日18时_今日实时人工智能热点速递
5000元起的鼠标哪个最值得入手?
腾讯ima知识库怎么分类管理?
短剧《史上最强洪荒修为》剧情介绍
海尔消毒柜自动消毒如何中止
博世壁挂炉关闭暖气怎么操作
男生高性价比充电头?
车载冰箱重置到出厂设置几步?
Windy卫星云图怎么看?云层变化识别技巧
WorkBuddy积分怎么获得?
5000-6000元鼠标有什么推荐?
管线机怎么接云米净水器
手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc