微信扫码
添加专属顾问
我要投稿
前面写过一次抱抱脸的Agent框架了,这次提供一个完整实验脚本,以及看看它的设计逻辑。再一次给大家安利一下,确实比较简单实用。
Agent的规划一般分为一次出来所有的action规划和一步一步的action规划。
transformers.agents中也主要支持这2种,但是变化很多,也可以体现出框架/Agent的灵活性。
多步的有2种,一种用codeblock的形式,下图为codeAgent的示例,一次用一个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"
)53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-11-03
震惊,Github开源,真正让程序员效率提升 90%的AI辅助工具来啦!!!
2025-11-03
Dify迎来最强开源对手!这个本地Agent内置微调+超细权限控制~
2025-11-03
我们大胆做了个决定,大会所有音乐bgm由AI生成,这部分预算可以省了!|Jinqiu Scan
2025-11-03
LongCat-Flash-Omni 正式发布并开源:开启全模态实时交互时代
2025-11-03
科大讯飞“王炸”开源!企业级智能体平台 Astron Agent:原生集成 RPA,Apache 2.0 商业友好!
2025-11-03
DeepSeek-OCR到底厉害在哪?
2025-11-02
刚刚,OpenAI开源了两个大模型~
2025-11-01
零一万物联合开源中国推出OAK平台,目标打造Agent世界的“基础设施”
            2025-08-20
2025-09-07
2025-08-20
2025-08-26
2025-08-22
2025-09-06
2025-10-20
2025-08-22
2025-09-08
2025-08-12
2025-11-03
2025-10-29
2025-10-28
2025-10-13
2025-09-29
2025-09-17
2025-09-09
2025-09-08