@posthog/ai 5.2.0 → 5.2.2
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/CHANGELOG.md +8 -0
- package/lib/index.cjs +28 -13
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.ts +23 -19
- package/lib/index.mjs +24 -8
- package/lib/index.mjs.map +1 -1
- package/lib/langchain/index.cjs +16 -4
- package/lib/langchain/index.cjs.map +1 -1
- package/lib/langchain/index.d.ts +2 -1
- package/lib/langchain/index.mjs +16 -4
- package/lib/langchain/index.mjs.map +1 -1
- package/lib/openai/index.cjs +8 -9
- package/lib/openai/index.cjs.map +1 -1
- package/lib/openai/index.d.ts +21 -18
- package/lib/openai/index.mjs +8 -5
- package/lib/openai/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/langchain/callbacks.ts +22 -4
- package/src/openai/index.ts +8 -4
- package/tests/callbacks.test.ts +48 -0
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import OpenAIOrignal, { APIPromise, ClientOptions as ClientOptions$1, AzureOpenAI } from 'openai';
|
|
1
|
+
import OpenAIOrignal, { OpenAI, APIPromise, ClientOptions as ClientOptions$1, AzureOpenAI } from 'openai';
|
|
2
2
|
import { PostHog } from 'posthog-node';
|
|
3
3
|
import { Stream } from 'openai/streaming';
|
|
4
4
|
import { ParsedResponse } from 'openai/resources/responses/responses';
|
|
@@ -10,10 +10,10 @@ import { GoogleGenAI } from '@google/genai';
|
|
|
10
10
|
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
|
|
11
11
|
import { Serialized } from '@langchain/core/load/serializable';
|
|
12
12
|
import { ChainValues } from '@langchain/core/utils/types';
|
|
13
|
-
import { BaseMessage } from '@langchain/core/messages';
|
|
14
13
|
import { LLMResult } from '@langchain/core/outputs';
|
|
15
14
|
import { AgentAction, AgentFinish } from '@langchain/core/agents';
|
|
16
15
|
import { DocumentInterface } from '@langchain/core/documents';
|
|
16
|
+
import { BaseMessage } from '@langchain/core/messages';
|
|
17
17
|
|
|
18
18
|
interface MonitoringParams {
|
|
19
19
|
posthogDistinctId?: string;
|
|
@@ -31,43 +31,46 @@ interface CostOverride {
|
|
|
31
31
|
outputCost: number;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
type
|
|
38
|
-
type
|
|
39
|
-
type
|
|
40
|
-
type
|
|
41
|
-
type
|
|
34
|
+
declare const Chat: typeof OpenAI.Chat;
|
|
35
|
+
declare const Completions: typeof OpenAI.Chat.Completions;
|
|
36
|
+
declare const Responses: typeof OpenAI.Responses;
|
|
37
|
+
type ChatCompletion$1 = OpenAI.ChatCompletion;
|
|
38
|
+
type ChatCompletionChunk$1 = OpenAI.ChatCompletionChunk;
|
|
39
|
+
type ChatCompletionCreateParamsBase$1 = OpenAI.Chat.Completions.ChatCompletionCreateParams;
|
|
40
|
+
type ChatCompletionCreateParamsNonStreaming$1 = OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming;
|
|
41
|
+
type ChatCompletionCreateParamsStreaming$1 = OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming;
|
|
42
|
+
type ResponsesCreateParamsBase = OpenAI.Responses.ResponseCreateParams;
|
|
43
|
+
type ResponsesCreateParamsNonStreaming = OpenAI.Responses.ResponseCreateParamsNonStreaming;
|
|
44
|
+
type ResponsesCreateParamsStreaming = OpenAI.Responses.ResponseCreateParamsStreaming;
|
|
42
45
|
interface MonitoringOpenAIConfig$1 extends ClientOptions$1 {
|
|
43
46
|
apiKey: string;
|
|
44
47
|
posthog: PostHog;
|
|
45
48
|
baseURL?: string;
|
|
46
49
|
}
|
|
47
50
|
type RequestOptions$1 = Record<string, any>;
|
|
48
|
-
declare class PostHogOpenAI extends
|
|
51
|
+
declare class PostHogOpenAI extends OpenAI {
|
|
49
52
|
private readonly phClient;
|
|
50
53
|
chat: WrappedChat$1;
|
|
51
54
|
responses: WrappedResponses;
|
|
52
55
|
constructor(config: MonitoringOpenAIConfig$1);
|
|
53
56
|
}
|
|
54
|
-
declare class WrappedChat$1 extends
|
|
57
|
+
declare class WrappedChat$1 extends Chat {
|
|
55
58
|
constructor(parentClient: PostHogOpenAI, phClient: PostHog);
|
|
56
59
|
completions: WrappedCompletions$1;
|
|
57
60
|
}
|
|
58
|
-
declare class WrappedCompletions$1 extends
|
|
61
|
+
declare class WrappedCompletions$1 extends Completions {
|
|
59
62
|
private readonly phClient;
|
|
60
|
-
constructor(client:
|
|
63
|
+
constructor(client: OpenAI, phClient: PostHog);
|
|
61
64
|
create(body: ChatCompletionCreateParamsNonStreaming$1 & MonitoringParams, options?: RequestOptions$1): APIPromise<ChatCompletion$1>;
|
|
62
65
|
create(body: ChatCompletionCreateParamsStreaming$1 & MonitoringParams, options?: RequestOptions$1): APIPromise<Stream<ChatCompletionChunk$1>>;
|
|
63
66
|
create(body: ChatCompletionCreateParamsBase$1 & MonitoringParams, options?: RequestOptions$1): APIPromise<ChatCompletion$1 | Stream<ChatCompletionChunk$1>>;
|
|
64
67
|
}
|
|
65
|
-
declare class WrappedResponses extends
|
|
68
|
+
declare class WrappedResponses extends Responses {
|
|
66
69
|
private readonly phClient;
|
|
67
|
-
constructor(client:
|
|
68
|
-
create(body: ResponsesCreateParamsNonStreaming & MonitoringParams, options?: RequestOptions$1): APIPromise<
|
|
69
|
-
create(body: ResponsesCreateParamsStreaming & MonitoringParams, options?: RequestOptions$1): APIPromise<Stream<
|
|
70
|
-
create(body: ResponsesCreateParamsBase & MonitoringParams, options?: RequestOptions$1): APIPromise<
|
|
70
|
+
constructor(client: OpenAI, phClient: PostHog);
|
|
71
|
+
create(body: ResponsesCreateParamsNonStreaming & MonitoringParams, options?: RequestOptions$1): APIPromise<OpenAI.Responses.Response>;
|
|
72
|
+
create(body: ResponsesCreateParamsStreaming & MonitoringParams, options?: RequestOptions$1): APIPromise<Stream<OpenAI.Responses.ResponseStreamEvent>>;
|
|
73
|
+
create(body: ResponsesCreateParamsBase & MonitoringParams, options?: RequestOptions$1): APIPromise<OpenAI.Responses.Response | Stream<OpenAI.Responses.ResponseStreamEvent>>;
|
|
71
74
|
parse<Params extends ResponsesCreateParamsBase, ParsedT = any>(body: Params & MonitoringParams, options?: RequestOptions$1): APIPromise<ParsedResponse<ParsedT>>;
|
|
72
75
|
}
|
|
73
76
|
|
|
@@ -229,6 +232,7 @@ declare class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
229
232
|
private _captureGeneration;
|
|
230
233
|
private _logDebugEvent;
|
|
231
234
|
private _getLangchainRunName;
|
|
235
|
+
private _convertLcToolCallsToOai;
|
|
232
236
|
private _convertMessageToDict;
|
|
233
237
|
private _parseUsageModel;
|
|
234
238
|
private parseUsage;
|
package/lib/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { OpenAI, AzureOpenAI } from 'openai';
|
|
2
2
|
import * as uuid from 'uuid';
|
|
3
3
|
import { v4 } from 'uuid';
|
|
4
4
|
import { Buffer } from 'buffer';
|
|
@@ -181,7 +181,10 @@ const sendEventToPosthog = async ({
|
|
|
181
181
|
}
|
|
182
182
|
};
|
|
183
183
|
|
|
184
|
-
|
|
184
|
+
const Chat = OpenAI.Chat;
|
|
185
|
+
const Completions = Chat.Completions;
|
|
186
|
+
const Responses = OpenAI.Responses;
|
|
187
|
+
class PostHogOpenAI extends OpenAI {
|
|
185
188
|
constructor(config) {
|
|
186
189
|
const {
|
|
187
190
|
posthog,
|
|
@@ -193,13 +196,13 @@ class PostHogOpenAI extends OpenAIOrignal {
|
|
|
193
196
|
this.responses = new WrappedResponses$1(this, this.phClient);
|
|
194
197
|
}
|
|
195
198
|
}
|
|
196
|
-
class WrappedChat$1 extends
|
|
199
|
+
class WrappedChat$1 extends Chat {
|
|
197
200
|
constructor(parentClient, phClient) {
|
|
198
201
|
super(parentClient);
|
|
199
202
|
this.completions = new WrappedCompletions$1(parentClient, phClient);
|
|
200
203
|
}
|
|
201
204
|
}
|
|
202
|
-
class WrappedCompletions$1 extends
|
|
205
|
+
class WrappedCompletions$1 extends Completions {
|
|
203
206
|
constructor(client, phClient) {
|
|
204
207
|
super(client);
|
|
205
208
|
this.phClient = phClient;
|
|
@@ -342,7 +345,7 @@ class WrappedCompletions$1 extends OpenAIOrignal.Chat.Completions {
|
|
|
342
345
|
}
|
|
343
346
|
}
|
|
344
347
|
}
|
|
345
|
-
class WrappedResponses$1 extends
|
|
348
|
+
class WrappedResponses$1 extends Responses {
|
|
346
349
|
constructor(client, phClient) {
|
|
347
350
|
super(client);
|
|
348
351
|
this.phClient = phClient;
|
|
@@ -2481,10 +2484,19 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2481
2484
|
}
|
|
2482
2485
|
return undefined;
|
|
2483
2486
|
}
|
|
2487
|
+
_convertLcToolCallsToOai(toolCalls) {
|
|
2488
|
+
return toolCalls.map(toolCall => ({
|
|
2489
|
+
type: 'function',
|
|
2490
|
+
id: toolCall.id,
|
|
2491
|
+
function: {
|
|
2492
|
+
name: toolCall.name,
|
|
2493
|
+
arguments: JSON.stringify(toolCall.args)
|
|
2494
|
+
}
|
|
2495
|
+
}));
|
|
2496
|
+
}
|
|
2484
2497
|
_convertMessageToDict(message) {
|
|
2485
2498
|
let messageDict = {};
|
|
2486
|
-
|
|
2487
|
-
const messageType = message._getType?.() || message.type;
|
|
2499
|
+
const messageType = message.getType();
|
|
2488
2500
|
switch (messageType) {
|
|
2489
2501
|
case 'human':
|
|
2490
2502
|
messageDict = {
|
|
@@ -2497,6 +2509,9 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2497
2509
|
role: 'assistant',
|
|
2498
2510
|
content: message.content
|
|
2499
2511
|
};
|
|
2512
|
+
if (message.tool_calls) {
|
|
2513
|
+
messageDict.tool_calls = this._convertLcToolCallsToOai(message.tool_calls);
|
|
2514
|
+
}
|
|
2500
2515
|
break;
|
|
2501
2516
|
case 'system':
|
|
2502
2517
|
messageDict = {
|
|
@@ -2518,9 +2533,10 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2518
2533
|
break;
|
|
2519
2534
|
default:
|
|
2520
2535
|
messageDict = {
|
|
2521
|
-
role: messageType
|
|
2536
|
+
role: messageType,
|
|
2522
2537
|
content: String(message.content)
|
|
2523
2538
|
};
|
|
2539
|
+
break;
|
|
2524
2540
|
}
|
|
2525
2541
|
if (message.additional_kwargs) {
|
|
2526
2542
|
messageDict = {
|