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

# 提供商 - OpenAI

> 使用 OpenAI 官方 API

## 提供商 - OpenAI

* **API 密钥：** [platform.openai.com](https://platform.openai.com)
* **可用模型：** [platform.openai.com/docs/models](https://platform.openai.com/docs/models)
* **模型定价：** [platform.openai.com/docs/pricing](https://platform.openai.com/docs/pricing)

## 环境配置

```bash theme={null} theme={null}
export OPENAI_COMPATIBLE_BASE_URL="https://api.openai.com/v1"
export OPENAI_COMPATIBLE_API_KEY="YOUR_OPENAI_API_KEY"
export OPENAI_COMPATIBLE_MODEL_NAME="gpt-5-nano"
```

## API 调用

<Tabs>
  <Tab title="cURL">
    ```bash theme={null}
    curl "$OPENAI_COMPATIBLE_BASE_URL/chat/completions" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer $OPENAI_COMPATIBLE_API_KEY" \
      -d @- <<EOF
    {
      "model": "${OPENAI_COMPATIBLE_MODEL_NAME}",
      "messages": [
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "Hello"}
      ]
    }
    EOF
    ```
  </Tab>

  <Tab title="Python">
    ```python openai-compatible-api-call.py theme={null}
    import os
    import requests

    base_url = os.environ["OPENAI_COMPATIBLE_BASE_URL"]
    api_key = os.environ["OPENAI_COMPATIBLE_API_KEY"]
    model = os.environ["OPENAI_COMPATIBLE_MODEL_NAME"]

    r = requests.post(
        f"{base_url}/chat/completions",
        headers={
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json",
        },
        json={
            "model": model,
            "messages": [
                {"role": "system", "content": "You are a helpful assistant"},
                {"role": "user", "content": "Hello"}
            ],
        },
    )
    r.raise_for_status()
    data = r.json()
    print(data["choices"][0]["message"]["content"])
    ```

    ```bash theme={null}
    pip install requests
    python openai-compatible-api-call.py
    ```
  </Tab>

  <Tab title="Python (OpenAI SDK)">
    ```python openai-compatible-api-call-with-openai-library.py theme={null}
    import os
    from openai import OpenAI

    client = OpenAI(
        base_url=os.environ["OPENAI_COMPATIBLE_BASE_URL"],
        api_key=os.environ["OPENAI_COMPATIBLE_API_KEY"],
    )

    model = os.environ["OPENAI_COMPATIBLE_MODEL_NAME"]

    completion = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": "You are a helpful assistant"},
            {"role": "user", "content": "Hello"}
        ],
    )

    print(completion.choices[0].message.content)
    ```

    ```bash theme={null}
    pip install openai
    python openai-compatible-api-call.py
    ```
  </Tab>

  <Tab title="TypeScript">
    ```ts openai-compatible-api-call.ts theme={null}
    const baseURL = process.env.OPENAI_COMPATIBLE_BASE_URL;
    const apiKey = process.env.OPENAI_COMPATIBLE_API_KEY;
    const model = process.env.OPENAI_COMPATIBLE_MODEL_NAME;

    const resp = await fetch(`${baseURL}/chat/completions`, {
      method: "POST",
      headers: {
        "Authorization": `Bearer ${apiKey}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        model,
        messages: [
          { role: "system", content: "You are a helpful assistant" },
          { role: "user", content: "Hello" }
        ],
      }),
    });

    if (!resp.ok) throw new Error(`HTTP ${resp.status}: ${await resp.text()}`);
    const data = await resp.json();
    console.log(data.choices?.[0]?.message?.content);
    ```

    ```bash theme={null}
    bun openai-compatible-api-call.ts
    ```
  </Tab>

  <Tab title="TypeScript (OpenAI SDK)">
    ```ts openai-compatible-api-call-with-openai-library.ts theme={null}
    import OpenAI from "openai";

    const client = new OpenAI({
      baseURL: process.env.OPENAI_COMPATIBLE_BASE_URL,
      apiKey: process.env.OPENAI_COMPATIBLE_API_KEY,
    });

    const model = process.env.OPENAI_COMPATIBLE_MODEL_NAME;

    const completion = await client.chat.completions.create({
      model,
      messages: [
        { role: "system", content: "You are a helpful assistant" },
        { role: "user", content: "Hello" }
      ],
    });

    console.log(completion.choices[0].message.content);
    ```

    ```bash theme={null}
    bun openai-compatible-api-call-with-openai-library.ts
    ```
  </Tab>
</Tabs>
