-
Notifications
You must be signed in to change notification settings - Fork 2.7k
basic hugging face provider #6134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
be4fcfa
basic hugging face provider
TGlide 921ede7
fetch hf models and providers
TGlide f24c3cd
save provider to config
TGlide cff1893
Merge remote-tracking branch 'origin/main' into hf-provider
mrubens 9f907ee
Update translations
mrubens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { fetchHuggingFaceModels, type HuggingFaceModel } from "../services/huggingface-models" | ||
|
|
||
| export interface HuggingFaceModelsResponse { | ||
| models: HuggingFaceModel[] | ||
| cached: boolean | ||
| timestamp: number | ||
| } | ||
|
|
||
| export async function getHuggingFaceModels(): Promise<HuggingFaceModelsResponse> { | ||
| const models = await fetchHuggingFaceModels() | ||
|
|
||
| return { | ||
| models, | ||
| cached: false, // We could enhance this to track if data came from cache | ||
| timestamp: Date.now(), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import OpenAI from "openai" | ||
| import { Anthropic } from "@anthropic-ai/sdk" | ||
|
|
||
| import type { ApiHandlerOptions } from "../../shared/api" | ||
| import { ApiStream } from "../transform/stream" | ||
| import { convertToOpenAiMessages } from "../transform/openai-format" | ||
| import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index" | ||
| import { DEFAULT_HEADERS } from "./constants" | ||
| import { BaseProvider } from "./base-provider" | ||
|
|
||
| export class HuggingFaceHandler extends BaseProvider implements SingleCompletionHandler { | ||
| private client: OpenAI | ||
| private options: ApiHandlerOptions | ||
|
|
||
| constructor(options: ApiHandlerOptions) { | ||
| super() | ||
| this.options = options | ||
|
|
||
| if (!this.options.huggingFaceApiKey) { | ||
| throw new Error("Hugging Face API key is required") | ||
| } | ||
|
|
||
| this.client = new OpenAI({ | ||
| baseURL: "https://router.huggingface.co/v1", | ||
| apiKey: this.options.huggingFaceApiKey, | ||
| defaultHeaders: DEFAULT_HEADERS, | ||
| }) | ||
| } | ||
|
|
||
| override async *createMessage( | ||
| systemPrompt: string, | ||
| messages: Anthropic.Messages.MessageParam[], | ||
| metadata?: ApiHandlerCreateMessageMetadata, | ||
| ): ApiStream { | ||
| const modelId = this.options.huggingFaceModelId || "meta-llama/Llama-3.3-70B-Instruct" | ||
| const temperature = this.options.modelTemperature ?? 0.7 | ||
|
|
||
| const params: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = { | ||
| model: modelId, | ||
| temperature, | ||
| messages: [{ role: "system", content: systemPrompt }, ...convertToOpenAiMessages(messages)], | ||
| stream: true, | ||
| stream_options: { include_usage: true }, | ||
| } | ||
|
|
||
| const stream = await this.client.chat.completions.create(params) | ||
|
|
||
| for await (const chunk of stream) { | ||
| const delta = chunk.choices[0]?.delta | ||
|
|
||
| if (delta?.content) { | ||
| yield { | ||
| type: "text", | ||
| text: delta.content, | ||
| } | ||
| } | ||
|
|
||
| if (chunk.usage) { | ||
| yield { | ||
| type: "usage", | ||
| inputTokens: chunk.usage.prompt_tokens || 0, | ||
| outputTokens: chunk.usage.completion_tokens || 0, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async completePrompt(prompt: string): Promise<string> { | ||
| const modelId = this.options.huggingFaceModelId || "meta-llama/Llama-3.3-70B-Instruct" | ||
|
|
||
| try { | ||
| const response = await this.client.chat.completions.create({ | ||
| model: modelId, | ||
| messages: [{ role: "user", content: prompt }], | ||
| }) | ||
|
|
||
| return response.choices[0]?.message.content || "" | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| throw new Error(`Hugging Face completion error: ${error.message}`) | ||
| } | ||
|
|
||
| throw error | ||
| } | ||
| } | ||
|
|
||
| override getModel() { | ||
| const modelId = this.options.huggingFaceModelId || "meta-llama/Llama-3.3-70B-Instruct" | ||
| return { | ||
| id: modelId, | ||
| info: { | ||
| maxTokens: 8192, | ||
| contextWindow: 131072, | ||
| supportsImages: false, | ||
| supportsPromptCache: false, | ||
| }, | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| export interface HuggingFaceModel { | ||
| _id: string | ||
| id: string | ||
| inferenceProviderMapping: InferenceProviderMapping[] | ||
| trendingScore: number | ||
| config: ModelConfig | ||
| tags: string[] | ||
| pipeline_tag: "text-generation" | "image-text-to-text" | ||
| library_name?: string | ||
| } | ||
|
|
||
| export interface InferenceProviderMapping { | ||
| provider: string | ||
| providerId: string | ||
| status: "live" | "staging" | "error" | ||
| task: "conversational" | ||
| } | ||
|
|
||
| export interface ModelConfig { | ||
| architectures: string[] | ||
| model_type: string | ||
| tokenizer_config?: { | ||
| chat_template?: string | Array<{ name: string; template: string }> | ||
| model_max_length?: number | ||
| } | ||
| } | ||
|
|
||
| interface HuggingFaceApiParams { | ||
| pipeline_tag?: "text-generation" | "image-text-to-text" | ||
| filter: string | ||
| inference_provider: string | ||
| limit: number | ||
| expand: string[] | ||
| } | ||
|
|
||
| const DEFAULT_PARAMS: HuggingFaceApiParams = { | ||
| filter: "conversational", | ||
| inference_provider: "all", | ||
| limit: 100, | ||
| expand: [ | ||
| "inferenceProviderMapping", | ||
| "config", | ||
| "library_name", | ||
| "pipeline_tag", | ||
| "tags", | ||
| "mask_token", | ||
| "trendingScore", | ||
| ], | ||
| } | ||
|
|
||
| const BASE_URL = "https://huggingface.co/api/models" | ||
| const CACHE_DURATION = 1000 * 60 * 60 // 1 hour | ||
|
|
||
| interface CacheEntry { | ||
| data: HuggingFaceModel[] | ||
| timestamp: number | ||
| status: "success" | "partial" | "error" | ||
| } | ||
|
|
||
| let cache: CacheEntry | null = null | ||
|
|
||
| function buildApiUrl(params: HuggingFaceApiParams): string { | ||
| const url = new URL(BASE_URL) | ||
|
|
||
| // Add simple params | ||
| Object.entries(params).forEach(([key, value]) => { | ||
| if (!Array.isArray(value)) { | ||
| url.searchParams.append(key, String(value)) | ||
| } | ||
| }) | ||
|
|
||
| // Handle array params specially | ||
| params.expand.forEach((item) => { | ||
| url.searchParams.append("expand[]", item) | ||
| }) | ||
|
|
||
| return url.toString() | ||
| } | ||
|
|
||
| const headers: HeadersInit = { | ||
| "Upgrade-Insecure-Requests": "1", | ||
| "Sec-Fetch-Dest": "document", | ||
| "Sec-Fetch-Mode": "navigate", | ||
| "Sec-Fetch-Site": "none", | ||
| "Sec-Fetch-User": "?1", | ||
| Priority: "u=0, i", | ||
| Pragma: "no-cache", | ||
| "Cache-Control": "no-cache", | ||
| } | ||
|
|
||
| const requestInit: RequestInit = { | ||
| credentials: "include", | ||
| headers, | ||
| method: "GET", | ||
| mode: "cors", | ||
| } | ||
|
|
||
| export async function fetchHuggingFaceModels(): Promise<HuggingFaceModel[]> { | ||
| const now = Date.now() | ||
|
|
||
| // Check cache | ||
| if (cache && now - cache.timestamp < CACHE_DURATION) { | ||
| console.log("Using cached Hugging Face models") | ||
| return cache.data | ||
| } | ||
|
|
||
| try { | ||
| console.log("Fetching Hugging Face models from API...") | ||
|
|
||
| // Fetch both text-generation and image-text-to-text models in parallel | ||
| const [textGenResponse, imgTextResponse] = await Promise.allSettled([ | ||
| fetch(buildApiUrl({ ...DEFAULT_PARAMS, pipeline_tag: "text-generation" }), requestInit), | ||
| fetch(buildApiUrl({ ...DEFAULT_PARAMS, pipeline_tag: "image-text-to-text" }), requestInit), | ||
| ]) | ||
|
|
||
| let textGenModels: HuggingFaceModel[] = [] | ||
| let imgTextModels: HuggingFaceModel[] = [] | ||
| let hasErrors = false | ||
|
|
||
| // Process text-generation models | ||
| if (textGenResponse.status === "fulfilled" && textGenResponse.value.ok) { | ||
| textGenModels = await textGenResponse.value.json() | ||
| } else { | ||
| console.error("Failed to fetch text-generation models:", textGenResponse) | ||
| hasErrors = true | ||
| } | ||
|
|
||
| // Process image-text-to-text models | ||
| if (imgTextResponse.status === "fulfilled" && imgTextResponse.value.ok) { | ||
| imgTextModels = await imgTextResponse.value.json() | ||
| } else { | ||
| console.error("Failed to fetch image-text-to-text models:", imgTextResponse) | ||
| hasErrors = true | ||
| } | ||
|
|
||
| // Combine and filter models | ||
| const allModels = [...textGenModels, ...imgTextModels] | ||
| .filter((model) => model.inferenceProviderMapping.length > 0) | ||
| .sort((a, b) => a.id.toLowerCase().localeCompare(b.id.toLowerCase())) | ||
|
|
||
| // Update cache | ||
| cache = { | ||
| data: allModels, | ||
| timestamp: now, | ||
| status: hasErrors ? "partial" : "success", | ||
| } | ||
|
|
||
| console.log(`Fetched ${allModels.length} Hugging Face models (status: ${cache.status})`) | ||
| return allModels | ||
| } catch (error) { | ||
| console.error("Error fetching Hugging Face models:", error) | ||
|
|
||
| // Return cached data if available | ||
| if (cache) { | ||
| console.log("Using stale cached data due to fetch error") | ||
| cache.status = "error" | ||
| return cache.data | ||
| } | ||
|
|
||
| // No cache available, return empty array | ||
| return [] | ||
| } | ||
| } | ||
|
|
||
| export function getCachedModels(): HuggingFaceModel[] | null { | ||
| return cache?.data || null | ||
| } | ||
|
|
||
| export function clearCache(): void { | ||
| cache = null | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better list sorted by trending https://router.huggingface.co/v1/models (more up to date and less models but better ones)