forked from pollinations/pollinations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestUtils.ts
More file actions
52 lines (47 loc) · 1.95 KB
/
requestUtils.ts
File metadata and controls
52 lines (47 loc) · 1.95 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
import { extractReferrer } from "../shared/extractFromRequest.js";
import type { RequestData } from "./types.js";
import { validateTextGenerationParams } from "./utils/parameterValidators.js";
export interface ExpressLikeRequest {
query: Record<string, unknown>;
body: Record<string, unknown>;
path: string;
params: Record<string, string>;
method: string;
headers: Record<string, string>;
}
export function getRequestData(req: ExpressLikeRequest): RequestData {
const data: Record<string, unknown> = { ...req.query, ...req.body };
const validated = validateTextGenerationParams(data);
const systemPrompt = (data.system as string) || null;
const isPrivate =
req.path?.startsWith("/openai") || validated.private === true;
const messages = (data.messages as RequestData["messages"]) || [
{ role: "user", content: req.params[0] },
];
if (systemPrompt) {
messages.unshift({ role: "system", content: systemPrompt });
}
return {
// Validated params (temperature, top_p, seed, model, stream, etc.)
...validated,
messages,
referrer: extractReferrer(req),
isPrivate,
// Passthrough params not handled by validateTextGenerationParams
tools: data.tools as unknown[] | undefined,
tool_choice: data.tool_choice,
modalities: data.modalities as string[] | undefined,
audio: data.audio as Record<string, unknown> | undefined,
response_format: data.response_format as RequestData["response_format"],
max_tokens: data.max_tokens as number | undefined,
max_completion_tokens: data.max_completion_tokens as number | undefined,
stop: data.stop,
stream_options: data.stream_options as
| Record<string, unknown>
| undefined,
logprobs: data.logprobs,
top_logprobs: data.top_logprobs,
logit_bias: data.logit_bias,
user: data.user,
} as RequestData;
}