@posthog/ai 1.0.1 → 1.2.0
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/package.json +1 -1
- package/src/index.ts +0 -2
- package/src/azure-openai/index.ts +0 -204
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import PostHogOpenAI from './openai'
|
|
2
|
-
import PostHogAzureOpenAI from './azure-openai'
|
|
3
2
|
import { wrapVercelLanguageModel } from './vercel/middleware'
|
|
4
3
|
|
|
5
4
|
export { PostHogOpenAI as OpenAI }
|
|
6
|
-
export { PostHogAzureOpenAI as AzureOpenAI }
|
|
7
5
|
export { wrapVercelLanguageModel as posthogWrappedLanguageModel }
|
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import OpenAIOrignal, { AzureOpenAI } from 'openai'
|
|
2
|
-
import { PostHog } from 'posthog-node'
|
|
3
|
-
import { v4 as uuidv4 } from 'uuid'
|
|
4
|
-
import { PassThrough } from 'stream'
|
|
5
|
-
import { mergeSystemPrompt, type MonitoringParams, sendEventToPosthog } from '../utils'
|
|
6
|
-
|
|
7
|
-
type ChatCompletion = OpenAIOrignal.ChatCompletion
|
|
8
|
-
type ChatCompletionChunk = OpenAIOrignal.ChatCompletionChunk
|
|
9
|
-
type ChatCompletionCreateParamsBase = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParams
|
|
10
|
-
type ChatCompletionCreateParamsNonStreaming = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParamsNonStreaming
|
|
11
|
-
type ChatCompletionCreateParamsStreaming = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParamsStreaming
|
|
12
|
-
import type { APIPromise, RequestOptions } from 'openai/core'
|
|
13
|
-
import type { Stream } from 'openai/streaming'
|
|
14
|
-
|
|
15
|
-
interface MonitoringOpenAIConfig {
|
|
16
|
-
apiKey: string
|
|
17
|
-
posthog: PostHog
|
|
18
|
-
baseURL?: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class PostHogAzureOpenAI extends AzureOpenAI {
|
|
22
|
-
private readonly phClient: PostHog
|
|
23
|
-
|
|
24
|
-
constructor(config: MonitoringOpenAIConfig) {
|
|
25
|
-
const { posthog, ...openAIConfig } = config
|
|
26
|
-
super(openAIConfig)
|
|
27
|
-
this.phClient = posthog
|
|
28
|
-
this.chat = new WrappedChat(this, this.phClient)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public chat: WrappedChat
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export class WrappedChat extends AzureOpenAI.Chat {
|
|
35
|
-
constructor(parentClient: PostHogAzureOpenAI, phClient: PostHog) {
|
|
36
|
-
super(parentClient)
|
|
37
|
-
this.completions = new WrappedCompletions(parentClient, phClient)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public completions: WrappedCompletions
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
44
|
-
private readonly phClient: PostHog
|
|
45
|
-
|
|
46
|
-
constructor(client: AzureOpenAI, phClient: PostHog) {
|
|
47
|
-
super(client)
|
|
48
|
-
this.phClient = phClient
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// --- Overload #1: Non-streaming
|
|
52
|
-
public create(
|
|
53
|
-
body: ChatCompletionCreateParamsNonStreaming & MonitoringParams,
|
|
54
|
-
options?: RequestOptions
|
|
55
|
-
): APIPromise<ChatCompletion>
|
|
56
|
-
|
|
57
|
-
// --- Overload #2: Streaming
|
|
58
|
-
public create(
|
|
59
|
-
body: ChatCompletionCreateParamsStreaming & MonitoringParams,
|
|
60
|
-
options?: RequestOptions
|
|
61
|
-
): APIPromise<Stream<ChatCompletionChunk>>
|
|
62
|
-
|
|
63
|
-
// --- Overload #3: Generic base
|
|
64
|
-
public create(
|
|
65
|
-
body: ChatCompletionCreateParamsBase & MonitoringParams,
|
|
66
|
-
options?: RequestOptions
|
|
67
|
-
): APIPromise<ChatCompletion | Stream<ChatCompletionChunk>>
|
|
68
|
-
|
|
69
|
-
// --- Implementation Signature
|
|
70
|
-
public create(
|
|
71
|
-
body: ChatCompletionCreateParamsBase & MonitoringParams,
|
|
72
|
-
options?: RequestOptions
|
|
73
|
-
): APIPromise<ChatCompletion | Stream<ChatCompletionChunk>> {
|
|
74
|
-
const {
|
|
75
|
-
posthog_distinct_id,
|
|
76
|
-
posthog_trace_id,
|
|
77
|
-
posthog_properties,
|
|
78
|
-
posthog_privacy_mode = false,
|
|
79
|
-
posthog_groups,
|
|
80
|
-
...openAIParams
|
|
81
|
-
} = body
|
|
82
|
-
|
|
83
|
-
const traceId = posthog_trace_id ?? uuidv4()
|
|
84
|
-
const startTime = Date.now()
|
|
85
|
-
|
|
86
|
-
const parentPromise = super.create(openAIParams, options)
|
|
87
|
-
|
|
88
|
-
if (openAIParams.stream) {
|
|
89
|
-
return parentPromise.then((value) => {
|
|
90
|
-
const passThroughStream = new PassThrough({ objectMode: true })
|
|
91
|
-
let accumulatedContent = ''
|
|
92
|
-
let usage: { input_tokens: number; output_tokens: number } = {
|
|
93
|
-
input_tokens: 0,
|
|
94
|
-
output_tokens: 0,
|
|
95
|
-
}
|
|
96
|
-
if ('tee' in value) {
|
|
97
|
-
const openAIStream = value
|
|
98
|
-
;(async () => {
|
|
99
|
-
try {
|
|
100
|
-
for await (const chunk of openAIStream) {
|
|
101
|
-
const delta = chunk?.choices?.[0]?.delta?.content ?? ''
|
|
102
|
-
accumulatedContent += delta
|
|
103
|
-
if (chunk.usage) {
|
|
104
|
-
usage = {
|
|
105
|
-
input_tokens: chunk.usage.prompt_tokens ?? 0,
|
|
106
|
-
output_tokens: chunk.usage.completion_tokens ?? 0,
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
passThroughStream.write(chunk)
|
|
110
|
-
}
|
|
111
|
-
const latency = (Date.now() - startTime) / 1000
|
|
112
|
-
sendEventToPosthog({
|
|
113
|
-
client: this.phClient,
|
|
114
|
-
distinctId: posthog_distinct_id ?? traceId,
|
|
115
|
-
traceId,
|
|
116
|
-
model: openAIParams.model,
|
|
117
|
-
provider: 'azure',
|
|
118
|
-
input: posthog_privacy_mode ? '' : mergeSystemPrompt(openAIParams, 'openai'),
|
|
119
|
-
output: [{ content: accumulatedContent, role: 'assistant' }],
|
|
120
|
-
latency,
|
|
121
|
-
baseURL: (this as any).baseURL ?? '',
|
|
122
|
-
params: body,
|
|
123
|
-
httpStatus: 200,
|
|
124
|
-
usage,
|
|
125
|
-
})
|
|
126
|
-
passThroughStream.end()
|
|
127
|
-
} catch (error) {
|
|
128
|
-
// error handling
|
|
129
|
-
sendEventToPosthog({
|
|
130
|
-
client: this.phClient,
|
|
131
|
-
distinctId: posthog_distinct_id ?? traceId,
|
|
132
|
-
traceId,
|
|
133
|
-
model: openAIParams.model,
|
|
134
|
-
provider: 'azure',
|
|
135
|
-
input: posthog_privacy_mode ? '' : mergeSystemPrompt(openAIParams, 'openai'),
|
|
136
|
-
output: [],
|
|
137
|
-
latency: 0,
|
|
138
|
-
baseURL: (this as any).baseURL ?? '',
|
|
139
|
-
params: body,
|
|
140
|
-
httpStatus: 500,
|
|
141
|
-
usage: {
|
|
142
|
-
input_tokens: 0,
|
|
143
|
-
output_tokens: 0,
|
|
144
|
-
},
|
|
145
|
-
})
|
|
146
|
-
passThroughStream.emit('error', error)
|
|
147
|
-
}
|
|
148
|
-
})()
|
|
149
|
-
}
|
|
150
|
-
return passThroughStream as unknown as Stream<ChatCompletionChunk>
|
|
151
|
-
}) as APIPromise<Stream<ChatCompletionChunk>>
|
|
152
|
-
} else {
|
|
153
|
-
const wrappedPromise = parentPromise.then(
|
|
154
|
-
(result) => {
|
|
155
|
-
if ('choices' in result) {
|
|
156
|
-
const latency = (Date.now() - startTime) / 1000
|
|
157
|
-
sendEventToPosthog({
|
|
158
|
-
client: this.phClient,
|
|
159
|
-
distinctId: posthog_distinct_id ?? traceId,
|
|
160
|
-
traceId,
|
|
161
|
-
model: openAIParams.model,
|
|
162
|
-
provider: 'azure',
|
|
163
|
-
input: posthog_privacy_mode ? '' : mergeSystemPrompt(openAIParams, 'openai'),
|
|
164
|
-
output: [{ content: result.choices[0].message.content, role: 'assistant' }],
|
|
165
|
-
latency,
|
|
166
|
-
baseURL: (this as any).baseURL ?? '',
|
|
167
|
-
params: body,
|
|
168
|
-
httpStatus: 200,
|
|
169
|
-
usage: {
|
|
170
|
-
input_tokens: result.usage?.prompt_tokens ?? 0,
|
|
171
|
-
output_tokens: result.usage?.completion_tokens ?? 0,
|
|
172
|
-
},
|
|
173
|
-
})
|
|
174
|
-
}
|
|
175
|
-
return result
|
|
176
|
-
},
|
|
177
|
-
(error) => {
|
|
178
|
-
sendEventToPosthog({
|
|
179
|
-
client: this.phClient,
|
|
180
|
-
distinctId: posthog_distinct_id ?? traceId,
|
|
181
|
-
traceId,
|
|
182
|
-
model: openAIParams.model,
|
|
183
|
-
provider: 'azure',
|
|
184
|
-
input: posthog_privacy_mode ? '' : mergeSystemPrompt(openAIParams, 'openai'),
|
|
185
|
-
output: [],
|
|
186
|
-
latency: 0,
|
|
187
|
-
baseURL: (this as any).baseURL ?? '',
|
|
188
|
-
params: body,
|
|
189
|
-
httpStatus: 500,
|
|
190
|
-
usage: {
|
|
191
|
-
input_tokens: 0,
|
|
192
|
-
output_tokens: 0,
|
|
193
|
-
},
|
|
194
|
-
})
|
|
195
|
-
throw error
|
|
196
|
-
}
|
|
197
|
-
) as APIPromise<ChatCompletion>
|
|
198
|
-
|
|
199
|
-
return wrappedPromise
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
export default PostHogAzureOpenAI
|