@@ -9,12 +9,9 @@ const Sampler = require('../../dd-trace/src/sampler')
99const { MEASURED } = require ( '../../../ext/tags' )
1010const { estimateTokens } = require ( './token-estimator' )
1111
12- // String#replaceAll unavailable on Node.js@v 14 (dd-trace@<=v3)
13- const RE_NEWLINE = / \n / g
14- const RE_TAB = / \t / g
12+ const makeUtilities = require ( '../../dd-trace/src/plugins/util/llm' )
1513
16- // TODO: In the future we should refactor config.js to make it requirable
17- let MAX_TEXT_LEN = 128
14+ let normalize
1815
1916function safeRequire ( path ) {
2017 try {
@@ -44,9 +41,11 @@ class OpenAiTracingPlugin extends TracingPlugin {
4441
4542 this . sampler = new Sampler ( 0.1 ) // default 10% log sampling
4643
47- // hoist the max length env var to avoid making all of these functions a class method
44+ // hoist the normalize function to avoid making all of these functions a class method
4845 if ( this . _tracerConfig ) {
49- MAX_TEXT_LEN = this . _tracerConfig . openaiSpanCharLimit
46+ const utilities = makeUtilities ( 'openai' , this . _tracerConfig )
47+
48+ normalize = utilities . normalize
5049 }
5150 }
5251
@@ -116,7 +115,7 @@ class OpenAiTracingPlugin extends TracingPlugin {
116115 // createEdit, createEmbedding, createModeration
117116 if ( payload . input ) {
118117 const normalized = normalizeStringOrTokenArray ( payload . input , false )
119- tags [ 'openai.request.input' ] = truncateText ( normalized )
118+ tags [ 'openai.request.input' ] = normalize ( normalized )
120119 openaiStore . input = normalized
121120 }
122121
@@ -594,7 +593,7 @@ function commonImageResponseExtraction (tags, body) {
594593 for ( let i = 0 ; i < body . data . length ; i ++ ) {
595594 const image = body . data [ i ]
596595 // exactly one of these two options is provided
597- tags [ `openai.response.images.${ i } .url` ] = truncateText ( image . url )
596+ tags [ `openai.response.images.${ i } .url` ] = normalize ( image . url )
598597 tags [ `openai.response.images.${ i } .b64_json` ] = image . b64_json && 'returned'
599598 }
600599}
@@ -731,14 +730,14 @@ function commonCreateResponseExtraction (tags, body, openaiStore, methodName) {
731730
732731 tags [ `openai.response.choices.${ choiceIdx } .finish_reason` ] = choice . finish_reason
733732 tags [ `openai.response.choices.${ choiceIdx } .logprobs` ] = specifiesLogProb ? 'returned' : undefined
734- tags [ `openai.response.choices.${ choiceIdx } .text` ] = truncateText ( choice . text )
733+ tags [ `openai.response.choices.${ choiceIdx } .text` ] = normalize ( choice . text )
735734
736735 // createChatCompletion only
737736 const message = choice . message || choice . delta // delta for streamed responses
738737 if ( message ) {
739738 tags [ `openai.response.choices.${ choiceIdx } .message.role` ] = message . role
740- tags [ `openai.response.choices.${ choiceIdx } .message.content` ] = truncateText ( message . content )
741- tags [ `openai.response.choices.${ choiceIdx } .message.name` ] = truncateText ( message . name )
739+ tags [ `openai.response.choices.${ choiceIdx } .message.content` ] = normalize ( message . content )
740+ tags [ `openai.response.choices.${ choiceIdx } .message.name` ] = normalize ( message . name )
742741 if ( message . tool_calls ) {
743742 const toolCalls = message . tool_calls
744743 for ( let toolIdx = 0 ; toolIdx < toolCalls . length ; toolIdx ++ ) {
@@ -795,24 +794,6 @@ function truncateApiKey (apiKey) {
795794 return apiKey && `sk-...${ apiKey . substr ( apiKey . length - 4 ) } `
796795}
797796
798- /**
799- * for cleaning up prompt and response
800- */
801- function truncateText ( text ) {
802- if ( ! text ) return
803- if ( typeof text !== 'string' || ! text || ( typeof text === 'string' && text . length === 0 ) ) return
804-
805- text = text
806- . replace ( RE_NEWLINE , '\\n' )
807- . replace ( RE_TAB , '\\t' )
808-
809- if ( text . length > MAX_TEXT_LEN ) {
810- return text . substring ( 0 , MAX_TEXT_LEN ) + '...'
811- }
812-
813- return text
814- }
815-
816797function tagChatCompletionRequestContent ( contents , messageIdx , tags ) {
817798 if ( typeof contents === 'string' ) {
818799 tags [ `openai.request.messages.${ messageIdx } .content` ] = contents
@@ -824,10 +805,10 @@ function tagChatCompletionRequestContent (contents, messageIdx, tags) {
824805 const type = content . type
825806 tags [ `openai.request.messages.${ messageIdx } .content.${ contentIdx } .type` ] = content . type
826807 if ( type === 'text' ) {
827- tags [ `openai.request.messages.${ messageIdx } .content.${ contentIdx } .text` ] = truncateText ( content . text )
808+ tags [ `openai.request.messages.${ messageIdx } .content.${ contentIdx } .text` ] = normalize ( content . text )
828809 } else if ( type === 'image_url' ) {
829810 tags [ `openai.request.messages.${ messageIdx } .content.${ contentIdx } .image_url.url` ] =
830- truncateText ( content . image_url . url )
811+ normalize ( content . image_url . url )
831812 }
832813 // unsupported type otherwise, won't be tagged
833814 }
@@ -1004,7 +985,7 @@ function normalizeStringOrTokenArray (input, truncate) {
1004985 const normalized = Array . isArray ( input )
1005986 ? `[${ input . join ( ', ' ) } ]` // "[1, 2, 999]"
1006987 : input // "foo"
1007- return truncate ? truncateText ( normalized ) : normalized
988+ return truncate ? normalize ( normalized ) : normalized
1008989}
1009990
1010991function defensiveArrayLength ( maybeArray ) {
0 commit comments