import { withExponentialBackoff } from "../../util/withExponentialBackoff.js";
import BaseEmbeddingsProvider from "./BaseEmbeddingsProvider.js";
async function embedOne(chunk, options, customFetch) {
    const fetchWithBackoff = () => withExponentialBackoff(() => customFetch(new URL("api/embeddings", options.apiBase), {
        method: "POST",
        body: JSON.stringify({
            model: options.model,
            prompt: chunk,
        }),
    }));
    const resp = await fetchWithBackoff();
    if (!resp.ok) {
        throw new Error(`Failed to embed chunk: ${await resp.text()}`);
    }
    return (await resp.json()).embedding;
}
class OllamaEmbeddingsProvider extends BaseEmbeddingsProvider {
    static defaultOptions = {
        apiBase: "http://localhost:11434/",
    };
    get id() {
        return this.options.model ?? "ollama";
    }
    async embed(chunks) {
        const results = [];
        for (const chunk of chunks) {
            results.push(await embedOne(chunk, this.options, this.fetch));
        }
        return results;
    }
}
export default OllamaEmbeddingsProvider;
