Module: Datadog::AIGuard::Evaluation
- Defined in:
- lib/datadog/ai_guard/evaluation.rb,
lib/datadog/ai_guard/evaluation/result.rb,
lib/datadog/ai_guard/evaluation/message.rb,
lib/datadog/ai_guard/evaluation/request.rb,
lib/datadog/ai_guard/evaluation/tool_call.rb,
lib/datadog/ai_guard/evaluation/content_part.rb,
lib/datadog/ai_guard/evaluation/no_op_result.rb,
lib/datadog/ai_guard/evaluation/content_builder.rb
Overview
module that contains a function for performing AI Guard Evaluation request
and creating ai_guard span with required tags
Defined Under Namespace
Modules: ContentPart Classes: ContentBuilder, Message, NoOpResult, Request, Result, ToolCall
Class Method Summary collapse
Class Method Details
.perform(messages, allow_raise: true) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/datadog/ai_guard/evaluation.rb', line 9 def perform(, allow_raise: true) raise ArgumentError, "Messages must not be empty" if &.empty? Tracing.trace(Ext::SPAN_NAME) do |span, trace| trace.keep! trace.set_tag( Tracing::Metadata::Ext::Distributed::TAG_DECISION_MAKER, Tracing::Sampling::Ext::Decision::AI_GUARD ) trace.set_distributed_source(Ext::PRODUCT_BIT) trace.set_tag(Ext::EVENT_TAG, true) trace.set_tag(Ext::TRACE_EXECUTED_TAG, "1") Ext::TRACE_ANOMALY_DETECTION_TAGS.each do |tag| value = trace.get_tag(tag) span.set_tag(tag, value) if value end if ( = .last) if .tool_call span.set_tag(Ext::TARGET_TAG, "tool") span.set_tag(Ext::TOOL_NAME_TAG, .tool_call.tool_name) elsif .tool_call_id span.set_tag(Ext::TARGET_TAG, "tool") if ( = .find { |m| m.tool_call&.id == .tool_call_id }) span.set_tag(Ext::TOOL_NAME_TAG, .tool_call.tool_name) # steep:ignore end else span.set_tag(Ext::TARGET_TAG, "prompt") end end request = Request.new() result = request.perform span.set_tag(Ext::ACTION_TAG, result.action) span.set_tag(Ext::REASON_TAG, result.reason) span.( Ext::METASTRUCT_TAG, { messages: truncate_content((request.)), attack_categories: result., sds: result.sds_findings, tag_probs: result.tag_probabilities, } ) if allow_raise && (result.deny? || result.abort?) && result.blocking_enabled? span.set_tag(Ext::BLOCKED_TAG, true) raise AIGuardAbortError.new(action: result.action, reason: result.reason, tags: result.) end result end end |
.perform_no_op ⇒ Object
67 68 69 70 71 |
# File 'lib/datadog/ai_guard/evaluation.rb', line 67 def perform_no_op AIGuard.logger&.warn("AI Guard is disabled, messages were not evaluated") NoOpResult.new end |