@@ -10,7 +10,7 @@ namespace OpenTelemetry.Instrumentation.AWS.Implementation;
1010
1111internal class AWSLlmModelProcessor
1212{
13- internal static void ProcessGenAiAttributes < T > ( Activity activity , T message , string model , bool isRequest )
13+ internal static void ProcessGenAiAttributes < T > ( Activity activity , T message , string modelName , bool isRequest )
1414 {
1515 // message can be either a request or a response. isRequest is used by the model-specific methods to determine
1616 // whether to extract the request or response attributes.
@@ -37,26 +37,29 @@ internal static void ProcessGenAiAttributes<T>(Activity activity, T message, str
3737 }
3838
3939 // extract model specific attributes based on model name
40- switch ( model )
40+ if ( modelName . Contains ( "amazon.titan" ) )
4141 {
42- case "amazon.titan" :
43- ProcessTitanModelAttributes ( activity , jsonObject , isRequest ) ;
44- break ;
45- case "anthropic.claude" :
46- ProcessClaudeModelAttributes ( activity , jsonObject , isRequest ) ;
47- break ;
48- case "meta.llama3" :
49- ProcessLlamaModelAttributes ( activity , jsonObject , isRequest ) ;
50- break ;
51- case "cohere.command" :
52- ProcessCommandModelAttributes ( activity , jsonObject , isRequest ) ;
53- break ;
54- case "ai21.jamba" :
55- ProcessJambaModelAttributes ( activity , jsonObject , isRequest ) ;
56- break ;
57- case "mistral.mistral" :
58- ProcessMistralModelAttributes ( activity , jsonObject , isRequest ) ;
59- break ;
42+ ProcessTitanModelAttributes ( activity , jsonObject , isRequest ) ;
43+ }
44+ else if ( modelName . Contains ( "anthropic.claude" ) )
45+ {
46+ ProcessClaudeModelAttributes ( activity , jsonObject , isRequest ) ;
47+ }
48+ else if ( modelName . Contains ( "meta.llama3" ) )
49+ {
50+ ProcessLlamaModelAttributes ( activity , jsonObject , isRequest ) ;
51+ }
52+ else if ( modelName . Contains ( "cohere.command" ) )
53+ {
54+ ProcessCommandModelAttributes ( activity , jsonObject , isRequest ) ;
55+ }
56+ else if ( modelName . Contains ( "ai21.jamba" ) )
57+ {
58+ ProcessJambaModelAttributes ( activity , jsonObject , isRequest ) ;
59+ }
60+ else if ( modelName . Contains ( "mistral.mistral" ) )
61+ {
62+ ProcessMistralModelAttributes ( activity , jsonObject , isRequest ) ;
6063 }
6164 }
6265 catch ( Exception ex )
@@ -135,28 +138,27 @@ private static void ProcessClaudeModelAttributes(Activity activity, Dictionary<s
135138 activity . SetTag ( AWSSemanticConventions . AttributeGenAiTemperature , temperature . GetDouble ( ) ) ;
136139 }
137140
138- if ( jsonBody . TryGetValue ( "max_tokens_to_sample " , out var maxTokens ) )
141+ if ( jsonBody . TryGetValue ( "max_tokens " , out var maxTokens ) )
139142 {
140143 activity . SetTag ( AWSSemanticConventions . AttributeGenAiMaxTokens , maxTokens . GetInt32 ( ) ) ;
141144 }
142-
143- // input tokens not provided in Claude response body, so we estimate the value based on input length
144- if ( jsonBody . TryGetValue ( "prompt" , out var input ) )
145- {
146- activity . SetTag ( AWSSemanticConventions . AttributeGenAiInputTokens , Convert . ToInt32 ( Math . Ceiling ( ( double ) input . GetString ( ) . Length / 6 ) ) ) ;
147- }
148145 }
149146 else
150147 {
151- if ( jsonBody . TryGetValue ( "stop_reason " , out var finishReasons ) )
148+ if ( jsonBody . TryGetValue ( "usage " , out var usage ) )
152149 {
153- activity . SetTag ( AWSSemanticConventions . AttributeGenAiFinishReasons , new string [ ] { finishReasons . GetString ( ) } ) ;
150+ if ( usage . TryGetProperty ( "input_tokens" , out var inputTokens ) )
151+ {
152+ activity . SetTag ( AWSSemanticConventions . AttributeGenAiInputTokens , inputTokens . GetInt32 ( ) ) ;
153+ }
154+ if ( usage . TryGetProperty ( "output_tokens" , out var outputTokens ) )
155+ {
156+ activity . SetTag ( AWSSemanticConventions . AttributeGenAiOutputTokens , outputTokens . GetInt32 ( ) ) ;
157+ }
154158 }
155-
156- // output tokens not provided in Claude response body, so we estimate the value based on output length
157- if ( jsonBody . TryGetValue ( "completion" , out var output ) )
159+ if ( jsonBody . TryGetValue ( "stop_reason" , out var finishReasons ) )
158160 {
159- activity . SetTag ( AWSSemanticConventions . AttributeGenAiOutputTokens , Convert . ToInt32 ( Math . Ceiling ( ( double ) output . GetString ( ) . Length / 6 ) ) ) ;
161+ activity . SetTag ( AWSSemanticConventions . AttributeGenAiFinishReasons , new string [ ] { finishReasons . GetString ( ) } ) ;
160162 }
161163 }
162164 }
0 commit comments