@effect/ai-openai 4.0.0-beta.70 → 4.0.0-beta.72
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 +88 -4
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +127 -8
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiConfig.d.ts +85 -17
- package/dist/OpenAiConfig.d.ts.map +1 -1
- package/dist/OpenAiConfig.js +53 -17
- package/dist/OpenAiConfig.js.map +1 -1
- package/dist/OpenAiEmbeddingModel.d.ts +127 -3
- package/dist/OpenAiEmbeddingModel.d.ts.map +1 -1
- package/dist/OpenAiEmbeddingModel.js +101 -3
- package/dist/OpenAiEmbeddingModel.js.map +1 -1
- package/dist/OpenAiLanguageModel.d.ts +139 -10
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +107 -8
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/dist/OpenAiSchema.d.ts +281 -12
- package/dist/OpenAiSchema.d.ts.map +1 -1
- package/dist/OpenAiSchema.js +227 -4
- package/dist/OpenAiSchema.js.map +1 -1
- package/dist/OpenAiTelemetry.d.ts +29 -1
- package/dist/OpenAiTelemetry.d.ts.map +1 -1
- package/dist/OpenAiTelemetry.js +29 -4
- package/dist/OpenAiTelemetry.js.map +1 -1
- package/dist/OpenAiTool.d.ts +130 -15
- package/dist/OpenAiTool.d.ts.map +1 -1
- package/dist/OpenAiTool.js +130 -15
- package/dist/OpenAiTool.js.map +1 -1
- package/dist/index.d.ts +1 -49
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -49
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/OpenAiClient.ts +129 -9
- package/src/OpenAiConfig.ts +85 -17
- package/src/OpenAiEmbeddingModel.ts +153 -3
- package/src/OpenAiLanguageModel.ts +171 -12
- package/src/OpenAiSchema.ts +309 -4
- package/src/OpenAiTelemetry.ts +50 -5
- package/src/OpenAiTool.ts +130 -15
- package/src/index.ts +1 -49
package/dist/index.js
CHANGED
|
@@ -3,15 +3,10 @@
|
|
|
3
3
|
*/
|
|
4
4
|
// @barrel: Auto-generated exports. Do not edit manually.
|
|
5
5
|
/**
|
|
6
|
-
* @since
|
|
6
|
+
* @since 1.0.0
|
|
7
7
|
*/
|
|
8
8
|
export * as Generated from "./Generated.js";
|
|
9
9
|
/**
|
|
10
|
-
* OpenAI Client module for interacting with OpenAI's API.
|
|
11
|
-
*
|
|
12
|
-
* Provides a type-safe, Effect-based client for OpenAI operations including
|
|
13
|
-
* completions, embeddings, and streaming responses.
|
|
14
|
-
*
|
|
15
10
|
* @since 4.0.0
|
|
16
11
|
*/
|
|
17
12
|
export * as OpenAiClient from "./OpenAiClient.js";
|
|
@@ -20,73 +15,30 @@ export * as OpenAiClient from "./OpenAiClient.js";
|
|
|
20
15
|
*/
|
|
21
16
|
export * as OpenAiClientGenerated from "./OpenAiClientGenerated.js";
|
|
22
17
|
/**
|
|
23
|
-
* The `OpenAiConfig` module provides contextual configuration for the
|
|
24
|
-
* `@effect/ai-openai` integration. It is used to customize how OpenAI clients
|
|
25
|
-
* are built and interpreted without threading configuration through every API
|
|
26
|
-
* call manually.
|
|
27
|
-
*
|
|
28
|
-
* The primary use case is installing an HTTP client transform with
|
|
29
|
-
* {@link withClientTransform}. This lets applications adapt the underlying
|
|
30
|
-
* OpenAI HTTP client for cross-cutting concerns such as custom middleware,
|
|
31
|
-
* instrumentation, proxying, or request policy changes while keeping the
|
|
32
|
-
* OpenAI service APIs unchanged.
|
|
33
|
-
*
|
|
34
|
-
* Configuration is scoped through Effect's context, so transforms only apply to
|
|
35
|
-
* the effect they are provided to and anything evaluated inside that scope.
|
|
36
|
-
* When multiple transforms are needed, compose them into a single
|
|
37
|
-
* `HttpClient => HttpClient` function before providing the configuration.
|
|
38
|
-
*
|
|
39
18
|
* @since 4.0.0
|
|
40
19
|
*/
|
|
41
20
|
export * as OpenAiConfig from "./OpenAiConfig.js";
|
|
42
21
|
/**
|
|
43
|
-
* OpenAI Embedding Model implementation.
|
|
44
|
-
*
|
|
45
|
-
* Provides an EmbeddingModel implementation for OpenAI's embeddings API.
|
|
46
|
-
*
|
|
47
22
|
* @since 4.0.0
|
|
48
23
|
*/
|
|
49
24
|
export * as OpenAiEmbeddingModel from "./OpenAiEmbeddingModel.js";
|
|
50
25
|
/**
|
|
51
|
-
* OpenAI error metadata augmentation.
|
|
52
|
-
*
|
|
53
|
-
* Provides OpenAI-specific metadata fields for AI error types through module
|
|
54
|
-
* augmentation, enabling typed access to OpenAI error details.
|
|
55
|
-
*
|
|
56
26
|
* @since 4.0.0
|
|
57
27
|
*/
|
|
58
28
|
export * as OpenAiError from "./OpenAiError.js";
|
|
59
29
|
/**
|
|
60
|
-
* OpenAI Language Model implementation.
|
|
61
|
-
*
|
|
62
|
-
* Provides a LanguageModel implementation for OpenAI's responses API,
|
|
63
|
-
* supporting text generation, structured output, tool calling, and streaming.
|
|
64
|
-
*
|
|
65
30
|
* @since 4.0.0
|
|
66
31
|
*/
|
|
67
32
|
export * as OpenAiLanguageModel from "./OpenAiLanguageModel.js";
|
|
68
33
|
/**
|
|
69
|
-
* Minimal local OpenAI schemas used by the handwritten Responses client path.
|
|
70
|
-
*
|
|
71
34
|
* @since 4.0.0
|
|
72
35
|
*/
|
|
73
36
|
export * as OpenAiSchema from "./OpenAiSchema.js";
|
|
74
37
|
/**
|
|
75
|
-
* OpenAI telemetry attributes for OpenTelemetry integration.
|
|
76
|
-
*
|
|
77
|
-
* Provides OpenAI-specific GenAI telemetry attributes following OpenTelemetry
|
|
78
|
-
* semantic conventions, extending the base GenAI attributes with OpenAI-specific
|
|
79
|
-
* request and response metadata.
|
|
80
|
-
*
|
|
81
38
|
* @since 4.0.0
|
|
82
39
|
*/
|
|
83
40
|
export * as OpenAiTelemetry from "./OpenAiTelemetry.js";
|
|
84
41
|
/**
|
|
85
|
-
* OpenAI provider-defined tools for use with the LanguageModel.
|
|
86
|
-
*
|
|
87
|
-
* Provides tools that are natively supported by OpenAI's API, including
|
|
88
|
-
* code interpreter, file search, and web search functionality.
|
|
89
|
-
*
|
|
90
42
|
* @since 4.0.0
|
|
91
43
|
*/
|
|
92
44
|
export * as OpenAiTool from "./OpenAiTool.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Generated","OpenAiClient","OpenAiClientGenerated","OpenAiConfig","OpenAiEmbeddingModel","OpenAiError","OpenAiLanguageModel","OpenAiSchema","OpenAiTelemetry","OpenAiTool"],"sources":["../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAIA;AAEA;;;AAGA,OAAO,KAAKA,SAAS,MAAM,gBAAgB;AAE3C
|
|
1
|
+
{"version":3,"file":"index.js","names":["Generated","OpenAiClient","OpenAiClientGenerated","OpenAiConfig","OpenAiEmbeddingModel","OpenAiError","OpenAiLanguageModel","OpenAiSchema","OpenAiTelemetry","OpenAiTool"],"sources":["../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAIA;AAEA;;;AAGA,OAAO,KAAKA,SAAS,MAAM,gBAAgB;AAE3C;;;AAGA,OAAO,KAAKC,YAAY,MAAM,mBAAmB;AAEjD;;;AAGA,OAAO,KAAKC,qBAAqB,MAAM,4BAA4B;AAEnE;;;AAGA,OAAO,KAAKC,YAAY,MAAM,mBAAmB;AAEjD;;;AAGA,OAAO,KAAKC,oBAAoB,MAAM,2BAA2B;AAEjE;;;AAGA,OAAO,KAAKC,WAAW,MAAM,kBAAkB;AAE/C;;;AAGA,OAAO,KAAKC,mBAAmB,MAAM,0BAA0B;AAE/D;;;AAGA,OAAO,KAAKC,YAAY,MAAM,mBAAmB;AAEjD;;;AAGA,OAAO,KAAKC,eAAe,MAAM,sBAAsB;AAEvD;;;AAGA,OAAO,KAAKC,UAAU,MAAM,iBAAiB","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/ai-openai",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.72",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "An OpenAI provider integration for Effect AI SDK",
|
|
@@ -43,10 +43,10 @@
|
|
|
43
43
|
"provenance": true
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"effect": "^4.0.0-beta.
|
|
46
|
+
"effect": "^4.0.0-beta.72"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"effect": "^4.0.0-beta.
|
|
49
|
+
"effect": "^4.0.0-beta.72"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"codegen": "effect-utils codegen",
|
package/src/OpenAiClient.ts
CHANGED
|
@@ -1,8 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `OpenAiClient` module provides the handwritten Effect service used by
|
|
3
|
+
* the OpenAI integration for Responses API and embedding requests. It builds on
|
|
4
|
+
* the Effect HTTP client, applies OpenAI authentication and organization or
|
|
5
|
+
* project headers, decodes the minimal schemas needed by higher-level modules,
|
|
6
|
+
* and maps transport or decoding failures into `AiError`.
|
|
3
7
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
8
|
+
* The service exposes a configured HTTP client plus helpers for non-streaming
|
|
9
|
+
* responses, server-sent event response streams, and embeddings. It also
|
|
10
|
+
* includes WebSocket mode for response streams when an application wants to use
|
|
11
|
+
* OpenAI's WebSocket transport instead of the default SSE path.
|
|
12
|
+
*
|
|
13
|
+
* **Common tasks**
|
|
14
|
+
*
|
|
15
|
+
* - Construct the service directly with {@link make}
|
|
16
|
+
* - Provide the service with {@link layer} or load settings from `Config` with
|
|
17
|
+
* {@link layerConfig}
|
|
18
|
+
* - Call `createResponse`, `createResponseStream`, or `createEmbedding` from
|
|
19
|
+
* code that depends on the `OpenAiClient` service
|
|
20
|
+
* - Enable WebSocket streaming around an effect with {@link withWebSocketMode}
|
|
21
|
+
* or through layers with {@link layerWebSocketMode}
|
|
22
|
+
*
|
|
23
|
+
* **Gotchas**
|
|
24
|
+
*
|
|
25
|
+
* - The default base URL is `https://api.openai.com/v1`; set `apiUrl` for
|
|
26
|
+
* proxies, local gateways, or compatible deployments.
|
|
27
|
+
* - A constructor `transformClient` is applied when the service is built, while
|
|
28
|
+
* scoped `OpenAiConfig` transforms are applied by request helpers when they
|
|
29
|
+
* run.
|
|
30
|
+
* - WebSocket mode requires a supported `Socket.WebSocketConstructor` layer and
|
|
31
|
+
* serializes response streams through the shared socket service.
|
|
32
|
+
* - This module is intentionally narrower than the generated OpenAI client; use
|
|
33
|
+
* `OpenAiClientGenerated` for direct access to generated endpoint helpers.
|
|
6
34
|
*
|
|
7
35
|
* @since 4.0.0
|
|
8
36
|
*/
|
|
@@ -90,7 +118,16 @@ export interface Service {
|
|
|
90
118
|
// =============================================================================
|
|
91
119
|
|
|
92
120
|
/**
|
|
93
|
-
* Service
|
|
121
|
+
* Service tag for the OpenAI client.
|
|
122
|
+
*
|
|
123
|
+
* **When to use**
|
|
124
|
+
*
|
|
125
|
+
* Use when accessing or providing the OpenAI client service through Effect's
|
|
126
|
+
* context.
|
|
127
|
+
*
|
|
128
|
+
* @see {@link make} for constructing an OpenAI client effectfully
|
|
129
|
+
* @see {@link layer} for providing a client from explicit options
|
|
130
|
+
* @see {@link layerConfig} for providing a client from `Config`
|
|
94
131
|
*
|
|
95
132
|
* @category services
|
|
96
133
|
* @since 4.0.0
|
|
@@ -150,6 +187,26 @@ const RedactedOpenAiHeaders = {
|
|
|
150
187
|
/**
|
|
151
188
|
* Creates an OpenAI client service with the given options.
|
|
152
189
|
*
|
|
190
|
+
* **When to use**
|
|
191
|
+
*
|
|
192
|
+
* Use to construct the OpenAI client service inside an effect when you need the
|
|
193
|
+
* service value directly.
|
|
194
|
+
*
|
|
195
|
+
* **Details**
|
|
196
|
+
*
|
|
197
|
+
* The returned service uses the current `HttpClient`, prepends `apiUrl` or
|
|
198
|
+
* `https://api.openai.com/v1`, adds the bearer token and optional OpenAI
|
|
199
|
+
* organization/project headers, accepts JSON responses, filters for successful
|
|
200
|
+
* HTTP statuses, and applies `transformClient` when provided.
|
|
201
|
+
*
|
|
202
|
+
* **Gotchas**
|
|
203
|
+
*
|
|
204
|
+
* A scoped `OpenAiConfig.withClientTransform` is applied when request helpers
|
|
205
|
+
* run, after the `transformClient` option supplied to `make`.
|
|
206
|
+
*
|
|
207
|
+
* @see {@link layer} for providing this client from explicit options
|
|
208
|
+
* @see {@link layerConfig} for loading client settings from `Config`
|
|
209
|
+
*
|
|
153
210
|
* @category constructors
|
|
154
211
|
* @since 4.0.0
|
|
155
212
|
*/
|
|
@@ -302,6 +359,14 @@ export const make = Effect.fnUntraced(
|
|
|
302
359
|
/**
|
|
303
360
|
* Creates a layer for the OpenAI client with the given options.
|
|
304
361
|
*
|
|
362
|
+
* **When to use**
|
|
363
|
+
*
|
|
364
|
+
* Use when you already have explicit `Options` values, such as an API key or
|
|
365
|
+
* custom API URL, and want to provide `OpenAiClient` as a `Layer`.
|
|
366
|
+
*
|
|
367
|
+
* @see {@link make} for constructing the client service effectfully
|
|
368
|
+
* @see {@link layerConfig} for loading client settings from `Config`
|
|
369
|
+
*
|
|
305
370
|
* @category layers
|
|
306
371
|
* @since 4.0.0
|
|
307
372
|
*/
|
|
@@ -309,8 +374,21 @@ export const layer = (options: Options): Layer.Layer<OpenAiClient, never, HttpCl
|
|
|
309
374
|
Layer.effect(OpenAiClient, make(options))
|
|
310
375
|
|
|
311
376
|
/**
|
|
312
|
-
* Creates a layer for the OpenAI client
|
|
313
|
-
*
|
|
377
|
+
* Creates a layer for the OpenAI client from provided `Config` values.
|
|
378
|
+
*
|
|
379
|
+
* **When to use**
|
|
380
|
+
*
|
|
381
|
+
* Use when client settings should be read from Effect `Config` values while
|
|
382
|
+
* providing `OpenAiClient` as a `Layer`.
|
|
383
|
+
*
|
|
384
|
+
* **Details**
|
|
385
|
+
*
|
|
386
|
+
* Only config values supplied in `options` are loaded. Omitted fields are
|
|
387
|
+
* passed to `make` as `undefined`, and `transformClient` is forwarded as a
|
|
388
|
+
* plain option.
|
|
389
|
+
*
|
|
390
|
+
* @see {@link make} for constructing the client service effectfully
|
|
391
|
+
* @see {@link layer} for providing the client from already-resolved options
|
|
314
392
|
*
|
|
315
393
|
* @category layers
|
|
316
394
|
* @since 4.0.0
|
|
@@ -381,6 +459,25 @@ export type ResponseStreamEvent = typeof OpenAiSchema.ResponseStreamEvent.Type
|
|
|
381
459
|
/**
|
|
382
460
|
* Service for creating OpenAI response streams over a WebSocket connection.
|
|
383
461
|
*
|
|
462
|
+
* **When to use**
|
|
463
|
+
*
|
|
464
|
+
* Use when code needs direct access to the WebSocket-backed response streaming
|
|
465
|
+
* service rather than wrapping an effect with WebSocket mode.
|
|
466
|
+
*
|
|
467
|
+
* **Details**
|
|
468
|
+
*
|
|
469
|
+
* `createResponseStream` sends a `response.create` message over the WebSocket
|
|
470
|
+
* connection and returns an HTTP response together with a stream of
|
|
471
|
+
* `ResponseStreamEvent` values.
|
|
472
|
+
*
|
|
473
|
+
* **Gotchas**
|
|
474
|
+
*
|
|
475
|
+
* WebSocket response streams are serialized to one request at a time by the
|
|
476
|
+
* shared socket service.
|
|
477
|
+
*
|
|
478
|
+
* @see {@link withWebSocketMode} for enabling WebSocket mode for one effect
|
|
479
|
+
* @see {@link layerWebSocketMode} for providing WebSocket mode through a layer
|
|
480
|
+
*
|
|
384
481
|
* @category Websocket mode
|
|
385
482
|
* @since 4.0.0
|
|
386
483
|
*/
|
|
@@ -470,7 +567,7 @@ const makeSocket = Effect.gen(function*() {
|
|
|
470
567
|
const event = decodeEvent(text)
|
|
471
568
|
if (event.type === "error" && "status" in event) {
|
|
472
569
|
const status = Number(event.status)
|
|
473
|
-
const error = "error" in event ? event.error : event
|
|
570
|
+
const error = "error" in event ? event.error as typeof ErrorEvent.Type.error : event
|
|
474
571
|
const json = JSON.stringify(error)
|
|
475
572
|
return Effect.fail(
|
|
476
573
|
AiError.make({
|
|
@@ -478,7 +575,7 @@ const makeSocket = Effect.gen(function*() {
|
|
|
478
575
|
method: "createResponseStream",
|
|
479
576
|
reason: AiError.reasonFromHttpStatus({
|
|
480
577
|
description: json,
|
|
481
|
-
status: isNaN(status) ? 500 : status,
|
|
578
|
+
status: isNaN(status) ? errorTypeToStatus[error.type] ?? 500 : status,
|
|
482
579
|
metadata: error as any,
|
|
483
580
|
http: {
|
|
484
581
|
body: json,
|
|
@@ -581,11 +678,24 @@ const ErrorEvent = Schema.Struct({
|
|
|
581
678
|
})
|
|
582
679
|
})
|
|
583
680
|
|
|
681
|
+
const errorTypeToStatus: Record<string, number> = {
|
|
682
|
+
invalid_request_error: 400,
|
|
683
|
+
invalid_api_key_error: 401,
|
|
684
|
+
insufficient_quota_error: 429,
|
|
685
|
+
rate_limit_error: 429,
|
|
686
|
+
service_unavailable_error: 503
|
|
687
|
+
}
|
|
688
|
+
|
|
584
689
|
const AllEvents = Schema.Union([ErrorEvent, OpenAiSchema.ResponseStreamEvent])
|
|
585
690
|
const decodeEvent = Schema.decodeUnknownSync(Schema.fromJsonString(AllEvents))
|
|
586
691
|
|
|
587
692
|
/**
|
|
588
|
-
* Uses OpenAI's
|
|
693
|
+
* Uses OpenAI's WebSocket mode for response streams within the provided effect.
|
|
694
|
+
*
|
|
695
|
+
* **When to use**
|
|
696
|
+
*
|
|
697
|
+
* Use to enable WebSocket mode around one effect that creates OpenAI response
|
|
698
|
+
* streams.
|
|
589
699
|
*
|
|
590
700
|
* **Gotchas**
|
|
591
701
|
*
|
|
@@ -596,6 +706,9 @@ const decodeEvent = Schema.decodeUnknownSync(Schema.fromJsonString(AllEvents))
|
|
|
596
706
|
*
|
|
597
707
|
* This is because it needs to use non-standard options for setting the Authorization header.
|
|
598
708
|
*
|
|
709
|
+
* @see {@link layerWebSocketMode} for providing WebSocket mode through a layer
|
|
710
|
+
* @see {@link OpenAiSocket} for direct access to the WebSocket-backed streaming service
|
|
711
|
+
*
|
|
599
712
|
* @category Websocket mode
|
|
600
713
|
* @since 4.0.0
|
|
601
714
|
*/
|
|
@@ -616,6 +729,11 @@ export const withWebSocketMode = <A, E, R>(
|
|
|
616
729
|
/**
|
|
617
730
|
* Uses OpenAI's websocket mode for all responses that use the Layer.
|
|
618
731
|
*
|
|
732
|
+
* **When to use**
|
|
733
|
+
*
|
|
734
|
+
* Use to provide WebSocket mode through layer composition for effects that use
|
|
735
|
+
* OpenAI response streaming.
|
|
736
|
+
*
|
|
619
737
|
* **Gotchas**
|
|
620
738
|
*
|
|
621
739
|
* This only works with the following WebSocket constructor layers:
|
|
@@ -625,6 +743,8 @@ export const withWebSocketMode = <A, E, R>(
|
|
|
625
743
|
*
|
|
626
744
|
* This is because it needs to use non-standard options for setting the Authorization header.
|
|
627
745
|
*
|
|
746
|
+
* @see {@link withWebSocketMode} for enabling WebSocket mode around a single effect
|
|
747
|
+
*
|
|
628
748
|
* @category Websocket mode
|
|
629
749
|
* @since 4.0.0
|
|
630
750
|
*/
|
package/src/OpenAiConfig.ts
CHANGED
|
@@ -1,19 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The `OpenAiConfig` module
|
|
3
|
-
* `@effect/ai-openai`
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* OpenAI
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
2
|
+
* The `OpenAiConfig` module carries request-time configuration for the
|
|
3
|
+
* `@effect/ai-openai` package through Effect's context. It currently exposes a
|
|
4
|
+
* scoped HTTP client transform used by OpenAI request helpers when they execute
|
|
5
|
+
* provider calls.
|
|
6
|
+
*
|
|
7
|
+
* **Mental model**
|
|
8
|
+
*
|
|
9
|
+
* Client constructors set the baseline HTTP client for an OpenAI service.
|
|
10
|
+
* `OpenAiConfig` is for narrower dynamic scopes: wrap an effect with
|
|
11
|
+
* {@link withClientTransform} and OpenAI requests evaluated inside that effect
|
|
12
|
+
* see the transformed `HttpClient`. Leaving the scope restores the previous
|
|
13
|
+
* configuration.
|
|
14
|
+
*
|
|
15
|
+
* **Common tasks**
|
|
16
|
+
*
|
|
17
|
+
* - Add request middleware, tracing, retry policy, proxy routing, or test
|
|
18
|
+
* interception around a group of OpenAI calls.
|
|
19
|
+
* - Apply a temporary HTTP client transform without rebuilding the OpenAI
|
|
20
|
+
* service layer.
|
|
21
|
+
* - Share one transform across all OpenAI helpers executed inside the same
|
|
22
|
+
* Effect scope.
|
|
23
|
+
*
|
|
24
|
+
* **Gotchas**
|
|
25
|
+
*
|
|
26
|
+
* - Only one `transformClient` value is stored in the scoped config. Compose
|
|
27
|
+
* transforms into a single `HttpClient => HttpClient` function when multiple
|
|
28
|
+
* behaviors should apply.
|
|
29
|
+
* - The transform affects OpenAI requests that read this contextual config; it
|
|
30
|
+
* does not mutate an already constructed HTTP client globally.
|
|
17
31
|
*
|
|
18
32
|
* @since 4.0.0
|
|
19
33
|
*/
|
|
@@ -23,8 +37,14 @@ import { dual } from "effect/Function"
|
|
|
23
37
|
import type { HttpClient } from "effect/unstable/http/HttpClient"
|
|
24
38
|
|
|
25
39
|
/**
|
|
26
|
-
* Context service
|
|
27
|
-
*
|
|
40
|
+
* Context service for scoped OpenAI configuration used by provider operations.
|
|
41
|
+
*
|
|
42
|
+
* **When to use**
|
|
43
|
+
*
|
|
44
|
+
* Use to provide scoped OpenAI client configuration, such as an HTTP client
|
|
45
|
+
* transform, to OpenAI provider operations without passing it through each call.
|
|
46
|
+
*
|
|
47
|
+
* @see {@link withClientTransform} for scoping an HTTP client transformation
|
|
28
48
|
*
|
|
29
49
|
* @category services
|
|
30
50
|
* @since 4.0.0
|
|
@@ -66,6 +86,22 @@ export declare namespace OpenAiConfig {
|
|
|
66
86
|
* Provides a scoped transform for the OpenAI HTTP client used by provider
|
|
67
87
|
* operations.
|
|
68
88
|
*
|
|
89
|
+
* **When to use**
|
|
90
|
+
*
|
|
91
|
+
* Use when a single effect or workflow needs temporary OpenAI HTTP client
|
|
92
|
+
* customization without rebuilding the client layer.
|
|
93
|
+
*
|
|
94
|
+
* **Details**
|
|
95
|
+
*
|
|
96
|
+
* Supports both data-first and data-last forms. The transform is stored in the
|
|
97
|
+
* scoped `OpenAiConfig` service and read by OpenAI provider operations while
|
|
98
|
+
* running the supplied effect.
|
|
99
|
+
*
|
|
100
|
+
* **Gotchas**
|
|
101
|
+
*
|
|
102
|
+
* If a transform is already present in the scoped config, this helper replaces
|
|
103
|
+
* it. Compose transforms manually when both should apply.
|
|
104
|
+
*
|
|
69
105
|
* @category configuration
|
|
70
106
|
* @since 4.0.0
|
|
71
107
|
*/
|
|
@@ -74,6 +110,22 @@ export const withClientTransform: {
|
|
|
74
110
|
* Provides a scoped transform for the OpenAI HTTP client used by provider
|
|
75
111
|
* operations.
|
|
76
112
|
*
|
|
113
|
+
* **When to use**
|
|
114
|
+
*
|
|
115
|
+
* Use when a single effect or workflow needs temporary OpenAI HTTP client
|
|
116
|
+
* customization without rebuilding the client layer.
|
|
117
|
+
*
|
|
118
|
+
* **Details**
|
|
119
|
+
*
|
|
120
|
+
* Supports both data-first and data-last forms. The transform is stored in the
|
|
121
|
+
* scoped `OpenAiConfig` service and read by OpenAI provider operations while
|
|
122
|
+
* running the supplied effect.
|
|
123
|
+
*
|
|
124
|
+
* **Gotchas**
|
|
125
|
+
*
|
|
126
|
+
* If a transform is already present in the scoped config, this helper replaces
|
|
127
|
+
* it. Compose transforms manually when both should apply.
|
|
128
|
+
*
|
|
77
129
|
* @category configuration
|
|
78
130
|
* @since 4.0.0
|
|
79
131
|
*/
|
|
@@ -82,6 +134,22 @@ export const withClientTransform: {
|
|
|
82
134
|
* Provides a scoped transform for the OpenAI HTTP client used by provider
|
|
83
135
|
* operations.
|
|
84
136
|
*
|
|
137
|
+
* **When to use**
|
|
138
|
+
*
|
|
139
|
+
* Use when a single effect or workflow needs temporary OpenAI HTTP client
|
|
140
|
+
* customization without rebuilding the client layer.
|
|
141
|
+
*
|
|
142
|
+
* **Details**
|
|
143
|
+
*
|
|
144
|
+
* Supports both data-first and data-last forms. The transform is stored in the
|
|
145
|
+
* scoped `OpenAiConfig` service and read by OpenAI provider operations while
|
|
146
|
+
* running the supplied effect.
|
|
147
|
+
*
|
|
148
|
+
* **Gotchas**
|
|
149
|
+
*
|
|
150
|
+
* If a transform is already present in the scoped config, this helper replaces
|
|
151
|
+
* it. Compose transforms manually when both should apply.
|
|
152
|
+
*
|
|
85
153
|
* @category configuration
|
|
86
154
|
* @since 4.0.0
|
|
87
155
|
*/
|
|
@@ -1,7 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `OpenAiEmbeddingModel` module provides the OpenAI implementation of
|
|
3
|
+
* Effect AI's `EmbeddingModel` service. It adapts the OpenAI embeddings
|
|
4
|
+
* endpoint into Effect AI's batch embedding interface, preserving input order
|
|
5
|
+
* and returning numeric vectors for each requested input.
|
|
3
6
|
*
|
|
4
|
-
*
|
|
7
|
+
* **Mental model**
|
|
8
|
+
*
|
|
9
|
+
* `OpenAiClient` owns transport, authentication, and provider request
|
|
10
|
+
* execution. This module owns embedding-specific configuration, response
|
|
11
|
+
* validation, and the `EmbeddingModel.EmbeddingModel` layer. Use {@link model}
|
|
12
|
+
* when you want an `AiModel` descriptor that also provides the configured
|
|
13
|
+
* embedding dimensions, or {@link layer} / {@link make} when the dimensions are
|
|
14
|
+
* managed separately.
|
|
15
|
+
*
|
|
16
|
+
* **Common tasks**
|
|
17
|
+
*
|
|
18
|
+
* - Provide an OpenAI-backed `EmbeddingModel.EmbeddingModel` from an existing
|
|
19
|
+
* `OpenAiClient`
|
|
20
|
+
* - Configure the OpenAI embedding model id, dimensions, encoding format, and
|
|
21
|
+
* other create-embedding request fields
|
|
22
|
+
* - Scope per-request defaults with {@link Config} and
|
|
23
|
+
* {@link withConfigOverride}
|
|
24
|
+
*
|
|
25
|
+
* **Gotchas**
|
|
26
|
+
*
|
|
27
|
+
* - The service expects OpenAI to return floating-point embedding arrays.
|
|
28
|
+
* Requesting base64 embeddings causes an `InvalidOutputError`.
|
|
29
|
+
* - Provider results are checked for missing, duplicate, or out-of-range
|
|
30
|
+
* indexes before they are exposed as Effect AI embedding results.
|
|
5
31
|
*
|
|
6
32
|
* @since 4.0.0
|
|
7
33
|
*/
|
|
@@ -25,7 +51,20 @@ import type * as OpenAiSchema from "./OpenAiSchema.ts"
|
|
|
25
51
|
export type Model = "text-embedding-ada-002" | "text-embedding-3-small" | "text-embedding-3-large"
|
|
26
52
|
|
|
27
53
|
/**
|
|
28
|
-
*
|
|
54
|
+
* Context service for OpenAI embedding model configuration.
|
|
55
|
+
*
|
|
56
|
+
* **When to use**
|
|
57
|
+
*
|
|
58
|
+
* Use when embedding requests need scoped OpenAI request defaults or overrides
|
|
59
|
+
* from Effect context.
|
|
60
|
+
*
|
|
61
|
+
* **Details**
|
|
62
|
+
*
|
|
63
|
+
* The service stores the OpenAI create-embedding request payload without
|
|
64
|
+
* `input`, carrying options such as `model`, `dimensions`, `encoding_format`,
|
|
65
|
+
* and `user`.
|
|
66
|
+
*
|
|
67
|
+
* @see {@link withConfigOverride} for scoping embedding request overrides
|
|
29
68
|
*
|
|
30
69
|
* @category services
|
|
31
70
|
* @since 4.0.0
|
|
@@ -48,6 +87,14 @@ export class Config extends Context.Service<
|
|
|
48
87
|
/**
|
|
49
88
|
* Creates an `AiModel` for an OpenAI embedding model with its configured vector dimensions.
|
|
50
89
|
*
|
|
90
|
+
* **When to use**
|
|
91
|
+
*
|
|
92
|
+
* Use to provide an OpenAI `EmbeddingModel` and its `Dimensions` service to an
|
|
93
|
+
* Effect program.
|
|
94
|
+
*
|
|
95
|
+
* @see {@link layer} for providing only the embedding model service
|
|
96
|
+
* @see {@link withConfigOverride} for scoped request configuration overrides
|
|
97
|
+
*
|
|
51
98
|
* @category constructors
|
|
52
99
|
* @since 4.0.0
|
|
53
100
|
*/
|
|
@@ -76,6 +123,29 @@ export const model = (
|
|
|
76
123
|
/**
|
|
77
124
|
* Creates an OpenAI embedding model service.
|
|
78
125
|
*
|
|
126
|
+
* **When to use**
|
|
127
|
+
*
|
|
128
|
+
* Use to construct the `EmbeddingModel.Service` effectfully when
|
|
129
|
+
* `OpenAiClient` is already available in the environment or when the service
|
|
130
|
+
* value is needed directly.
|
|
131
|
+
*
|
|
132
|
+
* **Details**
|
|
133
|
+
*
|
|
134
|
+
* The `model` option is sent with each embedding request. Constructor `config`
|
|
135
|
+
* supplies create-embedding request fields other than `model` and `input`, and
|
|
136
|
+
* scoped overrides from `withConfigOverride` are merged last for each request.
|
|
137
|
+
*
|
|
138
|
+
* **Gotchas**
|
|
139
|
+
*
|
|
140
|
+
* The service expects numeric embedding vectors. It fails with
|
|
141
|
+
* `InvalidOutputError` when the provider returns base64 embeddings,
|
|
142
|
+
* out-of-range indexes, duplicate indexes, or an unexpected number of
|
|
143
|
+
* embeddings.
|
|
144
|
+
*
|
|
145
|
+
* @see {@link layer} for providing the embedding model service as a layer
|
|
146
|
+
* @see {@link model} for creating an `AiModel` that also provides dimensions
|
|
147
|
+
* @see {@link withConfigOverride} for scoped request configuration overrides
|
|
148
|
+
*
|
|
79
149
|
* @category constructors
|
|
80
150
|
* @since 4.0.0
|
|
81
151
|
*/
|
|
@@ -102,6 +172,21 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
102
172
|
/**
|
|
103
173
|
* Creates a layer for the OpenAI embedding model.
|
|
104
174
|
*
|
|
175
|
+
* **When to use**
|
|
176
|
+
*
|
|
177
|
+
* Use when composing application layers and you want OpenAI to satisfy
|
|
178
|
+
* `EmbeddingModel.EmbeddingModel` while supplying `OpenAiClient` from another
|
|
179
|
+
* layer.
|
|
180
|
+
*
|
|
181
|
+
* **Gotchas**
|
|
182
|
+
*
|
|
183
|
+
* Use the default floating-point embedding format. The service expects numeric
|
|
184
|
+
* vectors and fails with `InvalidOutputError` if OpenAI returns base64
|
|
185
|
+
* embeddings.
|
|
186
|
+
*
|
|
187
|
+
* @see {@link make} for constructing the embedding model service effectfully
|
|
188
|
+
* @see {@link model} for creating an `AiModel` that also provides embedding dimensions
|
|
189
|
+
*
|
|
105
190
|
* @category layers
|
|
106
191
|
* @since 4.0.0
|
|
107
192
|
*/
|
|
@@ -114,6 +199,19 @@ export const layer = (options: {
|
|
|
114
199
|
/**
|
|
115
200
|
* Provides config overrides for OpenAI embedding model operations.
|
|
116
201
|
*
|
|
202
|
+
* **When to use**
|
|
203
|
+
*
|
|
204
|
+
* Use when a single effect or workflow needs scoped OpenAI embedding request
|
|
205
|
+
* defaults without rebuilding the embedding model service.
|
|
206
|
+
*
|
|
207
|
+
* **Details**
|
|
208
|
+
*
|
|
209
|
+
* Supports both data-first and data-last forms. Existing scoped config is read
|
|
210
|
+
* first, then the provided overrides are applied so override fields take
|
|
211
|
+
* precedence.
|
|
212
|
+
*
|
|
213
|
+
* @see {@link Config} for the scoped embedding request configuration service
|
|
214
|
+
*
|
|
117
215
|
* @category configuration
|
|
118
216
|
* @since 4.0.0
|
|
119
217
|
*/
|
|
@@ -121,6 +219,19 @@ export const withConfigOverride: {
|
|
|
121
219
|
/**
|
|
122
220
|
* Provides config overrides for OpenAI embedding model operations.
|
|
123
221
|
*
|
|
222
|
+
* **When to use**
|
|
223
|
+
*
|
|
224
|
+
* Use when a single effect or workflow needs scoped OpenAI embedding request
|
|
225
|
+
* defaults without rebuilding the embedding model service.
|
|
226
|
+
*
|
|
227
|
+
* **Details**
|
|
228
|
+
*
|
|
229
|
+
* Supports both data-first and data-last forms. Existing scoped config is read
|
|
230
|
+
* first, then the provided overrides are applied so override fields take
|
|
231
|
+
* precedence.
|
|
232
|
+
*
|
|
233
|
+
* @see {@link Config} for the scoped embedding request configuration service
|
|
234
|
+
*
|
|
124
235
|
* @category configuration
|
|
125
236
|
* @since 4.0.0
|
|
126
237
|
*/
|
|
@@ -128,6 +239,19 @@ export const withConfigOverride: {
|
|
|
128
239
|
/**
|
|
129
240
|
* Provides config overrides for OpenAI embedding model operations.
|
|
130
241
|
*
|
|
242
|
+
* **When to use**
|
|
243
|
+
*
|
|
244
|
+
* Use when a single effect or workflow needs scoped OpenAI embedding request
|
|
245
|
+
* defaults without rebuilding the embedding model service.
|
|
246
|
+
*
|
|
247
|
+
* **Details**
|
|
248
|
+
*
|
|
249
|
+
* Supports both data-first and data-last forms. Existing scoped config is read
|
|
250
|
+
* first, then the provided overrides are applied so override fields take
|
|
251
|
+
* precedence.
|
|
252
|
+
*
|
|
253
|
+
* @see {@link Config} for the scoped embedding request configuration service
|
|
254
|
+
*
|
|
131
255
|
* @category configuration
|
|
132
256
|
* @since 4.0.0
|
|
133
257
|
*/
|
|
@@ -136,6 +260,19 @@ export const withConfigOverride: {
|
|
|
136
260
|
/**
|
|
137
261
|
* Provides config overrides for OpenAI embedding model operations.
|
|
138
262
|
*
|
|
263
|
+
* **When to use**
|
|
264
|
+
*
|
|
265
|
+
* Use when a single effect or workflow needs scoped OpenAI embedding request
|
|
266
|
+
* defaults without rebuilding the embedding model service.
|
|
267
|
+
*
|
|
268
|
+
* **Details**
|
|
269
|
+
*
|
|
270
|
+
* Supports both data-first and data-last forms. Existing scoped config is read
|
|
271
|
+
* first, then the provided overrides are applied so override fields take
|
|
272
|
+
* precedence.
|
|
273
|
+
*
|
|
274
|
+
* @see {@link Config} for the scoped embedding request configuration service
|
|
275
|
+
*
|
|
139
276
|
* @category configuration
|
|
140
277
|
* @since 4.0.0
|
|
141
278
|
*/
|
|
@@ -143,6 +280,19 @@ export const withConfigOverride: {
|
|
|
143
280
|
/**
|
|
144
281
|
* Provides config overrides for OpenAI embedding model operations.
|
|
145
282
|
*
|
|
283
|
+
* **When to use**
|
|
284
|
+
*
|
|
285
|
+
* Use when a single effect or workflow needs scoped OpenAI embedding request
|
|
286
|
+
* defaults without rebuilding the embedding model service.
|
|
287
|
+
*
|
|
288
|
+
* **Details**
|
|
289
|
+
*
|
|
290
|
+
* Supports both data-first and data-last forms. Existing scoped config is read
|
|
291
|
+
* first, then the provided overrides are applied so override fields take
|
|
292
|
+
* precedence.
|
|
293
|
+
*
|
|
294
|
+
* @see {@link Config} for the scoped embedding request configuration service
|
|
295
|
+
*
|
|
146
296
|
* @category configuration
|
|
147
297
|
* @since 4.0.0
|
|
148
298
|
*/
|