当前位置: 首页 > news >正文

mini-swe-agent源码解读(进行中)

https://github.com/SWE-agent/mini-swe-agent
虽然声称100行代码的Agent(The 100 line AI agent),但实际上这个仓库几万行代码
在这里插入图片描述
说实在的,我读的速度都没有他写的速度快。

前置准备:

1、知道使用litellm调用大模型
2、prompt_toolkit来获取用户的终端输入
3、使用from rich.console import Console进行终端的输出
4、使用platformdirs获取用户目录,finding the right location to store user data and configuration varies per platform
5、typer: typer命令行工具开发库,说起typer你可能不知道,但是fastapi可能听过,typer和fastapi的主要开发者都是tiangolo。

6、变成系统命令
[project.scripts]
mini = “minisweagent.run.mini:app”
mini-swe-agent = “minisweagent.run.mini:app”
mini-extra = “minisweagent.run.mini_extra:main”
mini-e= “minisweagent.run.mini_extra:main”
7、使用dotenv从.env加载环境变量

dotenv.load_dotenv(dotenv_path=global_config_file)
8、使用subprocess.run来执行命令,这个是此agent能起作用的核心
(我们好奇cursor生成文件和修改文件用的是什么?)
9、from jinja2 import Template模版的渲染

入口

python run/mini.py -t “say hello world”

代码中抽象出了几个概念
model
agent:代理中有执行环境和model
Environments: 这个不是环境变量,这个分为本地环境和docker环境,环境可以执行命令

先从最令人激动的点模型调用说起

LitellmModel

model.query封装了litellm,这块可以理解

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

role有: system user assistant
当通过api来调用大模型的时候,就这三个
剩下的就是你怎么利用大模型的返回值了

然后看看对话是怎么处理的?

初始 self.messages: list[dict] = []是空的

system_template: str = "You are a helpful assistant that can do anything."
instance_template: str = ("Your task: {{task}}. Please reply with a single shell command in triple backticks. ""To finish, the first line of the output of the shell command must be 'MINI_SWE_AGENT_FINAL_OUTPUT'."
)self.messages = []self.add_message("system", self.config.system_template)self.add_message("user", self.render_template(self.config.instance_template, task=task))

第一次增加系统提示词和用户提示词
对话完成后,会把大模型的返回值,存储到self.messages里面,这样下次继续带着对话。

agent.run

    def run(self, task: str) -> tuple[str, str]:"""Run step() until agent is finished. Return exit status & message"""self.messages = []self.add_message("system", self.config.system_template)self.add_message("user", self.render_template(self.config.instance_template, task=task))while True:try:self.step()except NonTerminatingException as e:self.add_message("user", str(e))except TerminatingException as e:self.add_message("user", str(e))return type(e).__name__, str(e)

这是随便找的一个,你看在同一个对话下的数据实际上都携带着呢
在这里插入图片描述

查看流程图

在这里插入图片描述

execute前进行确认

在这里插入图片描述

# 这段代码学到了match user_input := self._prompt_and_handle_special(prompt).strip():case "" | "/y":pass  # confirmed, do nothingcase "/u":  # Skip execution action and get back to queryraise NonTerminatingException("Command not executed. Switching to human mode")case _:raise NonTerminatingException(f"Command not executed. The user rejected your command with the following message: {user_input}")

mode有三种
_MODE_COMMANDS_MAPPING = {“/u”: “human”, “/c”: “confirm”, “/y”: “yolo”}

命令的解析和执行
output = self.execute_action(self.parse_action(response))


好多人
在这里插入图片描述


思考:

意图识别、对话压缩

可以增加意图识别,因为用户的输入不一定就是任务,可以在任务前增加一个意图识别的过程。

如果即将超出上下文的显示可以对对话进行压缩

http://www.dtcms.com/a/311633.html

相关文章:

  • Redis 7 哈希(Hash)使用指南
  • 细分推广场景,让推客推广更贴合用户当下需求
  • 存储过程的介绍、基本语法、delimiter的使用
  • 未来交通:元宇宙技术重塑出行体验
  • 用Unity结合VCC更改人物模型出现的BUG
  • WebSocket断线重连机制:保障实时通信的高可用性
  • 疯狂星期四文案网第27天运营日记
  • 井云科技2D交互数字人:让智能服务触手可及的实用方案
  • KSP与ASM深度对比:原理、性能与使用场景
  • SpringBoot怎么查看服务端的日志
  • sqli-labs通关笔记-第28a关GET字符注入(关键字过滤绕过 手注法)
  • USB Device(VID_1f3a_PID_efe8) 驱动叹号
  • ART数据库索引结构--ART,The adaptive radix tree论文细读
  • 基于落霞归雁思维框架的软件需求管理实践指南
  • 字节Seed发布扩散语言模型,推理速度达2146 tokens/s,比同规模自回归快5.4倍
  • 【C++/STL】vector的OJ,深度剖析和模拟实现
  • Java多线程入门-基础概念与线程操作
  • JVM 01 运行区域
  • 动态规划经典模型:双数组问题的通用解决框架与实战
  • C++ STL 组件及其关系:从模块化设计到图形化展示
  • SpringBoot AOP
  • CYUSB3014-BZXC-USB3.0接口芯片-富利威
  • python---literal_eval函数
  • Python管道编程解析:构建高效数据流处理框架
  • Redis从入门到实战
  • Effective C++ 条款18:让接口容易被正确使用,不易被误用
  • IOT物联网平台发布,可私有化部署
  • 算法刷题【面试经典150题】
  • 技巧|SwanLab记录PR曲线攻略
  • 【Unity3D实例-功能-移动】小兵移动-通过鼠标点击进行