|
| 1 | +import fetch from 'node-fetch'; |
| 2 | +import {fetchWithCache} from '../cache'; |
| 3 | + |
| 4 | +import type { ApiProvider, ProviderEmbeddingResponse, ProviderResponse } from '../types'; |
| 5 | +import {REQUEST_TIMEOUT_MS} from './shared'; |
| 6 | + |
| 7 | +interface HuggingfaceTextGenerationOptions { |
| 8 | + top_k?: number; |
| 9 | + top_p?: number; |
| 10 | + temperature?: number; |
| 11 | + repetition_penalty?: number; |
| 12 | + max_new_tokens?: number; |
| 13 | + max_time?: number; |
| 14 | + return_full_text?: boolean; |
| 15 | + num_return_sequences?: number; |
| 16 | + do_sample?: boolean; |
| 17 | + use_cache?: boolean; |
| 18 | + wait_for_model?: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +export class HuggingfaceTextGenerationProvider implements ApiProvider { |
| 22 | + modelName: string; |
| 23 | + config: HuggingfaceTextGenerationOptions; |
| 24 | + |
| 25 | + constructor(modelName: string, options: { id?: string, config?: HuggingfaceTextGenerationOptions } = {}) { |
| 26 | + const { id, config } = options; |
| 27 | + this.modelName = modelName; |
| 28 | + this.id = id ? () => id : this.id; |
| 29 | + this.config = config || {}; |
| 30 | + } |
| 31 | + |
| 32 | + id(): string { |
| 33 | + return `huggingface:text-generation:${this.modelName}`; |
| 34 | + } |
| 35 | + |
| 36 | + toString(): string { |
| 37 | + return `[Huggingface Text Generation Provider ${this.modelName}]`; |
| 38 | + } |
| 39 | + |
| 40 | + async callApi(prompt: string): Promise<ProviderResponse> { |
| 41 | + const params = { |
| 42 | + inputs: prompt, |
| 43 | + parameters: { |
| 44 | + return_full_text: this.config.return_full_text ?? false, |
| 45 | + ...this.config |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + let response; |
| 50 | + try { |
| 51 | + response = await fetchWithCache(`https://api-inference.huggingface.co/models/${this.modelName}`, { |
| 52 | + method: 'POST', |
| 53 | + headers: { |
| 54 | + 'Content-Type': 'application/json', |
| 55 | + ...(process.env.HF_API_TOKEN ? { 'Authorization': `Bearer ${process.env.HF_API_TOKEN}` } : {}), |
| 56 | + }, |
| 57 | + body: JSON.stringify(params), |
| 58 | + }, REQUEST_TIMEOUT_MS); |
| 59 | + |
| 60 | + if (response.data.error) { |
| 61 | + return { |
| 62 | + error: `API call error: ${response.data.error}`, |
| 63 | + }; |
| 64 | + } |
| 65 | + if (!response.data[0]) { |
| 66 | + return { |
| 67 | + error: `Malformed response data: ${response.data}`, |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + return { |
| 72 | + output: response.data[0]?.generated_text, |
| 73 | + }; |
| 74 | + } catch(err) { |
| 75 | + return { |
| 76 | + error: `API call error: ${String(err)}. Output:\n${response?.data}`, |
| 77 | + }; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +interface HuggingfaceFeatureExtractionOptions { |
| 83 | + use_cache?: boolean; |
| 84 | + wait_for_model?: boolean; |
| 85 | +} |
| 86 | + |
| 87 | +export class HuggingfaceFeatureExtractionProvider implements ApiProvider { |
| 88 | + modelName: string; |
| 89 | + config: HuggingfaceFeatureExtractionOptions; |
| 90 | + |
| 91 | + constructor(modelName: string, options: { id?: string, config?: HuggingfaceFeatureExtractionOptions } = {}) { |
| 92 | + const { id, config } = options; |
| 93 | + this.modelName = modelName; |
| 94 | + this.id = id ? () => id : this.id; |
| 95 | + this.config = config || {}; |
| 96 | + } |
| 97 | + |
| 98 | + id(): string { |
| 99 | + return `huggingface:feature-extraction:${this.modelName}`; |
| 100 | + } |
| 101 | + |
| 102 | + toString(): string { |
| 103 | + return `[Huggingface Feature Extraction Provider ${this.modelName}]`; |
| 104 | + } |
| 105 | + |
| 106 | + async callApi(): Promise<ProviderResponse> { |
| 107 | + throw new Error('Cannot use a feature extraction provider for text generation'); |
| 108 | + } |
| 109 | + |
| 110 | + async callEmbeddingApi(text: string): Promise<ProviderEmbeddingResponse> { |
| 111 | + const params = { |
| 112 | + inputs: text, |
| 113 | + }; |
| 114 | + |
| 115 | + let response; |
| 116 | + try { |
| 117 | + response = await fetchWithCache(`https://api-inference.huggingface.co/models/${this.modelName}`, { |
| 118 | + method: 'POST', |
| 119 | + headers: { |
| 120 | + 'Content-Type': 'application/json', |
| 121 | + ...(process.env.HF_API_TOKEN ? { 'Authorization': `Bearer ${process.env.HF_API_TOKEN}` } : {}), |
| 122 | + }, |
| 123 | + body: JSON.stringify(params), |
| 124 | + }, REQUEST_TIMEOUT_MS); |
| 125 | + |
| 126 | + if (response.data.error) { |
| 127 | + return { |
| 128 | + error: `API call error: ${response.data.error}`, |
| 129 | + }; |
| 130 | + } |
| 131 | + if (!Array.isArray(response.data)) { |
| 132 | + return { |
| 133 | + error: `Malformed response data: ${response.data}`, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + return { |
| 138 | + embedding: response.data, |
| 139 | + }; |
| 140 | + } catch(err) { |
| 141 | + return { |
| 142 | + error: `API call error: ${String(err)}. Output:\n${response?.data}`, |
| 143 | + }; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
0 commit comments