@effect/ai-openai-compat 4.0.0-beta.8 → 4.0.0-beta.81
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/dist/OpenAiClient.d.ts +264 -52
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +97 -9
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiConfig.d.ts +68 -10
- package/dist/OpenAiConfig.d.ts.map +1 -1
- package/dist/OpenAiConfig.js +36 -7
- package/dist/OpenAiConfig.js.map +1 -1
- package/dist/OpenAiEmbeddingModel.d.ts +186 -0
- package/dist/OpenAiEmbeddingModel.d.ts.map +1 -0
- package/dist/OpenAiEmbeddingModel.js +190 -0
- package/dist/OpenAiEmbeddingModel.js.map +1 -0
- package/dist/OpenAiError.d.ts +109 -35
- package/dist/OpenAiError.d.ts.map +1 -1
- package/dist/OpenAiError.js +14 -1
- package/dist/OpenAiLanguageModel.d.ts +304 -20
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +157 -27
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/dist/OpenAiTelemetry.d.ts +76 -26
- package/dist/OpenAiTelemetry.d.ts.map +1 -1
- package/dist/OpenAiTelemetry.js +22 -10
- package/dist/OpenAiTelemetry.js.map +1 -1
- package/dist/index.d.ts +10 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -17
- package/dist/index.js.map +1 -1
- package/dist/internal/errors.js +4 -4
- package/dist/internal/errors.js.map +1 -1
- package/package.json +3 -3
- package/src/OpenAiClient.ts +273 -50
- package/src/OpenAiConfig.ts +69 -11
- package/src/OpenAiEmbeddingModel.ts +332 -0
- package/src/OpenAiError.ts +111 -35
- package/src/OpenAiLanguageModel.ts +425 -42
- package/src/OpenAiTelemetry.ts +81 -32
- package/src/index.ts +11 -17
- package/src/internal/errors.ts +4 -4
package/src/OpenAiTelemetry.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* OpenAI telemetry attributes
|
|
2
|
+
* The `OpenAiTelemetry` module defines OpenAI-compatible telemetry attributes
|
|
3
|
+
* and a helper for adding them to a tracing span. It keeps the standard GenAI
|
|
4
|
+
* telemetry attributes and adds request and response metadata under the
|
|
5
|
+
* `gen_ai.openai.*` OpenTelemetry namespaces.
|
|
3
6
|
*
|
|
4
|
-
*
|
|
5
|
-
* semantic conventions, extending the base GenAI attributes with OpenAI-specific
|
|
6
|
-
* request and response metadata.
|
|
7
|
-
*
|
|
8
|
-
* @since 1.0.0
|
|
7
|
+
* @since 4.0.0
|
|
9
8
|
*/
|
|
10
9
|
import { dual } from "effect/Function"
|
|
11
10
|
import * as String from "effect/String"
|
|
@@ -17,10 +16,14 @@ import * as Telemetry from "effect/unstable/ai/Telemetry"
|
|
|
17
16
|
* The attributes used to describe telemetry in the context of Generative
|
|
18
17
|
* Artificial Intelligence (GenAI) Models requests and responses.
|
|
19
18
|
*
|
|
20
|
-
*
|
|
19
|
+
* **Details**
|
|
20
|
+
*
|
|
21
|
+
* These attributes follow the OpenTelemetry generative AI semantic
|
|
22
|
+
* conventions:
|
|
23
|
+
* https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/
|
|
21
24
|
*
|
|
22
|
-
* @since 1.0.0
|
|
23
25
|
* @category models
|
|
26
|
+
* @since 4.0.0
|
|
24
27
|
*/
|
|
25
28
|
export type OpenAiTelemetryAttributes = Simplify<
|
|
26
29
|
& Telemetry.GenAITelemetryAttributes
|
|
@@ -30,10 +33,10 @@ export type OpenAiTelemetryAttributes = Simplify<
|
|
|
30
33
|
|
|
31
34
|
/**
|
|
32
35
|
* All telemetry attributes which are part of the GenAI specification,
|
|
33
|
-
* including the
|
|
36
|
+
* including the OpenAI-specific attributes.
|
|
34
37
|
*
|
|
35
|
-
* @since 1.0.0
|
|
36
38
|
* @category models
|
|
39
|
+
* @since 4.0.0
|
|
37
40
|
*/
|
|
38
41
|
export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & ResponseAttributes
|
|
39
42
|
|
|
@@ -41,8 +44,8 @@ export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & Respon
|
|
|
41
44
|
* Telemetry attributes which are part of the GenAI specification and are
|
|
42
45
|
* namespaced by `gen_ai.openai.request`.
|
|
43
46
|
*
|
|
44
|
-
* @since 1.0.0
|
|
45
47
|
* @category models
|
|
48
|
+
* @since 4.0.0
|
|
46
49
|
*/
|
|
47
50
|
export interface RequestAttributes {
|
|
48
51
|
/**
|
|
@@ -59,8 +62,8 @@ export interface RequestAttributes {
|
|
|
59
62
|
* Telemetry attributes which are part of the GenAI specification and are
|
|
60
63
|
* namespaced by `gen_ai.openai.response`.
|
|
61
64
|
*
|
|
62
|
-
* @since 1.0.0
|
|
63
65
|
* @category models
|
|
66
|
+
* @since 4.0.0
|
|
64
67
|
*/
|
|
65
68
|
export interface ResponseAttributes {
|
|
66
69
|
/**
|
|
@@ -75,32 +78,39 @@ export interface ResponseAttributes {
|
|
|
75
78
|
}
|
|
76
79
|
|
|
77
80
|
/**
|
|
78
|
-
* The `gen_ai.openai.request.response_format` attribute has
|
|
79
|
-
*
|
|
81
|
+
* The `gen_ai.openai.request.response_format` attribute has a list of
|
|
82
|
+
* well-known values.
|
|
83
|
+
*
|
|
84
|
+
* **Details**
|
|
80
85
|
*
|
|
81
86
|
* If one of them applies, then the respective value **MUST** be used;
|
|
82
87
|
* otherwise, a custom value **MAY** be used.
|
|
83
88
|
*
|
|
84
|
-
* @since 1.0.0
|
|
85
89
|
* @category models
|
|
90
|
+
* @since 4.0.0
|
|
86
91
|
*/
|
|
87
92
|
export type WellKnownResponseFormat = "json_object" | "json_schema" | "text"
|
|
88
93
|
|
|
89
94
|
/**
|
|
90
|
-
* The `gen_ai.openai.request.service_tier` attribute has
|
|
91
|
-
*
|
|
95
|
+
* The `gen_ai.openai.request.service_tier` attribute has a list of
|
|
96
|
+
* well-known values.
|
|
97
|
+
*
|
|
98
|
+
* **Details**
|
|
92
99
|
*
|
|
93
100
|
* If one of them applies, then the respective value **MUST** be used;
|
|
94
101
|
* otherwise, a custom value **MAY** be used.
|
|
95
102
|
*
|
|
96
|
-
* @since 1.0.0
|
|
97
103
|
* @category models
|
|
104
|
+
* @since 4.0.0
|
|
98
105
|
*/
|
|
99
106
|
export type WellKnownServiceTier = "auto" | "default"
|
|
100
107
|
|
|
101
108
|
/**
|
|
102
|
-
*
|
|
103
|
-
*
|
|
109
|
+
* Options accepted by `addGenAIAnnotations`, combining standard GenAI telemetry
|
|
110
|
+
* attributes with optional OpenAI-compatible request and response attributes.
|
|
111
|
+
*
|
|
112
|
+
* @category options
|
|
113
|
+
* @since 4.0.0
|
|
104
114
|
*/
|
|
105
115
|
export type OpenAiTelemetryAttributeOptions = Telemetry.GenAITelemetryAttributeOptions & {
|
|
106
116
|
openai?: {
|
|
@@ -117,33 +127,72 @@ const addOpenAiResponseAttributes = Telemetry.addSpanAttributes("gen_ai.openai.r
|
|
|
117
127
|
>
|
|
118
128
|
|
|
119
129
|
/**
|
|
120
|
-
* Applies the specified
|
|
130
|
+
* Applies the specified OpenAI GenAI telemetry attributes to the provided
|
|
121
131
|
* `Span`.
|
|
122
132
|
*
|
|
123
|
-
* **
|
|
133
|
+
* **When to use**
|
|
134
|
+
*
|
|
135
|
+
* Use to annotate an OpenAI-compatible model span with standard GenAI telemetry
|
|
136
|
+
* attributes and OpenAI-specific request or response metadata.
|
|
137
|
+
*
|
|
138
|
+
* **Details**
|
|
139
|
+
*
|
|
140
|
+
* Standard GenAI attributes are applied first. When OpenAI request or response
|
|
141
|
+
* metadata is present, it is written under `gen_ai.openai.request.*` and
|
|
142
|
+
* `gen_ai.openai.response.*` attributes.
|
|
143
|
+
*
|
|
144
|
+
* **Gotchas**
|
|
145
|
+
*
|
|
146
|
+
* Mutates the supplied `Span` in place.
|
|
124
147
|
*
|
|
125
|
-
* @
|
|
126
|
-
* @since
|
|
148
|
+
* @category tracing
|
|
149
|
+
* @since 4.0.0
|
|
127
150
|
*/
|
|
128
151
|
export const addGenAIAnnotations: {
|
|
129
152
|
/**
|
|
130
|
-
* Applies the specified
|
|
153
|
+
* Applies the specified OpenAI GenAI telemetry attributes to the provided
|
|
131
154
|
* `Span`.
|
|
132
155
|
*
|
|
133
|
-
* **
|
|
156
|
+
* **When to use**
|
|
134
157
|
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
158
|
+
* Use to annotate an OpenAI-compatible model span with standard GenAI telemetry
|
|
159
|
+
* attributes and OpenAI-specific request or response metadata.
|
|
160
|
+
*
|
|
161
|
+
* **Details**
|
|
162
|
+
*
|
|
163
|
+
* Standard GenAI attributes are applied first. When OpenAI request or response
|
|
164
|
+
* metadata is present, it is written under `gen_ai.openai.request.*` and
|
|
165
|
+
* `gen_ai.openai.response.*` attributes.
|
|
166
|
+
*
|
|
167
|
+
* **Gotchas**
|
|
168
|
+
*
|
|
169
|
+
* Mutates the supplied `Span` in place.
|
|
170
|
+
*
|
|
171
|
+
* @category tracing
|
|
172
|
+
* @since 4.0.0
|
|
137
173
|
*/
|
|
138
174
|
(options: OpenAiTelemetryAttributeOptions): (span: Span) => void
|
|
139
175
|
/**
|
|
140
|
-
* Applies the specified
|
|
176
|
+
* Applies the specified OpenAI GenAI telemetry attributes to the provided
|
|
141
177
|
* `Span`.
|
|
142
178
|
*
|
|
143
|
-
* **
|
|
179
|
+
* **When to use**
|
|
180
|
+
*
|
|
181
|
+
* Use to annotate an OpenAI-compatible model span with standard GenAI telemetry
|
|
182
|
+
* attributes and OpenAI-specific request or response metadata.
|
|
183
|
+
*
|
|
184
|
+
* **Details**
|
|
185
|
+
*
|
|
186
|
+
* Standard GenAI attributes are applied first. When OpenAI request or response
|
|
187
|
+
* metadata is present, it is written under `gen_ai.openai.request.*` and
|
|
188
|
+
* `gen_ai.openai.response.*` attributes.
|
|
189
|
+
*
|
|
190
|
+
* **Gotchas**
|
|
191
|
+
*
|
|
192
|
+
* Mutates the supplied `Span` in place.
|
|
144
193
|
*
|
|
145
|
-
* @
|
|
146
|
-
* @since
|
|
194
|
+
* @category tracing
|
|
195
|
+
* @since 4.0.0
|
|
147
196
|
*/
|
|
148
197
|
(span: Span, options: OpenAiTelemetryAttributeOptions): void
|
|
149
198
|
} = dual(2, (span: Span, options: OpenAiTelemetryAttributeOptions) => {
|
package/src/index.ts
CHANGED
|
@@ -1,41 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since
|
|
2
|
+
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
// @barrel: Auto-generated exports. Do not edit manually.
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* @since
|
|
8
|
+
* @since 4.0.0
|
|
9
9
|
*/
|
|
10
10
|
export * as OpenAiClient from "./OpenAiClient.ts"
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* @since
|
|
13
|
+
* @since 4.0.0
|
|
14
14
|
*/
|
|
15
15
|
export * as OpenAiConfig from "./OpenAiConfig.ts"
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* @since
|
|
18
|
+
* @since 4.0.0
|
|
19
|
+
*/
|
|
20
|
+
export * as OpenAiEmbeddingModel from "./OpenAiEmbeddingModel.ts"
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @since 4.0.0
|
|
19
24
|
*/
|
|
20
25
|
export * as OpenAiError from "./OpenAiError.ts"
|
|
21
26
|
|
|
22
27
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* Provides a LanguageModel implementation for OpenAI's chat completions API,
|
|
26
|
-
* supporting text generation, structured output, tool calling, and streaming.
|
|
27
|
-
*
|
|
28
|
-
* @since 1.0.0
|
|
28
|
+
* @since 4.0.0
|
|
29
29
|
*/
|
|
30
30
|
export * as OpenAiLanguageModel from "./OpenAiLanguageModel.ts"
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* Provides OpenAI-specific GenAI telemetry attributes following OpenTelemetry
|
|
36
|
-
* semantic conventions, extending the base GenAI attributes with OpenAI-specific
|
|
37
|
-
* request and response metadata.
|
|
38
|
-
*
|
|
39
|
-
* @since 1.0.0
|
|
33
|
+
* @since 4.0.0
|
|
40
34
|
*/
|
|
41
35
|
export * as OpenAiTelemetry from "./OpenAiTelemetry.ts"
|
package/src/internal/errors.ts
CHANGED
|
@@ -153,12 +153,12 @@ export const parseRateLimitHeaders = (headers: Record<string, string>) => {
|
|
|
153
153
|
let retryAfter: Duration.Duration | undefined
|
|
154
154
|
if (retryAfterRaw !== undefined) {
|
|
155
155
|
const parsed = Number.parse(retryAfterRaw)
|
|
156
|
-
if (parsed
|
|
157
|
-
retryAfter = Duration.seconds(parsed)
|
|
156
|
+
if (Option.isSome(parsed)) {
|
|
157
|
+
retryAfter = Duration.seconds(parsed.value)
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
160
|
const remainingRaw = headers["x-ratelimit-remaining-requests"]
|
|
161
|
-
const remaining = remainingRaw !== undefined ? Number.parse(remainingRaw)
|
|
161
|
+
const remaining = remainingRaw !== undefined ? Option.getOrNull(Number.parse(remainingRaw)) : null
|
|
162
162
|
return {
|
|
163
163
|
retryAfter,
|
|
164
164
|
limit: headers["x-ratelimit-limit-requests"] ?? null,
|
|
@@ -175,7 +175,7 @@ export const buildHttpRequestDetails = (
|
|
|
175
175
|
method: request.method,
|
|
176
176
|
url: request.url,
|
|
177
177
|
urlParams: Array.from(request.urlParams),
|
|
178
|
-
hash: request.hash,
|
|
178
|
+
hash: Option.getOrUndefined(request.hash),
|
|
179
179
|
headers: Redactable.redact(request.headers) as Record<string, string>
|
|
180
180
|
})
|
|
181
181
|
|