continue-custom/core/indexing/embeddings/FunctionNetworkEmbeddingsProvider.ts
2024-11-29 15:45:26 -06:00

69 lines
2 KiB
TypeScript

import { Response } from "node-fetch";
import { EmbeddingsProviderName, EmbedOptions } from "../../index.js";
import { withExponentialBackoff } from "../../util/withExponentialBackoff.js";
import BaseEmbeddingsProvider from "./BaseEmbeddingsProvider.js";
class FunctionNetworkEmbeddingsProvider extends BaseEmbeddingsProvider {
static providerName: EmbeddingsProviderName = "function-network";
static maxBatchSize = 128;
static defaultOptions: Partial<EmbedOptions> | undefined = {
apiBase: "https://api.function.network/v1/",
model: "baai/bge-base-en-v1.5",
};
private _getEndpoint() {
if (!this.options.apiBase) {
throw new Error(
"No API base URL provided. Please set the 'apiBase' option in config.json",
);
}
this.options.apiBase = this.options.apiBase.endsWith("/")
? this.options.apiBase
: `${this.options.apiBase}/`;
return new URL("embeddings", this.options.apiBase);
}
async embed(chunks: string[]) {
const batchedChunks = this.getBatchedChunks(chunks);
return (
await Promise.all(
batchedChunks.map(async (batch) => {
if (batch.length === 0) {
return [];
}
const fetchWithBackoff = () =>
withExponentialBackoff<Response>(() =>
this.fetch(this._getEndpoint(), {
method: "POST",
body: JSON.stringify({
input: batch,
model: this.options.model,
}),
headers: {
Authorization: `Bearer ${this.options.apiKey}`,
"Content-Type": "application/json",
},
}),
);
const resp = await fetchWithBackoff();
if (!resp.ok) {
throw new Error(await resp.text());
}
const data = (await resp.json()) as any;
return data.data.map(
(result: { embedding: number[] }) => result.embedding,
);
}),
)
).flat();
}
}
export default FunctionNetworkEmbeddingsProvider;