这是indexloc提供的服务,不要输入任何密码
Skip to content

Add gemini-embedding-exp-03-07 model support #3767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions frontend/src/components/EmbeddingSelection/GeminiOptions/index.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
const DEFAULT_MODELS = [
{
id: "embedding-001",
name: "Embedding 001",
},
{
id: "text-embedding-004",
name: "Text Embedding 004",
},
{
id: "gemini-embedding-exp-03-07",
name: "Gemini Embedding Exp 03 07",
},
];

export default function GeminiOptions({ settings }) {
return (
<div className="w-full flex flex-col gap-y-4">
Expand Down Expand Up @@ -27,14 +42,14 @@ export default function GeminiOptions({ settings }) {
className="border-none bg-theme-settings-input-bg border-gray-500 text-white text-sm rounded-lg block w-full p-2.5"
>
<optgroup label="Available embedding models">
{["text-embedding-004"].map((model) => {
{DEFAULT_MODELS.map((model) => {
return (
<option
key={model}
value={model}
selected={settings?.EmbeddingModelPref === model}
key={model.id}
value={model.id}
selected={settings?.EmbeddingModelPref === model.id}
>
{model}
{model.name}
</option>
);
})}
Expand Down
19 changes: 13 additions & 6 deletions server/utils/EmbeddingEngines/gemini/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const { toChunks } = require("../../helpers");

const MODEL_MAP = {
"embedding-001": 2048,
"text-embedding-004": 2048,
"gemini-embedding-exp-03-07": 8192,
};

class GeminiEmbedder {
constructor() {
if (!process.env.GEMINI_EMBEDDING_API_KEY)
Expand All @@ -16,9 +22,10 @@ class GeminiEmbedder {
this.maxConcurrentChunks = 4;

// https://ai.google.dev/gemini-api/docs/models/gemini#text-embedding-and-embedding
// TODO: May need to make this dynamic based on the model
this.embeddingMaxChunkLength = 2_048;
this.log(`Initialized with ${this.model}`);
this.embeddingMaxChunkLength = MODEL_MAP[this.model] || 2_048;
this.log(
`Initialized with ${this.model} - Max Size: ${this.embeddingMaxChunkLength}`
);
}

log(text, ...args) {
Expand All @@ -27,7 +34,7 @@ class GeminiEmbedder {

/**
* Embeds a single text input
* @param {string} textInput - The text to embed
* @param {string|string[]} textInput - The text to embed
* @returns {Promise<Array<number>>} The embedding values
*/
async embedTextInput(textInput) {
Expand All @@ -39,7 +46,7 @@ class GeminiEmbedder {

/**
* Embeds a list of text inputs
* @param {Array<string>} textInputs - The list of text to embed
* @param {string[]} textChunks - The list of text to embed
* @returns {Promise<Array<Array<number>>>} The embedding values
*/
async embedChunks(textChunks = []) {
Expand Down Expand Up @@ -98,7 +105,7 @@ class GeminiEmbedder {
};
});

if (!!error) throw new Error(`OpenAI Failed to embed: ${error}`);
if (!!error) throw new Error(`Gemini Failed to embed: ${error}`);
return data.length > 0 &&
data.every((embd) => embd.hasOwnProperty("embedding"))
? data.map((embd) => embd.embedding)
Expand Down