-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel-api.ts
More file actions
81 lines (69 loc) · 2.28 KB
/
Copy pathmodel-api.ts
File metadata and controls
81 lines (69 loc) · 2.28 KB
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
import { Config } from '@/utils';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
export interface ModelInfo {
model?: string;
apiKey?: string;
baseUrl?: string;
headers?: Record<string, string>;
provider?: string;
}
export type GetModelInfo = (params?: { config?: Config }) => Promise<ModelInfo>;
export class ModelAPI {
getModelInfo: GetModelInfo;
constructor(getModelInfo: GetModelInfo) {
this.getModelInfo = getModelInfo;
}
// abstract modelInfo(params: { config?: Config }): Promise<ModelInfo>;
private getProvider = async (params: { model?: string; config?: Config }) => {
const { model, config } = params;
const info = await this.getModelInfo({ config });
const provider = createOpenAICompatible({
name: model || info.model || '',
apiKey: info.apiKey,
baseURL: info.baseUrl || '',
headers: info.headers,
});
return { provider, model: model || info.model || '' };
};
private getModel = async (params: Parameters<ModelAPI['getProvider']>[0]) => {
const { provider, model } = await this.getProvider(params);
return provider(model);
};
private getEmbeddingModel = async (params: Parameters<ModelAPI['getProvider']>[0]) => {
const { provider, model } = await this.getProvider(params);
return provider.embeddingModel(model);
};
completion = async (params: {
messages: any[];
model?: string;
stream?: boolean;
config?: Config;
[key: string]: any;
}): Promise<
| import('ai').StreamTextResult<import('ai').ToolSet, any>
| import('ai').GenerateTextResult<import('ai').ToolSet, any>
> => {
const { messages, model, stream = false, config, ...kwargs } = params;
const { streamText, generateText } = await import('ai');
return await (stream ? streamText : generateText)({
model: await this.getModel({ model, config }),
messages,
...kwargs,
});
};
embedding = async (params: {
values: string[];
model?: string;
stream?: boolean;
config?: Config;
[key: string]: any;
}) => {
const { values, model, config, ...kwargs } = params;
const { embedMany } = await import('ai');
return await embedMany({
model: await this.getEmbeddingModel({ model, config }),
values,
...kwargs,
});
};
}