-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
145 lines (124 loc) · 3.07 KB
/
main.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 { config } from './config';
interface SuccessResponse {
model_version: string;
completions: {
completion: string;
log_probs: object;
raw_completion: string;
completion_tokens: string[];
finish_reason: string;
}[];
optimized_prompt?: {
type: 'text';
data: string;
}[];
num_tokens_prompt_total: number;
num_tokens_generated: number;
}
interface Completion {
Content: string;
TokenUsage?: number;
}
interface ConnectorResponse {
Completions: Completion[];
ModelType: string;
}
interface ChatCompletion {
output: string;
stats: { num_tokens_prompt_total: number };
}
const mapToResponse = (
outputs: (ChatCompletion | ErrorCompletion)[],
model: string,
): ConnectorResponse => {
return {
Completions: outputs.map((output) => {
if ('error' in output) {
return {
Content: output.error,
TokenUsage: output.usage,
};
}
return {
Content: output.output,
TokenUsage: output.stats.num_tokens_prompt_total,
};
}),
ModelType: model,
};
};
interface ErrorCompletion {
error: string;
model: string;
usage: undefined;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mapErrorToCompletion = (error: any, model: string): ErrorCompletion => {
const errorMessage = error.error || JSON.stringify(error);
return {
error: errorMessage,
model,
usage: undefined,
};
};
export interface ConnectorSetting {
SettingID: string;
Name: string;
Value?: string;
Type: string;
}
async function main(
model: string,
prompts: string[],
properties: Record<string, unknown>,
settings: Record<string, unknown>,
): Promise<ConnectorResponse> {
const url = `https://api.aleph-alpha.com/complete`;
const { maximum_tokens, ...restProperties } = properties;
const outputs: (ChatCompletion | ErrorCompletion)[] = [];
const defaultMaxToken = config.properties.filter(
(property) => property.id === 'maximum_tokens',
)[0];
try {
for (const prompt of prompts) {
try {
const dataContent = [{ type: 'text', data: prompt }];
const data = {
model,
prompt: dataContent,
maximum_tokens: maximum_tokens || defaultMaxToken.value,
...restProperties,
};
const assistantResponse: SuccessResponse = await (
await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${settings?.['BEARER_TOKEN']}`,
},
body: JSON.stringify(data),
})
).json();
console.log(assistantResponse);
outputs.push({
output: assistantResponse.completions
.map((completion) => completion.completion)
.join('\n'),
stats: {
num_tokens_prompt_total: assistantResponse.num_tokens_prompt_total,
},
});
console.log(`Response to prompt: ${prompt}`, assistantResponse);
} catch (error) {
const completionWithError = mapErrorToCompletion(error, model);
outputs.push(completionWithError);
}
}
return mapToResponse(outputs, model);
} catch (error) {
console.error('Error in main function:', error);
throw error;
}
}
export { main, config };