|
| 1 | +import OpenAI from "openai" |
| 2 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 3 | + |
| 4 | +import type { ApiHandlerOptions } from "../../shared/api" |
| 5 | +import { ApiStream } from "../transform/stream" |
| 6 | +import { convertToOpenAiMessages } from "../transform/openai-format" |
| 7 | +import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index" |
| 8 | +import { DEFAULT_HEADERS } from "./constants" |
| 9 | +import { BaseProvider } from "./base-provider" |
| 10 | + |
| 11 | +export class HuggingFaceHandler extends BaseProvider implements SingleCompletionHandler { |
| 12 | + private client: OpenAI |
| 13 | + private options: ApiHandlerOptions |
| 14 | + |
| 15 | + constructor(options: ApiHandlerOptions) { |
| 16 | + super() |
| 17 | + this.options = options |
| 18 | + |
| 19 | + if (!this.options.huggingFaceApiKey) { |
| 20 | + throw new Error("Hugging Face API key is required") |
| 21 | + } |
| 22 | + |
| 23 | + this.client = new OpenAI({ |
| 24 | + baseURL: "https://router.huggingface.co/v1", |
| 25 | + apiKey: this.options.huggingFaceApiKey, |
| 26 | + defaultHeaders: DEFAULT_HEADERS, |
| 27 | + }) |
| 28 | + } |
| 29 | + |
| 30 | + override async *createMessage( |
| 31 | + systemPrompt: string, |
| 32 | + messages: Anthropic.Messages.MessageParam[], |
| 33 | + metadata?: ApiHandlerCreateMessageMetadata, |
| 34 | + ): ApiStream { |
| 35 | + const modelId = this.options.huggingFaceModelId || "meta-llama/Llama-3.3-70B-Instruct" |
| 36 | + const temperature = this.options.modelTemperature ?? 0.7 |
| 37 | + |
| 38 | + const params: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = { |
| 39 | + model: modelId, |
| 40 | + temperature, |
| 41 | + messages: [{ role: "system", content: systemPrompt }, ...convertToOpenAiMessages(messages)], |
| 42 | + stream: true, |
| 43 | + stream_options: { include_usage: true }, |
| 44 | + } |
| 45 | + |
| 46 | + const stream = await this.client.chat.completions.create(params) |
| 47 | + |
| 48 | + for await (const chunk of stream) { |
| 49 | + const delta = chunk.choices[0]?.delta |
| 50 | + |
| 51 | + if (delta?.content) { |
| 52 | + yield { |
| 53 | + type: "text", |
| 54 | + text: delta.content, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (chunk.usage) { |
| 59 | + yield { |
| 60 | + type: "usage", |
| 61 | + inputTokens: chunk.usage.prompt_tokens || 0, |
| 62 | + outputTokens: chunk.usage.completion_tokens || 0, |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + async completePrompt(prompt: string): Promise<string> { |
| 69 | + const modelId = this.options.huggingFaceModelId || "meta-llama/Llama-3.3-70B-Instruct" |
| 70 | + |
| 71 | + try { |
| 72 | + const response = await this.client.chat.completions.create({ |
| 73 | + model: modelId, |
| 74 | + messages: [{ role: "user", content: prompt }], |
| 75 | + }) |
| 76 | + |
| 77 | + return response.choices[0]?.message.content || "" |
| 78 | + } catch (error) { |
| 79 | + if (error instanceof Error) { |
| 80 | + throw new Error(`Hugging Face completion error: ${error.message}`) |
| 81 | + } |
| 82 | + |
| 83 | + throw error |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + override getModel() { |
| 88 | + const modelId = this.options.huggingFaceModelId || "meta-llama/Llama-3.3-70B-Instruct" |
| 89 | + return { |
| 90 | + id: modelId, |
| 91 | + info: { |
| 92 | + maxTokens: 8192, |
| 93 | + contextWindow: 131072, |
| 94 | + supportsImages: false, |
| 95 | + supportsPromptCache: false, |
| 96 | + }, |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments