@effect/ai-openai-compat 4.0.0-beta.99 → 4.0.0-rc.109
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/AGENTS.md +395 -0
- package/CLAUDE.md +395 -0
- package/README.md +14 -0
- package/ai-docs/README.md +44 -0
- package/ai-docs/package.json +36 -0
- package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
- package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +39 -0
- package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
- package/ai-docs/src/01_effect/01_basics/index.md +5 -0
- package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
- package/ai-docs/src/01_effect/02_schema/index.md +7 -0
- package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
- package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
- package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
- package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
- package/ai-docs/src/01_effect/03_services/index.md +5 -0
- package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
- package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
- package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
- package/ai-docs/src/01_effect/04_errors/index.md +1 -0
- package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
- package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
- package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
- package/ai-docs/src/01_effect/05_resources/index.md +3 -0
- package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
- package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
- package/ai-docs/src/01_effect/06_running/index.md +1 -0
- package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
- package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
- package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
- package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
- package/ai-docs/src/03_stream/30_encoding.ts +165 -0
- package/ai-docs/src/03_stream/index.md +4 -0
- package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
- package/ai-docs/src/04_integration/index.md +5 -0
- package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
- package/ai-docs/src/05_batching/index.md +3 -0
- package/ai-docs/src/06_schedule/10_schedules.ts +110 -0
- package/ai-docs/src/06_schedule/index.md +3 -0
- package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
- package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
- package/ai-docs/src/07_datetime/index.md +5 -0
- package/ai-docs/src/08_observability/10_logging.ts +66 -0
- package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
- package/ai-docs/src/08_observability/index.md +7 -0
- package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
- package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
- package/ai-docs/src/09_testing/index.md +1 -0
- package/ai-docs/src/10_predicate/01_basics.ts +14 -0
- package/ai-docs/src/10_predicate/index.md +9 -0
- package/ai-docs/src/40_sql/10_basics.ts +175 -0
- package/ai-docs/src/40_sql/index.md +6 -0
- package/ai-docs/src/50_http-client/10_basics.ts +102 -0
- package/ai-docs/src/50_http-client/index.md +3 -0
- package/ai-docs/src/51_http-server/10_basics.ts +118 -0
- package/ai-docs/src/51_http-server/20_testing.ts +100 -0
- package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
- package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
- package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
- package/ai-docs/src/51_http-server/fixtures/api/Users.ts +107 -0
- package/ai-docs/src/51_http-server/fixtures/domain/User.ts +21 -0
- package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
- package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +37 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +80 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users.ts +183 -0
- package/ai-docs/src/51_http-server/index.md +3 -0
- package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
- package/ai-docs/src/60_child-process/index.md +3 -0
- package/ai-docs/src/70_cli/10_basics.ts +160 -0
- package/ai-docs/src/70_cli/index.md +5 -0
- package/ai-docs/src/71_ai/10_language-model.ts +156 -0
- package/ai-docs/src/71_ai/20_tools.ts +226 -0
- package/ai-docs/src/71_ai/30_chat.ts +158 -0
- package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
- package/ai-docs/src/71_ai/index.md +5 -0
- package/ai-docs/src/80_cluster/10_entities.ts +97 -0
- package/ai-docs/src/80_cluster/index.md +4 -0
- package/ai-docs/src/index.md +10 -0
- package/ai-docs/tsconfig.json +24 -0
- package/dist/OpenAiClient.d.ts +63 -53
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +31 -23
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiConfig.d.ts +1 -1
- package/dist/OpenAiConfig.js +1 -1
- package/dist/OpenAiConfig.js.map +1 -1
- package/dist/OpenAiEmbeddingModel.d.ts +1 -1
- package/dist/OpenAiEmbeddingModel.d.ts.map +1 -1
- package/dist/OpenAiEmbeddingModel.js +6 -9
- package/dist/OpenAiEmbeddingModel.js.map +1 -1
- package/dist/OpenAiLanguageModel.d.ts +17 -17
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +85 -24
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/dist/OpenAiTelemetry.d.ts +1 -1
- package/dist/OpenAiTelemetry.d.ts.map +1 -1
- package/dist/OpenAiTelemetry.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal/errors.js +9 -1
- package/dist/internal/errors.js.map +1 -1
- package/dist/internal/utilities.js +1 -1
- package/dist/internal/utilities.js.map +1 -1
- package/package.json +9 -7
- package/src/OpenAiClient.ts +124 -98
- package/src/OpenAiConfig.ts +2 -2
- package/src/OpenAiEmbeddingModel.ts +4 -5
- package/src/OpenAiLanguageModel.ts +125 -38
- package/src/OpenAiTelemetry.ts +1 -1
- package/src/internal/errors.ts +13 -1
- package/src/internal/utilities.ts +1 -1
package/src/OpenAiClient.ts
CHANGED
|
@@ -11,7 +11,7 @@ import * as Array from "effect/Array"
|
|
|
11
11
|
import type * as Config from "effect/Config"
|
|
12
12
|
import * as Context from "effect/Context"
|
|
13
13
|
import * as Effect from "effect/Effect"
|
|
14
|
-
import { identity
|
|
14
|
+
import { identity } from "effect/Function"
|
|
15
15
|
import * as Layer from "effect/Layer"
|
|
16
16
|
import * as Redacted from "effect/Redacted"
|
|
17
17
|
import * as Schema from "effect/Schema"
|
|
@@ -34,7 +34,7 @@ import { OpenAiConfig } from "./OpenAiConfig.ts"
|
|
|
34
34
|
* completions, streaming chat completions, and embeddings. Transport and
|
|
35
35
|
* schema decoding failures are mapped to `AiError`.
|
|
36
36
|
*
|
|
37
|
-
* @category
|
|
37
|
+
* @category services
|
|
38
38
|
* @since 4.0.0
|
|
39
39
|
*/
|
|
40
40
|
export interface Service {
|
|
@@ -99,10 +99,15 @@ export type Options = {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
const RedactedOpenAiHeaders = {
|
|
102
|
-
OpenAiOrganization: "
|
|
103
|
-
OpenAiProject: "
|
|
102
|
+
OpenAiOrganization: "openai-organization",
|
|
103
|
+
OpenAiProject: "openai-project"
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
const withRedactedHeaders = Effect.updateService(
|
|
107
|
+
Headers.CurrentRedactedNames,
|
|
108
|
+
Array.appendAll(Object.values(RedactedOpenAiHeaders))
|
|
109
|
+
)
|
|
110
|
+
|
|
106
111
|
/**
|
|
107
112
|
* Constructs an OpenAI-compatible client service from explicit options.
|
|
108
113
|
*
|
|
@@ -175,24 +180,27 @@ export const make = Effect.fnUntraced(
|
|
|
175
180
|
[body: CreateResponse200, response: HttpClientResponse.HttpClientResponse],
|
|
176
181
|
AiError.AiError
|
|
177
182
|
> =>
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
HttpClientRequest.post("/chat/completions")
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
183
|
+
resolveHttpClient.pipe(
|
|
184
|
+
Effect.flatMap((client) =>
|
|
185
|
+
HttpClientRequest.post("/chat/completions").pipe(
|
|
186
|
+
HttpClientRequest.bodyJsonUnsafe(payload),
|
|
187
|
+
HttpClient.filterStatusOk(client).execute,
|
|
188
|
+
Effect.flatMap((response) =>
|
|
189
|
+
Effect.map(decodeResponse(response), (
|
|
190
|
+
body
|
|
191
|
+
): [CreateResponse200, HttpClientResponse.HttpClientResponse] => [
|
|
192
|
+
body,
|
|
193
|
+
response
|
|
194
|
+
])
|
|
195
|
+
),
|
|
196
|
+
Effect.catchTags({
|
|
197
|
+
HttpClientError: (error) => Errors.mapHttpClientError(error, "createResponse"),
|
|
198
|
+
SchemaError: (error) => Effect.fail(Errors.mapSchemaError(error, "createResponse"))
|
|
199
|
+
})
|
|
200
|
+
)
|
|
201
|
+
),
|
|
202
|
+
withRedactedHeaders
|
|
203
|
+
)
|
|
196
204
|
|
|
197
205
|
const buildResponseStream = (
|
|
198
206
|
response: HttpClientResponse.HttpClientResponse
|
|
@@ -210,6 +218,7 @@ export const make = Effect.fnUntraced(
|
|
|
210
218
|
Stream.takeUntil((event) => event === "[DONE]"),
|
|
211
219
|
Stream.catchTags({
|
|
212
220
|
Retry: (error) => Stream.die(error),
|
|
221
|
+
SseError: (error) => Stream.fail(Errors.mapSseError(error, "createResponseStream")),
|
|
213
222
|
HttpClientError: (error) => Stream.fromEffect(Errors.mapHttpClientError(error, "createResponseStream"))
|
|
214
223
|
})
|
|
215
224
|
) as any
|
|
@@ -217,40 +226,46 @@ export const make = Effect.fnUntraced(
|
|
|
217
226
|
}
|
|
218
227
|
|
|
219
228
|
const createResponseStream: Service["createResponseStream"] = (payload) =>
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
HttpClientRequest.post("/chat/completions")
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
229
|
+
resolveHttpClient.pipe(
|
|
230
|
+
Effect.flatMap((client) =>
|
|
231
|
+
HttpClientRequest.post("/chat/completions").pipe(
|
|
232
|
+
HttpClientRequest.bodyJsonUnsafe({
|
|
233
|
+
...payload,
|
|
234
|
+
stream: true,
|
|
235
|
+
stream_options: {
|
|
236
|
+
include_usage: true
|
|
237
|
+
}
|
|
238
|
+
}),
|
|
239
|
+
HttpClient.filterStatusOk(client).execute,
|
|
240
|
+
Effect.map(buildResponseStream),
|
|
241
|
+
Effect.catchTag(
|
|
242
|
+
"HttpClientError",
|
|
243
|
+
(error) => Errors.mapHttpClientError(error, "createResponseStream")
|
|
244
|
+
)
|
|
235
245
|
)
|
|
236
|
-
)
|
|
246
|
+
),
|
|
247
|
+
withRedactedHeaders
|
|
248
|
+
)
|
|
237
249
|
|
|
238
250
|
const decodeEmbedding = HttpClientResponse.schemaBodyJson(CreateEmbeddingResponseSchema)
|
|
239
251
|
|
|
240
252
|
const createEmbedding = (
|
|
241
253
|
payload: CreateEmbeddingRequestJson
|
|
242
254
|
): Effect.Effect<CreateEmbedding200, AiError.AiError> =>
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
HttpClientRequest.post("/embeddings")
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
255
|
+
resolveHttpClient.pipe(
|
|
256
|
+
Effect.flatMap((client) =>
|
|
257
|
+
HttpClientRequest.post("/embeddings").pipe(
|
|
258
|
+
HttpClientRequest.bodyJsonUnsafe(payload),
|
|
259
|
+
HttpClient.filterStatusOk(client).execute,
|
|
260
|
+
Effect.flatMap(decodeEmbedding),
|
|
261
|
+
Effect.catchTags({
|
|
262
|
+
HttpClientError: (error) => Errors.mapHttpClientError(error, "createEmbedding"),
|
|
263
|
+
SchemaError: (error) => Effect.fail(Errors.mapSchemaError(error, "createEmbedding"))
|
|
264
|
+
})
|
|
265
|
+
)
|
|
266
|
+
),
|
|
267
|
+
withRedactedHeaders
|
|
268
|
+
)
|
|
254
269
|
|
|
255
270
|
return OpenAiClient.of({
|
|
256
271
|
client: httpClient,
|
|
@@ -259,10 +274,7 @@ export const make = Effect.fnUntraced(
|
|
|
259
274
|
createEmbedding
|
|
260
275
|
})
|
|
261
276
|
},
|
|
262
|
-
|
|
263
|
-
Headers.CurrentRedactedNames,
|
|
264
|
-
Array.appendAll(Object.values(RedactedOpenAiHeaders))
|
|
265
|
-
)
|
|
277
|
+
withRedactedHeaders
|
|
266
278
|
)
|
|
267
279
|
|
|
268
280
|
/**
|
|
@@ -340,7 +352,7 @@ type JsonObject = { readonly [x: string]: Schema.Json }
|
|
|
340
352
|
/**
|
|
341
353
|
* Optional response fields that can be requested with the `include` parameter.
|
|
342
354
|
*
|
|
343
|
-
* @category
|
|
355
|
+
* @category models
|
|
344
356
|
* @since 4.0.0
|
|
345
357
|
*/
|
|
346
358
|
export type IncludeEnum =
|
|
@@ -379,7 +391,7 @@ type InputFileContent = {
|
|
|
379
391
|
/**
|
|
380
392
|
* Content blocks accepted in input messages.
|
|
381
393
|
*
|
|
382
|
-
* @category
|
|
394
|
+
* @category models
|
|
383
395
|
* @since 4.0.0
|
|
384
396
|
*/
|
|
385
397
|
export type InputContent = InputTextContent | InputImageContent | InputFileContent
|
|
@@ -387,7 +399,7 @@ export type InputContent = InputTextContent | InputImageContent | InputFileConte
|
|
|
387
399
|
/**
|
|
388
400
|
* Text content block used for model-provided reasoning summaries.
|
|
389
401
|
*
|
|
390
|
-
* @category
|
|
402
|
+
* @category models
|
|
391
403
|
* @since 4.0.0
|
|
392
404
|
*/
|
|
393
405
|
export type SummaryTextContent = {
|
|
@@ -449,7 +461,7 @@ type FilePathAnnotation = {
|
|
|
449
461
|
/**
|
|
450
462
|
* Citation and file-path annotations attached to output text content.
|
|
451
463
|
*
|
|
452
|
-
* @category
|
|
464
|
+
* @category models
|
|
453
465
|
* @since 4.0.0
|
|
454
466
|
*/
|
|
455
467
|
export type Annotation =
|
|
@@ -488,7 +500,7 @@ type OutputMessage = {
|
|
|
488
500
|
* Reasoning output item containing encrypted reasoning content, summaries, and
|
|
489
501
|
* optional reasoning text.
|
|
490
502
|
*
|
|
491
|
-
* @category
|
|
503
|
+
* @category models
|
|
492
504
|
* @since 4.0.0
|
|
493
505
|
*/
|
|
494
506
|
export type ReasoningItem = {
|
|
@@ -545,7 +557,7 @@ type ItemReference = {
|
|
|
545
557
|
* Supports input messages, output messages, tool calls, tool outputs, reasoning
|
|
546
558
|
* items, custom tool interactions, and item references.
|
|
547
559
|
*
|
|
548
|
-
* @category
|
|
560
|
+
* @category models
|
|
549
561
|
* @since 4.0.0
|
|
550
562
|
*/
|
|
551
563
|
export type InputItem =
|
|
@@ -586,7 +598,7 @@ type CustomToolParam = {
|
|
|
586
598
|
/**
|
|
587
599
|
* Tool definitions that can be supplied to a Responses-style request.
|
|
588
600
|
*
|
|
589
|
-
* @category
|
|
601
|
+
* @category models
|
|
590
602
|
* @since 4.0.0
|
|
591
603
|
*/
|
|
592
604
|
export type Tool =
|
|
@@ -637,7 +649,7 @@ export type TextResponseFormatConfiguration =
|
|
|
637
649
|
* Request options for creating a Responses-style response with an
|
|
638
650
|
* OpenAI-compatible provider.
|
|
639
651
|
*
|
|
640
|
-
* @category
|
|
652
|
+
* @category models
|
|
641
653
|
* @since 4.0.0
|
|
642
654
|
*/
|
|
643
655
|
export type CreateResponse = {
|
|
@@ -677,7 +689,7 @@ export type CreateResponse = {
|
|
|
677
689
|
/**
|
|
678
690
|
* Token accounting reported on Responses-style response objects.
|
|
679
691
|
*
|
|
680
|
-
* @category
|
|
692
|
+
* @category models
|
|
681
693
|
* @since 4.0.0
|
|
682
694
|
*/
|
|
683
695
|
export type ResponseUsage = {
|
|
@@ -698,7 +710,7 @@ type OutputItem =
|
|
|
698
710
|
* Responses-style response object returned by compatible providers or embedded
|
|
699
711
|
* in response stream lifecycle events.
|
|
700
712
|
*
|
|
701
|
-
* @category
|
|
713
|
+
* @category models
|
|
702
714
|
* @since 4.0.0
|
|
703
715
|
*/
|
|
704
716
|
export type Response = {
|
|
@@ -855,7 +867,7 @@ export type ResponseStreamEvent =
|
|
|
855
867
|
* string. The `index` field identifies the input item that produced this
|
|
856
868
|
* embedding.
|
|
857
869
|
*
|
|
858
|
-
* @category
|
|
870
|
+
* @category models
|
|
859
871
|
* @since 4.0.0
|
|
860
872
|
*/
|
|
861
873
|
export type Embedding = {
|
|
@@ -867,7 +879,7 @@ export type Embedding = {
|
|
|
867
879
|
/**
|
|
868
880
|
* Request payload for the embeddings endpoint.
|
|
869
881
|
*
|
|
870
|
-
* @category
|
|
882
|
+
* @category models
|
|
871
883
|
* @since 4.0.0
|
|
872
884
|
*/
|
|
873
885
|
export type CreateEmbeddingRequest = {
|
|
@@ -881,7 +893,7 @@ export type CreateEmbeddingRequest = {
|
|
|
881
893
|
/**
|
|
882
894
|
* Successful response payload returned by the embeddings endpoint.
|
|
883
895
|
*
|
|
884
|
-
* @category
|
|
896
|
+
* @category models
|
|
885
897
|
* @since 4.0.0
|
|
886
898
|
*/
|
|
887
899
|
export type CreateEmbeddingResponse = {
|
|
@@ -897,21 +909,21 @@ export type CreateEmbeddingResponse = {
|
|
|
897
909
|
/**
|
|
898
910
|
* JSON request body accepted by the embeddings endpoint.
|
|
899
911
|
*
|
|
900
|
-
* @category
|
|
912
|
+
* @category models
|
|
901
913
|
* @since 4.0.0
|
|
902
914
|
*/
|
|
903
915
|
export type CreateEmbeddingRequestJson = CreateEmbeddingRequest
|
|
904
916
|
/**
|
|
905
917
|
* Decoded successful embeddings response body.
|
|
906
918
|
*
|
|
907
|
-
* @category
|
|
919
|
+
* @category models
|
|
908
920
|
* @since 4.0.0
|
|
909
921
|
*/
|
|
910
922
|
export type CreateEmbedding200 = CreateEmbeddingResponse
|
|
911
923
|
/**
|
|
912
924
|
* Structured content parts accepted in chat completion messages.
|
|
913
925
|
*
|
|
914
|
-
* @category
|
|
926
|
+
* @category models
|
|
915
927
|
* @since 4.0.0
|
|
916
928
|
*/
|
|
917
929
|
export type ChatCompletionContentPart =
|
|
@@ -929,7 +941,7 @@ export type ChatCompletionContentPart =
|
|
|
929
941
|
/**
|
|
930
942
|
* Tool call data attached to an assistant chat completion message.
|
|
931
943
|
*
|
|
932
|
-
* @category
|
|
944
|
+
* @category models
|
|
933
945
|
* @since 4.0.0
|
|
934
946
|
*/
|
|
935
947
|
export type ChatCompletionRequestToolCall = {
|
|
@@ -943,7 +955,7 @@ export type ChatCompletionRequestToolCall = {
|
|
|
943
955
|
/**
|
|
944
956
|
* Message shapes accepted by the chat completions endpoint.
|
|
945
957
|
*
|
|
946
|
-
* @category
|
|
958
|
+
* @category models
|
|
947
959
|
* @since 4.0.0
|
|
948
960
|
*/
|
|
949
961
|
export type ChatCompletionRequestMessage =
|
|
@@ -960,7 +972,7 @@ export type ChatCompletionRequestMessage =
|
|
|
960
972
|
/**
|
|
961
973
|
* Function tool definition accepted by the chat completions endpoint.
|
|
962
974
|
*
|
|
963
|
-
* @category
|
|
975
|
+
* @category models
|
|
964
976
|
* @since 4.0.0
|
|
965
977
|
*/
|
|
966
978
|
export type ChatCompletionTool = {
|
|
@@ -1010,7 +1022,7 @@ export type ChatCompletionResponseFormat =
|
|
|
1010
1022
|
/**
|
|
1011
1023
|
* Request payload for the OpenAI-compatible chat completions endpoint.
|
|
1012
1024
|
*
|
|
1013
|
-
* @category
|
|
1025
|
+
* @category models
|
|
1014
1026
|
* @since 4.0.0
|
|
1015
1027
|
*/
|
|
1016
1028
|
export type ChatCompletionRequest = {
|
|
@@ -1036,14 +1048,14 @@ export type ChatCompletionRequest = {
|
|
|
1036
1048
|
/**
|
|
1037
1049
|
* JSON request body used by this client when creating a chat completion response.
|
|
1038
1050
|
*
|
|
1039
|
-
* @category
|
|
1051
|
+
* @category models
|
|
1040
1052
|
* @since 4.0.0
|
|
1041
1053
|
*/
|
|
1042
1054
|
export type CreateResponseRequestJson = ChatCompletionRequest
|
|
1043
1055
|
/**
|
|
1044
1056
|
* Decoded successful chat completion response body returned by `createResponse`.
|
|
1045
1057
|
*
|
|
1046
|
-
* @category
|
|
1058
|
+
* @category models
|
|
1047
1059
|
* @since 4.0.0
|
|
1048
1060
|
*/
|
|
1049
1061
|
export type CreateResponse200 = ChatCompletionResponse
|
|
@@ -1056,8 +1068,8 @@ export type CreateResponse200 = ChatCompletionResponse
|
|
|
1056
1068
|
export type CreateResponse200Sse = ChatCompletionStreamEvent
|
|
1057
1069
|
|
|
1058
1070
|
const EmbeddingSchema = Schema.Struct({
|
|
1059
|
-
embedding: Schema.Union([Schema.Array(Schema.
|
|
1060
|
-
index: Schema.
|
|
1071
|
+
embedding: Schema.Union([Schema.Array(Schema.Finite), Schema.String]),
|
|
1072
|
+
index: Schema.Int,
|
|
1061
1073
|
object: Schema.optionalKey(Schema.String)
|
|
1062
1074
|
})
|
|
1063
1075
|
|
|
@@ -1066,8 +1078,8 @@ const CreateEmbeddingResponseSchema = Schema.Struct({
|
|
|
1066
1078
|
model: Schema.String,
|
|
1067
1079
|
object: Schema.optionalKey(Schema.Literal("list")),
|
|
1068
1080
|
usage: Schema.optionalKey(Schema.Struct({
|
|
1069
|
-
prompt_tokens: Schema.
|
|
1070
|
-
total_tokens: Schema.
|
|
1081
|
+
prompt_tokens: Schema.Int,
|
|
1082
|
+
total_tokens: Schema.Int
|
|
1071
1083
|
}))
|
|
1072
1084
|
})
|
|
1073
1085
|
|
|
@@ -1086,14 +1098,14 @@ const ChatCompletionToolFunctionDelta = Schema.Struct({
|
|
|
1086
1098
|
|
|
1087
1099
|
const ChatCompletionToolCall = Schema.Struct({
|
|
1088
1100
|
id: Schema.optionalKey(Schema.String),
|
|
1089
|
-
index: Schema.optionalKey(Schema.
|
|
1101
|
+
index: Schema.optionalKey(Schema.Int),
|
|
1090
1102
|
type: Schema.optionalKey(Schema.String),
|
|
1091
1103
|
function: Schema.optionalKey(ChatCompletionToolFunction)
|
|
1092
1104
|
})
|
|
1093
1105
|
|
|
1094
1106
|
const ChatCompletionToolCallDelta = Schema.Struct({
|
|
1095
1107
|
id: Schema.optionalKey(Schema.String),
|
|
1096
|
-
index: Schema.optionalKey(Schema.
|
|
1108
|
+
index: Schema.optionalKey(Schema.Int),
|
|
1097
1109
|
type: Schema.optionalKey(Schema.String),
|
|
1098
1110
|
function: Schema.optionalKey(ChatCompletionToolFunctionDelta)
|
|
1099
1111
|
})
|
|
@@ -1115,16 +1127,16 @@ const ChatCompletionDelta = Schema.Struct({
|
|
|
1115
1127
|
})
|
|
1116
1128
|
|
|
1117
1129
|
const ChatCompletionChoice = Schema.Struct({
|
|
1118
|
-
index: Schema.
|
|
1130
|
+
index: Schema.Int,
|
|
1119
1131
|
finish_reason: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
1120
1132
|
message: Schema.optionalKey(ChatCompletionMessage),
|
|
1121
1133
|
delta: Schema.optionalKey(ChatCompletionDelta)
|
|
1122
1134
|
})
|
|
1123
1135
|
|
|
1124
1136
|
const ChatCompletionUsage = Schema.Struct({
|
|
1125
|
-
prompt_tokens: Schema.
|
|
1126
|
-
completion_tokens: Schema.
|
|
1127
|
-
total_tokens: Schema.
|
|
1137
|
+
prompt_tokens: Schema.Int,
|
|
1138
|
+
completion_tokens: Schema.Int,
|
|
1139
|
+
total_tokens: Schema.Int,
|
|
1128
1140
|
prompt_tokens_details: Schema.optionalKey(Schema.Any),
|
|
1129
1141
|
completion_tokens_details: Schema.optionalKey(Schema.Any)
|
|
1130
1142
|
})
|
|
@@ -1132,7 +1144,7 @@ const ChatCompletionUsage = Schema.Struct({
|
|
|
1132
1144
|
const ChatCompletionResponse = Schema.Struct({
|
|
1133
1145
|
id: Schema.String,
|
|
1134
1146
|
model: Schema.String,
|
|
1135
|
-
created: Schema.
|
|
1147
|
+
created: Schema.Int,
|
|
1136
1148
|
choices: Schema.Array(ChatCompletionChoice),
|
|
1137
1149
|
usage: Schema.optionalKey(Schema.NullOr(ChatCompletionUsage)),
|
|
1138
1150
|
service_tier: Schema.optionalKey(Schema.String)
|
|
@@ -1141,7 +1153,7 @@ const ChatCompletionResponse = Schema.Struct({
|
|
|
1141
1153
|
const ChatCompletionChunk = Schema.Struct({
|
|
1142
1154
|
id: Schema.String,
|
|
1143
1155
|
model: Schema.String,
|
|
1144
|
-
created: Schema.
|
|
1156
|
+
created: Schema.Int,
|
|
1145
1157
|
choices: Schema.Array(ChatCompletionChoice),
|
|
1146
1158
|
usage: Schema.optionalKey(Schema.NullOr(ChatCompletionUsage)),
|
|
1147
1159
|
service_tier: Schema.optionalKey(Schema.String)
|
|
@@ -1150,35 +1162,35 @@ const ChatCompletionChunk = Schema.Struct({
|
|
|
1150
1162
|
/**
|
|
1151
1163
|
* Decoded tool-call object from a chat completion response or streaming chunk.
|
|
1152
1164
|
*
|
|
1153
|
-
* @category
|
|
1165
|
+
* @category models
|
|
1154
1166
|
* @since 4.0.0
|
|
1155
1167
|
*/
|
|
1156
1168
|
export type ChatCompletionToolCall = typeof ChatCompletionToolCall.Type
|
|
1157
1169
|
/**
|
|
1158
1170
|
* Decoded message object from a non-streaming chat completion choice.
|
|
1159
1171
|
*
|
|
1160
|
-
* @category
|
|
1172
|
+
* @category models
|
|
1161
1173
|
* @since 4.0.0
|
|
1162
1174
|
*/
|
|
1163
1175
|
export type ChatCompletionMessage = typeof ChatCompletionMessage.Type
|
|
1164
1176
|
/**
|
|
1165
1177
|
* Decoded choice object returned by chat completion responses and chunks.
|
|
1166
1178
|
*
|
|
1167
|
-
* @category
|
|
1179
|
+
* @category models
|
|
1168
1180
|
* @since 4.0.0
|
|
1169
1181
|
*/
|
|
1170
1182
|
export type ChatCompletionChoice = typeof ChatCompletionChoice.Type
|
|
1171
1183
|
/**
|
|
1172
1184
|
* Decoded token usage summary returned by chat completions.
|
|
1173
1185
|
*
|
|
1174
|
-
* @category
|
|
1186
|
+
* @category models
|
|
1175
1187
|
* @since 4.0.0
|
|
1176
1188
|
*/
|
|
1177
1189
|
export type ChatCompletionUsage = typeof ChatCompletionUsage.Type
|
|
1178
1190
|
/**
|
|
1179
1191
|
* Decoded successful response from the chat completions endpoint.
|
|
1180
1192
|
*
|
|
1181
|
-
* @category
|
|
1193
|
+
* @category models
|
|
1182
1194
|
* @since 4.0.0
|
|
1183
1195
|
*/
|
|
1184
1196
|
export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
|
|
@@ -1190,13 +1202,23 @@ export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
|
|
|
1190
1202
|
*/
|
|
1191
1203
|
export type ChatCompletionChunk = typeof ChatCompletionChunk.Type
|
|
1192
1204
|
/**
|
|
1193
|
-
*
|
|
1194
|
-
* sentinel.
|
|
1205
|
+
* A parsed chat completion event that does not match the expected chunk schema.
|
|
1195
1206
|
*
|
|
1196
1207
|
* @category streaming
|
|
1197
1208
|
* @since 4.0.0
|
|
1198
1209
|
*/
|
|
1199
|
-
export
|
|
1210
|
+
export interface UnknownChatCompletionEvent {
|
|
1211
|
+
readonly _tag: "UnknownChatCompletionEvent"
|
|
1212
|
+
readonly data: unknown
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* Streaming chat completion event, including decoded chunks, unknown parsed
|
|
1216
|
+
* events, and the `[DONE]` sentinel.
|
|
1217
|
+
*
|
|
1218
|
+
* @category streaming
|
|
1219
|
+
* @since 4.0.0
|
|
1220
|
+
*/
|
|
1221
|
+
export type ChatCompletionStreamEvent = ChatCompletionChunk | UnknownChatCompletionEvent | "[DONE]"
|
|
1200
1222
|
|
|
1201
1223
|
const parseJson = (value: string): unknown => {
|
|
1202
1224
|
try {
|
|
@@ -1215,7 +1237,11 @@ const decodeChatCompletionSseData = (
|
|
|
1215
1237
|
return data
|
|
1216
1238
|
}
|
|
1217
1239
|
const parsed = parseJson(data)
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1240
|
+
if (parsed === undefined) {
|
|
1241
|
+
return undefined
|
|
1242
|
+
}
|
|
1243
|
+
return isChatCompletionChunk(parsed) ? parsed : {
|
|
1244
|
+
_tag: "UnknownChatCompletionEvent",
|
|
1245
|
+
data: parsed
|
|
1246
|
+
}
|
|
1221
1247
|
}
|
package/src/OpenAiConfig.ts
CHANGED
|
@@ -36,7 +36,7 @@ export class OpenAiConfig extends Context.Service<
|
|
|
36
36
|
*/
|
|
37
37
|
static readonly getOrUndefined: Effect.Effect<typeof OpenAiConfig.Service | undefined> = Effect.map(
|
|
38
38
|
Effect.context<never>(),
|
|
39
|
-
|
|
39
|
+
Context.getOrUndefined(OpenAiConfig)
|
|
40
40
|
)
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -50,7 +50,7 @@ export declare namespace OpenAiConfig {
|
|
|
50
50
|
* Configuration consumed by OpenAI-compatible clients when they build or
|
|
51
51
|
* resolve the underlying HTTP client.
|
|
52
52
|
*
|
|
53
|
-
* @category
|
|
53
|
+
* @category services
|
|
54
54
|
* @since 4.0.0
|
|
55
55
|
*/
|
|
56
56
|
export interface Service {
|
|
@@ -46,7 +46,7 @@ type ModelConfig = Omit<ConfigOptions, "model"> & { readonly [x: string]: unknow
|
|
|
46
46
|
*
|
|
47
47
|
* @see {@link withConfigOverride} for scoping embedding request overrides
|
|
48
48
|
*
|
|
49
|
-
* @category
|
|
49
|
+
* @category services
|
|
50
50
|
* @since 4.0.0
|
|
51
51
|
*/
|
|
52
52
|
export class Config extends Context.Service<
|
|
@@ -121,10 +121,9 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
121
121
|
}): Effect.fn.Return<EmbeddingModel.Service, never, OpenAiClient> {
|
|
122
122
|
const client = yield* OpenAiClient
|
|
123
123
|
|
|
124
|
-
const makeConfig = Effect.
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
})
|
|
124
|
+
const makeConfig = Effect.contextWith((services: Context.Context<never>) =>
|
|
125
|
+
Effect.succeed({ model, ...providerConfig, ...Context.getOrUndefined(services, Config) })
|
|
126
|
+
)
|
|
128
127
|
|
|
129
128
|
return yield* EmbeddingModel.make({
|
|
130
129
|
embedMany: Effect.fnUntraced(function*({ inputs }) {
|