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

# OpenAI SDK

> OpenAI SDK は、Python およびJavaScript アプリケーションからOpenAI 互換エンドポイントを操作するためのインターフェースを提供します。以下の手順に従って、Connect AI のLLM Gateway 経由でリクエストをルーティングします。

## 前提条件

OpenAI SDK がConnect AI のLLM Gateway を使用できるように設定する前に、プロバイダーを登録し、ルーティングルールを作成し、プロジェクトを作成し、仮想キーを発行する必要があります。詳細については、[LLM Gateway](/ja/LLM-Gateway/LLM-Gateway) を参照してください。

<Note>Connect AI のPersonal Access Token（PAT）はLLM Gateway では使用できません。リクエストの認証に使用できるのは仮想キーのみです。</Note>

## SDK のインストール

<Tabs>
  <Tab title="Python">
    ```bash theme={null}
    pip install openai
    ```
  </Tab>

  <Tab title="JavaScript">
    ```bash theme={null}
    npm install openai
    ```
  </Tab>
</Tabs>

## リクエストの送信

`base_url`（Python）または`baseURL`（JavaScript）をConnect AI のLLM Gateway エンドポイントに設定し、仮想キーを`api_key` または`apiKey` として渡します。ゲートウェイは、プロジェクトのルーティングルールに基づいてプロバイダーとモデルを選択します。

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from openai import OpenAI

    client = OpenAI(
        base_url="https://cloud.cdata.com/gateway/v1",
        api_key="<your-virtual-key>",
    )

    response = client.chat.completions.create(
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(response.choices[0].message.content)
    ```
  </Tab>

  <Tab title="JavaScript">
    ```typescript theme={null}
    import OpenAI from "openai";

    const client = new OpenAI({
      baseURL: "https://cloud.cdata.com/gateway/v1",
      apiKey: "<your-virtual-key>",
    });

    const response = await client.chat.completions.create({
      messages: [{ role: "user", content: "Hello" }],
    });

    console.log(response.choices[0].message.content);
    ```
  </Tab>
</Tabs>

## 環境変数での設定

コード内で接続情報を渡す代わりに、環境変数を設定することもできます。OpenAI SDK はこれらを自動的に読み取ります。

<Tabs>
  <Tab title="macOS / Linux">
    ```bash theme={null}
    export OPENAI_BASE_URL="https://cloud.cdata.com/gateway/v1"
    export OPENAI_API_KEY="<your-virtual-key>"
    ```
  </Tab>

  <Tab title="Windows (PowerShell)">
    ```powershell theme={null}
    $env:OPENAI_BASE_URL = "https://cloud.cdata.com/gateway/v1"
    $env:OPENAI_API_KEY = "<your-virtual-key>"
    ```
  </Tab>
</Tabs>

環境変数を設定すると、明示的な引数なしでクライアントをインスタンス化できます：

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from openai import OpenAI

    client = OpenAI()  # reads OPENAI_BASE_URL and OPENAI_API_KEY from env

    response = client.chat.completions.create(
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(response.choices[0].message.content)
    ```
  </Tab>

  <Tab title="JavaScript">
    ```typescript theme={null}
    import OpenAI from "openai";

    const client = new OpenAI(); // reads OPENAI_BASE_URL and OPENAI_API_KEY from env

    const response = await client.chat.completions.create({
      messages: [{ role: "user", content: "Hello" }],
    });

    console.log(response.choices[0].message.content);
    ```
  </Tab>
</Tabs>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.