Skip to main content
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.
In Splunk SPL, you would use mvindex to access array elements by position.
| eval message_content=mvindex(messages, 2)
In ANSI SQL, you would unnest the array and use OFFSET to access specific positions.
SELECT 
  conversation_id,
  content as message_content
FROM conversations
CROSS JOIN UNNEST(messages) WITH OFFSET AS pos
WHERE pos = 2

Usage

Syntax

genai_get_content_by_index(messages, index)

Parameters

  • messages (dynamic, required): An array of message objects from a GenAI conversation. Each message typically contains role and content fields.
  • index (long, required): 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.

Use case examples

  • Log analysis
  • OpenTelemetry traces
  • Security logs
Extract the first user message in each conversation to analyze how users initiate conversations.Query
['sample-http-logs']
| where uri contains '/api/chat'
| extend first_message = genai_get_content_by_index(todynamic(response_body)['messages'], 1)
| where isnotempty(first_message)
| project _time, id, first_message
Run in PlaygroundOutput
_timeidfirst_message
2024-01-15T10:30:00Zuser_123Hello, I need help with my account.
2024-01-15T10:31:00Zuser_456Can you tell me about your services?
This query helps you understand how users typically start conversations, which can inform greeting messages and initial prompts.
  • 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: Gets the role at a specific index. Combine with this function to understand both role and content at positions.
  • array_length: Returns array length. Use this to check message count before accessing by index.
  • 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: Extracts the last assistant response. Use this when you need the most recent AI response.