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

# genai_get_content_by_index

> This page explains how to use the genai_get_content_by_index function in APL.

The `genai_get_content_by_index` function retrieves the content of a message at a specific position in a GenAI messages array. This allows you to access messages by their position in the conversation sequence.

You can use this function to extract specific messages in a conversation flow, analyze conversation structure, retrieve intermediate messages, or process conversations sequentially.

## For users of other query languages

If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.

<AccordionGroup>
  <Accordion title="Splunk SPL users">
    In Splunk SPL, you would use `mvindex` to access array elements by position.

    <CodeGroup>
      ```sql Splunk example theme={null}
      | eval message_content=mvindex(messages, 2)
      ```

      ```kusto APL equivalent theme={null}
      ['ai-logs']
      | extend message_content = genai_get_content_by_index(messages, 2)
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="ANSI SQL users">
    In ANSI SQL, you would unnest the array and use `OFFSET` to access specific positions.

    <CodeGroup>
      ```sql SQL example theme={null}
      SELECT 
        conversation_id,
        content as message_content
      FROM conversations
      CROSS JOIN UNNEST(messages) WITH OFFSET AS pos
      WHERE pos = 2
      ```

      ```kusto APL equivalent theme={null}
      ['ai-logs']
      | extend message_content = genai_get_content_by_index(messages, 2)
      ```
    </CodeGroup>
  </Accordion>
</AccordionGroup>

## Usage

### Syntax

```kusto theme={null}
genai_get_content_by_index(messages, index)
```

### Parameters

| Name     | Type    | Required | Description                                                                                                         |
| -------- | ------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
| messages | dynamic | Yes      | An array of message objects from a GenAI conversation. Each message typically contains `role` and `content` fields. |
| index    | long    | Yes      | The zero-based position of the message to retrieve. Use 0 for the first message, 1 for the second, etc.             |

### Returns

Returns a string containing the content of the message at the specified index, or an empty string if the index is out of bounds.

## Example

Get the content of the first user message in a GenAI conversation.

**Query**

```kusto theme={null}
['otel-demo-genai']
| extend first_message = genai_get_content_by_index(['attributes.gen_ai.input.messages'], 1)
| where isnotempty(first_message)
| project _time, first_message
| limit 3
```

[Run in Playground](https://play.axiom.co/axiom-play-qf1k/query?initForm=%7B%22apl%22%3A%22%5B%27otel-demo-genai%27%5D%20%7C%20extend%20first_message%20%3D%20genai_get_content_by_index\(%5B%27attributes.gen_ai.input.messages%27%5D%2C%201\)%20%7C%20where%20isnotempty\(first_message\)%20%7C%20project%20_time%2C%20first_message%20%7C%20limit%203%22%7D)

**Output**

| \_time               | first\_message                       |
| -------------------- | ------------------------------------ |
| 2024-01-15T10:30:00Z | Hello, I need help with my account.  |
| 2024-01-15T10:31:00Z | Can you tell me about your services? |

This query helps you understand how users typically start conversations, which can inform greeting messages and initial prompts.

## List of related functions

* [genai\_get\_content\_by\_role](/apl/scalar-functions/genai-functions/genai-get-content-by-role): Gets content filtered by role. Use this when you need messages from a specific role rather than a specific position.
* [genai\_get\_role](/apl/scalar-functions/genai-functions/genai-get-role): Gets the role at a specific index. Combine with this function to understand both role and content at positions.
* [array\_length](/apl/scalar-functions/array-functions/array-length): Returns array length. Use this to check message count before accessing by index.
* [genai\_extract\_user\_prompt](/apl/scalar-functions/genai-functions/genai-extract-user-prompt): Extracts the last user prompt. Use this when you need the most recent user message instead of a specific index.
* [genai\_extract\_assistant\_response](/apl/scalar-functions/genai-functions/genai-extract-assistant-response): Extracts the last assistant response. Use this when you need the most recent AI response.
