forked from pollinations/pollinations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportkeyUtils.js
More file actions
113 lines (96 loc) · 3.99 KB
/
portkeyUtils.js
File metadata and controls
113 lines (96 loc) · 3.99 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
import debug from "debug";
const log = debug("pollinations:portkey-utils");
const errorLog = debug("pollinations:portkey-utils:error");
/**
* Refreshes the Google Cloud access token by executing the gcloud CLI command
* @returns {string} The new access token
*/
// Helper function to extract resource name from Azure endpoint
export function extractResourceName(endpoint) {
if (endpoint === undefined || endpoint === null) return null;
log("Extracting resource name from endpoint:", endpoint);
// Extract resource name from both Azure OpenAI patterns:
// 1. https://pollinations4490940554.openai.azure.com
// 2. https://gpt-image-jp.cognitiveservices.azure.com
let match = endpoint.match(/https:\/\/([^.]+)\.openai\.azure\.com/);
if (!match) {
match = endpoint.match(
/https:\/\/([^.]+)\.cognitiveservices\.azure\.com/,
);
}
const result = match ? match[1] : null;
log("Extracted resource name:", result);
// If we can't extract the resource name, use a default value
if (!result || result === "undefined" || result === undefined) {
log("Using default resource name: pollinations");
return "pollinations";
}
return result;
} // Extract deployment names from endpoints
export function extractDeploymentName(endpoint) {
if (!endpoint) return null;
log("Extracting deployment name from endpoint:", endpoint);
// Extract deployment name (e.g., gpt-4o-mini from .../deployments/gpt-4o-mini/...)
const match = endpoint.match(/\/deployments\/([^/]+)/);
log("Extracted deployment name:", match ? match[1] : null);
return match ? match[1] : null;
}
// Extract API version from endpoints
export function extractApiVersion(endpoint) {
if (!endpoint)
return process.env.OPENAI_API_VERSION || "2024-08-01-preview";
log("Extracting API version from endpoint:", endpoint);
// Extract API version (e.g., 2024-08-01-preview from ...?api-version=2024-08-01-preview)
const match = endpoint.match(/api-version=([^&]+)/);
const version = match
? match[1]
: process.env.OPENAI_API_VERSION || "2024-08-01-preview";
log("Extracted API version:", version);
return version;
}
export /**
* Generate Portkey headers from a configuration object
* @param {Object} config - Model configuration object
* @returns {Object} - Headers object with x-portkey prefixes
*/
async function generatePortkeyHeaders(config) {
if (!config) {
errorLog("No configuration provided for header generation");
throw new Error("No configuration provided for header generation");
}
// Use individual headers approach (proven to work with Azure OpenAI)
// Set strictOpenAiCompliance to false to enable Perplexity citations
// NOTE: Must be "strict-open-ai-compliance" (with dash between "open" and "ai")
const headers = {
"x-portkey-strict-open-ai-compliance": "false",
};
// Get the auth key
let apiKey;
if (config.authKey) {
try {
if (typeof config.authKey === "function") {
const token = config.authKey();
apiKey = token instanceof Promise ? await token : token;
} else {
apiKey = config.authKey;
}
} catch (error) {
errorLog("Error getting auth token:", error);
throw error;
}
}
// Add all config properties as individual x-portkey-* headers
for (const [key, value] of Object.entries(config)) {
// Skip internal properties
if (key === "removeSeed" || key === "authKey") continue;
// Add as individual header with x-portkey- prefix
headers[`x-portkey-${key}`] = value;
}
// Add Authorization header if we have an API key
if (apiKey) {
headers["Authorization"] = `Bearer ${apiKey}`;
}
log("Generated Portkey headers:", Object.keys(headers));
log("strictOpenAiCompliance header value:", headers["x-portkey-strict-open-ai-compliance"]);
return headers;
}