import { BaseLLM } from "../index.js";
import { streamSse } from "../stream.js";
class HuggingFaceTGI extends BaseLLM {
    static providerName = "huggingface-tgi";
    static defaultOptions = {
        apiBase: "http://localhost:8080/",
    };
    constructor(options) {
        super(options);
        this.fetch(new URL("info", this.apiBase), {
            method: "GET",
        }).then(async (response) => {
            if (response.status !== 200) {
                console.warn("Error calling Hugging Face TGI /info endpoint: ", await response.text());
                return;
            }
            const json = await response.json();
            this.model = json.model_id;
            this.contextLength = Number.parseInt(json.max_input_length);
        });
    }
    _convertArgs(options, prompt) {
        const finalOptions = {
            max_new_tokens: options.maxTokens,
            best_of: 1,
            temperature: options.temperature,
            top_p: options.topP,
            top_k: options.topK,
            presence_penalty: options.presencePenalty,
            frequency_penalty: options.frequencyPenalty,
            stop: options.stop,
        };
        return finalOptions;
    }
    async *_streamComplete(prompt, options) {
        const args = this._convertArgs(options, prompt);
        const response = await this.fetch(new URL("generate_stream", this.apiBase), {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({ inputs: prompt, parameters: args }),
        });
        for await (const value of streamSse(response)) {
            yield value.token.text;
        }
    }
}
export default HuggingFaceTGI;
