@sixb/azure-ai-foundry 0.2.4 → 0.2.6
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/README.md +187 -2
- package/dist/decision.d.ts +18 -0
- package/dist/decision.d.ts.map +1 -0
- package/dist/discovery.d.ts +3 -0
- package/dist/discovery.d.ts.map +1 -1
- package/dist/embedding.d.ts +1 -1
- package/dist/embedding.d.ts.map +1 -1
- package/dist/image.d.ts +20 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1178 -71
- package/dist/index.js.map +15 -9
- package/dist/kinds.d.ts +13 -0
- package/dist/kinds.d.ts.map +1 -0
- package/dist/provider.d.ts +15 -0
- package/dist/provider.d.ts.map +1 -1
- package/dist/request.d.ts.map +1 -1
- package/dist/reranking.d.ts +18 -0
- package/dist/reranking.d.ts.map +1 -0
- package/dist/speech.d.ts +20 -0
- package/dist/speech.d.ts.map +1 -0
- package/dist/transcription.d.ts +20 -0
- package/dist/transcription.d.ts.map +1 -0
- package/dist/transport.d.ts +10 -2
- package/dist/transport.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/decision.ts +211 -0
- package/src/discovery.ts +18 -0
- package/src/embedding.ts +15 -36
- package/src/image.ts +457 -0
- package/src/index.ts +8 -0
- package/src/kinds.ts +100 -0
- package/src/provider.ts +73 -12
- package/src/request.ts +2 -1
- package/src/reranking.ts +264 -0
- package/src/speech.ts +390 -0
- package/src/transcription.ts +322 -0
- package/src/transport.ts +90 -11
package/src/provider.ts
CHANGED
|
@@ -27,6 +27,11 @@ import {
|
|
|
27
27
|
} from "./catalog"
|
|
28
28
|
import { foundryChatEstimator, foundryChatUsage } from "./chat-accounting"
|
|
29
29
|
import { type ChatRequestOptions, foundryChatRequest, validateChatOptions } from "./chat-request"
|
|
30
|
+
import {
|
|
31
|
+
type AzureAIFoundryDecisionModel,
|
|
32
|
+
type AzureAIFoundryDecisionOptions,
|
|
33
|
+
createFoundryDecision,
|
|
34
|
+
} from "./decision"
|
|
30
35
|
import {
|
|
31
36
|
type AzureAIFoundryCatalog,
|
|
32
37
|
type AzureAIFoundryDeployment,
|
|
@@ -40,6 +45,12 @@ import {
|
|
|
40
45
|
type AzureAIFoundryEmbeddingTransportOptions,
|
|
41
46
|
createFoundryEmbedding,
|
|
42
47
|
} from "./embedding"
|
|
48
|
+
import {
|
|
49
|
+
type AzureAIFoundryImageModel,
|
|
50
|
+
type AzureAIFoundryImageOptions,
|
|
51
|
+
createFoundryImage,
|
|
52
|
+
} from "./image"
|
|
53
|
+
import { article, deploymentKind, KIND_BINDINGS } from "./kinds"
|
|
43
54
|
import { foundryMessagesEstimator, foundryMessagesUsage } from "./messages-accounting"
|
|
44
55
|
import {
|
|
45
56
|
foundryMessagesRequest,
|
|
@@ -48,7 +59,22 @@ import {
|
|
|
48
59
|
validateMessagesOptions,
|
|
49
60
|
} from "./messages-request"
|
|
50
61
|
import { type RequestOptions, responsesRequest, validateOptions } from "./request"
|
|
62
|
+
import {
|
|
63
|
+
type AzureAIFoundryRerankingModel,
|
|
64
|
+
type AzureAIFoundryRerankingOptions,
|
|
65
|
+
createFoundryReranking,
|
|
66
|
+
} from "./reranking"
|
|
51
67
|
import { resolveModel } from "./resolution"
|
|
68
|
+
import {
|
|
69
|
+
type AzureAIFoundrySpeechModel,
|
|
70
|
+
type AzureAIFoundrySpeechOptions,
|
|
71
|
+
createFoundrySpeech,
|
|
72
|
+
} from "./speech"
|
|
73
|
+
import {
|
|
74
|
+
type AzureAIFoundryTranscriptionModel,
|
|
75
|
+
type AzureAIFoundryTranscriptionOptions,
|
|
76
|
+
createFoundryTranscription,
|
|
77
|
+
} from "./transcription"
|
|
52
78
|
import {
|
|
53
79
|
abortable,
|
|
54
80
|
type FoundryProtocol,
|
|
@@ -131,6 +157,25 @@ export interface AzureAIFoundryProvider extends LanguageModelProvider {
|
|
|
131
157
|
deploymentName: string,
|
|
132
158
|
options: AzureAIFoundryEmbeddingOptions
|
|
133
159
|
): AzureAIFoundryEmbeddingModel
|
|
160
|
+
/** A deployment of a decision model, such as Microsoft-Decision-1, for `models.decision`. */
|
|
161
|
+
decision(
|
|
162
|
+
deploymentName: string,
|
|
163
|
+
options?: AzureAIFoundryDecisionOptions
|
|
164
|
+
): AzureAIFoundryDecisionModel
|
|
165
|
+
/** A transcription deployment, such as gpt-4o-transcribe, for `models.audio.transcription`. */
|
|
166
|
+
transcription(
|
|
167
|
+
deploymentName: string,
|
|
168
|
+
options?: AzureAIFoundryTranscriptionOptions
|
|
169
|
+
): AzureAIFoundryTranscriptionModel
|
|
170
|
+
/** A Cohere rerank deployment, such as Cohere-rerank-v5-fast, for `models.reranking`. */
|
|
171
|
+
reranking(
|
|
172
|
+
deploymentName: string,
|
|
173
|
+
options?: AzureAIFoundryRerankingOptions
|
|
174
|
+
): AzureAIFoundryRerankingModel
|
|
175
|
+
/** A speech deployment, such as gpt-4o-mini-tts, for `models.audio.speech`. */
|
|
176
|
+
speech(deploymentName: string, options?: AzureAIFoundrySpeechOptions): AzureAIFoundrySpeechModel
|
|
177
|
+
/** An image deployment, such as gpt-image-1.5 or FLUX.1-Kontext-pro, for `models.image`. */
|
|
178
|
+
image(deploymentName: string, options?: AzureAIFoundryImageOptions): AzureAIFoundryImageModel
|
|
134
179
|
readonly catalog: AzureAIFoundryCatalog
|
|
135
180
|
chat(deploymentName: string, options?: AzureAIFoundryChatOptions): AzureAIFoundryModel<"chat">
|
|
136
181
|
}
|
|
@@ -183,6 +228,16 @@ export function createAzureAIFoundry(options: AzureAIFoundryOptions): AzureAIFou
|
|
|
183
228
|
model("messages", name, options)
|
|
184
229
|
const chat = (name: string, options: AzureAIFoundryChatOptions = {}) =>
|
|
185
230
|
model("chat", name, options)
|
|
231
|
+
const decision = (name: string, options: AzureAIFoundryDecisionOptions = {}) =>
|
|
232
|
+
createFoundryDecision(providerId, name, options, transport, deployments)
|
|
233
|
+
const transcription = (name: string, options: AzureAIFoundryTranscriptionOptions = {}) =>
|
|
234
|
+
createFoundryTranscription(providerId, name, options, transport, deployments)
|
|
235
|
+
const reranking = (name: string, options: AzureAIFoundryRerankingOptions = {}) =>
|
|
236
|
+
createFoundryReranking(providerId, name, options, transport, deployments)
|
|
237
|
+
const image = (name: string, options: AzureAIFoundryImageOptions = {}) =>
|
|
238
|
+
createFoundryImage(providerId, name, options, transport, deployments)
|
|
239
|
+
const speech = (name: string, options: AzureAIFoundrySpeechOptions = {}) =>
|
|
240
|
+
createFoundrySpeech(providerId, name, options, transport, deployments)
|
|
186
241
|
const list = async (protocol?: FoundryProtocol, refresh = false) => {
|
|
187
242
|
if (refresh) modelCatalog.invalidate()
|
|
188
243
|
const records = await deployments.list(refresh)
|
|
@@ -190,8 +245,9 @@ export function createAzureAIFoundry(options: AzureAIFoundryOptions): AzureAIFou
|
|
|
190
245
|
const results: LanguageModelDefinition[] = []
|
|
191
246
|
for (const deployment of records) {
|
|
192
247
|
const name = deployment.name
|
|
248
|
+
// Listings hold language models; every other kind has its own binding.
|
|
193
249
|
if (
|
|
194
|
-
|
|
250
|
+
deploymentKind(deployment) !== "language" ||
|
|
195
251
|
(await modelCatalog.getEmbedding(deployment.modelName, true))
|
|
196
252
|
)
|
|
197
253
|
continue
|
|
@@ -227,7 +283,19 @@ export function createAzureAIFoundry(options: AzureAIFoundryOptions): AzureAIFou
|
|
|
227
283
|
}
|
|
228
284
|
const auto = (name: string, options: AzureAIFoundryModelOptions = {}) =>
|
|
229
285
|
model(undefined, name, options)
|
|
230
|
-
return Object.assign(auto, {
|
|
286
|
+
return Object.assign(auto, {
|
|
287
|
+
providerId,
|
|
288
|
+
catalog,
|
|
289
|
+
responses,
|
|
290
|
+
messages,
|
|
291
|
+
chat,
|
|
292
|
+
embedding,
|
|
293
|
+
decision,
|
|
294
|
+
transcription,
|
|
295
|
+
reranking,
|
|
296
|
+
speech,
|
|
297
|
+
image,
|
|
298
|
+
})
|
|
231
299
|
}
|
|
232
300
|
|
|
233
301
|
class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryModel<Protocol> {
|
|
@@ -331,9 +399,10 @@ class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryMo
|
|
|
331
399
|
async resolve(options?: { readonly offline?: boolean }): Promise<FoundryModel<Protocol>> {
|
|
332
400
|
if (this.resolution) return this
|
|
333
401
|
const resolution = await this.deployments.resolve(this.modelId, options?.offline === true)
|
|
334
|
-
|
|
402
|
+
const kind = deploymentKind(resolution.deployment)
|
|
403
|
+
if (kind !== "language")
|
|
335
404
|
throw new UnsupportedModelFeatureError(
|
|
336
|
-
`${PREFIX} Use
|
|
405
|
+
`${PREFIX} Use ${KIND_BINDINGS[kind]} for ${article(kind)} ${kind} deployment.`
|
|
337
406
|
)
|
|
338
407
|
let catalogModel: CatalogModel | undefined
|
|
339
408
|
try {
|
|
@@ -506,11 +575,3 @@ class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryMo
|
|
|
506
575
|
}
|
|
507
576
|
}
|
|
508
577
|
}
|
|
509
|
-
|
|
510
|
-
function isEmbeddingDeployment(deployment: AzureAIFoundryDeployment): boolean {
|
|
511
|
-
return (
|
|
512
|
-
deployment.capabilities.embeddings === "true" ||
|
|
513
|
-
deployment.capabilities.embedding === "true" ||
|
|
514
|
-
/^(text-embedding-|cohere-embed)/i.test(deployment.modelName)
|
|
515
|
-
)
|
|
516
|
-
}
|
package/src/request.ts
CHANGED
|
@@ -72,7 +72,8 @@ export function responsesRequest(
|
|
|
72
72
|
deployment?: AzureAIFoundryDeployment
|
|
73
73
|
): JsonObject {
|
|
74
74
|
const max = prepareRequest(request, definition, options, scope)
|
|
75
|
-
const
|
|
75
|
+
const replayed = responsesInput(request.messages, definition.providerId, definition.modelId)
|
|
76
|
+
const input = replayed.map((item) => {
|
|
76
77
|
if (!isJsonObject(item)) return item
|
|
77
78
|
const content = Array.isArray(item.content)
|
|
78
79
|
? item.content.map((part) => {
|
package/src/reranking.ts
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineModelRateCard,
|
|
3
|
+
estimateModelReservation,
|
|
4
|
+
type ModelCostEstimator,
|
|
5
|
+
ModelProviderError,
|
|
6
|
+
type ModelRateCard,
|
|
7
|
+
type ModelUsage,
|
|
8
|
+
type RerankingModel,
|
|
9
|
+
type RerankingModelDefinition,
|
|
10
|
+
type RerankingModelRequest,
|
|
11
|
+
RerankingModelResponseError,
|
|
12
|
+
type RerankingModelResponseMetadata,
|
|
13
|
+
type RerankingModelResult,
|
|
14
|
+
type RerankingResult,
|
|
15
|
+
rateModelCall,
|
|
16
|
+
} from "@sixb/core/models"
|
|
17
|
+
import {
|
|
18
|
+
type AzureAIFoundryDeployment,
|
|
19
|
+
deploymentMetadata,
|
|
20
|
+
type FoundryDeployments,
|
|
21
|
+
type ResolvedDeployment,
|
|
22
|
+
} from "./discovery"
|
|
23
|
+
import { assertDeploymentKind } from "./kinds"
|
|
24
|
+
import type { AzureAIFoundryModelMetadata } from "./provider"
|
|
25
|
+
import { azurePublisher } from "./publisher"
|
|
26
|
+
import { abortable, type FoundryTransport, readJson, requestId } from "./transport"
|
|
27
|
+
import { counter, object, PREFIX, positiveInteger } from "./util"
|
|
28
|
+
|
|
29
|
+
export interface AzureAIFoundryRerankingOptions {
|
|
30
|
+
/** Deadline for one reranking, response included. Default: 30,000 ms. */
|
|
31
|
+
readonly timeoutMs?: number
|
|
32
|
+
readonly rateCard?: ModelRateCard
|
|
33
|
+
readonly costEstimator?: ModelCostEstimator
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface AzureAIFoundryRerankingModel extends RerankingModel {
|
|
37
|
+
readonly metadata: AzureAIFoundryModelMetadata
|
|
38
|
+
resolve(options?: { readonly offline?: boolean }): Promise<AzureAIFoundryRerankingModel>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 10,000 scores answer in well under 1 MiB. */
|
|
42
|
+
const MAX_RESPONSE_BYTES = 4 * 1024 * 1024
|
|
43
|
+
|
|
44
|
+
/** AbortSignal.timeout's largest delay. */
|
|
45
|
+
const MAX_TIMEOUT_MS = 2 ** 31 - 1
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Azure Retail Prices (verified 2026-11-13), by deployment SKU: Rerank v5 bills input tokens,
|
|
49
|
+
* USD per million. Rerank v4 bills USD 2 (fast) and 2.50 (pro) per thousand searches, a unit
|
|
50
|
+
* Sixb's rate cards cannot hold yet, so its cost stays unknown.
|
|
51
|
+
*/
|
|
52
|
+
const PUBLISHED: Readonly<Record<string, Readonly<Record<string, ModelRateCard>>>> = {
|
|
53
|
+
// Rerank V5 Fast {Glbl,DZ} 1M Tokens
|
|
54
|
+
"cohere-rerank-v5-fast": {
|
|
55
|
+
GlobalStandard: { currency: "USD", input: "0.02", output: "0" },
|
|
56
|
+
DataZoneStandard: { currency: "USD", input: "0.022", output: "0" },
|
|
57
|
+
},
|
|
58
|
+
// Rerank V5 Pro {Glbl,DZ} 1M Tokens
|
|
59
|
+
"cohere-rerank-v5-pro": {
|
|
60
|
+
GlobalStandard: { currency: "USD", input: "0.05", output: "0" },
|
|
61
|
+
DataZoneStandard: { currency: "USD", input: "0.055", output: "0" },
|
|
62
|
+
},
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface RerankingSettings {
|
|
66
|
+
readonly timeoutMs: number
|
|
67
|
+
readonly rateCard?: ModelRateCard
|
|
68
|
+
readonly costEstimator?: ModelCostEstimator
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface RerankingResolution extends ResolvedDeployment {
|
|
72
|
+
readonly costEstimator?: ModelCostEstimator
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function createFoundryReranking(
|
|
76
|
+
providerId: string,
|
|
77
|
+
modelId: string,
|
|
78
|
+
options: AzureAIFoundryRerankingOptions,
|
|
79
|
+
transport: FoundryTransport,
|
|
80
|
+
deployments: FoundryDeployments
|
|
81
|
+
): AzureAIFoundryRerankingModel {
|
|
82
|
+
if (!modelId.trim() || modelId !== modelId.trim())
|
|
83
|
+
throw new TypeError(`${PREFIX} deploymentName must be nonempty without surrounding whitespace.`)
|
|
84
|
+
positiveInteger(options.timeoutMs, "timeoutMs")
|
|
85
|
+
if (options.timeoutMs !== undefined && options.timeoutMs > MAX_TIMEOUT_MS)
|
|
86
|
+
throw new TypeError(`${PREFIX} timeoutMs must be at most ${MAX_TIMEOUT_MS}.`)
|
|
87
|
+
if (options.rateCard && options.costEstimator)
|
|
88
|
+
throw new TypeError(`${PREFIX} Configure either rateCard or costEstimator, not both.`)
|
|
89
|
+
// Validate and snapshot now: later edits to the caller's object change nothing.
|
|
90
|
+
const settings: RerankingSettings = Object.freeze({
|
|
91
|
+
timeoutMs: options.timeoutMs ?? 30_000,
|
|
92
|
+
...(options.rateCard ? { rateCard: defineModelRateCard(options.rateCard) } : {}),
|
|
93
|
+
...(options.costEstimator ? { costEstimator: options.costEstimator } : {}),
|
|
94
|
+
})
|
|
95
|
+
return rerankingBinding(providerId, modelId, settings, transport, deployments)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Discovery pins the deployed model for pricing; inference posts Cohere's v2 rerank request. */
|
|
99
|
+
function rerankingBinding(
|
|
100
|
+
providerId: string,
|
|
101
|
+
modelId: string,
|
|
102
|
+
settings: RerankingSettings,
|
|
103
|
+
transport: FoundryTransport,
|
|
104
|
+
deployments: FoundryDeployments,
|
|
105
|
+
resolution?: RerankingResolution
|
|
106
|
+
): AzureAIFoundryRerankingModel {
|
|
107
|
+
const deployment = resolution?.deployment
|
|
108
|
+
const definition: RerankingModelDefinition = Object.freeze({
|
|
109
|
+
kind: "reranking",
|
|
110
|
+
providerId,
|
|
111
|
+
modelId,
|
|
112
|
+
...(deployment ? { publisher: azurePublisher(deployment.modelPublisher) } : {}),
|
|
113
|
+
via: "Azure AI Foundry",
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
let pending: Promise<AzureAIFoundryRerankingModel> | undefined
|
|
117
|
+
const binding: AzureAIFoundryRerankingModel = Object.freeze({
|
|
118
|
+
providerId,
|
|
119
|
+
modelId,
|
|
120
|
+
definition,
|
|
121
|
+
metadata: deploymentMetadata(resolution),
|
|
122
|
+
...(resolution?.costEstimator ? { costEstimator: resolution.costEstimator } : {}),
|
|
123
|
+
|
|
124
|
+
async resolve(input?: { readonly offline?: boolean }) {
|
|
125
|
+
if (resolution) return binding
|
|
126
|
+
const resolved = await deployments.resolve(modelId, input?.offline === true)
|
|
127
|
+
assertDeploymentKind(resolved.deployment, modelId, "reranking")
|
|
128
|
+
const rateCard = settings.rateCard ?? publishedRateCard(resolved.deployment)
|
|
129
|
+
const costEstimator =
|
|
130
|
+
settings.costEstimator ?? (rateCard ? rerankingEstimator(rateCard) : undefined)
|
|
131
|
+
return rerankingBinding(providerId, modelId, settings, transport, deployments, {
|
|
132
|
+
...resolved,
|
|
133
|
+
...(costEstimator ? { costEstimator } : {}),
|
|
134
|
+
})
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
async rerank(request: RerankingModelRequest): Promise<RerankingModelResult> {
|
|
138
|
+
const signal = AbortSignal.any([
|
|
139
|
+
...(request.signal ? [request.signal] : []),
|
|
140
|
+
AbortSignal.timeout(settings.timeoutMs),
|
|
141
|
+
])
|
|
142
|
+
signal.throwIfAborted()
|
|
143
|
+
if (
|
|
144
|
+
typeof request.query !== "string" ||
|
|
145
|
+
!request.query.trim() ||
|
|
146
|
+
!Array.isArray(request.documents) ||
|
|
147
|
+
request.documents.some((text) => typeof text !== "string" || !text.trim())
|
|
148
|
+
)
|
|
149
|
+
throw new TypeError(`${PREFIX} Reranking requires a query and nonempty document strings.`)
|
|
150
|
+
const documents = [...request.documents]
|
|
151
|
+
if (!documents.length) return { results: [] }
|
|
152
|
+
if (!resolution) {
|
|
153
|
+
// A direct call pins the deployment once, as the runtime does before admission.
|
|
154
|
+
pending ??= binding.resolve().catch((error) => {
|
|
155
|
+
pending = undefined
|
|
156
|
+
throw error
|
|
157
|
+
})
|
|
158
|
+
const resolved = await abortable(() => pending!, signal)
|
|
159
|
+
return resolved.rerank({ query: request.query, documents, signal })
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Without top_n, Cohere scores every document, which Sixb requires.
|
|
163
|
+
const response = await transport.post(
|
|
164
|
+
JSON.stringify({ model: modelId, query: request.query, documents }),
|
|
165
|
+
signal,
|
|
166
|
+
providerId,
|
|
167
|
+
modelId,
|
|
168
|
+
"reranking"
|
|
169
|
+
)
|
|
170
|
+
const id = transport.redactText(response, requestId(response))
|
|
171
|
+
let body: unknown
|
|
172
|
+
try {
|
|
173
|
+
body = await readJson(response, signal, MAX_RESPONSE_BYTES)
|
|
174
|
+
} catch {
|
|
175
|
+
// The parse error can quote the body; only the status and request ID travel.
|
|
176
|
+
if (signal.aborted) throw signal.reason
|
|
177
|
+
throw new ModelProviderError(
|
|
178
|
+
`${PREFIX} Reranking returned invalid JSON.`,
|
|
179
|
+
providerId,
|
|
180
|
+
modelId,
|
|
181
|
+
{ code: "invalid_json", requestId: id, status: response.status }
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
const payload = object(body)
|
|
185
|
+
const responseId = typeof payload?.id === "string" && payload.id ? payload.id : undefined
|
|
186
|
+
const metadata: RerankingModelResponseMetadata = {
|
|
187
|
+
usage: rerankingUsage(payload),
|
|
188
|
+
...(id || responseId
|
|
189
|
+
? {
|
|
190
|
+
providerIds: {
|
|
191
|
+
...(id ? { requestId: id } : {}),
|
|
192
|
+
...(responseId ? { responseId } : {}),
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
: {}),
|
|
196
|
+
}
|
|
197
|
+
const results = rerankingResults(payload?.results, documents.length)
|
|
198
|
+
if (!results)
|
|
199
|
+
throw new RerankingModelResponseError(
|
|
200
|
+
`${PREFIX} Invalid reranking response: expected one finite score per document with unique indices.`,
|
|
201
|
+
providerId,
|
|
202
|
+
modelId,
|
|
203
|
+
metadata
|
|
204
|
+
)
|
|
205
|
+
return { ...metadata, results }
|
|
206
|
+
},
|
|
207
|
+
})
|
|
208
|
+
return binding
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function rerankingResults(value: unknown, count: number): RerankingResult[] | undefined {
|
|
212
|
+
if (!Array.isArray(value) || value.length !== count) return undefined
|
|
213
|
+
const seen = new Set<number>()
|
|
214
|
+
const results: RerankingResult[] = []
|
|
215
|
+
for (const entry of value) {
|
|
216
|
+
const index = object(entry)?.index
|
|
217
|
+
const score = object(entry)?.relevance_score
|
|
218
|
+
if (
|
|
219
|
+
typeof index !== "number" ||
|
|
220
|
+
!Number.isSafeInteger(index) ||
|
|
221
|
+
index < 0 ||
|
|
222
|
+
index >= count ||
|
|
223
|
+
seen.has(index) ||
|
|
224
|
+
typeof score !== "number" ||
|
|
225
|
+
!Number.isFinite(score)
|
|
226
|
+
)
|
|
227
|
+
return undefined
|
|
228
|
+
seen.add(index)
|
|
229
|
+
results.push({ index, score })
|
|
230
|
+
}
|
|
231
|
+
return results
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Rerank v5 bills `input_tokens`; v4 bills `search_units`, kept in `raw` only. */
|
|
235
|
+
function rerankingUsage(payload: Record<string, unknown> | undefined): ModelUsage {
|
|
236
|
+
const billed = object(object(payload?.meta)?.billed_units)
|
|
237
|
+
const inputTokens = counter(billed?.input_tokens)
|
|
238
|
+
return {
|
|
239
|
+
...(inputTokens === undefined ? {} : { inputTokens, outputTokens: 0 }),
|
|
240
|
+
...(billed ? { raw: billed } : {}),
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function publishedRateCard(deployment: AzureAIFoundryDeployment): ModelRateCard | undefined {
|
|
245
|
+
const skus = PUBLISHED[deployment.modelName.toLowerCase()]
|
|
246
|
+
const card =
|
|
247
|
+
skus && deployment.modelVersion === "1" && Object.hasOwn(skus, deployment.sku.name)
|
|
248
|
+
? skus[deployment.sku.name]
|
|
249
|
+
: undefined
|
|
250
|
+
return card ? defineModelRateCard(card) : undefined
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function rerankingEstimator(rateCard: ModelRateCard): ModelCostEstimator {
|
|
254
|
+
return {
|
|
255
|
+
estimateReservation: (tokens) => estimateModelReservation({ ...tokens, rateCard }),
|
|
256
|
+
estimate: ({ usage }) => {
|
|
257
|
+
// A search-unit bill is not a token bill, whatever the card; a zero count bills nothing.
|
|
258
|
+
const raw = usage.raw ?? {}
|
|
259
|
+
if (Object.entries(raw).some(([key, count]) => key !== "input_tokens" && count !== 0))
|
|
260
|
+
return { status: "unpriceable", reason: "missing-rate-card" }
|
|
261
|
+
return rateModelCall({ usage, rateCard })
|
|
262
|
+
},
|
|
263
|
+
}
|
|
264
|
+
}
|