@@ -24,47 +24,6 @@ import {
2424 InMemorySpanExporter ,
2525 SimpleSpanProcessor ,
2626} from "@opentelemetry/sdk-trace-node" ;
27- // Minimal transformation function to test LLM_INPUT_MESSAGES and LLM_OUTPUT_MESSAGES
28- const transformToStandardFormat = ( attributes : any ) => {
29- // Transform prompts to LLM_INPUT_MESSAGES
30- const inputMessages = [ ] ;
31- let i = 0 ;
32- while ( attributes [ `${ SpanAttributes . LLM_PROMPTS } .${ i } .role` ] ) {
33- const role = attributes [ `${ SpanAttributes . LLM_PROMPTS } .${ i } .role` ] ;
34- const content = attributes [ `${ SpanAttributes . LLM_PROMPTS } .${ i } .content` ] ;
35- if ( role && content ) {
36- inputMessages . push ( {
37- role,
38- parts : [ { type : "text" , content } ] ,
39- } ) ;
40- }
41- i ++ ;
42- }
43- if ( inputMessages . length > 0 ) {
44- attributes [ SpanAttributes . LLM_INPUT_MESSAGES ] =
45- JSON . stringify ( inputMessages ) ;
46- }
47-
48- // Transform completions to LLM_OUTPUT_MESSAGES
49- const outputMessages = [ ] ;
50- let j = 0 ;
51- while ( attributes [ `${ SpanAttributes . LLM_COMPLETIONS } .${ j } .role` ] ) {
52- const role = attributes [ `${ SpanAttributes . LLM_COMPLETIONS } .${ j } .role` ] ;
53- const content =
54- attributes [ `${ SpanAttributes . LLM_COMPLETIONS } .${ j } .content` ] ;
55- if ( role && content ) {
56- outputMessages . push ( {
57- role,
58- parts : [ { type : "text" , content } ] ,
59- } ) ;
60- }
61- j ++ ;
62- }
63- if ( outputMessages . length > 0 ) {
64- attributes [ SpanAttributes . LLM_OUTPUT_MESSAGES ] =
65- JSON . stringify ( outputMessages ) ;
66- }
67- } ;
6827
6928import type * as OpenAIModule from "openai" ;
7029import { toFile } from "openai" ;
@@ -919,54 +878,4 @@ describe("Test OpenAI instrumentation", async function () {
919878 4160 ,
920879 ) ;
921880 } ) ;
922-
923- it ( "should set LLM_INPUT_MESSAGES and LLM_OUTPUT_MESSAGES attributes for chat completions" , async ( ) => {
924- const result = await openai . chat . completions . create ( {
925- messages : [
926- { role : "user" , content : "Tell me a joke about OpenTelemetry" } ,
927- ] ,
928- model : "gpt-3.5-turbo" ,
929- } ) ;
930-
931- const spans = memoryExporter . getFinishedSpans ( ) ;
932- const completionSpan = spans . find ( ( span ) => span . name === "openai.chat" ) ;
933-
934- assert . ok ( result ) ;
935- assert . ok ( completionSpan ) ;
936-
937- // Apply transformations to create LLM_INPUT_MESSAGES and LLM_OUTPUT_MESSAGES
938- transformToStandardFormat ( completionSpan . attributes ) ;
939-
940- // Verify LLM_INPUT_MESSAGES attribute exists and is valid JSON
941- assert . ok ( completionSpan . attributes [ SpanAttributes . LLM_INPUT_MESSAGES ] ) ;
942- const inputMessages = JSON . parse (
943- completionSpan . attributes [ SpanAttributes . LLM_INPUT_MESSAGES ] as string ,
944- ) ;
945- assert . ok ( Array . isArray ( inputMessages ) ) ;
946- assert . strictEqual ( inputMessages . length , 1 ) ;
947-
948- // Check user message structure
949- assert . strictEqual ( inputMessages [ 0 ] . role , "user" ) ;
950- assert . ok ( Array . isArray ( inputMessages [ 0 ] . parts ) ) ;
951- assert . strictEqual ( inputMessages [ 0 ] . parts [ 0 ] . type , "text" ) ;
952- assert . strictEqual (
953- inputMessages [ 0 ] . parts [ 0 ] . content ,
954- "Tell me a joke about OpenTelemetry" ,
955- ) ;
956-
957- // Verify LLM_OUTPUT_MESSAGES attribute exists and is valid JSON
958- assert . ok ( completionSpan . attributes [ SpanAttributes . LLM_OUTPUT_MESSAGES ] ) ;
959- const outputMessages = JSON . parse (
960- completionSpan . attributes [ SpanAttributes . LLM_OUTPUT_MESSAGES ] as string ,
961- ) ;
962- assert . ok ( Array . isArray ( outputMessages ) ) ;
963- assert . strictEqual ( outputMessages . length , 1 ) ;
964-
965- // Check assistant response structure
966- assert . strictEqual ( outputMessages [ 0 ] . role , "assistant" ) ;
967- assert . ok ( Array . isArray ( outputMessages [ 0 ] . parts ) ) ;
968- assert . strictEqual ( outputMessages [ 0 ] . parts [ 0 ] . type , "text" ) ;
969- assert . ok ( outputMessages [ 0 ] . parts [ 0 ] . content ) ;
970- assert . ok ( typeof outputMessages [ 0 ] . parts [ 0 ] . content === "string" ) ;
971- } ) ;
972881} ) ;
0 commit comments