147 lines
4.0 KiB
TypeScript
147 lines
4.0 KiB
TypeScript
import { getRawSecret } from "@common/secret-manager";
|
|
import { getLogger } from "@obs/logger";
|
|
import {
|
|
getEmbeddingApiUrl,
|
|
getEmbeddingDimensions,
|
|
getEmbeddingModel,
|
|
getEmbeddingProvider,
|
|
getEmbeddingRequestTimeoutMs,
|
|
} from "../config.js";
|
|
|
|
export interface EmbeddingClient {
|
|
embed(text: string): Promise<number[]>;
|
|
embedBatch(texts: string[]): Promise<number[][]>;
|
|
readonly model: string;
|
|
readonly dimensions: number;
|
|
}
|
|
|
|
interface EmbeddingResponse {
|
|
data?: Array<{
|
|
embedding?: number[] | undefined;
|
|
index?: number | undefined;
|
|
}> | undefined;
|
|
model?: string | undefined;
|
|
}
|
|
|
|
abstract class HttpEmbeddingClient implements EmbeddingClient {
|
|
constructor(
|
|
readonly model: string,
|
|
readonly dimensions: number,
|
|
private readonly apiKey: string | undefined,
|
|
private readonly baseUrl: string,
|
|
) {}
|
|
|
|
async embed(text: string): Promise<number[]> {
|
|
const [embedding] = await this.embedBatch([text]);
|
|
if (!embedding) {
|
|
throw new Error("Embedding response did not include an embedding");
|
|
}
|
|
return embedding;
|
|
}
|
|
|
|
async embedBatch(texts: string[]): Promise<number[][]> {
|
|
const input = texts.map((text) => text.trim()).filter(Boolean);
|
|
if (input.length === 0) {
|
|
return [];
|
|
}
|
|
if (!this.apiKey) {
|
|
throw new Error(`${this.constructor.name} requires an embedding API key`);
|
|
}
|
|
|
|
const response = await fetch(`${this.baseUrl.replace(/\/+$/u, "")}/embeddings`, {
|
|
body: JSON.stringify({
|
|
dimensions: this.dimensions,
|
|
input,
|
|
model: this.model,
|
|
}),
|
|
headers: {
|
|
Authorization: `Bearer ${this.apiKey}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
method: "POST",
|
|
signal: AbortSignal.timeout(getEmbeddingRequestTimeoutMs()),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const body = (await response.text()).slice(0, 500);
|
|
throw new Error(`Embedding request failed (${response.status}): ${body}`);
|
|
}
|
|
|
|
const payload = (await response.json()) as EmbeddingResponse;
|
|
const rows = payload.data ?? [];
|
|
return rows
|
|
.sort((left, right) => (left.index ?? 0) - (right.index ?? 0))
|
|
.map((row) => row.embedding ?? []);
|
|
}
|
|
}
|
|
|
|
export class OpenRouterEmbeddingClient extends HttpEmbeddingClient {
|
|
constructor() {
|
|
super(
|
|
getEmbeddingModel("openrouter"),
|
|
getEmbeddingDimensions(),
|
|
getRawSecret("EMBEDDING_API_KEY") ?? getRawSecret("OPENROUTER_API_KEY"),
|
|
getEmbeddingApiUrl("openrouter"),
|
|
);
|
|
}
|
|
}
|
|
|
|
export class OpenAIEmbeddingClient extends HttpEmbeddingClient {
|
|
constructor() {
|
|
super(
|
|
getEmbeddingModel("openai").replace(/^openai\//u, ""),
|
|
getEmbeddingDimensions(),
|
|
getRawSecret("EMBEDDING_API_KEY") ?? getRawSecret("OPENAI_API_KEY"),
|
|
getEmbeddingApiUrl("openai"),
|
|
);
|
|
}
|
|
}
|
|
|
|
class FallbackEmbeddingClient implements EmbeddingClient {
|
|
readonly model: string;
|
|
readonly dimensions: number;
|
|
private readonly logger = getLogger();
|
|
|
|
constructor(
|
|
private readonly primary: EmbeddingClient,
|
|
private readonly fallback: EmbeddingClient,
|
|
) {
|
|
this.model = primary.model;
|
|
this.dimensions = primary.dimensions;
|
|
}
|
|
|
|
embed(text: string): Promise<number[]> {
|
|
return this.embedBatch([text]).then((embeddings) => {
|
|
const [embedding] = embeddings;
|
|
if (!embedding) {
|
|
throw new Error("Embedding response did not include an embedding");
|
|
}
|
|
return embedding;
|
|
});
|
|
}
|
|
|
|
async embedBatch(texts: string[]): Promise<number[][]> {
|
|
try {
|
|
return await this.primary.embedBatch(texts);
|
|
} catch (error) {
|
|
this.logger.warn(
|
|
{ err: error, provider: "openrouter" },
|
|
"[Memory] Primary embedding provider failed; falling back",
|
|
);
|
|
return this.fallback.embedBatch(texts);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function createEmbeddingClient(): EmbeddingClient {
|
|
const provider = getEmbeddingProvider();
|
|
if (provider === "openai") {
|
|
return new OpenAIEmbeddingClient();
|
|
}
|
|
|
|
return new FallbackEmbeddingClient(
|
|
new OpenRouterEmbeddingClient(),
|
|
new OpenAIEmbeddingClient(),
|
|
);
|
|
}
|