> ## 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.

# CometAPI를 OpenAI SDK와 함께 사용하기

> CometAPI API 키와 base URL을 설정해 CometAPI용 OpenAI Python 및 Node.js SDK를 구성합니다.

OpenAI SDK는 client 설정 두 가지, 즉 API key와 base URL만 변경하면 CometAPI와 함께 사용할 수 있습니다. 기존 OpenAI 호환 요청 코드는 그대로 유지하고, model ID만 사용 가능한 CometAPI model ID로 바꾸면 됩니다.

## SDK 설치

다음 명령어는 OpenAI Python SDK를 설치합니다:

```bash theme={null}
pip install openai
```

다음 명령어는 OpenAI Node.js SDK를 설치합니다:

```bash theme={null}
npm install openai
```

## Python 클라이언트

다음 Python 예제는 CometAPI client를 생성하고 채팅 요청을 보냅니다:

```python theme={null}
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["COMETAPI_KEY"],
    base_url="https://api.cometapi.com/v1",
)

completion = client.chat.completions.create(
    model="your-model-id",
    messages=[
        {
            "role": "user",
            "content": "Answer in one short sentence: What is CometAPI?",
        }
    ],
)

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

응답 객체에는 assistant 메시지가 포함됩니다:

```json theme={null}
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "CometAPI provides API access to models from multiple providers."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 16,
    "completion_tokens": 12,
    "total_tokens": 28
  }
}
```

## Node.js 클라이언트

다음 Node.js 예제는 OpenAI SDK와 함께 `baseURL`을 사용합니다:

```javascript theme={null}
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.COMETAPI_KEY,
  baseURL: "https://api.cometapi.com/v1",
});

const completion = await client.chat.completions.create({
  model: "your-model-id",
  messages: [
    {
      role: "user",
      content: "Answer in one short sentence: What is CometAPI?",
    },
  ],
});

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

응답 형태는 동일한 OpenAI 호환 채팅 완성 형식입니다:

```json theme={null}
{
  "id": "chatcmpl_example",
  "object": "chat.completion",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "CometAPI provides API access to models from multiple providers."
      }
    }
  ]
}
```

## 일반적인 오류

| 오류                     | 해결 방법                                                  |
| ---------------------- | ------------------------------------------------------ |
| `401`                  | `COMETAPI_KEY`가 설정되어 있고 요청에서 `Bearer` 인증을 사용하는지 확인하세요. |
| SDK still calls OpenAI | Python에서는 `base_url`을, Node.js에서는 `baseURL`을 설정하세요.    |
| Invalid model ID       | [Models page](/ko/overview/models)에서 model ID를 선택하세요.  |
| Missing `/v1`          | OpenAI 호환 SDK에는 `https://api.cometapi.com/v1`를 사용하세요.  |

## 관련 링크

* [CometAPI 빠른 시작](/ko/overview/quick-start)
* [채팅 완성](/api/text/chat)
* [응답](/api/text/responses)
* [모델 페이지](/ko/overview/models)
* [모델 디렉터리](https://www.cometapi.com/models/)
* [요금](https://www.cometapi.com/pricing/)
* [OpenAI Python SDK](https://github.com/openai/openai-python)
* [OpenAI Node.js SDK](https://github.com/openai/openai-node)

<script type="application/ld+json">
  {`
    {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "TechArticle",
        "@id": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk",
        "headline": "OpenAI SDK와 함께 CometAPI를 어떻게 사용하나요?",
        "description": "CometAPI API 키와 base URL을 설정하여 OpenAI Python 및 Node.js SDK와 함께 CometAPI를 사용하세요.",
        "url": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk",
        "author": {
          "@type": "Organization",
          "name": "CometAPI"
        },
        "publisher": {
          "@type": "Organization",
          "name": "CometAPI",
          "url": "https://www.cometapi.com"
        }
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "CometAPI 문서",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "가이드",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "OpenAI SDK와 함께 CometAPI를 어떻게 사용하나요?",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          }
        ]
      }
    ]
    }
    `}
</script>
