diff --git a/core/llm/llms/HuggingFaceInferenceAPI.ts b/core/llm/llms/HuggingFaceInferenceAPI.ts index 51acf308d..ba6286b95 100644 --- a/core/llm/llms/HuggingFaceInferenceAPI.ts +++ b/core/llm/llms/HuggingFaceInferenceAPI.ts @@ -1,34 +1,31 @@ import { BaseLLM } from ".."; -import { ChatMessage, CompletionOptions, ModelProvider } from "../.."; +import { CompletionOptions, LLMOptions, ModelProvider } from "../.."; +import { streamResponse } from "../stream"; class HuggingFaceInferenceAPI extends BaseLLM { static providerName: ModelProvider = "huggingface-inference-api"; - - private _convertArgs(options: CompletionOptions, prompt: string) { - const finalOptions = {}; - - return finalOptions; - } - - protected async _complete( - prompt: string, - options: CompletionOptions - ): Promise { - throw new Error("Method not implemented."); - } + static defaultOptions: Partial | undefined = { + apiBase: "https://api-inference.huggingface.co", + }; protected async *_streamComplete( prompt: string, options: CompletionOptions ): AsyncGenerator { - throw new Error("Method not implemented."); - } - - protected async *_streamChat( - messages: ChatMessage[], - options: CompletionOptions - ): AsyncGenerator { - throw new Error("Method not implemented."); + const response = await this.fetch( + `${this.apiBase}/models/${options.model}`, + { + headers: { Authorization: `Bearer ${this.apiKey}` }, + method: "POST", + body: JSON.stringify({ + inputs: prompt, + stream: true, + }), + } + ); + for await (const chunk of streamResponse(response)) { + yield chunk; + } } }