continue-custom/core/llm/llms/HuggingFaceInferenceAPI.ts
2024-02-20 22:01:34 -08:00

33 lines
956 B
TypeScript

import { BaseLLM } from "..";
import { CompletionOptions, LLMOptions, ModelProvider } from "../..";
import { streamResponse } from "../stream";
class HuggingFaceInferenceAPI extends BaseLLM {
static providerName: ModelProvider = "huggingface-inference-api";
static defaultOptions: Partial<LLMOptions> | undefined = {
apiBase: "https://api-inference.huggingface.co",
};
protected async *_streamComplete(
prompt: string,
options: CompletionOptions
): AsyncGenerator<string> {
const response = await this.fetch(`${this.apiBase}`, {
headers: {
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json",
Accept: "application/json",
},
method: "POST",
body: JSON.stringify({
inputs: prompt,
stream: true,
}),
});
for await (const chunk of streamResponse(response)) {
yield chunk;
}
}
}
export default HuggingFaceInferenceAPI;