C Programming Using Claude’s New Computer Use Model
原文:
towardsdatascience.com/c-programming-using-claudes-new-computer-use-model-feaad7e3e8db
如你所知,Claude 几天前发布了几条重大新闻。
其中一条是关于一个新的 3.5 Haiku 模型,它承诺能够模拟当前 3.0 Opus 模型的能力,但应该更快、更便宜。这是一个“即将推出”的公告,因为新模型实际上要到 11 月某个时候才会发布。
第二个主要话题,也是让 AI 世界陷入一阵狂热的是,升级后的 Sonnet 3.5 模型也拥有了一个新的能力。
被称为 Computer Use 的模型现在可以控制 PC 桌面。
它以一种非常自然、类似人类的方式做到这一点。
我的意思是,它可以打开窗口和应用程序,使用鼠标进行点选和输入文本,使用 Google 搜索网络等……
在这个阶段,Computer Use 是实验性的,容易出错,但仍然是一个令人兴奋的前景。
现在,你不需要是火箭科学家就能认识到,赋予 AI 这种能力 可能 会有些令人担忧。Claude 通过确保目前新模型只能在通过Docker的沙箱虚拟环境中执行这一功能,稍微缓解了这一点。
什么是 Docker?
Docker 是一个开源平台,旨在通过容器化自动化应用程序的部署、扩展和管理。容器轻量级、便携且自给自足,它们是打包应用程序代码、依赖项、库和配置的独立环境,允许它在不同的计算环境中一致运行。
Docker 通过在容器中隔离应用程序来增强应用安全性,确保每个应用程序在自己的环境中运行,并最小化来自漏洞和依赖的风险。此外,Docker 容器内置了诸如命名空间和控制组等安全功能,这些功能有助于限制对系统资源的访问,减少安全漏洞的潜在影响。
因此,要使用这种新的模型能力,如果你还没有安装和运行 Docker,你首先需要做的是安装并运行 Docker。
安装 Docker
转到,
点击大蓝色**下载 Docker Desktop**按钮,并选择适合你系统的适当安装类型。
当安装文件下载完成后,点击它以开始安装过程并遵循屏幕上的提示。
最后,你将被提示输入您的联系信息。请确保您选择以**个人身份登录 Docker。之后,您应该点击继续到 Docker Desktop**按钮,Docker 应该启动并运行。
由于我们将进行一些编码,所以你需要第二件事是一个 Anthropic API 密钥。你可以从Anthropic 控制台获取。注册或登录,然后你会看到一个像这样的屏幕,
Anthropic 网站图片
点击**获取 API 密钥**按钮,然后按照说明操作。注意你的密钥,因为你很快就需要它。
检查模型的编码能力
我立刻被这个新模型可能提供的功能所吸引,并立刻想到了它可能被用来代表我们编写和运行程序。因此,在本文的其余部分,我们将进一步探讨这一点。
在过去,我做过一些 C 语言编程,所以我想重温一下,并测试一下模型。
我有两个例子在心中。第一个是普遍的hello world程序,但第二个更具挑战性——一个数独求解器。
要指导模型在我们的编码挑战中,我们需要使用 Docker 启动它。
在 Anthropic 的 Computer Use GitHub Demo 中有详细说明如何进行此操作。点击这里前往那里,但基本上,从 WSL2 Ubuntu Linux 提示符中,我做了以下操作,
-
$ export ANTHROPIC_API_KEY=your_api_key
-
$ docker run -e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY -v $HOME/.anthropic:/home/computeruse/.anthropic -p 5900:5900 -p 8501:8501 -p 6080:6080 -p 8080:8080 -it ghcr.io/anthropics/anthropic-quickstarts:computer-use-demo-latest
第一次运行时,docker 命令需要一分钟左右的时间来完成,因为它需要下载所有需要的文件。最终,你会看到这条消息,
图片由作者提供
打开浏览器,在地址栏中输入上述 URL。这将启动应用程序,看起来像这样,
图片由作者提供
测试新模型
- hello world
我在屏幕左下角的提示聊天框中输入了以下内容。
打开一个终端窗口。使用 vi 创建一个 C 程序文件,该文件在终端上打印出文本"hello world"
我不会包括模型所拍的所有截图,因为有很多,但这里是有它回应的文本。
I'll help you create a simple C "hello world" program. Since we need a
terminal, let's first take a screenshot to see the desktop and
locate the terminal icon.
I see a terminal icon in the taskbar. Let's click on it to open a
terminal window.
Now that we have a terminal open, let's create a C program using vi. I'll
type the vi command to create a new file called hello.c:
I see that vi is not installed. Let's install it first:
Now let's try creating our hello.c file again:
Now I'll enter insert mode in vi by pressing 'i' and type our hello world
program:
Now I'll save and exit vi by pressing Escape to exit insert mode, then
typing :wq and Enter:
Now let's compile and run the program. First, we need to make sure we have
gcc installed:
Now let's compile and run our program:
如您从本文的标题图片中看到的,Claude 确实编译了它生成的 C 程序并成功运行了它。
我认为互动中最令人印象深刻的部分是当模型意识到它无法访问特定的程序(在这个例子中是 vi)时,它就继续安装它了!
2. 数独求解器
我这次的提示是,
打开终端并创建一个解决 9×9 数独谜题的 C 程序。程序应以 5 个随机数字在随机位置作为起点填充数独网格。显示这个起始谜题,然后解决谜题并显示最终解决方案。
这里是产生的输出。
Now, let's create a C program that implements a Sudoku solver. First, let's
make sure we have gcc installed:
/usr/bin/gcc
Reading package lists...
Building dependency tree...
Reading state information...
gcc is already the newest version (4:11.2.0-1ubuntu1).
gcc set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Now let's create our Sudoku solver program. I'll use the str_replace_editor
to create the file:
File created successfully at: /home/computeruse/sudoku.c
Now let's compile and run the program:
Generating Sudoku puzzle with 5 random numbers...
Starting position:
+-------+-------+-------+
| . . . | . . 8 | . . 5 |
| . . . | . . . | . . . |
| . . . | . . . | . . . |
+-------+-------+-------+
| . . . | . . . | . . . |
| . . . | . . . | 1 . . |
| . . . | . . . | . . 9 |
+-------+-------+-------+
| . . . | . . . | . . . |
| . . . | . . . | . . . |
| . . . | . . . | 6 . . |
+-------+-------+-------+
Solved puzzle:
+-------+-------+-------+
| 1 2 3 | 4 6 8 | 7 9 5 |
| 4 5 6 | 1 7 9 | 2 3 8 |
| 7 8 9 | 2 3 5 | 4 1 6 |
+-------+-------+-------+
| 2 1 4 | 3 9 6 | 5 8 7 |
| 3 9 5 | 7 8 2 | 1 6 4 |
| 6 7 8 | 5 1 4 | 3 2 9 |
+-------+-------+-------+
| 5 6 1 | 8 2 7 | 9 4 3 |
| 9 3 7 | 6 4 1 | 8 5 2 |
| 8 4 2 | 9 5 3 | 6 7 1 |
+-------+-------+-------+
为了证明这不是偶然,我手动进入终端,编译了代码并重新运行。以下是捕捉这一交互的图片。
图片由作者提供
应用程序继续解释程序的工作原理。在展示之前,我还要求它打印出它创建的 sudoku.c 文件的完整源代码。
Here's the result of running `cat -n` on /home/computeruse/sudoku.c:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4
5 #define N 9
6 #define EMPTY 0
7
8 // Function to check if a number can be placed in a given position
9 int isSafe(int grid[N][N], int row, int col, int num) {
10 // Check row
11 for (int x = 0; x < N; x++) {
12 if (grid[row][x] == num) {
13 return 0;
14 }
15 }
16
17 // Check column
18 for (int x = 0; x < N; x++) {
19 if (grid[x][col] == num) {
20 return 0;
21 }
22 }
23
24 // Check 3x3 box
25 int startRow = row - row % 3;
26 int startCol = col - col % 3;
27 for (int i = 0; i < 3; i++) {
28 for (int j = 0; j < 3; j++) {
29 if (grid[i + startRow][j + startCol] == num) {
30 return 0;
31 }
32 }
33 }
34
35 return 1;
36 }
37
38 // Function to solve Sudoku using backtracking
39 int solveSudoku(int grid[N][N]) {
40 int row, col;
41 int isEmpty = 0;
42
43 // Find an empty cell
44 for (row = 0; row < N; row++) {
45 for (col = 0; col < N; col++) {
46 if (grid[row][col] == EMPTY) {
47 isEmpty = 1;
48 break;
49 }
50 }
51 if (isEmpty) {
52 break;
53 }
54 }
55
56 // If no empty cell found, puzzle is solved
57 if (!isEmpty) {
58 return 1;
59 }
60
61 // Try digits 1 to 9
62 for (int num = 1; num <= 9; num++) {
63 if (isSafe(grid, row, col, num)) {
64 grid[row][col] = num;
65 if (solveSudoku(grid)) {
66 return 1;
67 }
68 grid[row][col] = EMPTY;
69 }
70 }
71
72 return 0;
73 }
74
75 // Function to print the grid
76 void printGrid(int grid[N][N]) {
77 printf("n+-------+-------+-------+n");
78 for (int i = 0; i < N; i++) {
79 printf("|");
80 for (int j = 0; j < N; j++) {
81 if (grid[i][j] == 0)
82 printf(" .");
83 else
84 printf(" %d", grid[i][j]);
85 if ((j + 1) % 3 == 0)
86 printf(" |");
87 }
88 printf("n");
89 if ((i + 1) % 3 == 0)
90 printf("+-------+-------+-------+n");
91 }
92 }
93
94 // Function to generate a random starting position
95 void generateStartingPosition(int grid[N][N], int numClues) {
96 // Initialize random seed
97 srand(time(NULL));
98
99 // Clear the grid
100 for (int i = 0; i < N; i++) {
101 for (int j = 0; j < N; j++) {
102 grid[i][j] = EMPTY;
103 }
104 }
105
106 // Add random numbers
107 for (int i = 0; i < numClues; i++) {
108 int row, col, num;
109 do {
110 row = rand() % N;
111 col = rand() % N;
112 num = 1 + (rand() % 9);
113 } while (!isSafe(grid, row, col, num) || grid[row][col] != EMPTY);
114
115 grid[row][col] = num;
116 }
117 }
118
119 int main() {
120 int grid[N][N] = {{0}};
121
122 printf("Generating Sudoku puzzle with 5 random numbers...n");
123 generateStartingPosition(grid, 5);
124
125 printf("nStarting position:");
126 printGrid(grid);
127
128 if (solveSudoku(grid)) {
129 printf("nSolved puzzle:");
130 printGrid(grid);
131 } else {
132 printf("nNo solution exists.n");
133 }
134
135 return 0;
136 }
以及模型对代码的解释…
Perfect! The program works as expected. Let me explain the key components of
the program:
The program uses a 9x9 grid represented as a 2D array
Main functions:
isSafe: Checks if a number can be placed in a given position
solveSudoku: Implements the backtracking algorithm to solve the puzzle
printGrid: Displays the grid in a nice formatted way
generateStartingPosition: Creates a random starting position with 5 numbers
The solution process:
First, it generates a random starting position with 5 numbers
Then it displays this initial puzzle
Using backtracking, it finds a valid solution
Finally, it displays the solved puzzle
The output shows:
The initial puzzle with 5 random numbers placed (shown as numbers, while empty
cells are shown as dots)
The complete solution that satisfies all Sudoku rules:
Each row contains numbers 1-9 exactly once
Each column contains numbers 1-9 exactly once
Each 3x3 box contains numbers 1-9 exactly once
The program uses a backtracking algorithm which:
Finds an empty cell
Tries numbers 1-9 in that cell
Recursively attempts to solve the rest of the puzzle
If a solution is not possible, it backtracks and tries a different number
You can run the program multiple times to get different starting positions
and solutions, as it uses random number generation to create the initial
puzzle.
Would you like me to explain any specific part of the code in more detail?
我的意思是,太棒了!这有多么令人印象深刻?
摘要
我不喜欢随波逐流,但我必须同意所有那些对新的计算机使用克劳德模型写好评的人。
这非常出色,我只看了它的一小部分,即它的编码能力。
它不能做你想要的所有事情。例如,拖放功能似乎超出了它的能力,它可能会在高图形分辨率下遇到困难。
在安全和安全性方面可能还需要做更多的工作,但请记住,这只是一个开始。
接下来会如何发展?请在评论中告诉我你的想法。
_ 好的,就到这里吧。希望你觉得这篇文章有用。如果你觉得有用,请访问我的个人资料页面这里。从那里,你可以看到我其他发表的故事,关注我或订阅以获取我发布新内容的通知。_
我知道现在经济困难,钱包紧张,但如果你觉得这篇文章有价值,请考虑买一杯小酒。
如果你喜欢这个内容,我认为你也会觉得这些文章值得一读。
更多推荐

所有评论(0)