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

# Use LlamaIndex with CometAPI

> Use this guide to configure LlamaIndex with CometAPI by setting the base URL, API key, and model or provider options.

LlamaIndex provides the `CometLLM` class as a first-class integration with CometAPI. Use it to power RAG pipelines, agents, and LLM chains with any model in CometAPI's catalog.

## Prerequisites

* Python 3.8+
* A CometAPI account with an active API key — [get yours here](https://www.cometapi.com/console/token)

<Steps>
  <Step title="Install the LlamaIndex CometAPI integration">
    ```bash theme={null}
    pip install llama-index-llms-cometapi llama-index
    ```
  </Step>

  <Step title="Set your API key">
    ```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>
      Using environment variables is safer than hardcoding credentials in scripts.
    </Note>
  </Step>

  <Step title="Initialize the model and make completion calls">
    ```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="Enable streaming">
    Use `stream_chat` or `stream_complete` for real-time chunked output:

    ```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="Tips and troubleshooting">
    * **Models**: See the [CometAPI Models page](/overview/models) for all available options.
    * **Using other models**: Initialize with a different current model ID, e.g. `CometLLM(api_key=api_key, model="your-model-id", max_tokens=1024)`.
    * **Fine-tuning**: Pass `temperature` and `max_tokens` directly to `CometLLM(...)`.
    * **Error handling**: Wrap calls in `try/except` to catch key errors or network issues.
    * **Security**: Never commit API keys to version control. Use environment variables.
    * **More docs**: [LlamaIndex documentation](https://docs.llamaindex.ai/) — [CometAPI quick start](/overview/quick-start) — [Colab example](https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/docs/examples/llm/cometapi.ipynb)
  </Accordion>
</AccordionGroup>
