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

# Sử dụng LlamaIndex với CometAPI

> Sử dụng hướng dẫn này để cấu hình LlamaIndex với CometAPI bằng cách thiết lập base URL, API key và các tùy chọn model hoặc provider.

LlamaIndex cung cấp lớp `CometLLM` như một tích hợp chính thức với CometAPI. Hãy sử dụng nó để vận hành các pipeline RAG, agent và chuỗi LLM với bất kỳ model nào trong danh mục của CometAPI.

## Điều kiện tiên quyết

* Python 3.8+
* Một tài khoản CometAPI với API key đang hoạt động — [lấy tại đây](https://www.cometapi.com/console/token)

<Steps>
  <Step title="Cài đặt tích hợp LlamaIndex CometAPI">
    ```bash theme={null}
    pip install llama-index-llms-cometapi llama-index
    ```
  </Step>

  <Step title="Thiết lập API key của bạn">
    ```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>
      Sử dụng biến môi trường an toàn hơn so với việc hardcode thông tin xác thực trong script.
    </Note>
  </Step>

  <Step title="Khởi tạo model và thực hiện các lệnh gọi completion">
    ```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="Bật streaming">
    Sử dụng `stream_chat` hoặc `stream_complete` để xuất đầu ra theo từng phần theo thời gian thực:

    ```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="Mẹo và khắc phục sự cố">
    * **Models**: Xem [trang Models của CometAPI](/vi/overview/models) để biết tất cả các tùy chọn hiện có.
    * **Sử dụng model khác**: Khởi tạo với một model ID hiện tại khác, ví dụ: `CometLLM(api_key=api_key, model="your-model-id", max_tokens=1024)`.
    * **Fine-tuning**: Truyền `temperature` và `max_tokens` trực tiếp vào `CometLLM(...)`.
    * **Xử lý lỗi**: Bọc các lệnh gọi trong `try/except` để bắt lỗi key hoặc sự cố mạng.
    * **Bảo mật**: Không bao giờ commit API key vào hệ thống quản lý phiên bản. Hãy sử dụng biến môi trường.
    * **Tài liệu khác**: [tài liệu LlamaIndex](https://docs.llamaindex.ai/) — [bắt đầu nhanh với CometAPI](/vi/overview/quick-start) — [ví dụ 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": "Sử dụng LlamaIndex với CometAPI",
        "description": "Sử dụng hướng dẫn này để cấu hình LlamaIndex với CometAPI bằng cách đặt URL cơ sở, API key và các tùy chọn model hoặc provider.",
        "step": [
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/llamaindex#step-1",
            "position": 1,
            "name": "Cài đặt tích hợp LlamaIndex CometAPI",
            "text": "Hoàn thành bước Cài đặt tích hợp LlamaIndex CometAPI trong hướng dẫn Sử dụng LlamaIndex với CometAPI."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/llamaindex#step-2",
            "position": 2,
            "name": "Thiết lập API key của bạn",
            "text": "Lưu trữ API key CometAPI của bạn trong biến môi trường hoặc trường cài đặt được tích hợp sử dụng."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/llamaindex#step-3",
            "position": 3,
            "name": "Khởi tạo model và thực hiện các lệnh gọi completion",
            "text": "Hoàn thành bước Khởi tạo model và thực hiện các lệnh gọi completion trong hướng dẫn Sử dụng LlamaIndex với CometAPI."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/llamaindex#step-4",
            "position": 4,
            "name": "Bật streaming",
            "text": "Hoàn thành bước Bật streaming trong hướng dẫn Sử dụng LlamaIndex với CometAPI."
          }
        ]
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "Tài liệu CometAPI",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "Tích hợp",
            "item": "https://apidoc.cometapi.com/integrations"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "Sử dụng LlamaIndex với CometAPI",
            "item": "https://apidoc.cometapi.com/integrations/llamaindex"
          }
        ]
      }
    ]
    }
    `}
</script>
