Skip to content

Commit

Permalink
Add custom dimension support for OpenAI embeddings.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Jan 26, 2024
1 parent b4dc3a4 commit 25aed33
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import dotenv from "dotenv";
import { embedMany, openai } from "modelfusion";

dotenv.config();

async function main() {
const embeddings = await embedMany({
model: openai.TextEmbedder({
model: "text-embedding-3-large",
dimensions: 256,
}),
values: [
"At first, Nox didn't know what to do with the pup.",
"He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
],
});

console.log(embeddings);
}

main().catch(console.error);
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface AbstractOpenAITextEmbeddingModelSettings

model: string;

dimensions?: number;
maxValuesPerCall?: number | undefined;
isUserIdForwardingEnabled?: boolean;
}
Expand Down Expand Up @@ -62,6 +63,7 @@ export abstract class AbstractOpenAITextEmbeddingModel<
body: {
model: this.modelName,
input: texts,
dimensions: this.settings.dimensions,
user: this.settings.isUserIdForwardingEnabled
? callOptions.run?.userId
: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ export class OpenAITextEmbeddingModel
this.tokenizer = new TikTokenTokenizer({ model: this.modelName });
this.contextWindowSize =
OPENAI_TEXT_EMBEDDING_MODELS[this.modelName].contextWindowSize;
this.dimensions = OPENAI_TEXT_EMBEDDING_MODELS[this.modelName].dimensions;

this.dimensions =
this.settings.dimensions ??
OPENAI_TEXT_EMBEDDING_MODELS[this.modelName].dimensions;
}

readonly provider = "openai" as const;
Expand Down

0 comments on commit 25aed33

Please sign in to comment.