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

# Llamaindex

The Nebuly SDK enables you to monitor all the requests made to:

* [LlamaIndex LLMs](#llms)
* [LlamaIndex Query Engine](#query-engine)
* [LlamaIndex Chat Engine](#chat-engine)

<Note>
  All of them are supported also when using `stream` or `async` mode.
</Note>

The process is straightforward, you just need to:

* import the `LlamaIndexTrackingHandler` from the Nebuly SDK
* setup the `LlamaIndexTrackingHandler` as global llama\_index handler

You can then use the platform to analyze the results and get insights about your LLM users.

### LLMs

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from nebuly.providers.llama_index import LlamaIndexTrackingHandler

    handler = LlamaIndexTrackingHandler(
        api_key="<YOUR_NEBULY_API_KEY>",
        user_id="<USER_ID>",
        # Nebuly additional kwargs...
    )

    import llama_index
    from llama_index.core.base.llms.types import ChatMessage, MessageRole
    from llama_index.llms.openai import OpenAI

    # Setup the global handler to track llms calls
    llama_index.core.global_handler = handler

    response = OpenAI(api_key="YOUR_OPENAI_API_KEY").chat(
        messages=[
            ChatMessage(
                role=MessageRole.USER, content="Who is Paul Graham?"
            )
        ]
    )
    ```
  </Tab>
</Tabs>

You can find a detailed explanation of the allowed nebuly additional keyword arguments below:

<ParamField path="user_id" type="string" required>
  An id or username uniquely identifying the end-user. We recommend hashing their username or email address, in order to avoid sending us any identifying information.
</ParamField>

<ParamField path="nebuly_tags" type="dict">
  Tag user interactions by adding key-value pairs using this parameter. Each key represents the tag name, and the corresponding value is the tag value.

  For example, if you want to tag an interaction with the model version used to reply to user input, provide it as an argument for nebuly\_tags, e.g. `{"version": "v1.0.0"}`. You have the flexibility to define custom tags, making them available as potential filters on the Nebuly platform.
</ParamField>

### Query Engine

You can download the file used in this example here: [paul\_graham\_essay.txt](https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt)

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from nebuly.providers.llama_index import LlamaIndexTrackingHandler

    handler = LlamaIndexTrackingHandler(
        api_key="<YOUR_NEBULY_API_KEY>", 
        user_id="<USER_ID>"
        # Nebuly additional kwargs...
    )

    import os
    import urllib.request

    import llama_index
    from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

    # Setup the global handler to track llms calls
    llama_index.core.global_handler = handler
    os.environ["OPENAI_API_KEY"] = "<YOUR_OPENAI_API_KEY>"

    # Define the directory and file paths - NEEDED FOR THIS EXAMPLE
    url = "https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt"
    dir_path = "data"
    file_path = os.path.join(dir_path, "paul_graham_essay.txt")
    os.makedirs(dir_path, exist_ok=True)
    urllib.request.urlretrieve(url, file_path)
    documents = SimpleDirectoryReader("data").load_data()
    index = VectorStoreIndex.from_documents(documents)
    query_engine = index.as_query_engine()
    response = query_engine.query("What did the author do growing up?")
    ```
  </Tab>
</Tabs>

You can find a detailed explanation of the allowed nebuly additional keyword arguments below:

<ParamField path="user_id" type="string" required>
  An id or username uniquely identifying the end-user. We recommend hashing their username or email address, in order to avoid sending us any identifying information.
</ParamField>

<ParamField path="nebuly_tags" type="dict">
  Tag user interactions by adding key-value pairs using this parameter. Each key represents the tag name, and the corresponding value is the tag value.

  For example, if you want to tag an interaction with the model version used to reply to user input, provide it as an argument for nebuly\_tags, e.g. `{"version": "v1.0.0"}`. You have the flexibility to define custom tags, making them available as potential filters on the Nebuly platform.
</ParamField>

### Chat Engine

You can download the file used in this example here: [paul\_graham\_essay.txt](https://raw.githubusercontent.com/run-llama/llama_index/main/examples/paul_graham_essay/data/paul_graham_essay.txt)

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from nebuly.providers.llama_index import LlamaIndexTrackingHandler

    handler = LlamaIndexTrackingHandler(
        api_key="<YOUR_NEBULY_API_KEY>",
        user_id="<USER_ID>",
        # Additional nebuly kwargs...
    )

    import os
    import urllib.request

    import llama_index
    from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
    from llama_index.core.base.llms.types import ChatMessage, MessageRole
    from llama_index.core.memory import ChatMemoryBuffer

    # Setup the global handler to track llms calls
    llama_index.core.global_handler = handler
    os.environ["OPENAI_API_KEY"] = "<YOUR_OPENAI_API_KEY>"

    # Define the directory and file paths - NEEDED FOR THIS EXAMPLE
    url = "https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt"
    dir_path = "data"
    file_path = os.path.join(dir_path, "paul_graham_essay.txt")
    os.makedirs(dir_path, exist_ok=True)
    urllib.request.urlretrieve(url, file_path)

    documents = SimpleDirectoryReader("data").load_data()
    index = VectorStoreIndex.from_documents(documents)
    memory = ChatMemoryBuffer.from_defaults()
    chat_engine = index.as_chat_engine(
        chat_mode="context",
        memory=memory,
        system_prompt=(
            "You are a chatbot, able to have normal interactions, as well as talk"
            " about an essay discussing Paul Graham's life."
        ),
    )
    response = chat_engine.chat(
        "What did Paul Graham do growing up?",
        chat_history=[
            ChatMessage(role=MessageRole.USER, content="Hello"),
            ChatMessage(role=MessageRole.ASSISTANT, content="Hello, how can I help you?"),
        ],
    )
    ```
  </Tab>
</Tabs>

You can find a detailed explanation of the allowed nebuly additional keyword arguments below:

<ParamField path="user_id" type="string" required>
  An id or username uniquely identifying the end-user. We recommend hashing their username or email address, in order to avoid sending us any identifying information.
</ParamField>

<ParamField path="nebuly_tags" type="dict">
  Tag user interactions by adding key-value pairs using this parameter. Each key represents the tag name, and the corresponding value is the tag value.

  For example, if you want to tag an interaction with the model version used to reply to user input, provide it as an argument for nebuly\_tags, e.g. `{"version": "v1.0.0"}`. You have the flexibility to define custom tags, making them available as potential filters on the Nebuly platform.
</ParamField>
