| import type { JSONObject, JSONValue } from '@ai-sdk/provider'; | ||
| import type { ProviderOptions } from '@ai-sdk/provider-utils'; | ||
| import type { ProviderMetadata } from '../types'; | ||
| import type { Warning } from '../types/warning'; | ||
| /** | ||
| * Event passed to the `onStart` callback for rerank operations. | ||
| * | ||
| * Called when the operation begins, before the reranking model is called. | ||
| */ | ||
| export interface RerankOnStartEvent { | ||
| /** Unique identifier for this rerank call, used to correlate events. */ | ||
| readonly callId: string; | ||
| /** Identifies the operation type ('ai.rerank'). */ | ||
| readonly operationId: string; | ||
| //** The provider identifier (e.g., 'openai', 'anthropic'). */ | ||
| readonly provider: string; | ||
| /** The specific model identifier (e.g., 'gpt-4o'). */ | ||
| readonly modelId: string; | ||
| /** The documents being reranked. */ | ||
| readonly documents: Array<JSONObject | string>; | ||
| /** The query to rerank the documents against. */ | ||
| readonly query: string; | ||
| /** Number of top documents to return. */ | ||
| readonly topN: number | undefined; | ||
| /** Maximum number of retries for failed requests. */ | ||
| readonly maxRetries: number; | ||
| /** Abort signal for cancelling the operation. */ | ||
| readonly abortSignal: AbortSignal | undefined; | ||
| /** Additional HTTP headers sent with the request. */ | ||
| readonly headers: Record<string, string | undefined> | undefined; | ||
| /** Additional provider-specific options. */ | ||
| readonly providerOptions: ProviderOptions | undefined; | ||
| /** Whether telemetry is enabled. */ | ||
| readonly isEnabled: boolean | undefined; | ||
| /** Whether to record inputs in telemetry. Enabled by default. */ | ||
| readonly recordInputs: boolean | undefined; | ||
| /** Whether to record outputs in telemetry. Enabled by default. */ | ||
| readonly recordOutputs: boolean | undefined; | ||
| /** Identifier from telemetry settings for grouping related operations. */ | ||
| readonly functionId: string | undefined; | ||
| /** Additional metadata from telemetry settings. */ | ||
| readonly metadata: Record<string, JSONValue> | undefined; | ||
| } | ||
| /** | ||
| * Event passed to the `onFinish` callback for rerank operations. | ||
| * | ||
| * Called when the operation completes, after the reranking model returns. | ||
| */ | ||
| export interface RerankOnFinishEvent { | ||
| /** Unique identifier for this rerank call, used to correlate events. */ | ||
| readonly callId: string; | ||
| /** Identifies the operation type ('ai.rerank'). */ | ||
| readonly operationId: string; | ||
| //** The provider identifier (e.g., 'openai', 'anthropic'). */ | ||
| readonly provider: string; | ||
| /** The specific model identifier (e.g., 'gpt-4o'). */ | ||
| readonly modelId: string; | ||
| /** The documents that were reranked. */ | ||
| readonly documents: Array<JSONObject | string>; | ||
| /** The query that documents were reranked against. */ | ||
| readonly query: string; | ||
| /** The reranked results sorted by relevance score in descending order. */ | ||
| readonly ranking: Array<{ | ||
| originalIndex: number; | ||
| score: number; | ||
| document: JSONObject | string; | ||
| }>; | ||
| /** Warnings from the reranking model. */ | ||
| readonly warnings: Array<Warning>; | ||
| /** Optional provider-specific metadata. */ | ||
| readonly providerMetadata: ProviderMetadata | undefined; | ||
| /** Response data including headers and body. */ | ||
| readonly response: { | ||
| id?: string; | ||
| timestamp: Date; | ||
| modelId: string; | ||
| headers?: Record<string, string>; | ||
| body?: unknown; | ||
| }; | ||
| /** Whether telemetry is enabled. */ | ||
| readonly isEnabled: boolean | undefined; | ||
| /** Whether to record inputs in telemetry. Enabled by default. */ | ||
| readonly recordInputs: boolean | undefined; | ||
| /** Whether to record outputs in telemetry. Enabled by default. */ | ||
| readonly recordOutputs: boolean | undefined; | ||
| /** Identifier from telemetry settings for grouping related operations. */ | ||
| readonly functionId: string | undefined; | ||
| /** Additional metadata from telemetry settings. */ | ||
| readonly metadata: Record<string, JSONValue> | undefined; | ||
| } |
@@ -156,3 +156,3 @@ "use strict"; | ||
| // src/version.ts | ||
| var VERSION = true ? "7.0.0-beta.32" : "0.0.0-test"; | ||
| var VERSION = true ? "7.0.0-beta.33" : "0.0.0-test"; | ||
@@ -159,0 +159,0 @@ // src/util/download/download.ts |
@@ -136,3 +136,3 @@ // internal/index.ts | ||
| // src/version.ts | ||
| var VERSION = true ? "7.0.0-beta.32" : "0.0.0-test"; | ||
| var VERSION = true ? "7.0.0-beta.33" : "0.0.0-test"; | ||
@@ -139,0 +139,0 @@ // src/util/download/download.ts |
+1
-1
| { | ||
| "name": "ai", | ||
| "version": "7.0.0-beta.32", | ||
| "version": "7.0.0-beta.33", | ||
| "description": "AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
| export { rerank } from './rerank'; | ||
| export type { RerankResult } from './rerank-result'; | ||
| export type { RerankOnStartEvent, RerankOnFinishEvent } from './rerank-events'; |
+133
-2
| import { JSONObject, RerankingModelV4CallOptions } from '@ai-sdk/provider'; | ||
| import { ProviderOptions } from '@ai-sdk/provider-utils'; | ||
| import { createIdGenerator, ProviderOptions } from '@ai-sdk/provider-utils'; | ||
| import { prepareRetries } from '../../src/util/prepare-retries'; | ||
| import { logWarnings } from '../logger/log-warnings'; | ||
| import { assembleOperationName } from '../telemetry/assemble-operation-name'; | ||
@@ -11,5 +12,12 @@ import { getBaseTelemetryAttributes } from '../telemetry/get-base-telemetry-attributes'; | ||
| import { RerankingModel } from '../types'; | ||
| import { notify } from '../util/notify'; | ||
| import type { Listener } from '../util/notify'; | ||
| import type { RerankOnFinishEvent, RerankOnStartEvent } from './rerank-events'; | ||
| import { RerankResult } from './rerank-result'; | ||
| import { logWarnings } from '../logger/log-warnings'; | ||
| const originalGenerateCallId = createIdGenerator({ | ||
| prefix: 'call', | ||
| size: 24, | ||
| }); | ||
| /** | ||
@@ -41,2 +49,5 @@ * Rerank documents using a reranking model. The type of the value is defined by the reranking model. | ||
| experimental_telemetry: telemetry, | ||
| experimental_onStart: onStart, | ||
| experimental_onFinish: onFinish, | ||
| _internal: { generateCallId = originalGenerateCallId } = {}, | ||
| }: { | ||
@@ -92,4 +103,71 @@ /** | ||
| providerOptions?: ProviderOptions; | ||
| /** | ||
| * Callback that is called when the rerank operation begins, | ||
| * before the reranking model is called. | ||
| */ | ||
| experimental_onStart?: Listener<RerankOnStartEvent>; | ||
| /** | ||
| * Callback that is called when the rerank operation completes, | ||
| * after the reranking model returns. | ||
| */ | ||
| experimental_onFinish?: Listener<RerankOnFinishEvent>; | ||
| /** | ||
| * Internal. For test use only. May change without notice. | ||
| */ | ||
| _internal?: { | ||
| generateCallId?: () => string; | ||
| }; | ||
| }): Promise<RerankResult<VALUE>> { | ||
| const callId = generateCallId(); | ||
| if (documents.length === 0) { | ||
| await notify({ | ||
| event: { | ||
| callId, | ||
| operationId: 'ai.rerank', | ||
| provider: model.provider, | ||
| modelId: model.modelId, | ||
| documents, | ||
| query, | ||
| topN, | ||
| maxRetries: maxRetriesArg ?? 2, | ||
| abortSignal, | ||
| headers, | ||
| providerOptions, | ||
| isEnabled: telemetry?.isEnabled, | ||
| recordInputs: telemetry?.recordInputs, | ||
| recordOutputs: telemetry?.recordOutputs, | ||
| functionId: telemetry?.functionId, | ||
| metadata: telemetry?.metadata, | ||
| }, | ||
| callbacks: [onStart], | ||
| }); | ||
| await notify({ | ||
| event: { | ||
| callId, | ||
| operationId: 'ai.rerank', | ||
| provider: model.provider, | ||
| modelId: model.modelId, | ||
| documents, | ||
| query, | ||
| ranking: [], | ||
| warnings: [], | ||
| providerMetadata: undefined, | ||
| response: { | ||
| timestamp: new Date(), | ||
| modelId: model.modelId, | ||
| }, | ||
| isEnabled: telemetry?.isEnabled, | ||
| recordInputs: telemetry?.recordInputs, | ||
| recordOutputs: telemetry?.recordOutputs, | ||
| functionId: telemetry?.functionId, | ||
| metadata: telemetry?.metadata, | ||
| }, | ||
| callbacks: [onFinish], | ||
| }); | ||
| return new DefaultRerankResult({ | ||
@@ -111,2 +189,24 @@ originalDocuments: [], | ||
| await notify({ | ||
| event: { | ||
| callId, | ||
| operationId: 'ai.rerank', | ||
| provider: model.provider, | ||
| modelId: model.modelId, | ||
| documents, | ||
| query, | ||
| topN, | ||
| maxRetries, | ||
| abortSignal, | ||
| headers, | ||
| providerOptions, | ||
| isEnabled: telemetry?.isEnabled, | ||
| recordInputs: telemetry?.recordInputs, | ||
| recordOutputs: telemetry?.recordOutputs, | ||
| functionId: telemetry?.functionId, | ||
| metadata: telemetry?.metadata, | ||
| }, | ||
| callbacks: [onStart], | ||
| }); | ||
| // detect the type of the documents: | ||
@@ -202,2 +302,33 @@ const documentsToSend: RerankingModelV4CallOptions['documents'] = | ||
| await notify({ | ||
| event: { | ||
| callId, | ||
| operationId: 'ai.rerank', | ||
| provider: model.provider, | ||
| modelId: model.modelId, | ||
| documents, | ||
| query, | ||
| ranking: ranking.map(r => ({ | ||
| originalIndex: r.index, | ||
| score: r.relevanceScore, | ||
| document: documents[r.index], | ||
| })), | ||
| warnings: warnings ?? [], | ||
| providerMetadata, | ||
| response: { | ||
| id: response?.id, | ||
| timestamp: response?.timestamp ?? new Date(), | ||
| modelId: response?.modelId ?? model.modelId, | ||
| headers: response?.headers, | ||
| body: response?.body, | ||
| }, | ||
| isEnabled: telemetry?.isEnabled, | ||
| recordInputs: telemetry?.recordInputs, | ||
| recordOutputs: telemetry?.recordOutputs, | ||
| functionId: telemetry?.functionId, | ||
| metadata: telemetry?.metadata, | ||
| }, | ||
| callbacks: [onFinish], | ||
| }); | ||
| return new DefaultRerankResult({ | ||
@@ -204,0 +335,0 @@ originalDocuments: documents, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
6811980
0.6%537
0.19%65522
0.8%130
1.56%