summaryrefslogtreecommitdiffhomepage
path: root/packages/web/src/content/docs/zh-cn/providers.mdx
blob: 80dfe1e93d076972ef319d1358179792b882b372 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
---
title: 提供商
description: 在 OpenCode 中使用任意 LLM 提供商。
---

import config from "../../../../config.mjs"
export const console = config.console

OpenCode 使用 [AI SDK](https://ai-sdk.dev/) 和 [Models.dev](https://models.dev),支持 **75+ LLM 提供商**,同时也支持运行本地模型。

要添加提供商,你需要:

1. 使用 `/connect` 命令添加提供商的 API 密钥。
2. 在 OpenCode 配置中设置该提供商。

---

### 凭据

使用 `/connect` 命令添加提供商的 API 密钥后,凭据会存储在
`~/.local/share/opencode/auth.json` 中。

---

### 配置

你可以通过 OpenCode 配置中的 `provider` 部分来自定义提供商。

---

#### 自定义 Base URL

你可以通过设置 `baseURL` 选项来自定义任何提供商的 Base URL。这在使用代理服务或自定义端点时非常有用。

```json title="opencode.json" {6}
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "anthropic": {
      "options": {
        "baseURL": "https://api.anthropic.com/v1"
      }
    }
  }
}
```

---

## OpenCode Zen

OpenCode Zen 是由 OpenCode 团队提供的模型列表,这些模型已经过测试和验证,能够与 OpenCode 良好配合使用。[了解更多](/docs/zen)。

:::tip
如果你是新用户,我们建议从 OpenCode Zen 开始。
:::

1. 在 TUI 中执行 `/connect` 命令,选择 opencode,然后前往 [opencode.ai/auth](https://opencode.ai/auth)。

   ```txt
   /connect
   ```

2. 登录后添加账单信息,然后复制你的 API 密钥。

3. 粘贴你的 API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 在 TUI 中执行 `/models` 查看我们推荐的模型列表。

   ```txt
   /models
   ```

它的使用方式与 OpenCode 中的其他提供商完全相同,且完全可选。

---

## 目录

下面我们来详细了解一些提供商。如果你想将某个提供商添加到列表中,欢迎提交 PR。

:::note
没有看到你想要的提供商?欢迎提交 PR。
:::

---

### 302.AI

1. 前往 [302.AI 控制台](https://302.ai/),创建账户并生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **302.AI**。

   ```txt
   /connect
   ```

3. 输入你的 302.AI API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型。

   ```txt
   /models
   ```

---

### Amazon Bedrock

要在 OpenCode 中使用 Amazon Bedrock:

1. 前往 Amazon Bedrock 控制台中的**模型目录**,申请访问你想要使用的模型。

   :::tip
   你需要先在 Amazon Bedrock 中获得对目标模型的访问权限。
   :::

2. 使用以下方法之一**配置身份验证**:

   ***

   #### 环境变量(快速上手)

   运行 opencode 时设置以下环境变量之一:

   ```bash
   # Option 1: Using AWS access keys
   AWS_ACCESS_KEY_ID=XXX AWS_SECRET_ACCESS_KEY=YYY opencode

   # Option 2: Using named AWS profile
   AWS_PROFILE=my-profile opencode

   # Option 3: Using Bedrock bearer token
   AWS_BEARER_TOKEN_BEDROCK=XXX opencode
   ```

   或者将它们添加到你的 bash 配置文件中:

   ```bash title="~/.bash_profile"
   export AWS_PROFILE=my-dev-profile
   export AWS_REGION=us-east-1
   ```

   ***

   #### 配置文件(推荐)

   如需项目级别或持久化的配置,请使用 `opencode.json`:

   ```json title="opencode.json"
   {
     "$schema": "https://opencode.ai/config.json",
     "provider": {
       "amazon-bedrock": {
         "options": {
           "region": "us-east-1",
           "profile": "my-aws-profile"
         }
       }
     }
   }
   ```

   **可用选项:**
   - `region` - AWS 区域(例如 `us-east-1`、`eu-west-1`)
   - `profile` - `~/.aws/credentials` 中的 AWS 命名配置文件
   - `endpoint` - VPC 端点的自定义端点 URL(通用 `baseURL` 选项的别名)

   :::tip
   配置文件中的选项优先级高于环境变量。
   :::

   ***

   #### 进阶:VPC 端点

   如果你使用 Bedrock 的 VPC 端点:

   ```json title="opencode.json"
   {
     "$schema": "https://opencode.ai/config.json",
     "provider": {
       "amazon-bedrock": {
         "options": {
           "region": "us-east-1",
           "profile": "production",
           "endpoint": "https://bedrock-runtime.us-east-1.vpce-xxxxx.amazonaws.com"
         }
       }
     }
   }
   ```

   :::note
   `endpoint` 选项是通用 `baseURL` 选项的别名,使用了 AWS 特有的术语。如果同时指定了 `endpoint` 和 `baseURL`,则 `endpoint` 优先。
   :::

   ***

   #### 认证方式
   - **`AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY`**:在 AWS 控制台中创建 IAM 用户并生成访问密钥
   - **`AWS_PROFILE`**:使用 `~/.aws/credentials` 中的命名配置文件。需要先通过 `aws configure --profile my-profile` 或 `aws sso login` 进行配置
   - **`AWS_BEARER_TOKEN_BEDROCK`**:从 Amazon Bedrock 控制台生成长期 API 密钥
   - **`AWS_WEB_IDENTITY_TOKEN_FILE` / `AWS_ROLE_ARN`**:适用于 EKS IRSA(服务账户的 IAM 角色)或其他支持 OIDC 联合的 Kubernetes 环境。使用服务账户注解时,Kubernetes 会自动注入这些环境变量。

   ***

   #### 认证优先级

   Amazon Bedrock 使用以下认证优先级:
   1. **Bearer Token** - `AWS_BEARER_TOKEN_BEDROCK` 环境变量或通过 `/connect` 命令获取的 Token
   2. **AWS 凭证链** - 配置文件、访问密钥、共享凭证、IAM 角色、Web Identity Token(EKS IRSA)、实例元数据

   :::note
   当设置了 Bearer Token(通过 `/connect` 或 `AWS_BEARER_TOKEN_BEDROCK`)时,它的优先级高于所有 AWS 凭证方式,包括已配置的配置文件。
   :::

3. 执行 `/models` 命令选择你想要的模型。

   ```txt
   /models
   ```

:::note
对于自定义推理配置文件,请在 key 中使用模型名称和提供商名称,并将 `id` 属性设置为 ARN。这可以确保正确的缓存行为:

```json title="opencode.json"
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "amazon-bedrock": {
      // ...
      "models": {
        "anthropic-claude-sonnet-4.5": {
          "id": "arn:aws:bedrock:us-east-1:xxx:application-inference-profile/yyy"
        }
      }
    }
  }
}
```

:::

---

### Anthropic

1. 注册完成后,执行 `/connect` 命令并选择 Anthropic。

   ```txt
   /connect
   ```

2. 你可以选择 **Claude Pro/Max** 选项,浏览器会自动打开并要求你进行身份验证。

   ```txt
   ┌ Select auth method
   │
   │ Claude Pro/Max
   │ Create an API Key
   │ Manually enter API Key
   └
   ```

3. 现在使用 `/models` 命令即可看到所有 Anthropic 模型。

   ```txt
   /models
   ```

:::info
在 OpenCode 中使用 Claude Pro/Max 订阅不是 [Anthropic](https://anthropic.com) 官方支持的用法。
:::

##### 使用 API 密钥

如果你没有 Pro/Max 订阅,也可以选择 **Create an API Key**。浏览器会自动打开并要求你登录 Anthropic,然后会提供一个代码供你粘贴到终端中。

如果你已经有 API 密钥,可以选择 **Manually enter API Key** 并将其粘贴到终端中。

---

### Atomic Chat

你可以通过 [Atomic Chat](https://atomic.chat) 配置 opencode 以使用本地模型。Atomic Chat 是一款桌面应用程序,它在 OpenAI 兼容的 API 服务器后面运行本地 LLM(默认端点 `http://127.0.0.1:1337/v1`)。

```json title="opencode.json" "atomic-chat" {5, 6, 8, 10-14}
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "atomic-chat": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Atomic Chat (local)",
      "options": {
        "baseURL": "http://127.0.0.1:1337/v1"
      },
      "models": {
        "<your-model-id>": {
          "name": "<your-model-name>"
        }
      }
    }
  }
}
```

在此示例中:

- `atomic-chat` 是自定义的提供商 ID。可以是任何你想要的字符串。
- `npm` 指定此提供商使用的包。这里使用 `@ai-sdk/openai-compatible` 来连接任何 OpenAI 兼容的 API。
- `name` 是提供商在界面中显示的名称。
- `options.baseURL` 是本地服务器的端点。根据你的 Atomic Chat 设置修改主机和端口。
- `models` 是模型 ID 到其显示名称的映射。每个 ID 必须与 `GET /v1/models` 返回的 `id` 匹配——运行 `curl http://127.0.0.1:1337/v1/models` 可列出 Atomic Chat 当前已加载的 ID。

:::tip
如果工具调用工作不佳,请选择一个对 tool calling 支持较好的已加载模型(例如 Qwen-Coder 或 DeepSeek-Coder 的变体)。
:::

---

### Azure OpenAI

:::note
如果遇到 "I'm sorry, but I cannot assist with that request" 错误,请尝试将 Azure 资源中的内容过滤器从 **DefaultV2** 更改为 **Default**。
:::

1. 前往 [Azure 门户](https://portal.azure.com/)并创建 **Azure OpenAI** 资源。你需要:
   - **资源名称**:这会成为你的 API 端点的一部分(`https://RESOURCE_NAME.openai.azure.com/`)
   - **API 密钥**:资源中的 `KEY 1` 或 `KEY 2`

2. 前往 [Azure AI Foundry](https://ai.azure.com/) 并部署一个模型。

   :::note
   部署名称必须与模型名称一致,OpenCode 才能正常工作。
   :::

3. 执行 `/connect` 命令并搜索 **Azure**。

   ```txt
   /connect
   ```

4. 输入你的 API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

5. 将资源名称设置为环境变量:

   ```bash
   AZURE_RESOURCE_NAME=XXX opencode
   ```

   或者添加到你的 bash 配置文件中:

   ```bash title="~/.bash_profile"
   export AZURE_RESOURCE_NAME=XXX
   ```

6. 执行 `/models` 命令选择你已部署的模型。

   ```txt
   /models
   ```

---

### Azure Cognitive Services

1. 前往 [Azure 门户](https://portal.azure.com/)并创建 **Azure OpenAI** 资源。你需要:
   - **资源名称**:这会成为你的 API 端点的一部分(`https://AZURE_COGNITIVE_SERVICES_RESOURCE_NAME.cognitiveservices.azure.com/`)
   - **API 密钥**:资源中的 `KEY 1` 或 `KEY 2`

2. 前往 [Azure AI Foundry](https://ai.azure.com/) 并部署一个模型。

   :::note
   部署名称必须与模型名称一致,OpenCode 才能正常工作。
   :::

3. 执行 `/connect` 命令并搜索 **Azure Cognitive Services**。

   ```txt
   /connect
   ```

4. 输入你的 API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

5. 将资源名称设置为环境变量:

   ```bash
   AZURE_COGNITIVE_SERVICES_RESOURCE_NAME=XXX opencode
   ```

   或者添加到你的 bash 配置文件中:

   ```bash title="~/.bash_profile"
   export AZURE_COGNITIVE_SERVICES_RESOURCE_NAME=XXX
   ```

6. 执行 `/models` 命令选择你已部署的模型。

   ```txt
   /models
   ```

---

### Baseten

1. 前往 [Baseten](https://app.baseten.co/),创建账户并生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **Baseten**。

   ```txt
   /connect
   ```

3. 输入你的 Baseten API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型。

   ```txt
   /models
   ```

---

### Cerebras

1. 前往 [Cerebras 控制台](https://inference.cerebras.ai/),创建账户并生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **Cerebras**。

   ```txt
   /connect
   ```

3. 输入你的 Cerebras API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _Qwen 3 Coder 480B_。

   ```txt
   /models
   ```

---

### Cloudflare AI Gateway

Cloudflare AI Gateway 允许你通过统一端点访问来自 OpenAI、Anthropic、Workers AI 等提供商的模型。通过 [Unified Billing](https://developers.cloudflare.com/ai-gateway/features/unified-billing/),你无需为每个提供商单独准备 API 密钥。

1. 前往 [Cloudflare 仪表盘](https://dash.cloudflare.com/),导航到 **AI** > **AI Gateway**,创建一个新的网关。

2. 将你的 Account ID 和 Gateway ID 设置为环境变量。

   ```bash title="~/.bash_profile"
   export CLOUDFLARE_ACCOUNT_ID=your-32-character-account-id
   export CLOUDFLARE_GATEWAY_ID=your-gateway-id
   ```

3. 执行 `/connect` 命令并搜索 **Cloudflare AI Gateway**。

   ```txt
   /connect
   ```

4. 输入你的 Cloudflare API Token。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

   或者将其设置为环境变量。

   ```bash title="~/.bash_profile"
   export CLOUDFLARE_API_TOKEN=your-api-token
   ```

5. 执行 `/models` 命令选择模型。

   ```txt
   /models
   ```

   你也可以通过 OpenCode 配置添加模型。

   ```json title="opencode.json"
   {
     "$schema": "https://opencode.ai/config.json",
     "provider": {
       "cloudflare-ai-gateway": {
         "models": {
           "openai/gpt-4o": {},
           "anthropic/claude-sonnet-4": {}
         }
       }
     }
   }
   ```

---

### Cortecs

1. 前往 [Cortecs 控制台](https://cortecs.ai/),创建账户并生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **Cortecs**。

   ```txt
   /connect
   ```

3. 输入你的 Cortecs API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _Kimi K2 Instruct_。

   ```txt
   /models
   ```

---

### DeepSeek

1. 前往 [DeepSeek 控制台](https://platform.deepseek.com/),创建账户并点击 **Create new API key**。

2. 执行 `/connect` 命令并搜索 **DeepSeek**。

   ```txt
   /connect
   ```

3. 输入你的 DeepSeek API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择 DeepSeek 模型,例如 _DeepSeek V4 Pro_。

   ```txt
   /models
   ```

---

### Deep Infra

1. 前往 [Deep Infra 仪表盘](https://deepinfra.com/dash),创建账户并生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **Deep Infra**。

   ```txt
   /connect
   ```

3. 输入你的 Deep Infra API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型。

   ```txt
   /models
   ```

---

### Firmware

1. 前往 [Firmware 仪表盘](https://app.firmware.ai/signup),创建账户并生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **Firmware**。

   ```txt
   /connect
   ```

3. 输入你的 Firmware API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型。

   ```txt
   /models
   ```

---

### Fireworks AI

1. 前往 [Fireworks AI 控制台](https://app.fireworks.ai/),创建账户并点击 **Create API Key**。

2. 执行 `/connect` 命令并搜索 **Fireworks AI**。

   ```txt
   /connect
   ```

3. 输入你的 Fireworks AI API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _Kimi K2 Instruct_。

   ```txt
   /models
   ```

---

### GitLab Duo

GitLab Duo 通过 GitLab 的 Anthropic 代理提供具有原生工具调用能力的 AI 驱动的代理聊天。

1. 执行 `/connect` 命令并选择 GitLab。

   ```txt
   /connect
   ```

2. 选择你的身份验证方式:

   ```txt
   ┌ Select auth method
   │
   │ OAuth (Recommended)
   │ Personal Access Token
   └
   ```

   #### 使用 OAuth(推荐)

   选择 **OAuth**,浏览器会自动打开进行授权。

   #### 使用个人访问令牌
   1. 前往 [GitLab 用户设置 > Access Tokens](https://gitlab.com/-/user_settings/personal_access_tokens)
   2. 点击 **Add new token**
   3. 名称填写 `OpenCode`,范围选择 `api`
   4. 复制令牌(以 `glpat-` 开头)
   5. 在终端中输入该令牌

3. 执行 `/models` 命令查看可用模型。

   ```txt
   /models
   ```

   提供三个基于 Claude 的模型:
   - **duo-chat-haiku-4-5**(默认)- 快速响应,适合简单任务
   - **duo-chat-sonnet-4-5** - 性能均衡,适合大多数工作流
   - **duo-chat-opus-4-5** - 最强大,适合复杂分析

:::note
你也可以通过指定 `GITLAB_TOKEN` 环境变量来避免将令牌存储在 OpenCode 的认证存储中。
:::

##### 自托管 GitLab

:::note[合规说明]
OpenCode 会使用一个小模型来执行部分 AI 任务,例如生成会话标题。默认情况下使用由 Zen 托管的 gpt-5-nano。如果你需要让 OpenCode 仅使用你自己的 GitLab 托管实例,请在 `opencode.json` 文件中添加以下内容。同时建议禁用会话共享。

```json
{
  "$schema": "https://opencode.ai/config.json",
  "small_model": "gitlab/duo-chat-haiku-4-5",
  "share": "disabled"
}
```

:::

对于自托管 GitLab 实例:

```bash
export GITLAB_INSTANCE_URL=https://gitlab.company.com
export GITLAB_TOKEN=glpat-...
```

如果你的实例运行了自定义 AI Gateway:

```bash
GITLAB_AI_GATEWAY_URL=https://ai-gateway.company.com
```

或者添加到你的 bash 配置文件中:

```bash title="~/.bash_profile"
export GITLAB_INSTANCE_URL=https://gitlab.company.com
export GITLAB_AI_GATEWAY_URL=https://ai-gateway.company.com
export GITLAB_TOKEN=glpat-...
```

:::note
你的 GitLab 管理员必须启用以下功能:

1. 为用户、群组或实例启用 [Duo Agent Platform](https://docs.gitlab.com/user/duo_agent_platform/turn_on_off/)
2. 功能标志(通过 Rails 控制台):
   - `agent_platform_claude_code`
   - `third_party_agents_enabled`
     :::

##### 自托管实例的 OAuth

要在自托管实例上使用 OAuth,你需要创建一个新应用(设置 → 应用),回调 URL 设置为 `http://127.0.0.1:8080/callback`,并选择以下范围:

- api(代表你访问 API)
- read_user(读取你的个人信息)
- read_repository(允许对仓库进行只读访问)

然后将应用 ID 导出为环境变量:

```bash
export GITLAB_OAUTH_CLIENT_ID=your_application_id_here
```

更多文档请参阅 [opencode-gitlab-auth](https://www.npmjs.com/package/opencode-gitlab-auth) 主页。

##### 配置

通过 `opencode.json` 进行自定义配置:

```json title="opencode.json"
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "gitlab": {
      "options": {
        "instanceUrl": "https://gitlab.com"
      }
    }
  }
}
```

##### GitLab API 工具(可选,但强烈推荐)

要访问 GitLab 工具(合并请求、Issue、流水线、CI/CD 等):

```json title="opencode.json"
{
  "$schema": "https://opencode.ai/config.json",
  "plugin": ["opencode-gitlab-plugin"]
}
```

该插件提供全面的 GitLab 仓库管理功能,包括 MR 审查、Issue 跟踪、流水线监控等。

---

### GitHub Copilot

要在 OpenCode 中使用你的 GitHub Copilot 订阅:

:::note
部分模型可能需要 [Pro+ 订阅](https://github.com/features/copilot/plans)才能使用。
:::

1. 执行 `/connect` 命令并搜索 GitHub Copilot。

   ```txt
   /connect
   ```

2. 前往 [github.com/login/device](https://github.com/login/device) 并输入验证码。

   ```txt
   ┌ Login with GitHub Copilot
   │
   │ https://github.com/login/device
   │
   │ Enter code: 8F43-6FCF
   │
   └ Waiting for authorization...
   ```

3. 现在执行 `/models` 命令选择你想要的模型。

   ```txt
   /models
   ```

---

### Google Vertex AI

要在 OpenCode 中使用 Google Vertex AI:

1. 前往 Google Cloud Console 中的**模型花园**,查看你所在区域可用的模型。

   :::note
   你需要一个启用了 Vertex AI API 的 Google Cloud 项目。
   :::

2. 设置所需的环境变量:
   - `GOOGLE_CLOUD_PROJECT`:你的 Google Cloud 项目 ID
   - `VERTEX_LOCATION`(可选):Vertex AI 的区域(默认为 `global`)
   - 身份验证(选择其一):
     - `GOOGLE_APPLICATION_CREDENTIALS`:服务账户 JSON 密钥文件的路径
     - 使用 gcloud CLI 进行身份验证:`gcloud auth application-default login`

   在运行 opencode 时设置:

   ```bash
   GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json GOOGLE_CLOUD_PROJECT=your-project-id opencode
   ```

   或者添加到你的 bash 配置文件中:

   ```bash title="~/.bash_profile"
   export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
   export GOOGLE_CLOUD_PROJECT=your-project-id
   export VERTEX_LOCATION=global
   ```

:::tip
`global` 区域可以提高可用性并减少错误,且不会产生额外费用。如果有数据驻留需求,请使用区域端点(例如 `us-central1`)。[了解更多](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-partner-models#regional_and_global_endpoints)
:::

3. 执行 `/models` 命令选择你想要的模型。

   ```txt
   /models
   ```

---

### Groq

1. 前往 [Groq 控制台](https://console.groq.com/),点击 **Create API Key** 并复制密钥。

2. 执行 `/connect` 命令并搜索 Groq。

   ```txt
   /connect
   ```

3. 输入该提供商的 API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择你想要的模型。

   ```txt
   /models
   ```

---

### Hugging Face

[Hugging Face Inference Providers](https://huggingface.co/docs/inference-providers) 提供对由 17+ 提供商支持的开放模型的访问。

1. 前往 [Hugging Face 设置](https://huggingface.co/settings/tokens/new?ownUserPermissions=inference.serverless.write&tokenType=fineGrained),创建一个具有调用 Inference Providers 权限的令牌。

2. 执行 `/connect` 命令并搜索 **Hugging Face**。

   ```txt
   /connect
   ```

3. 输入你的 Hugging Face 令牌。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _Kimi-K2-Instruct_ 或 _GLM-4.6_。

   ```txt
   /models
   ```

---

### Helicone

[Helicone](https://helicone.ai) 是一个 LLM 可观测性平台,为你的 AI 应用提供日志记录、监控和分析功能。Helicone AI Gateway 会根据模型自动将请求路由到对应的提供商。

1. 前往 [Helicone](https://helicone.ai),创建账户并在仪表盘中生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **Helicone**。

   ```txt
   /connect
   ```

3. 输入你的 Helicone API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型。

   ```txt
   /models
   ```

如需了解更多提供商以及缓存、速率限制等高级功能,请查阅 [Helicone 文档](https://docs.helicone.ai)。

#### 可选配置

如果 Helicone 的某些功能或模型未通过 OpenCode 自动配置,你随时可以手动配置。

[Helicone 模型目录](https://helicone.ai/models)中可以找到你需要添加的模型 ID。

```jsonc title="~/.config/opencode/opencode.jsonc"
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "helicone": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Helicone",
      "options": {
        "baseURL": "https://ai-gateway.helicone.ai",
      },
      "models": {
        "gpt-4o": {
          // Model ID (from Helicone's model directory page)
          "name": "GPT-4o", // Your own custom name for the model
        },
        "claude-sonnet-4-20250514": {
          "name": "Claude Sonnet 4",
        },
      },
    },
  },
}
```

#### 自定义请求头

Helicone 支持用于缓存、用户跟踪和会话管理等功能的自定义请求头。使用 `options.headers` 将它们添加到提供商配置中:

```jsonc title="~/.config/opencode/opencode.jsonc"
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "helicone": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Helicone",
      "options": {
        "baseURL": "https://ai-gateway.helicone.ai",
        "headers": {
          "Helicone-Cache-Enabled": "true",
          "Helicone-User-Id": "opencode",
        },
      },
    },
  },
}
```

##### 会话跟踪

Helicone 的 [Sessions](https://docs.helicone.ai/features/sessions) 功能允许你将相关的 LLM 请求归为一组。使用 [opencode-helicone-session](https://github.com/H2Shami/opencode-helicone-session) 插件可以自动将每个 OpenCode 对话记录为 Helicone 中的一个会话。

```bash
npm install -g opencode-helicone-session
```

将其添加到配置中。

```json title="opencode.json"
{
  "plugin": ["opencode-helicone-session"]
}
```

该插件会在你的请求中注入 `Helicone-Session-Id` 和 `Helicone-Session-Name` 请求头。在 Helicone 的 Sessions 页面中,你可以看到每个 OpenCode 对话都作为独立的会话列出。

##### 常用 Helicone 请求头

| 请求头                     | 描述                                                   |
| -------------------------- | ------------------------------------------------------ |
| `Helicone-Cache-Enabled`   | 启用响应缓存(`true`/`false`)                         |
| `Helicone-User-Id`         | 按用户跟踪指标                                         |
| `Helicone-Property-[Name]` | 添加自定义属性(例如 `Helicone-Property-Environment`) |
| `Helicone-Prompt-Id`       | 将请求与提示词版本关联                                 |

有关所有可用请求头,请参阅 [Helicone Header Directory](https://docs.helicone.ai/helicone-headers/header-directory)。

---

### llama.cpp

你可以通过 [llama.cpp](https://github.com/ggml-org/llama.cpp) 的 llama-server 工具配置 OpenCode 使用本地模型。

```json title="opencode.json" "llama.cpp" {5, 6, 8, 10-15}
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "llama.cpp": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "llama-server (local)",
      "options": {
        "baseURL": "http://127.0.0.1:8080/v1"
      },
      "models": {
        "qwen3-coder:a3b": {
          "name": "Qwen3-Coder: a3b-30b (local)",
          "limit": {
            "context": 128000,
            "output": 65536
          }
        }
      }
    }
  }
}
```

在这个示例中:

- `llama.cpp` 是自定义的提供商 ID,可以是任意字符串。
- `npm` 指定该提供商使用的包。这里使用 `@ai-sdk/openai-compatible` 来兼容任何 OpenAI 兼容的 API。
- `name` 是该提供商在 UI 中显示的名称。
- `options.baseURL` 是本地服务器的端点地址。
- `models` 是模型 ID 到其配置的映射。模型名称会显示在模型选择列表中。

---

### IO.NET

IO.NET 提供 17 个针对不同用例优化的模型:

1. 前往 [IO.NET 控制台](https://ai.io.net/),创建账户并生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **IO.NET**。

   ```txt
   /connect
   ```

3. 输入你的 IO.NET API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型。

   ```txt
   /models
   ```

---

### LM Studio

你可以通过 LM Studio 配置 OpenCode 使用本地模型。

```json title="opencode.json" "lmstudio" {5, 6, 8, 10-14}
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "lmstudio": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "LM Studio (local)",
      "options": {
        "baseURL": "http://127.0.0.1:1234/v1"
      },
      "models": {
        "google/gemma-3n-e4b": {
          "name": "Gemma 3n-e4b (local)"
        }
      }
    }
  }
}
```

在这个示例中:

- `lmstudio` 是自定义的提供商 ID,可以是任意字符串。
- `npm` 指定该提供商使用的包。这里使用 `@ai-sdk/openai-compatible` 来兼容任何 OpenAI 兼容的 API。
- `name` 是该提供商在 UI 中显示的名称。
- `options.baseURL` 是本地服务器的端点地址。
- `models` 是模型 ID 到其配置的映射。模型名称会显示在模型选择列表中。

---

### Moonshot AI

要使用 Moonshot AI 的 Kimi K2:

1. 前往 [Moonshot AI 控制台](https://platform.moonshot.ai/console),创建账户并点击 **Create API key**。

2. 执行 `/connect` 命令并搜索 **Moonshot AI**。

   ```txt
   /connect
   ```

3. 输入你的 Moonshot API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择 _Kimi K2_。

   ```txt
   /models
   ```

---

### MiniMax

1. 前往 [MiniMax API 控制台](https://platform.minimax.io/login),创建账户并生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **MiniMax**。

   ```txt
   /connect
   ```

3. 输入你的 MiniMax API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _M2.1_。

   ```txt
   /models
   ```

---

### Nebius Token Factory

1. 前往 [Nebius Token Factory 控制台](https://tokenfactory.nebius.com/),创建账户并点击 **Add Key**。

2. 执行 `/connect` 命令并搜索 **Nebius Token Factory**。

   ```txt
   /connect
   ```

3. 输入你的 Nebius Token Factory API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _Kimi K2 Instruct_。

   ```txt
   /models
   ```

---

### Ollama

你可以通过 Ollama 配置 OpenCode 使用本地模型。

:::tip
Ollama 可以自动为 OpenCode 进行配置。详见 [Ollama 集成文档](https://docs.ollama.com/integrations/opencode)。
:::

```json title="opencode.json" "ollama" {5, 6, 8, 10-14}
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Ollama (local)",
      "options": {
        "baseURL": "http://localhost:11434/v1"
      },
      "models": {
        "llama2": {
          "name": "Llama 2"
        }
      }
    }
  }
}
```

在这个示例中:

- `ollama` 是自定义的提供商 ID,可以是任意字符串。
- `npm` 指定该提供商使用的包。这里使用 `@ai-sdk/openai-compatible` 来兼容任何 OpenAI 兼容的 API。
- `name` 是该提供商在 UI 中显示的名称。
- `options.baseURL` 是本地服务器的端点地址。
- `models` 是模型 ID 到其配置的映射。模型名称会显示在模型选择列表中。

:::tip
如果工具调用不工作,请尝试增大 Ollama 中的 `num_ctx` 值。建议从 16k - 32k 左右开始。
:::

---

### Ollama Cloud

要在 OpenCode 中使用 Ollama Cloud:

1. 前往 [https://ollama.com/](https://ollama.com/) 登录或创建账户。

2. 导航到 **Settings** > **Keys**,点击 **Add API Key** 生成新的 API 密钥。

3. 复制 API 密钥以便在 OpenCode 中使用。

4. 执行 `/connect` 命令并搜索 **Ollama Cloud**。

   ```txt
   /connect
   ```

5. 输入你的 Ollama Cloud API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

6. **重要**:在 OpenCode 中使用云端模型之前,必须先将模型信息拉取到本地:

   ```bash
   ollama pull gpt-oss:20b-cloud
   ```

7. 执行 `/models` 命令选择你的 Ollama Cloud 模型。

   ```txt
   /models
   ```

---

### OpenAI

我们建议注册 [ChatGPT Plus 或 Pro](https://chatgpt.com/pricing)。

1. 注册完成后,执行 `/connect` 命令并选择 OpenAI。

   ```txt
   /connect
   ```

2. 你可以选择 **ChatGPT Plus/Pro** 选项,浏览器会自动打开并要求你进行身份验证。

   ```txt
   ┌ Select auth method
   │
   │ ChatGPT Plus/Pro
   │ Manually enter API Key
   └
   ```

3. 现在使用 `/models` 命令即可看到所有 OpenAI 模型。

   ```txt
   /models
   ```

##### 使用 API 密钥

如果你已经有 API 密钥,可以选择 **Manually enter API Key** 并将其粘贴到终端中。

---

### OpenCode Zen

OpenCode Zen 是由 OpenCode 团队提供的经过测试和验证的模型列表。[了解更多](/docs/zen)。

1. 登录 **<a href={console}>OpenCode Zen</a>** 并点击 **Create API Key**。

2. 执行 `/connect` 命令并搜索 **OpenCode Zen**。

   ```txt
   /connect
   ```

3. 输入你的 OpenCode API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _Qwen 3 Coder 480B_。

   ```txt
   /models
   ```

---

### OpenRouter

1. 前往 [OpenRouter 仪表盘](https://openrouter.ai/settings/keys),点击 **Create API Key** 并复制密钥。

2. 执行 `/connect` 命令并搜索 OpenRouter。

   ```txt
   /connect
   ```

3. 输入该提供商的 API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 默认已预加载了许多 OpenRouter 模型,执行 `/models` 命令选择你想要的模型。

   ```txt
   /models
   ```

   你也可以通过 OpenCode 配置添加更多模型。

   ```json title="opencode.json" {6}
   {
     "$schema": "https://opencode.ai/config.json",
     "provider": {
       "openrouter": {
         "models": {
           "somecoolnewmodel": {}
         }
       }
     }
   }
   ```

5. 你还可以通过 OpenCode 配置自定义模型。以下是指定提供商的示例:

   ```json title="opencode.json"
   {
     "$schema": "https://opencode.ai/config.json",
     "provider": {
       "openrouter": {
         "models": {
           "moonshotai/kimi-k2": {
             "options": {
               "provider": {
                 "order": ["baseten"],
                 "allow_fallbacks": false
               }
             }
           }
         }
       }
     }
   }
   ```

---

### SAP AI Core

SAP AI Core 通过统一平台提供对来自 OpenAI、Anthropic、Google、Amazon、Meta、Mistral 和 AI21 的 40+ 模型的访问。

1. 前往 [SAP BTP Cockpit](https://account.hana.ondemand.com/),导航到你的 SAP AI Core 服务实例,并创建服务密钥。

   :::tip
   服务密钥是一个包含 `clientid`、`clientsecret`、`url` 和 `serviceurls.AI_API_URL` 的 JSON 对象。你可以在 BTP Cockpit 的 **Services** > **Instances and Subscriptions** 下找到你的 AI Core 实例。
   :::

2. 执行 `/connect` 命令并搜索 **SAP AI Core**。

   ```txt
   /connect
   ```

3. 输入你的服务密钥 JSON。

   ```txt
   ┌ Service key
   │
   │
   └ enter
   ```

   或者设置 `AICORE_SERVICE_KEY` 环境变量:

   ```bash
   AICORE_SERVICE_KEY='{"clientid":"...","clientsecret":"...","url":"...","serviceurls":{"AI_API_URL":"..."}}' opencode
   ```

   或者添加到你的 bash 配置文件中:

   ```bash title="~/.bash_profile"
   export AICORE_SERVICE_KEY='{"clientid":"...","clientsecret":"...","url":"...","serviceurls":{"AI_API_URL":"..."}}'
   ```

4. 可选:设置部署 ID 和资源组:

   ```bash
   AICORE_DEPLOYMENT_ID=your-deployment-id AICORE_RESOURCE_GROUP=your-resource-group opencode
   ```

   :::note
   这些设置是可选的,应根据你的 SAP AI Core 配置进行设置。
   :::

5. 执行 `/models` 命令从 40+ 个可用模型中进行选择。

   ```txt
   /models
   ```

---

### STACKIT

STACKIT AI Model Serving 提供完全托管的主权托管环境,专注于 Llama、Mistral 和 Qwen 等大语言模型,在欧洲基础设施上实现最大程度的数据主权。

1. 前往 [STACKIT Portal](https://portal.stackit.cloud),导航到 **AI Model Serving**,为你的项目创建认证令牌。

   :::tip
   你需要先拥有 STACKIT 客户账户、用户账户和项目,才能创建认证令牌。
   :::

2. 执行 `/connect` 命令并搜索 **STACKIT**。

   ```txt
   /connect
   ```

3. 输入你的 STACKIT AI Model Serving 认证令牌。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _Qwen3-VL 235B_ 或 _Llama 3.3 70B_。

   ```txt
   /models
   ```

---

### OVHcloud AI Endpoints

1. 前往 [OVHcloud 管理面板](https://ovh.com/manager)。导航到 `Public Cloud` 部分,`AI & Machine Learning` > `AI Endpoints`,在 `API Keys` 标签页中点击 **Create a new API key**。

2. 执行 `/connect` 命令并搜索 **OVHcloud AI Endpoints**。

   ```txt
   /connect
   ```

3. 输入你的 OVHcloud AI Endpoints API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _gpt-oss-120b_。

   ```txt
   /models
   ```

---

### Scaleway

要在 OpenCode 中使用 [Scaleway Generative APIs](https://www.scaleway.com/en/docs/generative-apis/):

1. 前往 [Scaleway Console IAM 设置](https://console.scaleway.com/iam/api-keys)生成新的 API 密钥。

2. 执行 `/connect` 命令并搜索 **Scaleway**。

   ```txt
   /connect
   ```

3. 输入你的 Scaleway API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _devstral-2-123b-instruct-2512_ 或 _gpt-oss-120b_。

   ```txt
   /models
   ```

---

### Together AI

1. 前往 [Together AI 控制台](https://api.together.ai),创建账户并点击 **Add Key**。

2. 执行 `/connect` 命令并搜索 **Together AI**。

   ```txt
   /connect
   ```

3. 输入你的 Together AI API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _Kimi K2 Instruct_。

   ```txt
   /models
   ```

---

### Venice AI

1. 前往 [Venice AI 控制台](https://venice.ai),创建账户并生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **Venice AI**。

   ```txt
   /connect
   ```

3. 输入你的 Venice AI API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _Llama 3.3 70B_。

   ```txt
   /models
   ```

---

### Vercel AI Gateway

Vercel AI Gateway 允许你通过统一端点访问来自 OpenAI、Anthropic、Google、xAI 等提供商的模型。模型按原价提供,不额外加价。

1. 前往 [Vercel 仪表盘](https://vercel.com/),导航到 **AI Gateway** 标签页,点击 **API keys** 创建新的 API 密钥。

2. 执行 `/connect` 命令并搜索 **Vercel AI Gateway**。

   ```txt
   /connect
   ```

3. 输入你的 Vercel AI Gateway API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型。

   ```txt
   /models
   ```

你也可以通过 OpenCode 配置自定义模型。以下是指定提供商路由顺序的示例。

```json title="opencode.json"
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "vercel": {
      "models": {
        "anthropic/claude-sonnet-4": {
          "options": {
            "order": ["anthropic", "vertex"]
          }
        }
      }
    }
  }
}
```

一些常用的路由选项:

| 选项                | 描述                             |
| ------------------- | -------------------------------- |
| `order`             | 提供商尝试顺序                   |
| `only`              | 限制为特定提供商                 |
| `zeroDataRetention` | 仅使用具有零数据留存策略的提供商 |

---

### xAI

1. 前往 [xAI 控制台](https://console.x.ai/),创建账户并生成 API 密钥。

2. 执行 `/connect` 命令并搜索 **xAI**。

   ```txt
   /connect
   ```

3. 输入你的 xAI API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _Grok Beta_。

   ```txt
   /models
   ```

---

### Z.AI

1. 前往 [Z.AI API 控制台](https://z.ai/manage-apikey/apikey-list),创建账户并点击 **Create a new API key**。

2. 执行 `/connect` 命令并搜索 **Z.AI**。

   ```txt
   /connect
   ```

   如果你订阅了 **GLM Coding Plan**,请选择 **Z.AI Coding Plan**。

3. 输入你的 Z.AI API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 执行 `/models` 命令选择模型,例如 _GLM-4.7_。

   ```txt
   /models
   ```

---

### ZenMux

1. 前往 [ZenMux 仪表盘](https://zenmux.ai/settings/keys),点击 **Create API Key** 并复制密钥。

2. 执行 `/connect` 命令并搜索 ZenMux。

   ```txt
   /connect
   ```

3. 输入该提供商的 API 密钥。

   ```txt
   ┌ API key
   │
   │
   └ enter
   ```

4. 默认已预加载了许多 ZenMux 模型,执行 `/models` 命令选择你想要的模型。

   ```txt
   /models
   ```

   你也可以通过 OpenCode 配置添加更多模型。

   ```json title="opencode.json" {6}
   {
     "$schema": "https://opencode.ai/config.json",
     "provider": {
       "zenmux": {
         "models": {
           "somecoolnewmodel": {}
         }
       }
     }
   }
   ```

---

## 自定义提供商

要添加 `/connect` 命令中未列出的任何 **OpenAI 兼容**提供商:

:::tip
你可以在 OpenCode 中使用任何 OpenAI 兼容的提供商。大多数现代 AI 提供商都提供 OpenAI 兼容的 API。
:::

1. 执行 `/connect` 命令,向下滚动到 **Other**。

   ```bash
   $ /connect

   ┌  Add credential
   │
   ◆  Select provider
   │  ...
   │  ● Other
   └
   ```

2. 输入该提供商的唯一 ID。

   ```bash
   $ /connect

   ┌  Add credential
   │
   ◇  Enter provider id
   │  myprovider
   └
   ```

   :::note
   请选择一个容易记住的 ID,你将在配置文件中使用它。
   :::

3. 输入该提供商的 API 密钥。

   ```bash
   $ /connect

   ┌  Add credential
   │
   ▲  This only stores a credential for myprovider - you will need to configure it in opencode.json, check the docs for examples.
   │
   ◇  Enter your API key
   │  sk-...
   └
   ```

4. 在项目目录中创建或更新 `opencode.json` 文件:

   ```json title="opencode.json" ""myprovider"" {5-15}
   {
     "$schema": "https://opencode.ai/config.json",
     "provider": {
       "myprovider": {
         "npm": "@ai-sdk/openai-compatible",
         "name": "My AI ProviderDisplay Name",
         "options": {
           "baseURL": "https://api.myprovider.com/v1"
         },
         "models": {
           "my-model-name": {
             "name": "My Model Display Name"
           }
         }
       }
     }
   }
   ```

   以下是配置选项说明:
   - **npm**:要使用的 AI SDK 包,对于 OpenAI 兼容的提供商使用 `@ai-sdk/openai-compatible`(适用于 `/v1/chat/completions`)。如果你的提供商/模型走 `/v1/responses`,请使用 `@ai-sdk/openai`。
   - **name**:在 UI 中显示的名称。
   - **models**:可用模型。
   - **options.baseURL**:API 端点 URL。
   - **options.apiKey**:可选,如果不使用 auth 认证,可直接设置 API 密钥。
   - **options.headers**:可选,设置自定义请求头。

   更多高级选项请参见下面的示例。

5. 执行 `/models` 命令,你自定义的提供商和模型将出现在选择列表中。

---

##### 示例

以下是设置 `apiKey`、`headers` 和模型 `limit` 选项的示例。

```json title="opencode.json" {9,11,17-20}
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "myprovider": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "My AI ProviderDisplay Name",
      "options": {
        "baseURL": "https://api.myprovider.com/v1",
        "apiKey": "{env:ANTHROPIC_API_KEY}",
        "headers": {
          "Authorization": "Bearer custom-token"
        }
      },
      "models": {
        "my-model-name": {
          "name": "My Model Display Name",
          "limit": {
            "context": 200000,
            "output": 65536
          }
        }
      }
    }
  }
}
```

配置详情:

- **apiKey**:使用 `env` 变量语法设置,[了解更多](/docs/config#env-vars)。
- **headers**:随每个请求发送的自定义请求头。
- **limit.context**:模型接受的最大输入 Token 数。
- **limit.output**:模型可生成的最大 Token 数。

`limit` 字段让 OpenCode 了解你还剩余多少上下文空间。标准提供商会自动从 models.dev 拉取这些信息。

---

## 故障排除

如果你在配置提供商时遇到问题,请检查以下几点:

1. **检查认证设置**:运行 `opencode auth list` 查看该提供商的凭据是否已添加到配置中。

   这不适用于 Amazon Bedrock 等依赖环境变量进行认证的提供商。

2. 对于自定义提供商,请检查 OpenCode 配置并确认:
   - `/connect` 命令中使用的提供商 ID 与 OpenCode 配置中的 ID 一致。
   - 使用了正确的 npm 包。例如,Cerebras 应使用 `@ai-sdk/cerebras`。对于其他所有 OpenAI 兼容的提供商,使用 `@ai-sdk/openai-compatible`(`/v1/chat/completions`);如果模型走 `/v1/responses`,请使用 `@ai-sdk/openai`。同一 provider 混用时,可在模型下设置 `provider.npm` 覆盖默认值。
   - `options.baseURL` 字段中的 API 端点地址正确。