Overview

genai_extract_assistant_response

You can use this function to analyze AI responses, evaluate response quality, perform sentiment analysis on AI outputs, or track specific response patterns for monitoring and debugging.

Usage#

Syntax#

genai_extract_assistant_response(messages)

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.

Returns#

Returns a string containing the content of the last assistant message in the conversation, or an empty string if no assistant message is found.

Example#

Extract the assistant's response from a GenAI conversation.

Query

['otel-demo-genai']
| extend ai_response = genai_extract_assistant_response(['attributes.gen_ai.input.messages'])
| where strlen(ai_response) > 0
| project _time, ai_response
| limit 3

Run in Playground

Output

_time ai_response
2024-01-15T10:30:00Z To reset your password, click the 'Forgot Password' link on the login page.
2024-01-15T10:31:00Z Business hours are Monday to Friday, 9 AM to 5 PM EST.

This query extracts AI responses, helping you analyze response quality and patterns.

Other query languages#

Splunk SPL users

In Splunk SPL, you would need to use complex eval statements with mvfilter and mvindex to extract assistant messages.

Splunk example

| eval assistant_msgs=mvfilter(match(role, "assistant"))
| eval response=mvindex(assistant_msgs, -1)

APL equivalent

['ai-logs']
| extend response = genai_extract_assistant_response(messages)
ANSI SQL users

In ANSI SQL, you would need to unnest arrays, filter by role, and select the last message, which is more verbose.

SQL example

SELECT
  conversation_id,
  content as assistant_response
FROM (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY conversation_id ORDER BY msg_index DESC) as rn
  FROM conversations
  CROSS JOIN UNNEST(messages) WITH OFFSET AS msg_index
  WHERE role = 'assistant'
) WHERE rn = 1

APL equivalent

['ai-logs']
| extend assistant_response = genai_extract_assistant_response(messages)

Updated

Was this page helpful?