AI 写代码不必烧钱,电脑也能当程序员 | Your Mac Codes for Free. Almost Magic

AI 编程不一定要靠昂贵的订阅。用 Pi、Ollama 和本地代码模型,就能让 Mac 读懂项目、修改代码、运行测试,还能通过 Skills 和 Extensions 定制工作流。它不一定次次都是天才,但至少不会因为你多问一句,就突然递来一张 API 账单。
You don’t need an expensive subscription to start coding with AI. Pi, Ollama and a local coding model can inspect files, edit code, run tests, and follow your own reusable skills. It may occasionally need a little guidance, but at least your Mac won’t send you a surprise API bill.
完整实操案例
案例:打造一个完全免费的 AI 个人任务管理器
中文:用 Pi 和本地 AI 模型开发一个能添加任务、标记完成、保存数据并自动测试的个人任务管理器。/ English: Build a personal task manager with Pi and a local AI model that can add tasks, mark them complete, persist data, and run automated tests.
这个案例的目标不是单纯让 AI 生成一段代码,而是完整展示一条可复用的开发工作流:
使用本地模型,不购买 API 额度。
使用 Pi 读取、修改和执行项目代码。
使用 Git 保存检查点,避免 AI 改坏代码后无法恢复。
使用 Skills 保存项目开发规范。
使用 Extensions 扩展 Agent 的能力。
通过自动化测试验证结果。
通过 Git Diff 检查修改。
通过精简上下文和拆分任务,减少无意义的模型调用。
最后形成可以继续开发和维护的项目。
推荐技术栈:Pi + Ollama + Qwen2.5-Coder 7B + TypeScript + Node.js + Vitest + Git。
这套组合不需要 Docker,不需要付费 API,也不需要安装多个相互重叠的编程 Agent。
选择 Qwen2.5-Coder 7B 是为了兼顾你的 M2 芯片、16GB 内存和代码生成任务。它不是所有代码任务中最强的模型,但作为本地开发起点比较合理。
本案例使用本地模型运行,不使用 Ollama 的云端模型功能。
1. 检查开发环境/Check the development environment
先打开 macOS 的 Terminal,检查 Node.js、npm、Git 和 Homebrew 是否已经安装。/Open Terminal on macOS and check whether Node.js, npm, Git, and Homebrew are installed.
node -v
npm -v
git --version
brew --version
Pi 的 npm 安装方式要求 Node.js 22.19 或更新版本。/Pi’s npm installation requires Node.js 22.19 or newer.
如果没有安装 Homebrew,可以先按照官方说明安装。/If Homebrew is missing, install it using the official instructions.
https://brew.sh/
2. 安装 Node.js 和 Git/Install Node.js and Git
如果已经安装符合要求的 Node.js 和 Git,可以跳过这一步。/If you already have compatible versions of Node.js and Git, skip this step.
brew install node@22 git
确认 Node.js 版本满足要求。/Verify that the Node.js version meets the requirement.
node -v
npm -v
git --version
如果终端找不到 Node.js 22,先按照 Homebrew 的提示调整 PATH,再继续安装 Pi。/If the terminal cannot find Node.js 22, follow Homebrew’s instructions to adjust PATH before installing Pi.
3. 安装 Ollama/Install Ollama
Ollama 负责在你的 Mac 上运行本地模型,不需要连接付费模型 API。/Ollama runs the model locally on your Mac without requiring a paid model API.
brew install --cask ollama
如果 Homebrew 安装方式不可用,可以从 Ollama 官方网站下载 macOS 版本。/If the Homebrew installation method is unavailable, download the macOS version from the official Ollama website.
https://ollama.com/download
安装完成后,启动 Ollama 应用,并在终端检查是否可以使用命令行。/After installation, launch Ollama and check whether its command-line interface is available.
ollama --version
4. 下载本地代码模型/Download the local coding model
本案例使用 Qwen2.5-Coder 7B,因为它比大型模型更适合在 16GB 内存的电脑上起步。/This example uses Qwen2.5-Coder 7B because it is a more practical starting point for a computer with 16GB of memory than a large model.
ollama pull qwen2.5-coder:7b
下载完成后,运行一次模型,确认本地推理正常。/After downloading the model, run it once to verify that local inference works.
ollama run qwen2.5-coder:7b
在模型提示符中输入一句简单的代码问题,确认模型可以回复,然后输入退出命令。/Enter a simple coding question at the model prompt, confirm that it responds, and then exit the session.
/bye
模型文件需要占用磁盘空间,实际运行速度取决于内存、上下文长度及其他正在运行的应用。/The model occupies disk space, and actual performance depends on memory, context length, and other running applications.
5. 安装 Pi/Install Pi
使用官方安装器安装 Pi。/Install Pi using the official installer.
curl -fsSL https://pi.dev/install.sh | sh
安装后检查版本。/Check the installed version.
pi --version
如果安装器没有自动配置 PATH,请按照安装提示修正后再继续。/If the installer does not configure PATH automatically, follow its instructions before continuing.
6. 验证 Pi 能否连接本地模型/Verify that Pi can connect to the local model
Pi 支持 Ollama 模型提供商,可以使用本地运行的模型。/Pi supports the Ollama provider and can use models running locally.
先确认 Ollama 服务正常运行,并确认模型已经下载。/First confirm that the Ollama service is running and the model has been downloaded.
ollama list
然后在终端查看 Pi 的模型帮助信息。/Then inspect Pi’s model-related help information.
pi --help
尝试启动本地模型。/Try launching the local model.
pi --provider ollama --model qwen2.5-coder:7b
如果当前版本的 Pi 对模型名称或提供商参数有不同要求,请以当前版本的官方文档为准。/If your installed Pi version expects a different provider or model identifier, follow the documentation for that version.
进入交互界面后,先让模型解释当前目录中的一个简单文件,再尝试让它修改一个小问题。/Once the interactive interface opens, ask the model to explain a simple file in the current directory before asking it to fix a small issue.
7. 创建项目/Create the project
先创建独立项目目录,避免 AI 误改其他文件。/Create a dedicated project directory to prevent accidental changes to unrelated files.
mkdir -p ~/Projects/free-ai-task-manager
cd ~/Projects/free-ai-task-manager
初始化 Git。/Initialize Git.
git init
创建 Node.js 项目。/Create a Node.js project.
npm init -y
此时项目还没有业务代码,正适合让 AI 从一个干净的目录开始。/The project has no application code yet, making this a clean starting point for the AI agent.
8. 安装 TypeScript 和测试工具/Install TypeScript and testing tools
使用 TypeScript 编写业务代码,并使用 Vitest 自动化测试。/Use TypeScript for application code and Vitest for automated testing.
npm install -D typescript vitest tsx @types/node
生成 TypeScript 配置。/Generate the TypeScript configuration.
npx tsc --init
先不急着开发业务功能,接下来让 AI 按照明确的需求建立项目结构。/Do not rush into implementing features yet; next, ask the AI to establish the project structure from a clear specification.
9. 定义项目需求/Define the project requirements
在项目根目录创建 README.md,写明这个任务管理器必须实现的功能。/Create a README.md in the project root describing the required features of the task manager.
cat > README.md <<'EOF'
# Free AI Task Manager
A small local task manager built with TypeScript.
## Requirements
- Add a task with a title.
- List all tasks.
- Mark a task as completed.
- Delete a task.
- Persist tasks in a local JSON file.
- Validate empty task titles.
- Handle missing or invalid data files safely.
- Include automated tests.
- Keep the implementation simple and dependency-light.
## Quality requirements
- Use clear TypeScript types.
- Keep business logic separate from the CLI.
- Never delete existing task data without an explicit request.
- Run automated tests before declaring the task complete.
- Explain changed files and test results.
EOF
这些需求足够真实,也足够小,适合在普通电脑上完成和验证。/These requirements are realistic yet small enough to implement and verify on an ordinary computer.
10. 建立第一个 Git 检查点/Create the first Git checkpoint
在让 AI 修改代码之前,先保存当前项目状态。/Save the current project state before allowing the AI to modify code.
git add .
git commit -m "chore: initialize task manager"
如果 Git 提示缺少用户信息,可以按自己的实际信息配置姓名和邮箱。/If Git reports missing identity information, configure your own name and email address.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
提交前确认配置符合自己的实际情况,不要直接照抄示例邮箱。/Verify the configuration before committing; do not use the example email address literally.
11. 为 Pi 创建项目 Skill/Create a project skill for Pi
Pi 的 Skills 可以保存可复用的工作规范,让模型不必每次都重新学习项目要求。/Pi Skills store reusable working instructions so the model does not need to relearn the project requirements every time.
创建项目级 Skill 目录。/Create the project-level skill directory.
mkdir -p .pi/skills/task-manager
创建 Skill 文件。/Create the skill file.
cat > .pi/skills/task-manager/SKILL.md <<'EOF'
---
name: task-manager
description: Build and maintain the local TypeScript task manager.
---
# Task Manager Development Rules
- Read README.md before changing application code.
- Keep business logic separate from CLI code.
- Use TypeScript types for task data.
- Preserve existing task data.
- Validate user input.
- Add or update tests for every behaviour change.
- Run the relevant tests after editing.
- Inspect git diff before finishing.
- Explain any tests that could not be run.
EOF
这个 Skill 将项目的开发规则集中保存,方便后续反复使用。/This skill centralizes the project’s development rules for repeated use.
12. 让 Pi 实现核心功能/Ask Pi to implement the core features
启动 Pi。/Start Pi.
pi --provider ollama --model qwen2.5-coder:7b
向 Pi 提交下面的任务。/Give Pi the following task.
Read README.md and the task-manager skill.
Build the task manager in small, testable TypeScript modules.
Implement:
1. Add a task.
2. List tasks.
3. Complete a task.
4. Delete a task.
5. Persist tasks in a local JSON file.
6. Validate empty titles.
7. Handle missing or invalid data safely.
8. Add automated tests.
Keep the implementation simple.
Do not delete existing data.
Run the tests and inspect the final diff.
Explain how to use the application.
让模型先读取需求,再修改文件,并在执行命令前检查它准备做什么。/Let the model read the requirements first, then modify files, while reviewing the commands it intends to execute.
如果它一次生成了过多代码,可以要求它先完成数据模型和测试,再实现命令行交互。/If it generates too much code at once, ask it to implement the data model and tests first, followed by the command-line interface.
13. 运行测试并检查代码/Run tests and inspect the code
退出 Pi 后,在终端运行项目测试。/Exit Pi and run the project tests in the terminal.
npx vitest run
再运行 TypeScript 检查。/Then run the TypeScript check.
npx tsc --noEmit
如果出现错误,把完整错误信息交给 Pi,让它定位问题、修复并重新运行测试。/If errors occur, give the complete error output to Pi and ask it to diagnose the issue, fix it, and rerun the tests.
不能因为 AI 说“已经完成”就直接认定正确,必须以实际测试结果为准。/Do not assume the work is correct merely because the AI says it is finished; rely on actual test results.
14. 检查 Git Diff/Review the Git diff
查看 AI 修改了哪些文件。/Inspect which files the AI changed.
git status --short
git diff --check
git diff
确认没有意外删除文件、写入密钥或引入不必要的依赖。/Check that no files were unexpectedly deleted, no secrets were added, and no unnecessary dependencies were introduced.
如果修改符合预期,就创建第二个检查点。/If the changes are acceptable, create the second checkpoint.
git add .
git commit -m "feat: implement local task manager"
15. 添加一个 Pi Extension/Add a Pi extension
Skill 负责提供工作规范,而 Extension 可以扩展 Agent 的交互能力。/A Skill provides working instructions, while an Extension can expand the agent’s interactive capabilities.
本案例增加一个简单的 /project-check 命令,用来提醒自己检查项目状态。/This example adds a simple /project-check command to remind us to inspect the project state.
创建扩展目录。/Create the extension directory.
mkdir -p .pi/extensions
创建扩展文件。/Create the extension file.
cat > .pi/extensions/project-check.ts <<'EOF'
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
export default function (pi: ExtensionAPI) {
pi.registerCommand("project-check", {
description: "Show a reminder to validate the project",
handler: async (_args, ctx) => {
ctx.ui.notify(
"Run tests, inspect git diff, and review changed files.",
"info"
);
},
});
}
EOF
这个 Extension 使用 Pi 的扩展 API 注册命令。/This extension registers a command using Pi’s extension API.
具体扩展发现规则可能随版本变化,因此如果项目级扩展没有自动加载,请检查当前版本的官方 Extensions 文档。/Extension discovery rules may vary by version, so consult the official Extensions documentation if the project-level extension is not loaded automatically.
16. 验证 Extension/Verify the extension
重新启动 Pi。/Restart Pi.
pi --provider ollama --model qwen2.5-coder:7b
在 Pi 中运行新命令。/Run the new command inside Pi.
/project-check
如果命令成功显示提示,说明已经完成一个最小的自定义 Extension。/If the command displays the reminder successfully, the minimal custom extension is working.
如果出现加载错误,先检查扩展路径、依赖解析和当前 Pi 版本的 API,再修改代码。/If a loading error occurs, check the extension path, dependency resolution, and the API for your installed Pi version before changing the code.
17. 验证完整业务流程/Verify the complete workflow
让 Pi 解释实际使用方式,或者直接在终端运行它生成的 CLI 命令。/Ask Pi to explain how to use the application, or run the CLI commands it generated directly in the terminal.
依次测试添加任务、查看任务、完成任务和删除任务。/Test adding, listing, completing, and deleting tasks in sequence.
关闭程序后重新启动,确认任务仍然存在。/Close and restart the application to confirm that tasks persist.
尝试输入空任务标题,确认程序能够正确拒绝无效输入。/Try entering an empty task title and verify that the application rejects invalid input correctly.
18. 控制上下文与模型消耗/Control context and model usage
本地模型不会产生远程模型 API 账单,但仍然消耗计算资源和时间。/A local model does not create a remote model API bill, but it still consumes computing resources and time.
把任务拆成小块,每次只让 Pi 完成一个清晰的目标。/Split work into small tasks and give Pi one clear objective at a time.
完成一个阶段后,先运行测试、检查 Diff 并保存 Git 检查点,再开始下一阶段。/After each phase, run tests, inspect the diff, and save a Git checkpoint before moving to the next phase.
不必机械地清空所有会话;如果历史已经与当前任务无关,可以开启新会话并重新提供必要的项目说明。/You do not need to clear every session mechanically; when previous history is no longer relevant, start a fresh session and provide the necessary project instructions.
19. 最终验收/Final acceptance
确认项目能正常安装依赖、通过测试、通过 TypeScript 检查,并能完成全部任务管理操作。/Confirm that dependencies install correctly, tests pass, TypeScript checks succeed, and all task-management operations work.
检查 Git 状态,确保没有未解释的修改或敏感信息。/Inspect Git status and ensure there are no unexplained changes or sensitive information.
git status --short
git log -3 --oneline
如果以上检查全部通过,就拥有了一个可运行、可测试、可继续扩展的本地 AI 编程项目。/If all checks pass, you have a runnable, tested, and extensible project built with a local AI coding workflow.
评论
发表评论