Repository navigation
Expand file tree
/
Copy pathtextGeneratorDrivers.ts
More file actions
257 lines (240 loc) · 9.19 KB
/
Copy pathtextGeneratorDrivers.ts
File metadata and controls
257 lines (240 loc) · 9.19 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import type { TextGeneratorKind, TextGeneratorModel } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
type HttpClientResponse,
} from "effect/http";
/** Where a text generator is reached, with its key already read from wherever settings keep it. */
export interface TextGeneratorConnection {
readonly baseUrl: string;
readonly apiKey: string;
/** Names the generator in errors, such as its label or host. */
readonly label: string;
}
/** Why a model API call failed, worded for the person who configured it. */
export class TextGeneratorRequestError extends Schema.TaggedError<TextGeneratorRequestError>()(
"TextGeneratorRequestError",
{ detail: Schema.String, cause: Schema.optional(Schema.Defect()) },
) {}
export interface TextGeneratorDriver {
readonly kind: TextGeneratorKind;
readonly generate: (
connection: TextGeneratorConnection,
model: string,
prompt: string,
) => Effect.Effect<string, TextGeneratorRequestError, HttpClient.HttpClient>;
readonly listModels: (
connection: TextGeneratorConnection,
) => Effect.Effect<
ReadonlyArray<TextGeneratorModel>,
TextGeneratorRequestError,
HttpClient.HttpClient
>;
}
const GENERATE_TIMEOUT = "2 minutes";
const LIST_MODELS_TIMEOUT = "25 seconds";
const MAX_MODEL_PAGES = 10;
const ApiErrorBody = Schema.Struct({
error: Schema.Struct({ message: Schema.String }),
});
const decodeApiErrorBody = Schema.decodeUnknownOption(ApiErrorBody);
const describeStatus = (label: string, status: number, message: string | undefined) => {
const reason =
status === 401 || status === 403
? "rejected the API key"
: status === 404
? "couldn't find that model or endpoint"
: status === 429
? "is rate-limiting requests"
: `returned HTTP ${status}`;
const detail = message?.trim().slice(0, 300);
return `${label} ${reason}${detail ? `: ${detail}` : "."}`;
};
const endpoint = (connection: TextGeneratorConnection, path: string) =>
`${connection.baseUrl.replace(/\/+$/, "")}${path}`;
/** Sends a request and decodes a 2xx JSON body; anything else becomes a readable error. */
const sendJson = Effect.fn("TextGenerator.sendJson")(function* <S extends Schema.Top>(
label: string,
request: HttpClientRequest.HttpClientRequest,
schema: S,
) {
const http = yield* HttpClient.HttpClient;
const response: HttpClientResponse.HttpClientResponse = yield* http.execute(request).pipe(
// A redirect could carry the key to another origin.
Effect.provideService(FetchHttpClient.RequestInit, { redirect: "error" }),
Effect.provideService(HttpClient.TracerPropagationEnabled, false),
Effect.mapError(
(cause) =>
new TextGeneratorRequestError({
detail: `Couldn't reach ${label}. Check the base URL and your connection.`,
cause,
}),
),
);
const body = yield* response.json.pipe(Effect.orElseSucceed((): unknown => undefined));
if (response.status < 200 || response.status >= 300) {
const message = decodeApiErrorBody(body).pipe((option) =>
option._tag === "Some" ? option.value.error.message : undefined,
);
return yield* new TextGeneratorRequestError({
detail: describeStatus(label, response.status, message),
});
}
return yield* Schema.decodeUnknownEffect(schema)(body).pipe(
Effect.mapError(
(cause) =>
new TextGeneratorRequestError({
detail: `${label} returned a reply T3 Code couldn't read.`,
cause,
}),
),
);
});
const withTimeout =
(label: string, duration: typeof GENERATE_TIMEOUT | typeof LIST_MODELS_TIMEOUT) =>
<A, R>(effect: Effect.Effect<A, TextGeneratorRequestError, R>) =>
effect.pipe(
Effect.timeoutOrElse({
duration,
orElse: () =>
Effect.fail(
new TextGeneratorRequestError({ detail: `${label} took too long to respond.` }),
),
}),
);
const sortModels = (models: Iterable<TextGeneratorModel>) =>
[...models].sort((left, right) => left.id.localeCompare(right.id));
const readModel = (entry: { readonly id: string; readonly name?: string | undefined }) => {
const id = entry.id.trim();
return id ? { id, name: entry.name?.trim() || id } : undefined;
};
const ChatCompletion = Schema.Struct({
choices: Schema.Array(
Schema.Struct({
finish_reason: Schema.NullOr(Schema.String),
message: Schema.Struct({ content: Schema.NullOr(Schema.String) }),
}),
),
});
const OpenAIModelList = Schema.Struct({
data: Schema.Array(Schema.Struct({ id: Schema.String, name: Schema.optional(Schema.String) })),
});
const openAICompatible: TextGeneratorDriver = {
kind: "openaiCompatible",
generate: (connection, model, prompt) => {
const { label } = connection;
let request = HttpClientRequest.post(endpoint(connection, "/chat/completions")).pipe(
HttpClientRequest.bodyJsonUnsafe({ model, messages: [{ role: "user", content: prompt }] }),
);
if (connection.apiKey) request = HttpClientRequest.bearerToken(request, connection.apiKey);
return sendJson(label, request, ChatCompletion).pipe(
Effect.flatMap((completion) => {
const choice = completion.choices[0];
const text = choice?.message.content?.trim();
if (choice?.finish_reason === "length") {
return Effect.fail(
new TextGeneratorRequestError({ detail: `${label} reached its output limit.` }),
);
}
return text
? Effect.succeed(text)
: Effect.fail(new TextGeneratorRequestError({ detail: `${label} returned no text.` }));
}),
withTimeout(label, GENERATE_TIMEOUT),
);
},
listModels: (connection) => {
const { label } = connection;
let request = HttpClientRequest.get(endpoint(connection, "/models"));
if (connection.apiKey) request = HttpClientRequest.bearerToken(request, connection.apiKey);
return sendJson(label, request, OpenAIModelList).pipe(
Effect.map((list) => sortModels(list.data.flatMap((entry) => readModel(entry) ?? []))),
withTimeout(label, LIST_MODELS_TIMEOUT),
);
},
};
const ANTHROPIC_VERSION = "2023-06-01";
const AnthropicMessage = Schema.Struct({
stop_reason: Schema.NullOr(Schema.String),
content: Schema.Array(
Schema.Struct({ type: Schema.String, text: Schema.optional(Schema.String) }),
),
});
const AnthropicModelPage = Schema.Struct({
data: Schema.Array(
Schema.Struct({ id: Schema.String, display_name: Schema.optional(Schema.String) }),
),
has_more: Schema.optional(Schema.Boolean),
last_id: Schema.optional(Schema.NullOr(Schema.String)),
});
const anthropicRequest = (request: HttpClientRequest.HttpClientRequest, apiKey: string) =>
request.pipe(
HttpClientRequest.setHeader("x-api-key", apiKey),
HttpClientRequest.setHeader("anthropic-version", ANTHROPIC_VERSION),
);
const anthropic: TextGeneratorDriver = {
kind: "anthropic",
generate: (connection, model, prompt) => {
const { label } = connection;
const request = anthropicRequest(
HttpClientRequest.post(endpoint(connection, "/messages")).pipe(
HttpClientRequest.bodyJsonUnsafe({
model,
max_tokens: 8192,
messages: [{ role: "user", content: prompt }],
}),
),
connection.apiKey,
);
return sendJson(label, request, AnthropicMessage).pipe(
Effect.flatMap((message) => {
if (message.stop_reason === "max_tokens") {
return Effect.fail(
new TextGeneratorRequestError({ detail: `${label} reached its output limit.` }),
);
}
const text = message.content
.flatMap((block) => (block.type === "text" && block.text ? [block.text] : []))
.join("")
.trim();
return text
? Effect.succeed(text)
: Effect.fail(new TextGeneratorRequestError({ detail: `${label} returned no text.` }));
}),
withTimeout(label, GENERATE_TIMEOUT),
);
},
listModels: Effect.fn("TextGenerator.anthropic.listModels")(
function* (connection) {
const { label } = connection;
const models = new Map<string, TextGeneratorModel>();
let after: string | undefined;
for (let page = 0; page < MAX_MODEL_PAGES; page++) {
let request = HttpClientRequest.get(endpoint(connection, "/models")).pipe(
HttpClientRequest.setUrlParam("limit", "1000"),
);
if (after) request = HttpClientRequest.setUrlParam(request, "after_id", after);
const result = yield* sendJson(
label,
anthropicRequest(request, connection.apiKey),
AnthropicModelPage,
);
for (const entry of result.data) {
const model = readModel({ id: entry.id, name: entry.display_name });
if (model) models.set(model.id, model);
}
if (!result.has_more || !result.last_id || result.last_id === after) break;
after = result.last_id;
}
return sortModels(models.values());
},
(effect, connection) => effect.pipe(withTimeout(connection.label, LIST_MODELS_TIMEOUT)),
),
};
export const TEXT_GENERATOR_DRIVERS: Readonly<Record<TextGeneratorKind, TextGeneratorDriver>> = {
openaiCompatible: openAICompatible,
anthropic,
};