-
Notifications
You must be signed in to change notification settings - Fork 2k
/
hf.ts
157 lines (130 loc) Β· 4.4 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { LLM, type BaseLLMParams } from "@langchain/core/language_models/llms";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
/**
* 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;
/**
* Credentials to use for the request. If this is a string, it will be passed straight on. If it's a boolean, true will be "include" and false will not send credentials at all.
*/
includeCredentials?: string | boolean;
}
/**
* Class implementing the Large Language Model (LLM) interface using the
* Hugging Face Inference API for text generation.
* @example
* ```typescript
* const model = new HuggingFaceInference({
* model: "gpt2",
* temperature: 0.7,
* maxTokens: 50,
* });
*
* const res = await model.call(
* "Question: What would be a good company name for a company that makes colorful socks?\nAnswer:"
* );
* console.log({ res });
* ```
*/
export class HuggingFaceInference extends LLM implements HFInput {
lc_serializable = true;
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;
includeCredentials: string | boolean | 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;
this.includeCredentials = fields?.includeCredentials;
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, {
includeCredentials: this.includeCredentials,
}).endpoint(this.endpointUrl)
: new HfInference(this.apiKey, {
includeCredentials: this.includeCredentials,
});
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`"
);
}
}
}