mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-05 17:20:22 +00:00
* fix(memory): add input_type to Voyage AI embeddings for improved retrieval Voyage AI recommends passing input_type='document' when indexing and input_type='query' when searching. This improves retrieval quality by optimising the embedding space for each direction. Changes: - embedQuery now passes input_type: 'query' - embedBatch now passes input_type: 'document' - Batch API request_params includes input_type: 'document' - Tests updated to verify input_type is passed correctly * Changelog: note Voyage embeddings input_type fix (#10818) (thanks @mcinteerj) --------- Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
93 lines
3.0 KiB
TypeScript
93 lines
3.0 KiB
TypeScript
import type { EmbeddingProvider, EmbeddingProviderOptions } from "./embeddings.js";
|
|
import { requireApiKey, resolveApiKeyForProvider } from "../agents/model-auth.js";
|
|
|
|
export type VoyageEmbeddingClient = {
|
|
baseUrl: string;
|
|
headers: Record<string, string>;
|
|
model: string;
|
|
};
|
|
|
|
export const DEFAULT_VOYAGE_EMBEDDING_MODEL = "voyage-4-large";
|
|
const DEFAULT_VOYAGE_BASE_URL = "https://api.voyageai.com/v1";
|
|
|
|
export function normalizeVoyageModel(model: string): string {
|
|
const trimmed = model.trim();
|
|
if (!trimmed) return DEFAULT_VOYAGE_EMBEDDING_MODEL;
|
|
if (trimmed.startsWith("voyage/")) return trimmed.slice("voyage/".length);
|
|
return trimmed;
|
|
}
|
|
|
|
export async function createVoyageEmbeddingProvider(
|
|
options: EmbeddingProviderOptions,
|
|
): Promise<{ provider: EmbeddingProvider; client: VoyageEmbeddingClient }> {
|
|
const client = await resolveVoyageEmbeddingClient(options);
|
|
const url = `${client.baseUrl.replace(/\/$/, "")}/embeddings`;
|
|
|
|
const embed = async (input: string[], input_type?: "query" | "document"): Promise<number[][]> => {
|
|
if (input.length === 0) return [];
|
|
const body: { model: string; input: string[]; input_type?: "query" | "document" } = {
|
|
model: client.model,
|
|
input,
|
|
};
|
|
if (input_type) body.input_type = input_type;
|
|
|
|
const res = await fetch(url, {
|
|
method: "POST",
|
|
headers: client.headers,
|
|
body: JSON.stringify(body),
|
|
});
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
throw new Error(`voyage embeddings failed: ${res.status} ${text}`);
|
|
}
|
|
const payload = (await res.json()) as {
|
|
data?: Array<{ embedding?: number[] }>;
|
|
};
|
|
const data = payload.data ?? [];
|
|
return data.map((entry) => entry.embedding ?? []);
|
|
};
|
|
|
|
return {
|
|
provider: {
|
|
id: "voyage",
|
|
model: client.model,
|
|
embedQuery: async (text) => {
|
|
const [vec] = await embed([text], "query");
|
|
return vec ?? [];
|
|
},
|
|
embedBatch: async (texts) => embed(texts, "document"),
|
|
},
|
|
client,
|
|
};
|
|
}
|
|
|
|
export async function resolveVoyageEmbeddingClient(
|
|
options: EmbeddingProviderOptions,
|
|
): Promise<VoyageEmbeddingClient> {
|
|
const remote = options.remote;
|
|
const remoteApiKey = remote?.apiKey?.trim();
|
|
const remoteBaseUrl = remote?.baseUrl?.trim();
|
|
|
|
const apiKey = remoteApiKey
|
|
? remoteApiKey
|
|
: requireApiKey(
|
|
await resolveApiKeyForProvider({
|
|
provider: "voyage",
|
|
cfg: options.config,
|
|
agentDir: options.agentDir,
|
|
}),
|
|
"voyage",
|
|
);
|
|
|
|
const providerConfig = options.config.models?.providers?.voyage;
|
|
const baseUrl = remoteBaseUrl || providerConfig?.baseUrl?.trim() || DEFAULT_VOYAGE_BASE_URL;
|
|
const headerOverrides = Object.assign({}, providerConfig?.headers, remote?.headers);
|
|
const headers: Record<string, string> = {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${apiKey}`,
|
|
...headerOverrides,
|
|
};
|
|
const model = normalizeVoyageModel(options.model);
|
|
return { baseUrl, headers, model };
|
|
}
|