Skip to content

Commit 541f53d

Browse files
authored
Feat/tracer update (#3)
* chore(@cozeloop/ai): adjust code details * feat(@cozeloop/ai): support reporting the baggage field * chore(@cozeloop/ai): trace demo
1 parent 4e5dc31 commit 541f53d

File tree

17 files changed

+266
-118
lines changed

17 files changed

+266
-118
lines changed

examples/cozeloop-ai-node/src/api/api-client.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export async function run() {
2323
// axiosOptions: {},
2424
/** Custom headers */
2525
// headers: {};
26-
headers: { 'x-tt-env': 'boe_commercial' }, // TODO: remove
2726
});
2827

2928
const resp = await apiClient.post<{ code: number }>('/v1/loop/prompts/mget', {

examples/cozeloop-ai-node/src/prompt/hub.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export async function run() {
99
apiClient: {
1010
// baseURL: 'api_base_url',
1111
// token: 'your_api_token',
12-
headers: { 'x-tt-env': 'boe_commercial' }, // TODO: remove
1312
},
1413
});
1514

examples/cozeloop-ai-node/src/tracer/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ loopTracer.initialize({
1717
apiClient: {
1818
// baseURL: 'https://api.coze.cn',
1919
// token: 'your_api_token',
20-
headers: { 'x-tt-env': 'boe_commercial' }, // TODO: remove
2120
},
2221
/** Allow ultra long text report */
2322
// ultraLargeReport: true,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { cozeLoopTracer } from '@cozeloop/ai';
2+
3+
import { doSomething } from './utils';
4+
5+
export async function runCustom() {
6+
// Wrap any function to make it traceable
7+
await cozeLoopTracer.traceable(
8+
async parentSpan => {
9+
// Manually set input
10+
cozeLoopTracer.setInput(parentSpan, 'xxx');
11+
12+
// Invoke any function, if it throws error, error will be caught and automatically set span as error
13+
const result = await doSomething();
14+
15+
// Or, you can manually set error
16+
cozeLoopTracer.setError(parentSpan, 'custom error message');
17+
18+
// You can also trace nested span, the parent-child relationship of span will be automatically concatenated
19+
await cozeLoopTracer.traceable(
20+
async childSpan => {
21+
// Set custom tags
22+
childSpan.setAttribute('custom-tag', 'xxx');
23+
24+
await doSomething();
25+
},
26+
{
27+
name: 'TestCustomChildSpan',
28+
type: 'MyCustomType',
29+
},
30+
);
31+
32+
// Automatically set return value as output
33+
return result;
34+
},
35+
{
36+
name: 'TestCustomParentSpan',
37+
type: 'MyCustomType',
38+
},
39+
);
40+
}

examples/cozeloop-ai-node/src/tracer/index.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import { cozeLoopTracer } from '@cozeloop/ai';
22

3-
import { runModel, runRoot, runCustom } from './simple';
3+
import { runRoot } from './root';
44
import { runMultiModality } from './multi-modality';
5+
import { runModel } from './llm';
56
import { runLargeText } from './large-text';
6-
7-
// initialize tracer globally
8-
cozeLoopTracer.initialize({
9-
/** workspace id, use process.env.COZELOOP_WORKSPACE_ID when unprovided */
10-
// workspaceId: 'your_workspace_id',
11-
apiClient: {
12-
// baseURL: 'https://api.coze.cn',
13-
// token: 'your_api_token',
14-
headers: { 'x-tt-env': 'boe_commercial' }, // TODO: remove
15-
},
16-
/** Allow ultra long text report */
17-
ultraLargeReport: true,
18-
});
7+
import { runCustom } from './basic';
198

209
export async function run() {
10+
// initialize tracer globally
11+
cozeLoopTracer.initialize({
12+
/** workspace id, use process.env.COZELOOP_WORKSPACE_ID when unprovided */
13+
// workspaceId: 'your_workspace_id',
14+
apiClient: {
15+
// baseURL: 'https://api.coze.cn',
16+
// token: 'your_api_token',
17+
},
18+
/** Allow ultra long text report */
19+
ultraLargeReport: true,
20+
});
21+
2122
await Promise.all([
2223
runRoot(),
2324
runCustom(),

examples/cozeloop-ai-node/src/tracer/large-text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function runLargeText() {
2121
await cozeLoopTracer.traceable(
2222
async span => {
2323
// Reporting of ultra-long texts will only take effect when the
24-
// input satisfies the LoopTraceLLMCallInput structure
24+
// input / output satisfies the LoopTraceLLMCallInput / LoopTraceLLMCallOutput structure
2525
const input: LoopTraceLLMCallInput = {
2626
messages: [
2727
{

examples/cozeloop-ai-node/src/tracer/simple.ts renamed to examples/cozeloop-ai-node/src/tracer/llm.ts

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,11 @@
1-
import { setTimeout } from 'node:timers/promises';
2-
1+
import { SpanKind } from '@cozeloop/ai';
32
import {
43
COZELOOP_TRACE_BUSINESS_TAGS,
54
cozeLoopTracer,
65
type LoopTraceLLMCallInput,
7-
type LoopTraceLLMCallOutput,
8-
SpanKind,
96
} from '@cozeloop/ai';
107

11-
async function doSomething() {
12-
await setTimeout(2000, 'result');
13-
}
14-
15-
async function fakeLLMCall(): Promise<LoopTraceLLMCallOutput> {
16-
return await setTimeout(2000, {
17-
choices: [
18-
{
19-
index: 0,
20-
finish_reason: 'stop',
21-
message: {
22-
role: 'assistant',
23-
content: "hi, I'm xx model",
24-
},
25-
},
26-
],
27-
});
28-
}
29-
30-
export async function runRoot() {
31-
// We recommend concatenating a complete user request into a trace,
32-
// so the recommended approach is to report a root span at the entrance of the entire execution
33-
await cozeLoopTracer.traceable(
34-
async () => {
35-
// execute your method
36-
const result = await doSomething();
37-
38-
return result;
39-
},
40-
{
41-
name: 'TestRootSpan',
42-
type: 'RootSpanType',
43-
},
44-
);
45-
}
46-
47-
export async function runCustom() {
48-
// Wrap any function to make it traceable
49-
await cozeLoopTracer.traceable(
50-
async parentSpan => {
51-
// Manually set input
52-
cozeLoopTracer.setInput(parentSpan, 'xxx');
53-
54-
// Invoke any function, if it throws error, error will be caught and automatically set span as error
55-
const result = await doSomething();
56-
57-
// Or, you can manually set error
58-
cozeLoopTracer.setError(parentSpan, 'custom error message');
59-
60-
// You can also trace nested span, the parent-child relationship of span will be automatically concatenated
61-
await cozeLoopTracer.traceable(
62-
async childSpan => {
63-
// Set custom tags
64-
childSpan.setAttribute('custom-tag', 'xxx');
65-
66-
await doSomething();
67-
},
68-
{
69-
name: 'TestCustomChildSpan',
70-
type: 'MyCustomType',
71-
},
72-
);
73-
74-
// Automatically set return value as output
75-
return result;
76-
},
77-
{
78-
name: 'TestCustomParentSpan',
79-
type: 'MyCustomType',
80-
},
81-
);
82-
}
8+
import { fakeLLMCall } from './utils';
839

8410
export async function runModel() {
8511
// Wrap model invoke function to make it traceable

examples/cozeloop-ai-node/src/tracer/multi-modality.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function runMultiModality() {
1414
await cozeLoopTracer.traceable(
1515
async span => {
1616
// Reporting of multi modality will only take effect when the
17-
// input satisfies the LoopTraceLLMCallInput structure
17+
// input / output satisfies the LoopTraceLLMCallInput / LoopTraceLLMCallOutput structure
1818
const input: LoopTraceLLMCallInput = {
1919
messages: [
2020
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { cozeLoopTracer } from '@cozeloop/ai';
2+
3+
import { doSomething } from './utils';
4+
5+
export async function runRoot() {
6+
// We recommend concatenating a complete user request into a trace,
7+
// so the recommended approach is to report a root span at the entrance of the entire execution
8+
await cozeLoopTracer.traceable(
9+
async () => {
10+
// execute your method
11+
const result = await doSomething();
12+
13+
return result;
14+
},
15+
{
16+
name: 'TestRootSpan',
17+
type: 'RootSpanType',
18+
// you can set your own baggage fields (eg. userId),
19+
// these fields will be automatically passed through and set in all sub-spans
20+
userId: 'uid-123',
21+
messageId: 'msg-123',
22+
threadId: 'thread-123',
23+
},
24+
);
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { setTimeout } from 'node:timers/promises';
2+
3+
import { type LoopTraceLLMCallOutput } from '@cozeloop/ai';
4+
5+
export async function doSomething() {
6+
await setTimeout(2000, 'result');
7+
}
8+
9+
export async function fakeLLMCall(): Promise<LoopTraceLLMCallOutput> {
10+
return await setTimeout(2000, {
11+
choices: [
12+
{
13+
index: 0,
14+
finish_reason: 'stop',
15+
message: {
16+
role: 'assistant',
17+
content: "hi, I'm xx model",
18+
},
19+
},
20+
],
21+
});
22+
}

0 commit comments

Comments
 (0)