@langinsight/ai-sdk 0.0.3 → 0.0.4
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 +15 -10
- package/dist/index.cjs +20 -8
- package/dist/index.d.cts +11 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,9 +17,9 @@ const handler = new LangInsight.CallbackHandler({
|
|
|
17
17
|
apiKey: LANGINSIGHT_API_KEY,
|
|
18
18
|
endpoint: LANGINSIGHT_ENDPOINT,
|
|
19
19
|
metadata: { userId: "admin", sessionId: "admin" },
|
|
20
|
-
onSuccess: (
|
|
20
|
+
onSuccess: (input, output) => {
|
|
21
21
|
// traceId の取得・DB永続化はここで
|
|
22
|
-
console.log("Trace sent:",
|
|
22
|
+
console.log("Trace sent:", output.id);
|
|
23
23
|
},
|
|
24
24
|
});
|
|
25
25
|
|
|
@@ -46,7 +46,7 @@ const handler = new LangInsight.CallbackHandler({
|
|
|
46
46
|
apiKey: LANGINSIGHT_API_KEY,
|
|
47
47
|
endpoint: LANGINSIGHT_ENDPOINT,
|
|
48
48
|
metadata: { userId: "admin", sessionId: "admin" },
|
|
49
|
-
onSuccess: (
|
|
49
|
+
onSuccess: (input, output) => console.log("Trace sent:", output.id),
|
|
50
50
|
onFailure: (err) => console.error("Trace failed:", err),
|
|
51
51
|
});
|
|
52
52
|
|
|
@@ -65,21 +65,25 @@ for await (const chunk of result.textStream) {
|
|
|
65
65
|
|
|
66
66
|
### Feedback (score)
|
|
67
67
|
|
|
68
|
-
Trace ID の取得や DB 永続化は `onSuccess` コールバックで受け取った `
|
|
68
|
+
Trace ID の取得や DB 永続化は `onSuccess` コールバックで受け取った `output` を使って行い、その `output.id` を `score()` に渡してフィードバックを送信する。`handler.result` を await することでも取得可能。
|
|
69
69
|
|
|
70
70
|
```typescript
|
|
71
71
|
let lastOutputTraceId: string | undefined;
|
|
72
72
|
|
|
73
73
|
const handler = new LangInsight.CallbackHandler({
|
|
74
74
|
...options,
|
|
75
|
-
onSuccess: (
|
|
76
|
-
lastOutputTraceId =
|
|
75
|
+
onSuccess: (input, output) => {
|
|
76
|
+
lastOutputTraceId = output.id; // DB 永続化などもここで
|
|
77
77
|
},
|
|
78
78
|
});
|
|
79
79
|
|
|
80
80
|
// report() または onFinish の後
|
|
81
81
|
await handler.score({ traceId: lastOutputTraceId!, value: 1 }); // 👍
|
|
82
82
|
await handler.score({ traceId: lastOutputTraceId!, value: -1 }); // 👎
|
|
83
|
+
|
|
84
|
+
// または handler.result を使用
|
|
85
|
+
const { input, output } = await handler.result;
|
|
86
|
+
console.log("Input trace:", input.id, "Output trace:", output.id);
|
|
83
87
|
```
|
|
84
88
|
|
|
85
89
|
## Options
|
|
@@ -93,11 +97,12 @@ await handler.score({ traceId: lastOutputTraceId!, value: -1 }); // 👎
|
|
|
93
97
|
| `metadata.sessionId`| `string` | Yes | Session ID (e.g. conversation id) |
|
|
94
98
|
| `metadata.modelName` | `string` | No | Model name (defaults to result response when not set) |
|
|
95
99
|
| `metadata.*` | `any` | No | Additional key-value pairs |
|
|
96
|
-
| `onSuccess` | `(
|
|
100
|
+
| `onSuccess` | `(input: Trace, output: Trace) => void` | No | Callback invoked with both input and output traces when trace submission succeeds. Use `output.id` to get traceId and persist to DB. |
|
|
97
101
|
| `onFailure` | `(error: Error) => void` | No | Callback invoked with the error when trace submission fails |
|
|
98
102
|
|
|
99
103
|
## API
|
|
100
104
|
|
|
101
|
-
- **`report(result, { input })`** — Sends traces for a `generateText` result.
|
|
102
|
-
- **`onFinish({ input })`** — Returns a function suitable for `streamText
|
|
103
|
-
- **`score({ traceId, value })`** — Submits feedback (1 = thumbs up, -1 = thumbs down) for the given trace. Use the `
|
|
105
|
+
- **`report(result, { input })`** — Sends traces for a `generateText` result. Both input and output traces are passed to `onSuccess` on success.
|
|
106
|
+
- **`onFinish({ input })`** — Returns a function suitable for `streamText`'s `onFinish`. Sends traces when the stream finishes; both traces are passed to `onSuccess`.
|
|
107
|
+
- **`score({ traceId, value })`** — Submits feedback (1 = thumbs up, -1 = thumbs down) for the given trace. Use the `output.id` received in `onSuccess` as `traceId`.
|
|
108
|
+
- **`result`** — A promise that resolves with `{ input: Trace, output: Trace }` when traces are sent successfully.
|
package/dist/index.cjs
CHANGED
|
@@ -321,8 +321,15 @@ var LangInsight;
|
|
|
321
321
|
constructor(options) {
|
|
322
322
|
this.options = options;
|
|
323
323
|
this.client = hc(options.endpoint, { fetch });
|
|
324
|
+
this.result = new Promise((resolve, reject) => {
|
|
325
|
+
this.resolve = resolve;
|
|
326
|
+
this.reject = reject;
|
|
327
|
+
});
|
|
324
328
|
}
|
|
325
329
|
client;
|
|
330
|
+
resolve = null;
|
|
331
|
+
reject = null;
|
|
332
|
+
result;
|
|
326
333
|
/**
|
|
327
334
|
* streamText の onFinish コールバックとして使用
|
|
328
335
|
*
|
|
@@ -353,8 +360,8 @@ var LangInsight;
|
|
|
353
360
|
* ```ts
|
|
354
361
|
* const handler = new LangInsight.CallbackHandler({
|
|
355
362
|
* ...options,
|
|
356
|
-
* onSuccess: (
|
|
357
|
-
* saveTraceToDb(
|
|
363
|
+
* onSuccess: (input, output) => {
|
|
364
|
+
* saveTraceToDb(output); // traceId = output.id
|
|
358
365
|
* },
|
|
359
366
|
* });
|
|
360
367
|
* const result = await generateText({ model: openai("gpt-4o"), prompt: "Hello!" });
|
|
@@ -370,11 +377,11 @@ var LangInsight;
|
|
|
370
377
|
}
|
|
371
378
|
/**
|
|
372
379
|
* フィードバック(👍/👎)を送信
|
|
373
|
-
* traceId は onSuccess コールバックで受け取った
|
|
380
|
+
* traceId は onSuccess コールバックで受け取った output.id を使用する。
|
|
374
381
|
*
|
|
375
382
|
* @example
|
|
376
383
|
* ```ts
|
|
377
|
-
* onSuccess: (
|
|
384
|
+
* onSuccess: (input, output) => { lastOutputTraceId = output.id; },
|
|
378
385
|
* // ...
|
|
379
386
|
* await handler.score({ traceId: lastOutputTraceId, value: 1 }); // 👍
|
|
380
387
|
* ```
|
|
@@ -438,14 +445,19 @@ var LangInsight;
|
|
|
438
445
|
})
|
|
439
446
|
]);
|
|
440
447
|
if (!res.every((r) => r.ok)) {
|
|
441
|
-
|
|
448
|
+
const [a, b] = await Promise.all(res.map((r) => r.text()));
|
|
449
|
+
console.error(a, b);
|
|
442
450
|
throw new Error("Failed to send traces");
|
|
443
451
|
}
|
|
452
|
+
const inputTrace = await res[0].json();
|
|
444
453
|
const outputTrace = await res[1].json();
|
|
445
|
-
this.
|
|
454
|
+
this.resolve?.({ input: inputTrace, output: outputTrace });
|
|
455
|
+
this.options.onSuccess?.(inputTrace, outputTrace);
|
|
446
456
|
} catch (error) {
|
|
447
|
-
|
|
448
|
-
this.
|
|
457
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
458
|
+
this.reject?.(err);
|
|
459
|
+
console.error(err);
|
|
460
|
+
this.options.onFailure?.(err);
|
|
449
461
|
}
|
|
450
462
|
}
|
|
451
463
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -123,12 +123,18 @@ declare namespace LangInsight {
|
|
|
123
123
|
sessionId: string;
|
|
124
124
|
[key: string]: any;
|
|
125
125
|
};
|
|
126
|
-
onSuccess?: (
|
|
126
|
+
onSuccess?: (input: TraceEntity, output: TraceEntity) => void;
|
|
127
127
|
onFailure?: (error: Error) => void;
|
|
128
128
|
}
|
|
129
129
|
class CallbackHandler {
|
|
130
130
|
private readonly options;
|
|
131
131
|
private readonly client;
|
|
132
|
+
private resolve;
|
|
133
|
+
private reject;
|
|
134
|
+
readonly result: Promise<{
|
|
135
|
+
input: TraceEntity;
|
|
136
|
+
output: TraceEntity;
|
|
137
|
+
}>;
|
|
132
138
|
constructor(options: Options);
|
|
133
139
|
/**
|
|
134
140
|
* streamText の onFinish コールバックとして使用
|
|
@@ -158,8 +164,8 @@ declare namespace LangInsight {
|
|
|
158
164
|
* ```ts
|
|
159
165
|
* const handler = new LangInsight.CallbackHandler({
|
|
160
166
|
* ...options,
|
|
161
|
-
* onSuccess: (
|
|
162
|
-
* saveTraceToDb(
|
|
167
|
+
* onSuccess: (input, output) => {
|
|
168
|
+
* saveTraceToDb(output); // traceId = output.id
|
|
163
169
|
* },
|
|
164
170
|
* });
|
|
165
171
|
* const result = await generateText({ model: openai("gpt-4o"), prompt: "Hello!" });
|
|
@@ -171,11 +177,11 @@ declare namespace LangInsight {
|
|
|
171
177
|
}): Promise<void>;
|
|
172
178
|
/**
|
|
173
179
|
* フィードバック(👍/👎)を送信
|
|
174
|
-
* traceId は onSuccess コールバックで受け取った
|
|
180
|
+
* traceId は onSuccess コールバックで受け取った output.id を使用する。
|
|
175
181
|
*
|
|
176
182
|
* @example
|
|
177
183
|
* ```ts
|
|
178
|
-
* onSuccess: (
|
|
184
|
+
* onSuccess: (input, output) => { lastOutputTraceId = output.id; },
|
|
179
185
|
* // ...
|
|
180
186
|
* await handler.score({ traceId: lastOutputTraceId, value: 1 }); // 👍
|
|
181
187
|
* ```
|