OpenCode环境搭建及常用操作

文章目录

一、Skills环境安装

1.Node.js

2.安装opencode

sh 复制代码
npm i -g opencode-ai

3.启动opencode

sh 复制代码
# cmd命令行输入
opencode

二、模型设置

1.免费模型设置

  • 在命令行输入:/models
  • 回车选入标有Free的模型

2.付费模型设置

2.1 设置API key

  • 进入到opencode页面,输入 /connect
  • 搜索:Zhipu AI
  • 回车后,输入key

2.2 设置模型

  • 输入:/models

三、实战案例

1.让AI写入一首诗到本地的txt文件

  • 操作词:写一首古诗词,到当前目录的poem.txt文件

2.查询数据生成报告

  • 本案例使用MySQL8数据库

2.1 创建数据表和数据

sql 复制代码
-- 1. 创建用户表 users
DROP TABLE IF EXISTS users;
CREATE TABLE users (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '用户ID',
    email VARCHAR(128) NOT NULL COMMENT '用户邮箱(唯一)',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    status TINYINT NOT NULL DEFAULT 1 COMMENT '状态:1-正常,0-禁用,2-注销',
    PRIMARY KEY (id),
    UNIQUE KEY uk_email (email)  -- 邮箱唯一约束
)  COMMENT='用户表';

-- 2. 创建商品表 products
DROP TABLE IF EXISTS products;
CREATE TABLE products (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '商品ID',
    name VARCHAR(64) NOT NULL COMMENT '商品名称',
    price DECIMAL(10,2) NOT NULL COMMENT '商品价格(保留2位小数)',
    category VARCHAR(32) NOT NULL COMMENT '商品分类',
    PRIMARY KEY (id)
)  COMMENT='商品表';

-- 3. 创建订单表 orders(关联用户和商品,这里扩展order_items更合理,但按你的需求简化)
DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '订单ID',
    user_id BIGINT UNSIGNED NOT NULL COMMENT '关联用户ID',
    amount DECIMAL(10,2) NOT NULL COMMENT '订单总金额',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '下单时间',
    status TINYINT NOT NULL DEFAULT 0 COMMENT '订单状态:0-待支付,1-已支付,2-已取消,3-已完成',
    PRIMARY KEY (id),
    KEY idx_user_id (user_id),  -- 按用户ID查询订单的索引
    -- 外键关联用户表(可选,生产环境可根据业务决定是否启用外键)
    CONSTRAINT fk_order_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE RESTRICT
)  COMMENT='订单表';

-- 插入用户数据
INSERT INTO users (email, created_at, status) VALUES
('user1@example.com', '2026-01-01 10:00:00', 1),
('user2@example.com', '2026-01-02 11:00:00', 1),
('user3@example.com', '2026-01-03 14:00:00', 0);  -- 禁用状态的用户

-- 插入商品数据
INSERT INTO products (name, price, category) VALUES
('小米14手机', 3999.00, '手机'),
('华为MatePad', 2499.00, '平板'),
('苹果AirPods Pro', 1799.00, '耳机'),
('罗技机械键盘', 299.00, '外设'),
('金士顿U盘128G', 59.90, '存储设备');

-- 插入订单数据(关联用户ID)
INSERT INTO orders (user_id, amount, created_at, status) VALUES
(1, 3999.00, '2026-01-01 10:30:00', 3),  -- user1 已完成的手机订单
(1, 299.00, '2026-01-02 09:15:00', 1),   -- user1 已支付的键盘订单
(2, 1799.00, '2026-01-03 15:20:00', 0),  -- user2 待支付的耳机订单
(3, 2499.00, '2026-01-04 11:40:00', 2);  -- 禁用用户user3 已取消的平板订单

2.2 创建技能

  • 在这个目录下面创建一个 SKILL.md:%userprofile%.config\opencode\skill\sql-query-helper

  • 以下是SKILL.md的内容


    name: sql-query-helper
    description: |
    Help write and optimize SQL queries for the company database.
    Use when Claude needs to query the production database or help with SQL query optimization.

    SQL Query Helper

    Database Schema

    Our main tables:

    • users (id, email, created_at, status)
    • orders (id, user id, amount, created_at, status)
    • products (id, name, price, category)

    Query Guidelines

    1. Always use parameterized queries
    2. Include LIMIT clauses for safety
    3. Use indexes on WHERE clauses
    4. Test queries in staging first

    Common Queies

    Active Users

    ```sql
    SELECT id, email, last_login
    FROM users
    WHERE status = 'active'
    ORDER BY last_login DESC
    LIMIT 100;
    ```

    Revenue by Month

    ```sql
    SELECT
    DATE_TRUNC('month', created_at) as month,
    SUM(amount) as revenue
    FROM orders
    WHERE status = 'completed'
    GROUP BY month
    ORDER BY month DESC;
    ```

2.3 创建数据库连接信息

  • 文件名称:.env

    DB_HOST:"192.168.11.135"
    DB_PORT:3306
    DB_NAME:"opencode"
    DB_USER:"root"
    DB_PASSWORD:"King1234"

2.4 生成分析报告

  • 操作词:查询一下当前订单数据,并且分析经营情况,生成报告写入到本地,请使用本地conda工具的mysql_query环境进行查询数据
  • 最终生成报告效果

四、Skills资源

相关推荐
Hehuyi_In5 小时前
opencode + skills 漏洞复盘分析试用
ai·skills·opencode
hallo1281 天前
opencode执行/start-work出现错误:Model not found: opencode/glm-4.7-free.
opencode
夜魔0091 天前
OpenCode、OpenClaw、Claude Code skill tool失败问题解决
ripgrep·fetch·error·skill·rg·opencode
feasibility.2 天前
AI自动化的陷阱:2026年开年ai爆发潮下的冷思考
人工智能·经验分享·自动化·程序员创富·vibe coding·opencode·openclaw
谷新龙0014 天前
opencode入门
opencode
烁烁闪闪烁烁5 天前
【weelinking系列Claude教程】 04 - Claude Code 安装与配置
人工智能·chatgpt·ai编程·claude·cursor·claude code·opencode
鸿乃江边鸟6 天前
oh-my-opencode 实践--用户授权登陆Spring Boot项目
大模型·opencode·oh my opencode
五蕴非空6 天前
AI工具实践日记(二):在 OpenClaw 中调用 OpenCode 进行开发任务
ai助手·openspec·opencode·openclaw
骤跌7 天前
04-智能体协同工作架构设计
架构设计·opencode