-
Notifications
You must be signed in to change notification settings - Fork 39
/
api.ts
145 lines (130 loc) 路 3.78 KB
/
api.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
import type { AIPromptConfig, AIServiceOptions } from '../../text/types.js';
import type { AITextChatRequest } from '../../tracing/types.js';
import type { API } from '../../util/apicall.js';
import { BaseAI } from '../base.js';
import type { TextModelConfig, TextResponse } from '../types.js';
import { modelInfoHuggingFace } from './info.js';
import {
type HuggingFaceConfig,
HuggingFaceModel,
type HuggingFaceRequest,
type HuggingFaceResponse
} from './types.js';
/**
* HuggingFace: Default Model options for text generation
* @export
*/
export const HuggingFaceDefaultOptions = (): HuggingFaceConfig => ({
model: HuggingFaceModel.MetaLlama270BChatHF,
maxNewTokens: 500,
temperature: 0,
topP: 1
});
/**
* HuggingFace: Default model options for more creative text generation
* @export
*/
export const HuggingFaceCreativeOptions = (): HuggingFaceConfig => ({
...HuggingFaceDefaultOptions(),
model: HuggingFaceModel.MetaLlama270BChatHF,
temperature: 0.9
});
export interface HuggingFaceArgs {
apiKey: string;
config?: Readonly<HuggingFaceConfig>;
options?: Readonly<AIServiceOptions>;
}
/**
* HuggingFace: AI Service
* @export
*/
export class HuggingFace extends BaseAI<
HuggingFaceRequest,
unknown,
HuggingFaceResponse,
unknown,
unknown
> {
private config: HuggingFaceConfig;
constructor({
apiKey,
config = HuggingFaceDefaultOptions(),
options
}: Readonly<HuggingFaceArgs>) {
if (!apiKey || apiKey === '') {
throw new Error('HuggingFace API key not set');
}
super({
name: 'HuggingFace',
apiURL: 'https://api-inference.huggingface.co',
headers: { Authorization: `Bearer ${apiKey}` },
modelInfo: modelInfoHuggingFace,
models: { model: config.model },
options,
supportFor: { functions: false }
});
this.config = config;
}
override getModelConfig(): TextModelConfig {
const { config } = this;
return {
maxTokens: config.maxNewTokens,
temperature: config.temperature,
topP: config.topP,
topK: config.topK,
n: config.numReturnSequences,
presencePenalty: config.repetitionPenalty
} as TextModelConfig;
}
override generateChatReq = (
req: Readonly<AITextChatRequest>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_config: Readonly<AIPromptConfig>
): [API, HuggingFaceRequest] => {
const model = req.modelInfo?.name ?? this.config.model;
const functionsList = req.functions
? `Functions:\n${JSON.stringify(req.functions, null, 2)}\n`
: '';
const prompt = req.chatPrompt
?.map((msg) => {
return `${msg.role}: ${msg.content}`;
})
.join('\n');
const inputs = `${functionsList} ${prompt}`.trim();
const apiConfig = {
name: '/models'
};
const reqValue: HuggingFaceRequest = {
model,
inputs,
parameters: {
max_new_tokens: req.modelConfig?.maxTokens ?? this.config.maxNewTokens,
repetition_penalty:
req.modelConfig?.presencePenalty ?? this.config.repetitionPenalty,
temperature: req.modelConfig?.temperature ?? this.config.temperature,
top_p: req.modelConfig?.topP ?? this.config.topP,
top_k: req.modelConfig?.topK ?? this.config.topK,
return_full_text: this.config.returnFullText,
num_return_sequences: this.config.numReturnSequences,
do_sample: this.config.doSample,
max_time: this.config.maxTime
},
options: {
use_cache: this.config.useCache,
wait_for_model: this.config.waitForModel
}
};
return [apiConfig, reqValue];
};
override generateChatResp = (
resp: Readonly<HuggingFaceResponse>
): TextResponse => {
return {
results: [
{
content: resp.generated_text
}
]
};
};
}