推理提供商文档

Codex

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Codex

Codex 是 OpenAI 开发的智能编程命令行界面(CLI),它运行在您的终端中,能够读取并编辑您的代码库、执行命令以及处理多步骤的开发任务。通过将其指向 Hugging Face 推理提供商(Inference Providers),您可以将任何最新的可用开源模型作为其后端模型。

概述

Codex 允许您在其配置文件中定义自定义模型提供商。通过将 Hugging Face 路由(router)添加为提供商,所有 Codex 请求都将通过推理提供商进行路由,让您能够通过一个兼容 OpenAI 的 API 访问广泛的开源模型。

先决条件

配置

快速设置

  1. huggingface.co/settings/tokens 创建一个具有推理提供商(Inference Providers)权限的 Hugging Face 令牌

  2. 将您的令牌设置为环境变量

export HF_TOKEN=hf_...
  1. ~/.codex/config.toml 中将 Hugging Face 添加为模型提供商
[model_providers.huggingface]
name = "Hugging Face"
base_url = "https://router.huggingface.co/v1"
env_key = "HF_TOKEN"
wire_api = "responses"
  1. ~/.codex/huggingface.config.toml 独立文件中创建配置文件,选择该提供商及具体模型
model_provider = "huggingface"
model = "openai/gpt-oss-120b"

对于 0.134.0 之前的 Codex 版本,您必须在 config.toml 内部的 [profiles.<name>] 表中定义配置文件。

  1. 使用 Hugging Face 配置文件启动 Codex
codex --profile huggingface

您也可以以非交互方式运行单次命令

codex exec --profile huggingface "Explain what this repository does."

openai/gpt-oss-120b 替换为 推理提供商上提供的任何模型。您可以附加提供商后缀来锁定特定的提供商(例如 openai/gpt-oss-120b:groq)。设置明确的提供商可实现确定性路由,而省略它则允许路由器回退到备用提供商以提高鲁棒性。您还可以附加 :cheapest:fastest 后缀来优先选择更便宜或更快的提供商。

若要在不编辑配置的情况下为单次运行覆盖模型,请传递 -m 参数

codex exec --profile huggingface -m "zai-org/GLM-5.1" "Return OK."

Codex 自定义提供商需要设置 wire_api = "responses",该设置将请求通过兼容 OpenAI 的 Responses API 进行路由。请确保您选择的模型在推理提供商上支持聊天补全(chat completion)。

将账单计入组织

若要将推理用量计费到 Hugging Face 组织而非个人账户,请在 ~/.codex/config.toml 的提供商定义中添加 X-HF-Bill-To 请求头

[model_providers.huggingface]
name = "Hugging Face"
base_url = "https://router.huggingface.co/v1"
env_key = "HF_TOKEN"
wire_api = "responses"
http_headers = { "X-HF-Bill-To" = "your-org-name" }

"your-org-name" 替换为您想要计费的组织名称。您必须在该组织中拥有写入权限。

资源

在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.