Skip to content

Commit

Permalink
update ts client
Browse files Browse the repository at this point in the history
  • Loading branch information
spikechroma committed Aug 28, 2024
1 parent 2aa4680 commit a974fda
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
3 changes: 2 additions & 1 deletion clients/js/src/ChromaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
} from "./types";
import {
prepareRecordRequest,
prepareRecordRequestWithIDsOptional,
toArray,
toArrayOfArrays,
validateTenantDatabase,
Expand Down Expand Up @@ -420,7 +421,7 @@ export class ChromaClient {
const resp = (await this.api.add(
collection.id,
// TODO: For some reason the auto generated code requires metadata to be defined here.
(await prepareRecordRequest(
(await prepareRecordRequestWithIDsOptional(
params,
collection.embeddingFunction,
)) as GeneratedApi.AddEmbedding,
Expand Down
63 changes: 56 additions & 7 deletions clients/js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { ChromaConnectionError } from "./Errors";
import { IEmbeddingFunction } from "./embeddings/IEmbeddingFunction";
import {
AddRecordsParams,
BaseRecordOperationParams,
BaseRecordOperationParamsWithIDsOptional,
Collection,
Metadata,
MultiRecordOperationParams,
MultiRecordOperationParamsWithIDsOptional,
UpdateRecordsParams,
UpsertRecordsParams,
} from "./types";

// a function to convert a non-Array object to an Array
Expand Down Expand Up @@ -84,9 +85,9 @@ export function isBrowser() {

function arrayifyParams(
params: BaseRecordOperationParamsWithIDsOptional,
): MultiRecordOperationParams {
): MultiRecordOperationParamsWithIDsOptional {
return {
ids: params.ids !== undefined ? toArray(params.ids) : [],
ids: params.ids !== undefined ? toArray(params.ids) : undefined,
embeddings: params.embeddings
? toArrayOfArrays(params.embeddings)
: undefined,
Expand All @@ -98,11 +99,11 @@ function arrayifyParams(
}

export async function prepareRecordRequest(
reqParams: AddRecordsParams | UpdateRecordsParams,
reqParams: UpsertRecordsParams | UpdateRecordsParams,
embeddingFunction: IEmbeddingFunction,
update?: true,
): Promise<MultiRecordOperationParams> {
const { ids, embeddings, metadatas, documents } = arrayifyParams(reqParams);
const { ids = [], embeddings, metadatas, documents } = arrayifyParams(reqParams);

if (!embeddings && !documents && !update) {
throw new Error("embeddings and documents cannot both be undefined");
Expand All @@ -111,8 +112,8 @@ export async function prepareRecordRequest(
const embeddingsArray = embeddings
? embeddings
: documents
? await embeddingFunction.generate(documents)
: undefined;
? await embeddingFunction.generate(documents)
: undefined;

if (!embeddingsArray && !update) {
throw new Error("Failed to generate embeddings for your request.");
Expand Down Expand Up @@ -144,6 +145,54 @@ export async function prepareRecordRequest(
};
}

export async function prepareRecordRequestWithIDsOptional(
reqParams: AddRecordsParams,
embeddingFunction: IEmbeddingFunction,
): Promise<MultiRecordOperationParamsWithIDsOptional> {
const { ids, embeddings, metadatas, documents } = arrayifyParams(reqParams);

if (!embeddings && !documents) {
throw new Error("embeddings and documents cannot both be undefined");
}

const embeddingsArray = embeddings
? embeddings
: documents
? await embeddingFunction.generate(documents)
: undefined;

if (!embeddingsArray) {
throw new Error("Failed to generate embeddings for your request.");
}

if (ids) {
for (let i = 0; i < ids.length; i += 1) {
if (typeof ids[i] !== "string") {
throw new Error(
`Expected ids to be strings, found ${typeof ids[i]} at index ${i}`,
);
}
}

const uniqueIds = new Set(ids);
if (uniqueIds.size !== ids.length) {
const duplicateIds = ids.filter(
(item, index) => ids.indexOf(item) !== index,
);
throw new Error(
`ID's must be unique, found duplicates for: ${duplicateIds}`,
);
}
}

return {
ids,
metadatas,
documents,
embeddings: embeddingsArray,
};
}

function notifyUserOfLegacyMethod(newMethod: string) {
return async () => {
throw new Error(
Expand Down

0 comments on commit a974fda

Please sign in to comment.