From 19c28f0a065e8c8b57c4fc66df5efbe457376db3 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Mon, 19 Feb 2024 22:45:39 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20huggingface=20inference=20API=20sup?= =?UTF-8?q?port?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/llm/llms/HuggingFaceInferenceAPI.ts | 41 +++++++++++------------- 1 file changed, 19 insertions(+), 22 deletions(-) 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; + } } }