Module: Datadog::AIGuard::Contrib::RubyLLM::ChatInstrumentation

Defined in:
lib/datadog/ai_guard/contrib/ruby_llm/chat_instrumentation.rb

Overview

module that gets prepended to RubyLLM::Chat

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.evaluate!(messages) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/datadog/ai_guard/contrib/ruby_llm/chat_instrumentation.rb', line 10

def evaluate!(messages)
  ai_guard_messages = messages.flat_map do |message|
    if message.tool_call?
      message.tool_calls.map do |tool_call_id, tool_call|
        AIGuard.assistant(id: tool_call_id, tool_name: tool_call.name, arguments: tool_call.arguments.to_s)
      end
    elsif message.tool_result?
      AIGuard.tool(tool_call_id: message.tool_call_id, content: message.content)
    else
      AIGuard.message(role: message.role, content: message.content)
    end
  end

  AIGuard.evaluate(*ai_guard_messages, allow_raise: true)
end

Instance Method Details

#complete(&block) ⇒ Object



27
28
29
30
31
# File 'lib/datadog/ai_guard/contrib/ruby_llm/chat_instrumentation.rb', line 27

def complete(&block)
  Datadog::AIGuard::Contrib::RubyLLM::ChatInstrumentation.evaluate!(messages)

  super
end

#handle_tool_calls(response, &block) ⇒ Object



33
34
35
36
37
# File 'lib/datadog/ai_guard/contrib/ruby_llm/chat_instrumentation.rb', line 33

def handle_tool_calls(response, &block)
  Datadog::AIGuard::Contrib::RubyLLM::ChatInstrumentation.evaluate!(messages)

  super
end