> ## 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와 함께 LlamaIndex 사용하기

> 이 가이드를 사용해 base URL, API 키, model 또는 provider 옵션을 설정하여 LlamaIndex를 CometAPI와 구성하세요.

LlamaIndex는 CometAPI와의 기본 통합으로 `CometLLM` 클래스를 제공합니다. 이를 사용해 CometAPI 카탈로그의 모든 모델로 RAG 파이프라인, 에이전트, 그리고 LLM 체인을 구동할 수 있습니다.

## 사전 준비 사항

* Python 3.8+
* 활성 API 키가 있는 CometAPI 계정 — [여기에서 발급받으세요](https://www.cometapi.com/console/token)

<Steps>
  <Step title="LlamaIndex CometAPI 통합 설치">
    ```bash theme={null}
    pip install llama-index-llms-cometapi llama-index
    ```
  </Step>

  <Step title="API 키 설정">
    ```python theme={null}
    from llama_index.llms.cometapi import CometLLM
    import os

    os.environ["COMETAPI_KEY"] = "<COMETAPI_KEY>"
    api_key = os.getenv("COMETAPI_KEY")
    ```

    <Note>
      환경 변수를 사용하는 것이 스크립트에 자격 증명을 하드코딩하는 것보다 더 안전합니다.
    </Note>
  </Step>

  <Step title="모델 초기화 및 완성 호출 실행">
    ```python theme={null}
    from llama_index.core.llms import ChatMessage

    llm = CometLLM(
        api_key=api_key,
        max_tokens=256,
        context_window=4096,
        model="your-model-id",
    )

    # Chat call
    messages = [
        ChatMessage(role="system", content="You are a helpful assistant"),
        ChatMessage(role="user", content="Say 'Hi' only!"),
    ]
    resp = llm.chat(messages)
    print(resp)

    # Completion call
    resp = llm.complete("Who is Kaiming He?")
    print(resp)
    ```
  </Step>

  <Step title="스트리밍 활성화">
    `stream_chat` 또는 `stream_complete`를 사용해 실시간 청크 출력이 가능합니다:

    ```python theme={null}
    # Streaming chat
    message = ChatMessage(role="user", content="Tell me what ResNet is")
    for chunk in llm.stream_chat([message]):
        print(chunk.delta, end="")

    # Streaming completion
    for chunk in llm.stream_complete("Tell me about Large Language Models"):
        print(chunk.delta, end="")
    ```
  </Step>
</Steps>

<AccordionGroup>
  <Accordion title="팁 및 문제 해결">
    * **모델**: 사용 가능한 모든 옵션은 [CometAPI 모델 페이지](/ko/overview/models)를 참조하세요.
    * **다른 모델 사용**: 예를 들어 다른 현재 model ID로 초기화하세요: `CometLLM(api_key=api_key, model="your-model-id", max_tokens=1024)`.
    * **파인튜닝(Fine-tuning)**: `temperature` 및 `max_tokens`를 `CometLLM(...)`에 직접 전달하세요.
    * **오류 처리**: 키 오류나 네트워크 문제를 잡기 위해 호출을 `try/except`로 감싸세요.
    * **보안**: API 키를 버전 관리에 절대 커밋하지 마세요. 환경 변수를 사용하세요.
    * **추가 문서**: [LlamaIndex 문서](https://docs.llamaindex.ai/) — [CometAPI 빠른 시작](/ko/overview/quick-start) — [Colab 예제](https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/docs/examples/llm/cometapi.ipynb)
  </Accordion>
</AccordionGroup>

<script type="application/ld+json">
  {`
    {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "HowTo",
        "@id": "https://apidoc.cometapi.com/integrations/llamaindex#howto",
        "name": "CometAPI와 함께 LlamaIndex 사용",
        "description": "이 가이드를 사용하여 base URL, API 키, 그리고 모델 또는 provider 옵션을 설정해 LlamaIndex를 CometAPI와 함께 구성하세요.",
        "step": [
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/llamaindex#step-1",
            "position": 1,
            "name": "LlamaIndex CometAPI 통합 설치",
            "text": "CometAPI와 함께 LlamaIndex 사용 가이드에서 LlamaIndex CometAPI 통합 설치 단계를 완료하세요."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/llamaindex#step-2",
            "position": 2,
            "name": "API 키 설정",
            "text": "통합에서 사용하는 환경 변수 또는 설정 필드에 CometAPI API 키를 저장하세요."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/llamaindex#step-3",
            "position": 3,
            "name": "모델 초기화 및 완성 호출 실행",
            "text": "CometAPI와 함께 LlamaIndex 사용 가이드에서 모델 초기화 및 완성 호출 실행 단계를 완료하세요."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/llamaindex#step-4",
            "position": 4,
            "name": "스트리밍 활성화",
            "text": "CometAPI와 함께 LlamaIndex 사용 가이드에서 스트리밍 활성화 단계를 완료하세요."
          }
        ]
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "CometAPI 문서",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "통합",
            "item": "https://apidoc.cometapi.com/integrations"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "CometAPI와 함께 LlamaIndex 사용",
            "item": "https://apidoc.cometapi.com/integrations/llamaindex"
          }
        ]
      }
    ]
    }
    `}
</script>
