-
Notifications
You must be signed in to change notification settings - Fork 2k
/
hf.ts
130 lines (106 loc) Β· 3.54 KB
/
hf.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { getEnvironmentVariable } from "../util/env.js";
import { LLM, BaseLLMParams } from "./base.js";
/**
* Interface defining the parameters for configuring the Hugging Face
* model for text generation.
*/
export interface HFInput {
/** Model to use */
model: string;
/** Custom inference endpoint URL to use */
endpointUrl?: string;
/** Sampling temperature to use */
temperature?: number;
/**
* Maximum number of tokens to generate in the completion.
*/
maxTokens?: number;
/** Total probability mass of tokens to consider at each step */
topP?: number;
/** Integer to define the top tokens considered within the sample operation to create new text. */
topK?: number;
/** Penalizes repeated tokens according to frequency */
frequencyPenalty?: number;
/** API key to use. */
apiKey?: string;
}
/**
* Class implementing the Large Language Model (LLM) interface using the
* Hugging Face Inference API for text generation.
*/
export class HuggingFaceInference extends LLM implements HFInput {
get lc_secrets(): { [key: string]: string } | undefined {
return {
apiKey: "HUGGINGFACEHUB_API_KEY",
};
}
model = "gpt2";
temperature: number | undefined = undefined;
maxTokens: number | undefined = undefined;
topP: number | undefined = undefined;
topK: number | undefined = undefined;
frequencyPenalty: number | undefined = undefined;
apiKey: string | undefined = undefined;
endpointUrl: string | undefined = undefined;
constructor(fields?: Partial<HFInput> & BaseLLMParams) {
super(fields ?? {});
this.model = fields?.model ?? this.model;
this.temperature = fields?.temperature ?? this.temperature;
this.maxTokens = fields?.maxTokens ?? this.maxTokens;
this.topP = fields?.topP ?? this.topP;
this.topK = fields?.topK ?? this.topK;
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
this.apiKey =
fields?.apiKey ?? getEnvironmentVariable("HUGGINGFACEHUB_API_KEY");
this.endpointUrl = fields?.endpointUrl;
if (!this.apiKey) {
throw new Error(
"Please set an API key for HuggingFace Hub in the environment variable HUGGINGFACEHUB_API_KEY or in the apiKey field of the HuggingFaceInference constructor."
);
}
}
_llmType() {
return "hf";
}
/** @ignore */
async _call(
prompt: string,
options: this["ParsedCallOptions"]
): Promise<string> {
const { HfInference } = await HuggingFaceInference.imports();
const hf = this.endpointUrl
? new HfInference(this.apiKey).endpoint(this.endpointUrl)
: new HfInference(this.apiKey);
const res = await this.caller.callWithOptions(
{ signal: options.signal },
hf.textGeneration.bind(hf),
{
model: this.model,
parameters: {
// make it behave similar to openai, returning only the generated text
return_full_text: false,
temperature: this.temperature,
max_new_tokens: this.maxTokens,
top_p: this.topP,
top_k: this.topK,
repetition_penalty: this.frequencyPenalty,
},
inputs: prompt,
}
);
return res.generated_text;
}
/** @ignore */
static async imports(): Promise<{
HfInference: typeof import("@huggingface/inference").HfInference;
}> {
try {
const { HfInference } = await import("@huggingface/inference");
return { HfInference };
} catch (e) {
throw new Error(
"Please install huggingface as a dependency with, e.g. `yarn add @huggingface/inference`"
);
}
}
}