@effect/ai-openai-compat 4.0.0-beta.7 → 4.0.0-beta.71
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 +250 -51
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +108 -9
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiConfig.d.ts +83 -10
- package/dist/OpenAiConfig.d.ts.map +1 -1
- package/dist/OpenAiConfig.js +51 -7
- package/dist/OpenAiConfig.js.map +1 -1
- package/dist/OpenAiEmbeddingModel.d.ts +214 -0
- package/dist/OpenAiEmbeddingModel.d.ts.map +1 -0
- package/dist/OpenAiEmbeddingModel.js +218 -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 +326 -18
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +126 -25
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/dist/OpenAiTelemetry.d.ts +72 -22
- package/dist/OpenAiTelemetry.d.ts.map +1 -1
- package/dist/OpenAiTelemetry.js +47 -8
- 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 +283 -49
- package/src/OpenAiConfig.ts +84 -11
- package/src/OpenAiEmbeddingModel.ts +360 -0
- package/src/OpenAiError.ts +111 -35
- package/src/OpenAiLanguageModel.ts +409 -40
- package/src/OpenAiTelemetry.ts +103 -27
- package/src/index.ts +11 -17
- package/src/internal/errors.ts +4 -4
package/src/OpenAiClient.ts
CHANGED
|
@@ -1,14 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `OpenAiClient` module provides an Effect service for calling
|
|
3
|
+
* OpenAI-compatible chat completions and embeddings APIs. It builds on the
|
|
4
|
+
* Effect HTTP client, adds authentication and OpenAI header handling, and
|
|
5
|
+
* exposes typed helpers for regular responses, server-sent event streaming, and
|
|
6
|
+
* embedding requests.
|
|
7
|
+
*
|
|
8
|
+
* **Common tasks**
|
|
9
|
+
*
|
|
10
|
+
* - Create a client service directly with {@link make}
|
|
11
|
+
* - Provide the service as a layer with {@link layer} or {@link layerConfig}
|
|
12
|
+
* - Send non-streaming chat completion requests with `createResponse`
|
|
13
|
+
* - Send streaming chat completion requests with `createResponseStream`
|
|
14
|
+
* - Generate embeddings with `createEmbedding`
|
|
15
|
+
* - Reuse the exported request and response types when integrating compatible providers
|
|
16
|
+
*
|
|
17
|
+
* **Gotchas**
|
|
18
|
+
*
|
|
19
|
+
* - The default base URL is `https://api.openai.com/v1`; set `apiUrl` for other
|
|
20
|
+
* OpenAI-compatible providers.
|
|
21
|
+
* - `createResponseStream` forces `stream: true` and requests usage events with
|
|
22
|
+
* `stream_options.include_usage`.
|
|
23
|
+
* - HTTP and schema decoding failures are mapped into `AiError`.
|
|
24
|
+
*
|
|
25
|
+
* @since 4.0.0
|
|
3
26
|
*/
|
|
4
27
|
import * as Array from "effect/Array"
|
|
5
28
|
import type * as Config from "effect/Config"
|
|
29
|
+
import * as Context from "effect/Context"
|
|
6
30
|
import * as Effect from "effect/Effect"
|
|
7
31
|
import { identity, pipe } from "effect/Function"
|
|
8
32
|
import * as Layer from "effect/Layer"
|
|
9
33
|
import * as Redacted from "effect/Redacted"
|
|
10
34
|
import * as Schema from "effect/Schema"
|
|
11
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
12
35
|
import * as Stream from "effect/Stream"
|
|
13
36
|
import type * as AiError from "effect/unstable/ai/AiError"
|
|
14
37
|
import * as Sse from "effect/unstable/encoding/Sse"
|
|
@@ -20,8 +43,16 @@ import * as Errors from "./internal/errors.ts"
|
|
|
20
43
|
import { OpenAiConfig } from "./OpenAiConfig.ts"
|
|
21
44
|
|
|
22
45
|
/**
|
|
23
|
-
*
|
|
46
|
+
* Effect service interface for OpenAI-compatible chat completions and embeddings.
|
|
47
|
+
*
|
|
48
|
+
* **Details**
|
|
49
|
+
*
|
|
50
|
+
* Exposes the configured HTTP client plus helpers for non-streaming chat
|
|
51
|
+
* completions, streaming chat completions, and embeddings. Transport and
|
|
52
|
+
* schema decoding failures are mapped to `AiError`.
|
|
53
|
+
*
|
|
24
54
|
* @category models
|
|
55
|
+
* @since 4.0.0
|
|
25
56
|
*/
|
|
26
57
|
export interface Service {
|
|
27
58
|
readonly client: HttpClient.HttpClient
|
|
@@ -46,16 +77,35 @@ export interface Service {
|
|
|
46
77
|
}
|
|
47
78
|
|
|
48
79
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
80
|
+
* Context service tag for the OpenAI-compatible chat completions and embeddings client.
|
|
81
|
+
*
|
|
82
|
+
* **When to use**
|
|
83
|
+
*
|
|
84
|
+
* Use when building effects that depend on the low-level OpenAI-compatible
|
|
85
|
+
* client through context rather than receiving the client as a value.
|
|
86
|
+
*
|
|
87
|
+
* **Details**
|
|
88
|
+
*
|
|
89
|
+
* The tagged service is the `Service` interface produced by `make` and provided
|
|
90
|
+
* by `layer` or `layerConfig`.
|
|
91
|
+
*
|
|
92
|
+
* @see {@link Service} for the operations provided by the service
|
|
93
|
+
* @see {@link make} for constructing the service from explicit options
|
|
94
|
+
* @see {@link layer} for providing the service from explicit options
|
|
95
|
+
* @see {@link layerConfig} for loading client settings from `Config`
|
|
96
|
+
*
|
|
97
|
+
* @category services
|
|
98
|
+
* @since 4.0.0
|
|
51
99
|
*/
|
|
52
|
-
export class OpenAiClient extends
|
|
100
|
+
export class OpenAiClient extends Context.Service<OpenAiClient, Service>()(
|
|
53
101
|
"@effect/ai-openai-compat/OpenAiClient"
|
|
54
102
|
) {}
|
|
55
103
|
|
|
56
104
|
/**
|
|
57
|
-
*
|
|
105
|
+
* Configuration options used to construct an OpenAI-compatible client.
|
|
106
|
+
*
|
|
58
107
|
* @category models
|
|
108
|
+
* @since 4.0.0
|
|
59
109
|
*/
|
|
60
110
|
export type Options = {
|
|
61
111
|
readonly apiKey?: Redacted.Redacted<string> | undefined
|
|
@@ -71,8 +121,30 @@ const RedactedOpenAiHeaders = {
|
|
|
71
121
|
}
|
|
72
122
|
|
|
73
123
|
/**
|
|
74
|
-
*
|
|
124
|
+
* Constructs an OpenAI-compatible client service from explicit options.
|
|
125
|
+
*
|
|
126
|
+
* **When to use**
|
|
127
|
+
*
|
|
128
|
+
* Use to construct the OpenAI-compatible client service inside an effect when
|
|
129
|
+
* you need the service value directly.
|
|
130
|
+
*
|
|
131
|
+
* **Details**
|
|
132
|
+
*
|
|
133
|
+
* The returned service uses the current `HttpClient`, prepends `apiUrl` or
|
|
134
|
+
* `https://api.openai.com/v1`, adds authentication and OpenAI
|
|
135
|
+
* organization/project headers, accepts JSON responses, and applies
|
|
136
|
+
* `transformClient` when provided.
|
|
137
|
+
*
|
|
138
|
+
* **Gotchas**
|
|
139
|
+
*
|
|
140
|
+
* A scoped `OpenAiConfig.withClientTransform` is applied when request helpers
|
|
141
|
+
* run, after the `transformClient` option supplied to `make`.
|
|
142
|
+
*
|
|
143
|
+
* @see {@link layer} for providing this client from explicit options
|
|
144
|
+
* @see {@link layerConfig} for loading client settings from `Config`
|
|
145
|
+
*
|
|
75
146
|
* @category constructors
|
|
147
|
+
* @since 4.0.0
|
|
76
148
|
*/
|
|
77
149
|
export const make = Effect.fnUntraced(
|
|
78
150
|
function*(options: Options): Effect.fn.Return<Service, never, HttpClient.HttpClient> {
|
|
@@ -212,21 +284,48 @@ export const make = Effect.fnUntraced(
|
|
|
212
284
|
)
|
|
213
285
|
|
|
214
286
|
/**
|
|
215
|
-
*
|
|
287
|
+
* Creates a layer that provides an OpenAI-compatible client from explicit options.
|
|
288
|
+
*
|
|
289
|
+
* **When to use**
|
|
290
|
+
*
|
|
291
|
+
* Use to install `OpenAiClient` in an application layer when the client options
|
|
292
|
+
* are already available as values rather than loaded from `Config`.
|
|
293
|
+
*
|
|
294
|
+
* @see {@link make} for constructing the client service effectfully
|
|
295
|
+
* @see {@link layerConfig} for loading client settings from `Config`
|
|
296
|
+
*
|
|
216
297
|
* @category layers
|
|
298
|
+
* @since 4.0.0
|
|
217
299
|
*/
|
|
218
300
|
export const layer = (options: Options): Layer.Layer<OpenAiClient, never, HttpClient.HttpClient> =>
|
|
219
301
|
Layer.effect(OpenAiClient, make(options))
|
|
220
302
|
|
|
221
303
|
/**
|
|
222
|
-
*
|
|
304
|
+
* Creates a layer that loads OpenAI-compatible client settings from `Config`
|
|
305
|
+
* values before constructing the service.
|
|
306
|
+
*
|
|
307
|
+
* **When to use**
|
|
308
|
+
*
|
|
309
|
+
* Use when OpenAI-compatible client settings should be read from Effect
|
|
310
|
+
* `Config` values while providing `OpenAiClient` as a layer.
|
|
311
|
+
*
|
|
312
|
+
* **Details**
|
|
313
|
+
*
|
|
314
|
+
* Only config values supplied in `options` are loaded. Omitted fields are
|
|
315
|
+
* passed to `make` as `undefined`, and `transformClient` is forwarded as a
|
|
316
|
+
* plain option.
|
|
317
|
+
*
|
|
318
|
+
* @see {@link make} for constructing the client service effectfully
|
|
319
|
+
* @see {@link layer} for providing the client from already-resolved options
|
|
320
|
+
*
|
|
223
321
|
* @category layers
|
|
322
|
+
* @since 4.0.0
|
|
224
323
|
*/
|
|
225
324
|
export const layerConfig = (options?: {
|
|
226
|
-
readonly apiKey?: Config.Config<Redacted.Redacted<string
|
|
325
|
+
readonly apiKey?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
227
326
|
readonly apiUrl?: Config.Config<string> | undefined
|
|
228
|
-
readonly organizationId?: Config.Config<Redacted.Redacted<string
|
|
229
|
-
readonly projectId?: Config.Config<Redacted.Redacted<string
|
|
327
|
+
readonly organizationId?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
328
|
+
readonly projectId?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
230
329
|
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
|
|
231
330
|
}): Layer.Layer<OpenAiClient, Config.ConfigError, HttpClient.HttpClient> =>
|
|
232
331
|
Layer.effect(
|
|
@@ -257,7 +356,10 @@ export const layerConfig = (options?: {
|
|
|
257
356
|
type JsonObject = { readonly [x: string]: Schema.Json }
|
|
258
357
|
|
|
259
358
|
/**
|
|
260
|
-
*
|
|
359
|
+
* Optional response fields that can be requested with the `include` parameter.
|
|
360
|
+
*
|
|
361
|
+
* @category response
|
|
362
|
+
* @since 4.0.0
|
|
261
363
|
*/
|
|
262
364
|
export type IncludeEnum =
|
|
263
365
|
| "message.input_image.image_url"
|
|
@@ -265,7 +367,10 @@ export type IncludeEnum =
|
|
|
265
367
|
| "message.output_text.logprobs"
|
|
266
368
|
|
|
267
369
|
/**
|
|
268
|
-
*
|
|
370
|
+
* Lifecycle status shared by message, reasoning, and tool-call items.
|
|
371
|
+
*
|
|
372
|
+
* @category models
|
|
373
|
+
* @since 4.0.0
|
|
269
374
|
*/
|
|
270
375
|
export type MessageStatus = "in_progress" | "completed" | "incomplete"
|
|
271
376
|
|
|
@@ -290,12 +395,18 @@ type InputFileContent = {
|
|
|
290
395
|
}
|
|
291
396
|
|
|
292
397
|
/**
|
|
293
|
-
*
|
|
398
|
+
* Content blocks accepted in input messages.
|
|
399
|
+
*
|
|
400
|
+
* @category request
|
|
401
|
+
* @since 4.0.0
|
|
294
402
|
*/
|
|
295
403
|
export type InputContent = InputTextContent | InputImageContent | InputFileContent
|
|
296
404
|
|
|
297
405
|
/**
|
|
298
|
-
*
|
|
406
|
+
* Text content block used for model-provided reasoning summaries.
|
|
407
|
+
*
|
|
408
|
+
* @category response
|
|
409
|
+
* @since 4.0.0
|
|
299
410
|
*/
|
|
300
411
|
export type SummaryTextContent = {
|
|
301
412
|
readonly type: "summary_text"
|
|
@@ -354,7 +465,10 @@ type FilePathAnnotation = {
|
|
|
354
465
|
}
|
|
355
466
|
|
|
356
467
|
/**
|
|
357
|
-
*
|
|
468
|
+
* Citation and file-path annotations attached to output text content.
|
|
469
|
+
*
|
|
470
|
+
* @category response
|
|
471
|
+
* @since 4.0.0
|
|
358
472
|
*/
|
|
359
473
|
export type Annotation =
|
|
360
474
|
| FileCitationAnnotation
|
|
@@ -389,7 +503,11 @@ type OutputMessage = {
|
|
|
389
503
|
}
|
|
390
504
|
|
|
391
505
|
/**
|
|
392
|
-
*
|
|
506
|
+
* Reasoning output item containing encrypted reasoning content, summaries, and
|
|
507
|
+
* optional reasoning text.
|
|
508
|
+
*
|
|
509
|
+
* @category response
|
|
510
|
+
* @since 4.0.0
|
|
393
511
|
*/
|
|
394
512
|
export type ReasoningItem = {
|
|
395
513
|
readonly type: "reasoning"
|
|
@@ -438,7 +556,15 @@ type ItemReference = {
|
|
|
438
556
|
}
|
|
439
557
|
|
|
440
558
|
/**
|
|
441
|
-
*
|
|
559
|
+
* Item shapes accepted by a Responses-style `input` field.
|
|
560
|
+
*
|
|
561
|
+
* **Details**
|
|
562
|
+
*
|
|
563
|
+
* Supports input messages, output messages, tool calls, tool outputs, reasoning
|
|
564
|
+
* items, custom tool interactions, and item references.
|
|
565
|
+
*
|
|
566
|
+
* @category request
|
|
567
|
+
* @since 4.0.0
|
|
442
568
|
*/
|
|
443
569
|
export type InputItem =
|
|
444
570
|
| {
|
|
@@ -476,7 +602,10 @@ type CustomToolParam = {
|
|
|
476
602
|
}
|
|
477
603
|
|
|
478
604
|
/**
|
|
479
|
-
*
|
|
605
|
+
* Tool definitions that can be supplied to a Responses-style request.
|
|
606
|
+
*
|
|
607
|
+
* @category request
|
|
608
|
+
* @since 4.0.0
|
|
480
609
|
*/
|
|
481
610
|
export type Tool =
|
|
482
611
|
| FunctionTool
|
|
@@ -501,7 +630,11 @@ type ToolChoice =
|
|
|
501
630
|
}
|
|
502
631
|
|
|
503
632
|
/**
|
|
504
|
-
*
|
|
633
|
+
* Text output format configuration for plain text, JSON object, or JSON Schema
|
|
634
|
+
* responses.
|
|
635
|
+
*
|
|
636
|
+
* @category configuration
|
|
637
|
+
* @since 4.0.0
|
|
505
638
|
*/
|
|
506
639
|
export type TextResponseFormatConfiguration =
|
|
507
640
|
| {
|
|
@@ -519,7 +652,11 @@ export type TextResponseFormatConfiguration =
|
|
|
519
652
|
}
|
|
520
653
|
|
|
521
654
|
/**
|
|
522
|
-
*
|
|
655
|
+
* Request options for creating a Responses-style response with an
|
|
656
|
+
* OpenAI-compatible provider.
|
|
657
|
+
*
|
|
658
|
+
* @category request
|
|
659
|
+
* @since 4.0.0
|
|
523
660
|
*/
|
|
524
661
|
export type CreateResponse = {
|
|
525
662
|
readonly metadata?: Readonly<Record<string, string>> | null | undefined
|
|
@@ -556,7 +693,10 @@ export type CreateResponse = {
|
|
|
556
693
|
}
|
|
557
694
|
|
|
558
695
|
/**
|
|
559
|
-
*
|
|
696
|
+
* Token accounting reported on Responses-style response objects.
|
|
697
|
+
*
|
|
698
|
+
* @category response
|
|
699
|
+
* @since 4.0.0
|
|
560
700
|
*/
|
|
561
701
|
export type ResponseUsage = {
|
|
562
702
|
readonly input_tokens: number
|
|
@@ -573,7 +713,11 @@ type OutputItem =
|
|
|
573
713
|
| CustomToolCall
|
|
574
714
|
|
|
575
715
|
/**
|
|
576
|
-
*
|
|
716
|
+
* Responses-style response object returned by compatible providers or embedded
|
|
717
|
+
* in response stream lifecycle events.
|
|
718
|
+
*
|
|
719
|
+
* @category response
|
|
720
|
+
* @since 4.0.0
|
|
577
721
|
*/
|
|
578
722
|
export type Response = {
|
|
579
723
|
readonly id: string
|
|
@@ -699,7 +843,10 @@ type UnknownResponseStreamEvent = {
|
|
|
699
843
|
}
|
|
700
844
|
|
|
701
845
|
/**
|
|
702
|
-
*
|
|
846
|
+
* Server-sent event shapes emitted by Responses-style response streams.
|
|
847
|
+
*
|
|
848
|
+
* @category streaming
|
|
849
|
+
* @since 4.0.0
|
|
703
850
|
*/
|
|
704
851
|
export type ResponseStreamEvent =
|
|
705
852
|
| ResponseCreatedEvent
|
|
@@ -718,7 +865,16 @@ export type ResponseStreamEvent =
|
|
|
718
865
|
| UnknownResponseStreamEvent
|
|
719
866
|
|
|
720
867
|
/**
|
|
721
|
-
*
|
|
868
|
+
* Represents one embedding item returned by an OpenAI-compatible embeddings API.
|
|
869
|
+
*
|
|
870
|
+
* **Details**
|
|
871
|
+
*
|
|
872
|
+
* The embedding can be returned either as a numeric vector or as a base64-encoded
|
|
873
|
+
* string. The `index` field identifies the input item that produced this
|
|
874
|
+
* embedding.
|
|
875
|
+
*
|
|
876
|
+
* @category response
|
|
877
|
+
* @since 4.0.0
|
|
722
878
|
*/
|
|
723
879
|
export type Embedding = {
|
|
724
880
|
readonly embedding: ReadonlyArray<number> | string
|
|
@@ -727,7 +883,10 @@ export type Embedding = {
|
|
|
727
883
|
}
|
|
728
884
|
|
|
729
885
|
/**
|
|
730
|
-
*
|
|
886
|
+
* Request payload for the embeddings endpoint.
|
|
887
|
+
*
|
|
888
|
+
* @category request
|
|
889
|
+
* @since 4.0.0
|
|
731
890
|
*/
|
|
732
891
|
export type CreateEmbeddingRequest = {
|
|
733
892
|
readonly input: string | ReadonlyArray<string> | ReadonlyArray<number> | ReadonlyArray<ReadonlyArray<number>>
|
|
@@ -738,7 +897,10 @@ export type CreateEmbeddingRequest = {
|
|
|
738
897
|
}
|
|
739
898
|
|
|
740
899
|
/**
|
|
741
|
-
*
|
|
900
|
+
* Successful response payload returned by the embeddings endpoint.
|
|
901
|
+
*
|
|
902
|
+
* @category response
|
|
903
|
+
* @since 4.0.0
|
|
742
904
|
*/
|
|
743
905
|
export type CreateEmbeddingResponse = {
|
|
744
906
|
readonly data: ReadonlyArray<Embedding>
|
|
@@ -751,15 +913,24 @@ export type CreateEmbeddingResponse = {
|
|
|
751
913
|
}
|
|
752
914
|
|
|
753
915
|
/**
|
|
754
|
-
*
|
|
916
|
+
* JSON request body accepted by the embeddings endpoint.
|
|
917
|
+
*
|
|
918
|
+
* @category request
|
|
919
|
+
* @since 4.0.0
|
|
755
920
|
*/
|
|
756
921
|
export type CreateEmbeddingRequestJson = CreateEmbeddingRequest
|
|
757
922
|
/**
|
|
758
|
-
*
|
|
923
|
+
* Decoded successful embeddings response body.
|
|
924
|
+
*
|
|
925
|
+
* @category response
|
|
926
|
+
* @since 4.0.0
|
|
759
927
|
*/
|
|
760
928
|
export type CreateEmbedding200 = CreateEmbeddingResponse
|
|
761
929
|
/**
|
|
762
|
-
*
|
|
930
|
+
* Structured content parts accepted in chat completion messages.
|
|
931
|
+
*
|
|
932
|
+
* @category request
|
|
933
|
+
* @since 4.0.0
|
|
763
934
|
*/
|
|
764
935
|
export type ChatCompletionContentPart =
|
|
765
936
|
| {
|
|
@@ -774,7 +945,10 @@ export type ChatCompletionContentPart =
|
|
|
774
945
|
}
|
|
775
946
|
}
|
|
776
947
|
/**
|
|
777
|
-
*
|
|
948
|
+
* Tool call data attached to an assistant chat completion message.
|
|
949
|
+
*
|
|
950
|
+
* @category request
|
|
951
|
+
* @since 4.0.0
|
|
778
952
|
*/
|
|
779
953
|
export type ChatCompletionRequestToolCall = {
|
|
780
954
|
readonly id: string
|
|
@@ -785,7 +959,10 @@ export type ChatCompletionRequestToolCall = {
|
|
|
785
959
|
}
|
|
786
960
|
}
|
|
787
961
|
/**
|
|
788
|
-
*
|
|
962
|
+
* Message shapes accepted by the chat completions endpoint.
|
|
963
|
+
*
|
|
964
|
+
* @category request
|
|
965
|
+
* @since 4.0.0
|
|
789
966
|
*/
|
|
790
967
|
export type ChatCompletionRequestMessage =
|
|
791
968
|
| {
|
|
@@ -799,7 +976,10 @@ export type ChatCompletionRequestMessage =
|
|
|
799
976
|
readonly content: string
|
|
800
977
|
}
|
|
801
978
|
/**
|
|
802
|
-
*
|
|
979
|
+
* Function tool definition accepted by the chat completions endpoint.
|
|
980
|
+
*
|
|
981
|
+
* @category request
|
|
982
|
+
* @since 4.0.0
|
|
803
983
|
*/
|
|
804
984
|
export type ChatCompletionTool = {
|
|
805
985
|
readonly type: "function"
|
|
@@ -811,7 +991,10 @@ export type ChatCompletionTool = {
|
|
|
811
991
|
}
|
|
812
992
|
}
|
|
813
993
|
/**
|
|
814
|
-
*
|
|
994
|
+
* Controls whether the model may call tools and can force a specific function.
|
|
995
|
+
*
|
|
996
|
+
* @category configuration
|
|
997
|
+
* @since 4.0.0
|
|
815
998
|
*/
|
|
816
999
|
export type ChatCompletionToolChoice =
|
|
817
1000
|
| "none"
|
|
@@ -824,7 +1007,10 @@ export type ChatCompletionToolChoice =
|
|
|
824
1007
|
}
|
|
825
1008
|
}
|
|
826
1009
|
/**
|
|
827
|
-
*
|
|
1010
|
+
* JSON response format configuration for chat completion requests.
|
|
1011
|
+
*
|
|
1012
|
+
* @category configuration
|
|
1013
|
+
* @since 4.0.0
|
|
828
1014
|
*/
|
|
829
1015
|
export type ChatCompletionResponseFormat =
|
|
830
1016
|
| {
|
|
@@ -840,7 +1026,10 @@ export type ChatCompletionResponseFormat =
|
|
|
840
1026
|
}
|
|
841
1027
|
}
|
|
842
1028
|
/**
|
|
843
|
-
*
|
|
1029
|
+
* Request payload for the OpenAI-compatible chat completions endpoint.
|
|
1030
|
+
*
|
|
1031
|
+
* @category request
|
|
1032
|
+
* @since 4.0.0
|
|
844
1033
|
*/
|
|
845
1034
|
export type ChatCompletionRequest = {
|
|
846
1035
|
readonly model: string
|
|
@@ -855,21 +1044,32 @@ export type ChatCompletionRequest = {
|
|
|
855
1044
|
readonly tools?: ReadonlyArray<ChatCompletionTool> | undefined
|
|
856
1045
|
readonly tool_choice?: ChatCompletionToolChoice | undefined
|
|
857
1046
|
readonly service_tier?: string | undefined
|
|
1047
|
+
readonly reasoning?: unknown
|
|
858
1048
|
readonly stream?: boolean | undefined
|
|
859
1049
|
readonly stream_options?: {
|
|
860
1050
|
readonly include_usage?: boolean | undefined
|
|
861
1051
|
} | undefined
|
|
1052
|
+
readonly [x: string]: unknown
|
|
862
1053
|
}
|
|
863
1054
|
/**
|
|
864
|
-
*
|
|
1055
|
+
* JSON request body used by this client when creating a chat completion response.
|
|
1056
|
+
*
|
|
1057
|
+
* @category request
|
|
1058
|
+
* @since 4.0.0
|
|
865
1059
|
*/
|
|
866
1060
|
export type CreateResponseRequestJson = ChatCompletionRequest
|
|
867
1061
|
/**
|
|
868
|
-
*
|
|
1062
|
+
* Decoded successful chat completion response body returned by `createResponse`.
|
|
1063
|
+
*
|
|
1064
|
+
* @category response
|
|
1065
|
+
* @since 4.0.0
|
|
869
1066
|
*/
|
|
870
1067
|
export type CreateResponse200 = ChatCompletionResponse
|
|
871
1068
|
/**
|
|
872
|
-
*
|
|
1069
|
+
* Decoded server-sent event payload emitted by `createResponseStream`.
|
|
1070
|
+
*
|
|
1071
|
+
* @category streaming
|
|
1072
|
+
* @since 4.0.0
|
|
873
1073
|
*/
|
|
874
1074
|
export type CreateResponse200Sse = ChatCompletionStreamEvent
|
|
875
1075
|
|
|
@@ -894,6 +1094,11 @@ const ChatCompletionToolFunction = Schema.Struct({
|
|
|
894
1094
|
arguments: Schema.optionalKey(Schema.String)
|
|
895
1095
|
})
|
|
896
1096
|
|
|
1097
|
+
const ChatCompletionToolFunctionDelta = Schema.Struct({
|
|
1098
|
+
name: Schema.optionalKey(Schema.String),
|
|
1099
|
+
arguments: Schema.optionalKey(Schema.String)
|
|
1100
|
+
})
|
|
1101
|
+
|
|
897
1102
|
const ChatCompletionToolCall = Schema.Struct({
|
|
898
1103
|
id: Schema.optionalKey(Schema.String),
|
|
899
1104
|
index: Schema.optionalKey(Schema.Number),
|
|
@@ -901,6 +1106,13 @@ const ChatCompletionToolCall = Schema.Struct({
|
|
|
901
1106
|
function: Schema.optionalKey(ChatCompletionToolFunction)
|
|
902
1107
|
})
|
|
903
1108
|
|
|
1109
|
+
const ChatCompletionToolCallDelta = Schema.Struct({
|
|
1110
|
+
id: Schema.optionalKey(Schema.String),
|
|
1111
|
+
index: Schema.optionalKey(Schema.Number),
|
|
1112
|
+
type: Schema.optionalKey(Schema.String),
|
|
1113
|
+
function: Schema.optionalKey(ChatCompletionToolFunctionDelta)
|
|
1114
|
+
})
|
|
1115
|
+
|
|
904
1116
|
const ChatCompletionMessage = Schema.Struct({
|
|
905
1117
|
role: Schema.optionalKey(Schema.String),
|
|
906
1118
|
content: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
@@ -910,7 +1122,7 @@ const ChatCompletionMessage = Schema.Struct({
|
|
|
910
1122
|
const ChatCompletionDelta = Schema.Struct({
|
|
911
1123
|
role: Schema.optionalKey(Schema.String),
|
|
912
1124
|
content: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
913
|
-
tool_calls: Schema.optionalKey(Schema.Array(
|
|
1125
|
+
tool_calls: Schema.optionalKey(Schema.Array(ChatCompletionToolCallDelta))
|
|
914
1126
|
})
|
|
915
1127
|
|
|
916
1128
|
const ChatCompletionChoice = Schema.Struct({
|
|
@@ -947,31 +1159,53 @@ const ChatCompletionChunk = Schema.Struct({
|
|
|
947
1159
|
})
|
|
948
1160
|
|
|
949
1161
|
/**
|
|
950
|
-
*
|
|
1162
|
+
* Decoded tool-call object from a chat completion response or streaming chunk.
|
|
1163
|
+
*
|
|
1164
|
+
* @category response
|
|
1165
|
+
* @since 4.0.0
|
|
951
1166
|
*/
|
|
952
1167
|
export type ChatCompletionToolCall = typeof ChatCompletionToolCall.Type
|
|
953
1168
|
/**
|
|
954
|
-
*
|
|
1169
|
+
* Decoded message object from a non-streaming chat completion choice.
|
|
1170
|
+
*
|
|
1171
|
+
* @category response
|
|
1172
|
+
* @since 4.0.0
|
|
955
1173
|
*/
|
|
956
1174
|
export type ChatCompletionMessage = typeof ChatCompletionMessage.Type
|
|
957
1175
|
/**
|
|
958
|
-
*
|
|
1176
|
+
* Decoded choice object returned by chat completion responses and chunks.
|
|
1177
|
+
*
|
|
1178
|
+
* @category response
|
|
1179
|
+
* @since 4.0.0
|
|
959
1180
|
*/
|
|
960
1181
|
export type ChatCompletionChoice = typeof ChatCompletionChoice.Type
|
|
961
1182
|
/**
|
|
962
|
-
*
|
|
1183
|
+
* Decoded token usage summary returned by chat completions.
|
|
1184
|
+
*
|
|
1185
|
+
* @category response
|
|
1186
|
+
* @since 4.0.0
|
|
963
1187
|
*/
|
|
964
1188
|
export type ChatCompletionUsage = typeof ChatCompletionUsage.Type
|
|
965
1189
|
/**
|
|
966
|
-
*
|
|
1190
|
+
* Decoded successful response from the chat completions endpoint.
|
|
1191
|
+
*
|
|
1192
|
+
* @category response
|
|
1193
|
+
* @since 4.0.0
|
|
967
1194
|
*/
|
|
968
1195
|
export type ChatCompletionResponse = typeof ChatCompletionResponse.Type
|
|
969
1196
|
/**
|
|
970
|
-
*
|
|
1197
|
+
* Decoded streaming chunk emitted by the chat completions endpoint.
|
|
1198
|
+
*
|
|
1199
|
+
* @category streaming
|
|
1200
|
+
* @since 4.0.0
|
|
971
1201
|
*/
|
|
972
1202
|
export type ChatCompletionChunk = typeof ChatCompletionChunk.Type
|
|
973
1203
|
/**
|
|
974
|
-
*
|
|
1204
|
+
* Streaming chat completion event, including decoded chunks and the `[DONE]`
|
|
1205
|
+
* sentinel.
|
|
1206
|
+
*
|
|
1207
|
+
* @category streaming
|
|
1208
|
+
* @since 4.0.0
|
|
975
1209
|
*/
|
|
976
1210
|
export type ChatCompletionStreamEvent = ChatCompletionChunk | "[DONE]"
|
|
977
1211
|
|