@carbon-js/sdk 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -1,26 +1,32 @@
|
|
|
1
|
-
# SDK
|
|
1
|
+
# Carbon SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A typescript SDK for capturing and ingesting events from AI application into [Carbon](https://oncarbon.site) for analytics and observability. For more details, see the [Carbon docs](https://docs.oncarbon.site)
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the SDK:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @carbon-js/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
Using Carbon SDK to instrument an OpenAI SDK application:
|
|
6
16
|
|
|
7
17
|
```ts
|
|
18
|
+
import OpenAI from "openai";
|
|
8
19
|
import { Carbon } from "@carbon-js/sdk";
|
|
9
20
|
import { wrapOpenAISdk } from "@carbon-js/sdk/ai";
|
|
10
21
|
|
|
11
|
-
const carbon = new Carbon(
|
|
12
|
-
const openai = wrapOpenAISdk(
|
|
13
|
-
```
|
|
22
|
+
const carbon = new Carbon();
|
|
23
|
+
const openai = wrapOpenAISdk(new OpenAI(), carbon);
|
|
14
24
|
|
|
15
|
-
|
|
25
|
+
const completion = await openai.chat.completions.create({
|
|
26
|
+
model: "gpt-5.4-nano",
|
|
27
|
+
messages: [{ role: "user", content: "What is the capital of France?" }],
|
|
28
|
+
});
|
|
16
29
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
bunx tsc --noEmit -p packages/sdk/tsconfig.json
|
|
20
|
-
bun test packages/sdk/tests
|
|
21
|
-
```
|
|
22
|
-
## Smoke Test
|
|
23
|
-
|
|
24
|
-
```bash
|
|
25
|
-
bun run --cwd packages/sdk example:vercel
|
|
30
|
+
// Flush: only needed in serverless and other short-lived environments.
|
|
31
|
+
await carbon.flushPendingEvents();
|
|
26
32
|
```
|
|
@@ -7,13 +7,17 @@ function createWrappedChatCompletionRunTools(args) {
|
|
|
7
7
|
const runTools = args.client.chat.completions.runTools.bind(args.client.chat.completions);
|
|
8
8
|
return ((body, requestOptions) => {
|
|
9
9
|
const { carbon: carbonObject, ...openAiBody } = body;
|
|
10
|
+
const tracedCarbonObject = {
|
|
11
|
+
...carbonObject,
|
|
12
|
+
traceId: carbonObject?.traceId ?? args.carbon.createTraceId()
|
|
13
|
+
};
|
|
10
14
|
const bodyWithInternalCapture = {
|
|
11
15
|
...openAiBody,
|
|
12
|
-
carbon:
|
|
16
|
+
carbon: tracedCarbonObject,
|
|
13
17
|
[SKIP_CAPTURE_FIELD]: true
|
|
14
18
|
};
|
|
15
19
|
const factory = new OpenAIEventFactory({
|
|
16
|
-
carbonObject
|
|
20
|
+
carbonObject: tracedCarbonObject
|
|
17
21
|
});
|
|
18
22
|
const startTimeMs = Date.now();
|
|
19
23
|
let runner;
|
|
@@ -38,7 +42,7 @@ function createWrappedChatCompletionRunTools(args) {
|
|
|
38
42
|
attachRunToolsCapture({
|
|
39
43
|
body: openAiBody,
|
|
40
44
|
carbon: args.carbon,
|
|
41
|
-
carbonObject,
|
|
45
|
+
carbonObject: tracedCarbonObject,
|
|
42
46
|
factory,
|
|
43
47
|
mode: openAiBody.stream ? "stream" : "generate",
|
|
44
48
|
runner
|