import {z} from 'zod' export type DbDataEmbedInputs = Record; export type DbDataVectorValues = Array; export type DbDataSearchRecordsVector = {values?: DbDataVectorValues | undefined, sparse_values?: Array | undefined, sparse_indices?: Array | undefined}; export type DbDataSearchMatchTerms = {strategy?: string | undefined, terms?: Array | undefined}; export type DbDataSearchRecordsRequest = {query: {top_k: number, filter?: Record | undefined, inputs?: DbDataEmbedInputs | undefined, vector?: DbDataSearchRecordsVector | undefined, id?: string | undefined, match_terms?: DbDataSearchMatchTerms | undefined}, fields?: Array | undefined, rerank?: ({model: string, rank_fields: Array, top_n?: number | undefined, parameters?: {[key: string]: unknown} | undefined, query?: string | undefined}) | undefined}; export type CreateApiRecordsNamespacesNamespaceSearchArgs = {'X-Pinecone-Api-Version': string, namespace: string, body: DbDataSearchRecordsRequest}; export type CreateApiRecordsNamespacesNamespaceSearchOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dbDataHit = z.object({_id: z.string(), _score: z.number(), fields: z.object({})}); export const dbDataSearchUsage = z.object({read_units: z.number().int().gte(0), embed_total_tokens: z.number().int().gte(0).optional(), rerank_units: z.number().int().gte(0).optional()}); export const dbDataSearchRecordsResponse = z.object({result: z.object({hits: z.array(dbDataHit)}), usage: dbDataSearchUsage}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Search with text * * POST /records/namespaces/{namespace}/search */ export const createApiRecordsNamespacesNamespaceSearch = async (args: CreateApiRecordsNamespacesNamespaceSearchArgs, options?: CreateApiRecordsNamespacesNamespaceSearchOptions) => { const {baseUrl = "https://api.pinecone.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Pinecone-Api-Version'] != null) headers.set("X-Pinecone-Api-Version", String(args['X-Pinecone-Api-Version'])) if (ApiKeyAuth !== undefined) headers.set("Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/records/namespaces/${encodeURIComponent(String(args.namespace))}/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dbDataSearchRecordsResponse, await res.json()) };