如果你通过 Codex CLI、IDE 扩展或 Codex Web 使用 Codex,你也可以通过编程方式控制它。
在以下情况下使用 SDK:
- 将 Codex 控制集成到 CI/CD 流水线中
- 创建能与 Codex 协作以执行复杂工程任务的自定义智能体
- 将 Codex 构建到你自己的内部工具和工作流中
- 在你自己的应用程序中集成 Codex
TypeScript 库
TypeScript 库提供了一种在应用程序内控制 Codex 的方法,比非交互模式更全面、更灵活。
请在服务端使用该库;它需要 Node.js 18 或更高版本。
安装
要开始使用,请使用以下命令安装 Codex SDK npm:
npm install @openai/codex-sdk
用量
启动一个 Codex 线程并使用你的提示词运行它。
import { Codex } from "@openai/codex-sdk";
const codex = new Codex();
const thread = codex.startThread();
const result = await thread.run(
"Make a plan to diagnose and fix the CI failures"
);
console.log(result);
像往常一样调用 run() 再次调用以在同一线程中继续,或者通过提供线程 ID 恢复之前的线程。
// running the same thread
const result = await thread.run("Implement the plan");
console.log(result);
// resuming past thread
const threadId = "<thread-id>";
const thread2 = codex.resumeThread(threadId);
const result2 = await thread2.run("Pick up where you left off");
console.log(result2);
欲了解更多详情,请查看 TypeScript 仓库.
Python 库
Python SDK 是实验性的,通过 JSON-RPC 控制本地 Codex 应用服务端。它需要 Python 3.10 或更高版本,以及开源 Codex 仓库的本地检出。
安装
在 Codex 仓库根目录下,以可编辑模式安装 SDK:
cd sdk/python
python -m pip install -e .
对于手动的本地 SDK 使用,请传入 AppServerConfig(codex_bin=...) 以指向本地 codex 二进制文件,或者使用仓库中的示例和 notebook 引导程序。
用量
启动 Codex,创建一个线程,并运行提示词:
from codex_app_server import Codex
with Codex() as codex:
thread = codex.thread_start(model="gpt-5.4")
result = thread.run("Make a plan to diagnose and fix the CI failures")
print(result.final_response)
使用 AsyncCodex 当你的应用程序已经是异步时:
import asyncio
from codex_app_server import AsyncCodex
async def main() -> None:
async with AsyncCodex() as codex:
thread = await codex.thread_start(model="gpt-5.4")
result = await thread.run("Implement the plan")
print(result.final_response)
asyncio.run(main())
欲了解更多详情,请查看 Python 仓库.