> ## Content Index
> Fetch the complete content index at: https://www.hubwiz.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# OpenHands：AI软件工程师
- URL: https://www.hubwiz.com/blog/openhands-ai-software-engineer/
- Published: 2024-11-12T02:04:30.000Z
- Updated: 2024-11-12T02:04:49.000Z
- Description: OpenHands（原名 OpenDevin）是一个由 AI 驱动的软件开发代理平台，AI代理可以执行人类开发人员可以执行的任何操作：修改代码、运行命令、浏览网页、调用 API，甚至可以从 StackOverflow 复制代码片段。
- Author: admin
- Tags: TOOL, SOFTWARE DEVELOPMENT, OPENHANDS, AGENT, DOCKER, LITELLM, #Import 2026-10-14 14:41

[ OpenHands](https://github.com/All-Hands-AI/OpenHands?ref=hubwiz.com)（原名 OpenDevin）是一个由 AI 驱动的软件开发代理平台，AI代理可以执行人类开发人员可以执行的任何操作：修改代码、运行命令、浏览网页、调用 API，甚至可以从 StackOverflow 复制代码片段。

## 1、OpenHands安装与设置

系统要求：

- Docker 版本 26.0.0+ 或 Docker Desktop 4.31.0+。
- 必须使用 Linux 或 Mac OS。
- 如果使用的是 Windows，则必须使用 WSL。

### 1.1 启动应用

运行 OpenHands 最简单的方法是使用 Docker：

```
docker pull docker.all-hands.dev/all-hands-ai/runtime:0.13-nikolaik

docker run -it --pull=always \
    -e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:0.13-nikolaik \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -p 3000:3000 \
    -e LOG_ALL_EVENTS=true \
    --add-host host.docker.internal:host-gateway \
    --name openhands-app \
    docker.all-hands.dev/all-hands-ai/openhands:0.13
```

你会发现 OpenHands 在 `http://localhost:3000` 上运行！

你还可以以可编写脚本的[无头模式](https://docs.all-hands.dev/modules/usage/how-to/headless-mode?ref=hubwiz.com)、[交互式 CLI ](https://docs.all-hands.dev/modules/usage/how-to/cli-mode?ref=hubwiz.com)或使用 [OpenHands GitHub Action](https://docs.all-hands.dev/modules/usage/how-to/github-action?ref=hubwiz.com) 运行 OpenHands。

### 1.2 设置大模型

启动 OpenHands 后，需要设置大模型。你会看到一个设置模式。你必须选择 LLM 提供商和 LLM 模型并输入相应的 API 密钥。你可以随时通过选择 UI 中的“设置”按钮（齿轮图标）来更改这些内容。

如果列表中不存在所需的 LLM 模型，你可以切换“高级选项”，并在“自定义模型”文本框中使用正确的前缀手动输入它。高级选项还允许你根据需要指定基本 URL。

![](https://www.hubwiz.com/blog/content/images/2024/11/image-912.png)

Anthropic 的[ Claude 3.5 Sonnet](https://www.anthropic.com/api?ref=hubwiz.com) (anthropic/claude-3-5-sonnet-20241022) 效果最好，但你还有很多选择，具体请参考本文第4部分。

## 2、OpenHands快速上手

OpenHands 可以帮助你处理各种各样的工程任务。但这项技术仍然很新，我们距离拥有能够在没有任何指导的情况下承担大型复杂工程任务的代理还有很长的路要走。因此，了解代理擅长做什么以及在哪些方面可能需要帮助非常重要。

### 2.1 Hello, OpenHands!

你可能想要尝试的第一件事是一个简单的“hello world”示例。这可能比听起来更复杂！

尝试使用以下方式提示代理：

```
Please write a bash script hello.sh that prints "hello world!"
```

> 请编写一个打印“hello world！”的 bash 脚本 hello.sh

你应该看到代理不仅编写了脚本，还设置了正确的权限并运行脚本来检查输出。

你可以继续提示代理完善您的代码。这是使用代理的好方法。从简单开始，然后迭代：

```
Please modify hello.sh so that it accepts a name as the first argument, but defaults to "world"
```

> 请修改 hello.sh，使其接受名称作为第一个参数，但默认为“world”

你也可以使用你需要的任何语言，尽管代理可能需要花一些时间来设置其环境！

```
Please convert hello.sh to a Ruby script, and run it
```

> 请将 hello.sh 转换为 Ruby 脚本，然后运行它

### 2.2 从头开始构建应用

代理在“绿地”任务（不需要任何现有代码库上下文的任务）方面表现非常出色，它们可以从头开始。

最好从一个简单的任务开始，然后对其进行迭代。最好尽可能具体地说明你想要什么、技术栈应该是什么等。

例如，我们可以构建一个 TODO 应用程序：

```
Please build a basic TODO list app in React. It should be frontend-only, and all state should be kept in localStorage.
```

> 请在 React 中构建一个基本的 TODO 列表应用程序。它应该是前端专用的，并且所有状态都应保存在 localStorage 中。

一旦有了框架，我们就可以继续对应用程序进行迭代：

```
Please allow adding an optional due date to every task
```

> 请允许为每个任务添加可选的截止日期

就像正常开发一样，经常提交和推送代码是很好的。这样，如果代理偏离轨道，你总是可以恢复到旧状态。你可以要求代理为你提交和推送：

```
Please commit the changes and push them to a new branch called "feature/due-dates"
```

> 请提交更改并将其推送到名为“feature/due-dates”的新分支

### 2.3 添加新代码

OpenHands 还可以出色地将新代码添加到现有代码库中。

例如，你可以要求 OpenHands 向你的项目添加一个新的 GitHub 操作，以检查你的代码。OpenHands 可能会查看你的代码库以查看它应该使用哪种语言，但随后它可以将新文件放入 `./github/workflows/lint.yml` ：

```
Please add a GitHub action that lints the code in this repository
```

> 请添加一个 GitHub 操作来检查此存储库中的代码

某些任务可能需要更多上下文。虽然 OpenHands 可以使用 `ls` 和 `grep` 搜索你的代码库，但预先提供上下文可以使其移动得更快、更准确。而且它会花费你更少的令牌：

```
Please modify ./backend/api/routes.js to add a new route that returns a list of all tasks
```

> 请修改 `./backend/api/routes.js` 以添加返回所有任务列表的新路由

```
Please add a new React component that displays a list of Widgets to the ./frontend/components directory. It should use the existing Widget component.
```

> 请向 `./frontend/components` 目录添加显示 Widget 列表的新 React 组件。它应该使用现有的 Widget 组件。

### 2.4 代码重构

OpenHands 非常擅长重构现有代码，尤其是小块代码。你可能不想尝试重新构建整个代码库，但拆分长文件和函数、重命名变量等往往非常有效。考虑以下提示：

```
Please rename all the single-letter variables in ./app.go
```

> 请重命名 `./app.go` 中的所有单字母变量

```
Please break the function build_and_deploy_widgets into two functions, build_widgets and deploy_widgets in widget.php
```

> 请将 `build_and_deploy_widgets` 函数拆分为 `widget.php` 中的两个函数 `build_widgets` 和 `deploy_widgets`

```
Please break ./api/routes.js into separate files for each route
```

> 请将 `./api/routes.js` 拆分为每个路由的单独文件

### 2.5 Bug修复

OpenHands 还可以帮助你追踪和修复代码中的错误。但是，正如任何开发人员都知道的那样，修复错误可能非常棘手，而且 OpenHands 通常需要更多上下文。如果你已经诊断出错误，但希望 OpenHands 找出逻辑，这将有所帮助。

考虑以下提示：

```
Currently the email field in the /subscribe endpoint is rejecting .io domains. Please fix this.
```

> 目前 `/subscribe` 端点中的电子邮件字段拒绝 `.io` 域。请修复此问题。

```
The search_widgets function in ./app.py is doing a case-sensitive search. Please make it case-insensitive.
```

> `./app.py` 中的 `search_widgets` 函数正在执行区分大小写的搜索。请将其设置为不区分大小写。

在使用代理修复错误时，进行测试驱动开发通常会有所帮助。你可以要求代理编写新测试，然后进行迭代，直到修复错误：

```
The hello function crashes on the empty string. Please write a test that reproduces this bug, then fix the code so it passes.
```

> `hello` 函数在空字符串上崩溃。请编写一个重现此错误的测试，然后修复代码以使其通过。

### 2.6 更多任务

OpenHands 能够帮助完成几乎任何编码任务。但需要一些练习才能充分利用它。请记住：

- 将任务保持在小规模
- 尽可能具体
- 提供尽可能多的背景信息
- 经常承诺和推动

## 3、OpenHands提示最佳实践

与 OpenHands AI 软件开发人员合作时，提供清晰有效的提示至关重要。本指南概述了创建提示的最佳实践，这些提示将产生最准确和最有用的响应。

良好的提示的特点：

- 具体：它们准确地解释了应该添加哪些功能或需要修复哪些错误。
- 位置特定：如果已知，它们会解释代码库中应该修改的位置。
- 适当的范围：它们应该是单个功能的大小，通常不超过 100 行代码。

良好的提示示例：

```
"Add a function calculate_average in utils/math_operations.py that takes a list of numbers as input and returns their average."
```

> "在 utils/math\_operations.py 中添加一个函数 calculate\_average，它将数字列表作为输入并返回它们的平均值。”

```
"Fix the TypeError in frontend/src/components/UserProfile.tsx occurring on line 42. The error suggests we're trying to access a property of undefined."
```

> “修复第 42 行出现的 frontend/src/components/UserProfile.tsx 中的 TypeError。错误表明我们正在尝试访问未定义的属性。”

```
"Implement input validation for the email field in the registration form. Update frontend/src/components/RegistrationForm.tsx to check if the email is in a valid format before submission."
```

> “对注册表单中的电子邮件字段实施输入验证。在提交之前，更新 frontend/src/components/RegistrationForm.tsx 以检查电子邮件是否为有效格式。”

糟糕的提示示例：

```
"Make the code better." (Too vague, not concrete)
```

> “改进代码。”（太模糊，不具体）

```
"Rewrite the entire backend to use a different framework." (Not appropriately scoped)
```

> “重写整个后端以使用不同的框架。”（范围不适当）

```
"There's a bug somewhere in the user authentication. Can you find and fix it?" (Lacks specificity and location information)
```

> “用户身份验证中某处有错误。你能找到并修复它吗？”（缺乏特异性和位置信息）

有效提示的技巧：

- 尽可能具体地说明期望的结果或要解决的问题。
- 提供上下文，包括相关文件路径和行号（如果有）。
- 将大型任务分解为更小、更易于管理的提示。
- 包括任何相关的错误消息或日志。
- 如果从上下文中看不出来，请指定编程语言或框架。

请记住，提示越精确、越有信息量，AI 就越能帮助你开发或修改 OpenHands 软件。

## 4、OpenHands支持的大模型

OpenHands 可以连接到任何 LiteLLM 支持的 LLM。但是，它需要强大的模型才能工作。

根据我们对编码任务语言模型的评估（使用 SWE-bench 数据集），我们可以提供一些模型选择建议。可以参考[这篇比较LLM的博客文章](https://www.all-hands.dev/blog/evaluation-of-llms-as-coding-agents-on-swe-bench-at-30x-speed?ref=hubwiz.com)和这篇[包含最新结果的博客文章](https://www.all-hands.dev/blog/openhands-codeact-21-an-open-state-of-the-art-software-development-agent?ref=hubwiz.com)。

选择模型时，请同时考虑输出的质量和相关成本。以下是调查结果的摘要：

- Claude 3.5 Sonnet 是相当好的，在 OpenHands 中的默认代理的 SWE-Bench Verified 上实现了 53% 的解决率。
- GPT-4o 落后，o1-mini 的表现实际上比 GPT-4o 差一些。我们深入分析了结果，简要地看，o1 似乎有时会“想太多”，执行额外的环境配置任务，而它本来可以继续完成任务。
- 最后，最强大的开放模型是 Llama 3.1 405 B 和 deepseek-v2.5，它们的表现相当不错，甚至超越了一些封闭模型。

请参阅[完整文章](https://www.all-hands.dev/blog/evaluation-of-llms-as-coding-agents-on-swe-bench-at-30x-speed?ref=hubwiz.com)了解更多详情。

根据这些发现和社区反馈，以下模型已被验证可以与 OpenHands 很好地配合使用：

- claude-3-5-sonnet（推荐）
- gpt-4 / gpt-4o
- llama-3.1-405b
- deepseek-v2.5

有关可用提供商和模型的完整列表，请参阅[ litellm 文档](https://docs.litellm.ai/docs/providers?ref=hubwiz.com)。

---

原文链接：[OpenHands: Code Less, Make More](https://github.com/All-Hands-AI/OpenHands?ref=hubwiz.com)

汇智网翻译整理，转载请标明出处