-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathRequestDataService.ts
More file actions
80 lines (64 loc) · 1.91 KB
/
Copy pathRequestDataService.ts
File metadata and controls
80 lines (64 loc) · 1.91 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
import { Request } from 'express';
import { BaseProvider } from '../providers/BaseProvider';
import {
extractGeminiModelName,
isGeminiStreamingPath,
} from '../utils/gemini/string-parsing';
import { env } from '../env';
export function extractModelName(req: Request): string | undefined {
const model = req.body.model;
if (model && model !== undefined) {
return model;
}
const gatewayModel = req.headers['ai-language-model-id'];
if (typeof gatewayModel === 'string' && gatewayModel.length > 0) {
return gatewayModel;
}
const modelFromPath = extractGeminiModelName(req);
if (modelFromPath && modelFromPath !== undefined) {
return modelFromPath;
}
return undefined;
}
export function extractMaxOutputTokens(req: Request): number {
// OpenAI Format
const maxOutputTokens = req.body.max_output_tokens;
if (maxOutputTokens && maxOutputTokens !== undefined) {
return maxOutputTokens;
}
// Anthropic Format
const maxTokens = req.body.max_tokens;
if (maxTokens && maxTokens !== undefined) {
return maxTokens;
}
// Gemini Format
const geminiMaxOutputTokens = req.body.generationConfig?.maxOutputTokens;
if (geminiMaxOutputTokens && geminiMaxOutputTokens !== undefined) {
return geminiMaxOutputTokens;
}
return Number(env.MAX_OUTPUT_TOKENS) || 4096;
}
function extractGeminiIsStream(req: Request): boolean {
const path = req.path;
return path.endsWith(':streamGenerateContent');
}
export function extractIsStream(req: Request): boolean {
const stream = req.body.stream;
if (stream && stream !== undefined) {
return stream;
}
const gatewayStream = req.headers['ai-language-model-streaming'];
if (gatewayStream === 'true') {
return true;
}
if (isGeminiStreamingPath(req.path)) {
return true;
}
return false;
}
export function formatUpstreamUrl(
provider: BaseProvider,
req: Request
): string {
return provider.formatUpstreamUrl(req);
}