forked from pollinations/pollinations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestUtils.js
More file actions
108 lines (90 loc) · 3.28 KB
/
requestUtils.js
File metadata and controls
108 lines (90 loc) · 3.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
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
import debug from "debug";
import dotenv from "dotenv";
// Import shared utilities for authentication and environment handling
import { extractReferrer } from "../shared/extractFromRequest.js";
// Import parameter validators
import {
validateTextGenerationParams,
validateJsonMode,
} from "./utils/parameterValidators.js";
// Load environment variables including .env.local overrides
// Load .env.local first (higher priority), then .env as fallback
dotenv.config();
dotenv.config({ path: ".env.local" });
const log = debug("pollinations:requestUtils");
/**
* Common function to handle request data
* @param {object} req - Express request object
* @returns {object} - Processed request data
*/
export function getRequestData(req) {
const query = req.query || {};
const body = req.body || {};
const data = { ...query, ...body };
// Use validators to eliminate duplication
const validated = validateTextGenerationParams(data);
const systemPrompt = data.system ? data.system : null;
const isPrivate = req.path?.startsWith("/openai")
? true
: validated.private === true;
// Use shared referrer extraction utility
const referrer = extractReferrer(req);
// Extract audio parameters
const modalities = data.modalities;
const audio = data.audio;
// Extract tools and tool_choice for function calling
const tools = data.tools || undefined;
const tool_choice = data.tool_choice || undefined;
// Preserve the original response_format object if it exists
const response_format = data.response_format || undefined;
// Extract max_tokens for controlling response length
const max_tokens = data.max_tokens || undefined;
const max_completion_tokens = data.max_completion_tokens || undefined;
// Extract stop sequences
const stop = data.stop || undefined;
// Extract stream_options for streaming configuration
const stream_options = data.stream_options || undefined;
// Extract logprobs for log probabilities
const logprobs = data.logprobs || undefined;
const top_logprobs = data.top_logprobs || undefined;
// Extract logit_bias for token bias
const logit_bias = data.logit_bias || undefined;
// Extract user identifier
const user = data.user || undefined;
const messages = data.messages || [
{ role: "user", content: req.params[0] },
];
if (systemPrompt) {
messages.unshift({ role: "system", content: systemPrompt });
}
return {
messages,
jsonMode: validated.jsonMode,
seed: validated.seed,
model: validated.model,
temperature: validated.temperature,
top_p: validated.top_p,
presence_penalty: validated.presence_penalty,
frequency_penalty: validated.frequency_penalty,
repetition_penalty: validated.repetition_penalty,
referrer,
stream: validated.stream,
isPrivate,
voice: validated.voice,
tools,
tool_choice,
modalities,
audio,
reasoning_effort: validated.reasoning_effort,
thinking_budget: validated.thinking_budget,
response_format,
max_tokens,
max_completion_tokens,
stop,
stream_options,
logprobs,
top_logprobs,
logit_bias,
user,
};
}