|
1 |
| -import { setTimeout } from 'node:timers/promises'; |
2 |
| - |
| 1 | +import { SpanKind } from '@cozeloop/ai'; |
3 | 2 | import {
|
4 | 3 | COZELOOP_TRACE_BUSINESS_TAGS,
|
5 | 4 | cozeLoopTracer,
|
6 | 5 | type LoopTraceLLMCallInput,
|
7 |
| - type LoopTraceLLMCallOutput, |
8 |
| - SpanKind, |
9 | 6 | } from '@cozeloop/ai';
|
10 | 7 |
|
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'; |
83 | 9 |
|
84 | 10 | export async function runModel() {
|
85 | 11 | // Wrap model invoke function to make it traceable
|
|
0 commit comments