> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sử dụng Codex với CometAPI

> Sử dụng hướng dẫn này để cấu hình Codex với CometAPI bằng cách chỉnh sửa cài đặt provider trong config.toml ở cấp người dùng.

Sử dụng hướng dẫn này để chạy [Codex](https://developers.openai.com/codex/quickstart) với CometAPI làm model provider.

Tài liệu tham khảo chính thức:

* [Khởi động nhanh Codex](https://developers.openai.com/codex/quickstart)
* [Kiến thức cơ bản về cấu hình Codex](https://developers.openai.com/codex/config-basic)
* [Các model provider tùy chỉnh của Codex](https://developers.openai.com/codex/config-advanced#custom-model-providers)
* [Xác thực Codex](https://developers.openai.com/codex/auth)
* [Codex trên Windows](https://developers.openai.com/codex/windows)

## Điều kiện tiên quyết

| Yêu cầu          | Chi tiết                                                                                                         |
| ---------------- | ---------------------------------------------------------------------------------------------------------------- |
| OS               | macOS, Linux, Windows PowerShell gốc, hoặc Windows với WSL                                                       |
| Git              | Khuyến nghị 2.23+                                                                                                |
| Codex            | Cài đặt ứng dụng Codex hoặc Codex CLI từ [Khởi động nhanh Codex](https://developers.openai.com/codex/quickstart) |
| CometAPI API key | Lấy một khóa từ [bảng điều khiển CometAPI](https://www.cometapi.com/console/token)                               |
| Model ID         | Sử dụng một model ID từ [trang Models](/vi/overview/models)                                                      |

## Chọn cách thiết lập

Chúng tôi cung cấp hai tùy chọn để giúp bạn cấu hình Codex nhanh chóng.

<CardGroup cols={2}>
  <Card title="Tùy chọn 1: Cấu hình Codex thủ công (khuyến nghị)" icon="bolt" href="#configure-codex-manually">
    Chỉnh sửa trực tiếp tệp `~/.codex/config.toml` ở cấp người dùng. Đây là cách đáng tin cậy nhất, đặc biệt với người dùng Windows và WSL.
  </Card>

  <Card title="Tùy chọn 2: Cấu hình bằng script" icon="puzzle-piece" href="#run-the-setup-script">
    Chạy script thiết lập như một lối tắt. Script sẽ ghi cùng cấu hình provider và lưu CometAPI API key cho Codex.
  </Card>
</CardGroup>

## Cấu hình Codex thủ công

Codex đọc các mặc định provider cá nhân từ tệp `~/.codex/config.toml` ở cấp người dùng. Hãy cấu hình provider CometAPI tại đó, không phải trong tệp `.codex/config.toml` của dự án. Codex bỏ qua các thiết lập provider và provider-auth trong các tệp cấu hình dự án.

Thiết lập được khuyến nghị sử dụng một provider có tên `cometapi` và xác thực dựa trên lệnh. Cách này giúp giữ CometAPI tách biệt với provider OpenAI tích hợp sẵn, không yêu cầu kế thừa biến môi trường từ shell, và không thay thế `~/.codex/auth.json`.

<Tabs>
  <Tab title="macOS / Linux / WSL">
    Lưu CometAPI API key của bạn vào một tệp key cục bộ:

    ```bash theme={null}
    mkdir -p "$HOME/.codex"
    printf "%s\n" "$COMETAPI_KEY" > "$HOME/.codex/cometapi_key"
    chmod 600 "$HOME/.codex/cometapi_key"
    ```

    Thêm cấu hình này vào `~/.codex/config.toml`:

    ```toml theme={null}
    model_provider = "cometapi"
    model = "your-model-id"

    [model_providers.cometapi]
    name = "CometAPI"
    base_url = "https://api.cometapi.com/v1"
    wire_api = "responses"

    [model_providers.cometapi.auth]
    command = "sh"
    args = ["-c", "cat \"$HOME/.codex/cometapi_key\""]
    ```
  </Tab>

  <Tab title="Windows PowerShell">
    Lưu CometAPI API key của bạn vào thư mục home Codex trên Windows:

    ```powershell theme={null}
    New-Item -ItemType Directory -Force "$HOME\.codex" | Out-Null
    Set-Content -NoNewline -Path "$HOME\.codex\cometapi_key" -Value $env:COMETAPI_KEY
    ```

    Thêm cấu hình này vào `$HOME\.codex\config.toml`:

    ```toml theme={null}
    model_provider = "cometapi"
    model = "your-model-id"

    [model_providers.cometapi]
    name = "CometAPI"
    base_url = "https://api.cometapi.com/v1"
    wire_api = "responses"

    [model_providers.cometapi.auth]
    command = "powershell.exe"
    args = [
      "-NoProfile",
      "-Command",
      "$p=Join-Path $HOME '.codex/cometapi_key'; (Get-Content -Raw $p).Trim()",
    ]
    ```
  </Tab>
</Tabs>

<Note>
  Codex gốc trên Windows sử dụng `$HOME\.codex`, thường là `C:\Users\<user>\.codex`. WSL sử dụng `~/.codex` của bản phân phối Linux. Hãy chỉnh sửa thư mục tương ứng với môi trường nơi tác nhân Codex chạy.
</Note>

## Sử dụng xác thực bằng biến môi trường

Nếu bạn muốn giữ CometAPI API key trong một biến môi trường, hãy dùng `env_key` thay cho khối `[model_providers.cometapi.auth]`.

Chỉ sử dụng cấu hình provider này khi `COMETAPI_KEY` có sẵn trong môi trường khởi chạy Codex:

```toml theme={null}
model_provider = "cometapi"
model = "your-model-id"

[model_providers.cometapi]
name = "CometAPI"
base_url = "https://api.cometapi.com/v1"
wire_api = "responses"
env_key = "COMETAPI_KEY"
```

<Warning>
  Không kết hợp `env_key` với `[model_providers.cometapi.auth]`. Codex chỉ hỗ trợ một phương thức xác thực cho mỗi provider tùy chỉnh.
</Warning>

## Chạy script thiết lập

Script thiết lập là tùy chọn. Nó ghi cùng một cấu hình provider `cometapi` vào `~/.codex/config.toml`, lưu trữ CometAPI API key của bạn trong `~/.codex/cometapi_key`, tạo bản sao lưu trước khi thay đổi tệp, và xác minh cấu hình bằng `codex exec` khi Codex CLI khả dụng.

Với macOS, Linux hoặc WSL, hãy chạy trình cài đặt tương tác:

```bash theme={null}
sh -c "$(curl -fsSL https://raw.githubusercontent.com/cometapi-dev/integrations/main/codex/setup.sh)"
```

Với thiết lập không tương tác, truyền API key một cách tường minh:

```bash theme={null}
curl -fsSL https://raw.githubusercontent.com/cometapi-dev/integrations/main/codex/setup.sh | sh -s -- --key "$COMETAPI_KEY"
```

Với PowerShell gốc trên Windows, hãy chạy trình cài đặt tương tác:

```powershell theme={null}
powershell -c "irm 'https://raw.githubusercontent.com/cometapi-dev/integrations/main/codex/setup.ps1' | iex"
```

Với thiết lập Windows không tương tác, truyền API key một cách tường minh:

```powershell theme={null}
powershell -c "& ([scriptblock]::Create((irm 'https://raw.githubusercontent.com/cometapi-dev/integrations/main/codex/setup.ps1'))) -Key $env:COMETAPI_KEY"
```

<Warning>
  Theo mặc định, script không thay thế `~/.codex/auth.json` và không xóa đăng nhập ChatGPT. Chỉ dùng `--force-auth-json` hoặc `-ForceAuthJson` nếu bạn muốn script quản lý đăng nhập Codex bằng API key thông qua `auth.json`.
</Warning>

## Chọn hoặc thay đổi model ID

Sử dụng một model ID từ [trang Models](/vi/overview/models). Trong cấu hình thủ công, thay đổi giá trị `model` trong `~/.codex/config.toml`.

Với macOS, Linux hoặc WSL, truyền `--model` khi bạn chạy script thiết lập:

```bash theme={null}
curl -fsSL https://raw.githubusercontent.com/cometapi-dev/integrations/main/codex/setup.sh | sh -s -- --key "$COMETAPI_KEY" --model your-model-id
```

Với PowerShell gốc trên Windows, truyền `-Model` khi bạn chạy script thiết lập:

```powershell theme={null}
powershell -c "& ([scriptblock]::Create((irm 'https://raw.githubusercontent.com/cometapi-dev/integrations/main/codex/setup.ps1'))) -Key $env:COMETAPI_KEY -Model 'your-model-id'"
```

<Note>
  Script sử dụng `CODEX_HOME` khi nó được thiết lập. Nếu không, nó sẽ ghi vào `~/.codex` của môi trường hiện tại.
</Note>

## Xác minh thiết lập

Để xác minh thiết lập bằng Codex CLI, hãy chạy lệnh chỉ đọc này từ bất kỳ dự án cục bộ nào:

```bash theme={null}
codex exec --ephemeral --skip-git-repo-check --sandbox read-only --color never "Reply exactly with: COMETAPI_CODEX_OK"
```

Nếu Codex CLI không khả dụng trong `PATH`, hãy mở ứng dụng Codex và gửi một prompt ngắn từ một dự án cục bộ.

## Khắc phục sự cố

<AccordionGroup>
  <Accordion title="Codex vẫn sử dụng provider OpenAI mặc định">
    Xác nhận rằng `model_provider = "cometapi"` có trong tệp `~/.codex/config.toml` ở cấp người dùng.
  </Accordion>

  <Accordion title="API key đã thay đổi">
    Cập nhật `~/.codex/cometapi_key`, hoặc chạy lại script thiết lập với giá trị `$COMETAPI_KEY` đã được cập nhật.
  </Accordion>

  <Accordion title="Script PowerShell bị lỗi trong quá trình kiểm tra trước khi chạy">
    Sử dụng các bước PowerShell gốc thủ công trên Windows, sau đó chạy lệnh xác minh.
  </Accordion>

  <Accordion title="Thiết lập shell qua pipe không yêu cầu API key">
    Sử dụng `--key "$COMETAPI_KEY"`, đặt `COMETAPI_KEY`, hoặc chạy lệnh tương tác `sh -c "$(curl ...)"`.
  </Accordion>

  <Accordion title="Kết nối bị hết thời gian chờ hoặc dùng sai base URL">
    Xác nhận rằng `base_url` là `https://api.cometapi.com/v1` trong `~/.codex/config.toml`.
  </Accordion>

  <Accordion title="Không tìm thấy model">
    Kiểm tra [trang Models](/vi/overview/models) để xem các model ID khả dụng.
  </Accordion>

  <Accordion title="Thiết lập Windows ảnh hưởng đến sai môi trường">
    Chỉnh sửa `$HOME\.codex` cho chế độ Windows gốc, hoặc chỉnh sửa `~/.codex` bên trong WSL cho chế độ agent WSL.
  </Accordion>

  <Accordion title="Đăng nhập ChatGPT đã thay đổi">
    Không sử dụng `--force-auth-json` hoặc `-ForceAuthJson` trừ khi bạn muốn chế độ đăng nhập bằng API-key.
  </Accordion>
</AccordionGroup>

<script type="application/ld+json">
  {`
    {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "HowTo",
        "@id": "https://apidoc.cometapi.com/integrations/codex#howto",
        "name": "Sử dụng Codex với CometAPI",
        "description": "Sử dụng hướng dẫn này để cấu hình Codex với CometAPI bằng cách chỉnh sửa cài đặt provider trong config.toml ở cấp người dùng.",
        "step": [
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/codex#step-1",
            "position": 1,
            "name": "Xem lại các điều kiện tiên quyết",
            "text": "Hoàn thành bước Xem lại các điều kiện tiên quyết trong hướng dẫn Sử dụng Codex với CometAPI."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/codex#step-2",
            "position": 2,
            "name": "Chọn đường dẫn thiết lập",
            "text": "Hoàn thành bước Chọn đường dẫn thiết lập trong hướng dẫn Sử dụng Codex với CometAPI."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/codex#step-3",
            "position": 3,
            "name": "Cấu hình Codex thủ công",
            "text": "Hoàn thành bước Cấu hình Codex thủ công trong hướng dẫn Sử dụng Codex với CometAPI."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/codex#step-4",
            "position": 4,
            "name": "Sử dụng xác thực bằng biến môi trường",
            "text": "Hoàn thành bước Sử dụng xác thực bằng biến môi trường trong hướng dẫn Sử dụng Codex với CometAPI."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/codex#step-5",
            "position": 5,
            "name": "Chạy script thiết lập",
            "text": "Hoàn thành bước Chạy script thiết lập trong hướng dẫn Sử dụng Codex với CometAPI."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/codex#step-6",
            "position": 6,
            "name": "Chọn hoặc thay đổi model ID",
            "text": "Hoàn thành bước Chọn hoặc thay đổi model ID trong hướng dẫn Sử dụng Codex với CometAPI."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/codex#step-7",
            "position": 7,
            "name": "Xác minh thiết lập",
            "text": "Hoàn thành bước Xác minh thiết lập trong hướng dẫn Sử dụng Codex với CometAPI."
          }
        ]
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "Tài liệu CometAPI",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "Tích hợp",
            "item": "https://apidoc.cometapi.com/integrations"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "Sử dụng Codex với CometAPI",
            "item": "https://apidoc.cometapi.com/integrations/codex"
          }
        ]
      }
    ]
    }
    `}
</script>
