summaryrefslogtreecommitdiffhomepage
path: root/packages/web/src/content/docs/zh-tw/providers.mdx
blob: c87417095992d6368a9229aa752d3e10f148e678 (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
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
---
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 Zen`,然後前往 [opencode.ai/auth](https://opencode.ai/zen)。

   ```txt
   /connect
   ```

2. 登入後新增帳單資訊,然後複製您的 API 金鑰。

3. 貼上您的 API 金鑰。

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

4. 在 TUI 中執行 `/models` 查看我們推薦的模型列表。

   ```txt
   /models
   ```

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

---

## OpenCode Go

OpenCode Go 是一個低成本的訂閱計畫,提供對 OpenCode 團隊提供的流行開放編碼模型的可靠存取,這些模型已經過測試和驗證,能夠與 OpenCode 良好配合使用。

1. 在 TUI 中執行 `/connect` 指令,選擇 `OpenCode Go`,然後前往 [opencode.ai/auth](https://opencode.ai/zen)。

   ```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`
   - **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`。
   - `options.baseURL` 欄位中的 API 端點位址正確。