Skip to content

Commit ad63d87

Browse files
committed
feat: report custom pre/post processing
1 parent 018b242 commit ad63d87

File tree

7 files changed

+79
-10
lines changed

7 files changed

+79
-10
lines changed

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ai-semantic-conventions/src/SemanticAttributes.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export const SpanAttributes = {
4040
TRACELOOP_WORKFLOW_NAME: "traceloop.workflow.name",
4141
TRACELOOP_ENTITY_NAME: "traceloop.entity.name",
4242
TRACELOOP_ASSOCIATION_PROPERTIES: "traceloop.association.properties",
43+
44+
// LLM Pre/Post Processing
45+
TRACELOOP_CUSTOM_PROCESSING_STEP_TYPE: "traceloop.processing.type",
46+
TRACELOOP_CUSTOM_PROCESSING_INPUT: "traceloop.processing.input",
47+
TRACELOOP_CUSTOM_PROCESSING_OUTPUT: "traceloop.processing.output",
4348
};
4449

4550
export enum LLMRequestTypeValues {
@@ -56,3 +61,8 @@ export enum TraceloopSpanKindValues {
5661
TOOL = "tool",
5762
UNKNOWN = "unknown",
5863
}
64+
65+
export enum TraceloopCustomProcessingStepTypeValues {
66+
PRE_PROCESSING = "pre_process",
67+
POST_PROCESSING = "post_process",
68+
}

packages/sample-app/src/sample_with.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ async function chat() {
1212
return await traceloop.withWorkflow("sample_chat", async () => {
1313
const chatCompletion = await openai.chat.completions.create({
1414
messages: [
15-
{ role: "user", content: "Tell me a joke about OpenTelemetry" },
15+
{
16+
role: "user",
17+
content:
18+
"Tell me a joke about OpenTelemetry and explain in a separate line",
19+
},
1620
],
1721
model: "gpt-3.5-turbo",
1822
});
1923

20-
return chatCompletion.choices[0].message.content;
24+
const jokeWithExplanation = chatCompletion.choices[0].message.content;
25+
const onlyExplanation = jokeWithExplanation?.split("Explanation:")[1];
26+
27+
traceloop.reportPostProcessing(
28+
jokeWithExplanation || "",
29+
onlyExplanation || "",
30+
);
31+
32+
return onlyExplanation;
2133
});
2234
}
2335

packages/traceloop-sdk/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
"dependencies": {
3535
"@opentelemetry/exporter-trace-otlp-proto": "^0.44.0",
3636
"@opentelemetry/sdk-node": "^0.44.0",
37-
"@traceloop/instrumentation-llamaindex": "^0.0.31",
38-
"@traceloop/instrumentation-openai": "^0.0.31",
37+
"@traceloop/ai-semantic-conventions": "*",
38+
"@traceloop/instrumentation-llamaindex": "*",
39+
"@traceloop/instrumentation-openai": "*",
3940
"@types/nunjucks": "^3.2.5",
4041
"fetch-retry": "^5.0.6",
4142
"nunjucks": "^3.2.4",

packages/traceloop-sdk/src/lib/node-server-sdk.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { initInstrumentations } from "./tracing";
33
export * from "./errors";
44
export { InitializeOptions } from "./interfaces";
55
export { initialize } from "./configuration";
6-
export { forceFlush } from "./tracing";
6+
export {
7+
forceFlush,
8+
reportPreProcessing,
9+
reportPostProcessing,
10+
} from "./tracing";
711
export * from "./tracing/decorators";
812
export * from "./tracing/association";
913
export * from "./tracing/score";

packages/traceloop-sdk/src/lib/tracing/index.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ import { Instrumentation } from "@opentelemetry/instrumentation";
1111
import { InitializeOptions } from "../interfaces";
1212
import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai";
1313
import { LlamaIndexInstrumentation } from "@traceloop/instrumentation-llamaindex";
14-
import { SpanAttributes } from "@traceloop/ai-semantic-conventions";
15-
import { ASSOCATION_PROPERTIES_KEY, WORKFLOW_NAME_KEY } from "./tracing";
14+
import {
15+
SpanAttributes,
16+
TraceloopCustomProcessingStepTypeValues,
17+
} from "@traceloop/ai-semantic-conventions";
18+
import {
19+
ASSOCATION_PROPERTIES_KEY,
20+
WORKFLOW_NAME_KEY,
21+
reportCustomProcessing,
22+
} from "./tracing";
1623
import { Telemetry } from "../telemetry/telemetry";
1724
import { TraceloopSampler } from "./sampler";
1825

@@ -113,3 +120,19 @@ export const startTracing = (options: InitializeOptions) => {
113120
export const forceFlush = async () => {
114121
await _spanProcessor.forceFlush();
115122
};
123+
124+
export const reportPreProcessing = (input: string, output: string) => {
125+
reportCustomProcessing(
126+
TraceloopCustomProcessingStepTypeValues.PRE_PROCESSING,
127+
input,
128+
output,
129+
);
130+
};
131+
132+
export const reportPostProcessing = (input: string, output: string) => {
133+
reportCustomProcessing(
134+
TraceloopCustomProcessingStepTypeValues.POST_PROCESSING,
135+
input,
136+
output,
137+
);
138+
};

packages/traceloop-sdk/src/lib/tracing/tracing.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { trace, createContextKey } from "@opentelemetry/api";
2-
2+
import {
3+
SpanAttributes,
4+
TraceloopCustomProcessingStepTypeValues,
5+
} from "@traceloop/ai-semantic-conventions";
36
const TRACER_NAME = "traceloop.tracer";
47
export const WORKFLOW_NAME_KEY = createContextKey("workflow_name");
58
export const ASSOCATION_PROPERTIES_KEY = createContextKey(
@@ -9,3 +12,18 @@ export const ASSOCATION_PROPERTIES_KEY = createContextKey(
912
export const getTracer = () => {
1013
return trace.getTracer(TRACER_NAME);
1114
};
15+
16+
export const reportCustomProcessing = (
17+
kind: TraceloopCustomProcessingStepTypeValues,
18+
input: string,
19+
output: string,
20+
) => {
21+
const s = getTracer().startSpan(`custom.${kind}`, {
22+
attributes: {
23+
[SpanAttributes.TRACELOOP_CUSTOM_PROCESSING_STEP_TYPE]: kind,
24+
[SpanAttributes.TRACELOOP_CUSTOM_PROCESSING_INPUT]: input,
25+
[SpanAttributes.TRACELOOP_CUSTOM_PROCESSING_OUTPUT]: output,
26+
},
27+
});
28+
s.end();
29+
};

0 commit comments

Comments
 (0)