Skip to content

Commit 5cfd993

Browse files
committed
move test file
1 parent ae63671 commit 5cfd993

File tree

3 files changed

+56
-91
lines changed

3 files changed

+56
-91
lines changed

packages/instrumentation-openai/test/instrumentation.test.ts

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6928
import type * as OpenAIModule from "openai";
7029
import { 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
});

packages/instrumentation-openai/test/recordings/Test-OpenAI-instrumentation_1770406427/should-set-LLM_INPUT_MESSAGES-and-LLM_OUTPUT_MESSAGES-attributes-for-chat-completions_99541399/recording.har renamed to packages/traceloop-sdk/recordings/AI-SDK-Transformations_1770406427/should-set-LLM_INPUT_MESSAGES-and-LLM_OUTPUT_MESSAGES-attributes-for-chat-completions_99541399/recording.har

File renamed without changes.

packages/traceloop-sdk/test/ai-sdk-integration.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as assert from "assert";
1919
import { openai as vercel_openai } from "@ai-sdk/openai";
2020
import { google as vercel_google } from "@ai-sdk/google";
2121
import { generateText } from "ai";
22+
import { SpanAttributes } from "@traceloop/ai-semantic-conventions";
2223

2324
import * as traceloop from "../src";
2425

@@ -216,4 +217,59 @@ describe("Test AI SDK Integration with Recording", function () {
216217
assert.ok(generateTextSpan.attributes["gen_ai.usage.completion_tokens"]);
217218
assert.ok(generateTextSpan.attributes["llm.usage.total_tokens"]);
218219
});
220+
221+
it("should set LLM_INPUT_MESSAGES and LLM_OUTPUT_MESSAGES attributes for chat completions", async () => {
222+
const result = await traceloop.withWorkflow(
223+
{ name: "test_transformations_workflow" },
224+
async () => {
225+
return await generateText({
226+
messages: [
227+
{ role: "user", content: "Tell me a joke about OpenTelemetry" },
228+
],
229+
model: vercel_openai("gpt-3.5-turbo"),
230+
experimental_telemetry: { isEnabled: true },
231+
});
232+
},
233+
);
234+
235+
assert.ok(result);
236+
assert.ok(result.text);
237+
238+
const spans = memoryExporter.getFinishedSpans();
239+
const aiSdkSpan = spans.find((span) => span.name.startsWith("ai.generateText"));
240+
241+
assert.ok(aiSdkSpan);
242+
243+
// Verify LLM_INPUT_MESSAGES attribute exists and is valid JSON
244+
assert.ok(aiSdkSpan.attributes[SpanAttributes.LLM_INPUT_MESSAGES]);
245+
const inputMessages = JSON.parse(
246+
aiSdkSpan.attributes[SpanAttributes.LLM_INPUT_MESSAGES] as string,
247+
);
248+
assert.ok(Array.isArray(inputMessages));
249+
assert.strictEqual(inputMessages.length, 1);
250+
251+
// Check user message structure
252+
assert.strictEqual(inputMessages[0].role, "user");
253+
assert.ok(Array.isArray(inputMessages[0].parts));
254+
assert.strictEqual(inputMessages[0].parts[0].type, "text");
255+
assert.strictEqual(
256+
inputMessages[0].parts[0].content,
257+
"Tell me a joke about OpenTelemetry",
258+
);
259+
260+
// Verify LLM_OUTPUT_MESSAGES attribute exists and is valid JSON
261+
assert.ok(aiSdkSpan.attributes[SpanAttributes.LLM_OUTPUT_MESSAGES]);
262+
const outputMessages = JSON.parse(
263+
aiSdkSpan.attributes[SpanAttributes.LLM_OUTPUT_MESSAGES] as string,
264+
);
265+
assert.ok(Array.isArray(outputMessages));
266+
assert.strictEqual(outputMessages.length, 1);
267+
268+
// Check assistant response structure
269+
assert.strictEqual(outputMessages[0].role, "assistant");
270+
assert.ok(Array.isArray(outputMessages[0].parts));
271+
assert.strictEqual(outputMessages[0].parts[0].type, "text");
272+
assert.ok(outputMessages[0].parts[0].content);
273+
assert.ok(typeof outputMessages[0].parts[0].content === "string");
274+
});
219275
});

0 commit comments

Comments
 (0)