-
Notifications
You must be signed in to change notification settings - Fork 315
/
huggingface.ts
141 lines (132 loc) · 3.91 KB
/
huggingface.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
import {
HfInference,
type Options as HfInferenceOptions,
} from "@huggingface/inference";
import { BaseLLM } from "./base.js";
import type {
ChatMessage,
ChatResponse,
ChatResponseChunk,
LLMChatParamsNonStreaming,
LLMChatParamsStreaming,
LLMMetadata,
ToolCallLLMMessageOptions,
} from "./types.js";
import { streamConverter, wrapLLMEvent } from "./utils.js";
const DEFAULT_PARAMS = {
temperature: 0.1,
topP: 1,
maxTokens: undefined,
contextWindow: 3900,
};
export type HFConfig = Partial<typeof DEFAULT_PARAMS> &
HfInferenceOptions & {
model: string;
accessToken: string;
endpoint?: string;
};
/**
Wrapper on the Hugging Face's Inference API.
API Docs: https://huggingface.co/docs/huggingface.js/inference/README
List of tasks with models: huggingface.co/api/tasks
Note that Conversational API is not yet supported by the Inference API.
They recommend using the text generation API instead.
See: https://github.com/huggingface/huggingface.js/issues/586#issuecomment-2024059308
*/
export class HuggingFaceInferenceAPI extends BaseLLM {
model: string;
temperature: number;
topP: number;
maxTokens?: number;
contextWindow: number;
hf: HfInference;
constructor(init: HFConfig) {
super();
const {
model,
temperature,
topP,
maxTokens,
contextWindow,
accessToken,
endpoint,
...hfInferenceOpts
} = init;
this.hf = new HfInference(accessToken, hfInferenceOpts);
this.model = model;
this.temperature = temperature ?? DEFAULT_PARAMS.temperature;
this.topP = topP ?? DEFAULT_PARAMS.topP;
this.maxTokens = maxTokens ?? DEFAULT_PARAMS.maxTokens;
this.contextWindow = contextWindow ?? DEFAULT_PARAMS.contextWindow;
if (endpoint) this.hf.endpoint(endpoint);
}
get metadata(): LLMMetadata {
return {
model: this.model,
temperature: this.temperature,
topP: this.topP,
maxTokens: this.maxTokens,
contextWindow: this.contextWindow,
tokenizer: undefined,
};
}
chat(
params: LLMChatParamsStreaming,
): Promise<AsyncIterable<ChatResponseChunk>>;
chat(params: LLMChatParamsNonStreaming): Promise<ChatResponse>;
@wrapLLMEvent
async chat(
params: LLMChatParamsStreaming | LLMChatParamsNonStreaming,
): Promise<AsyncIterable<ChatResponseChunk> | ChatResponse<object>> {
if (params.stream) return this.streamChat(params);
return this.nonStreamChat(params);
}
private messagesToPrompt(messages: ChatMessage<ToolCallLLMMessageOptions>[]) {
let prompt = "";
for (const message of messages) {
if (message.role === "system") {
prompt += `<|system|>\n${message.content}</s>\n`;
} else if (message.role === "user") {
prompt += `<|user|>\n${message.content}</s>\n`;
} else if (message.role === "assistant") {
prompt += `<|assistant|>\n${message.content}</s>\n`;
}
}
// ensure we start with a system prompt, insert blank if needed
if (!prompt.startsWith("<|system|>\n")) {
prompt = "<|system|>\n</s>\n" + prompt;
}
// add final assistant prompt
prompt = prompt + "<|assistant|>\n";
return prompt;
}
protected async nonStreamChat(
params: LLMChatParamsNonStreaming,
): Promise<ChatResponse> {
const res = await this.hf.textGeneration({
model: this.model,
inputs: this.messagesToPrompt(params.messages),
parameters: this.metadata,
});
return {
raw: res,
message: {
content: res.generated_text,
role: "assistant",
},
};
}
protected async *streamChat(
params: LLMChatParamsStreaming,
): AsyncIterable<ChatResponseChunk> {
const stream = this.hf.textGenerationStream({
model: this.model,
inputs: this.messagesToPrompt(params.messages),
parameters: this.metadata,
});
yield* streamConverter(stream, (chunk) => ({
delta: chunk.token.text,
raw: chunk,
}));
}
}