feat: add Azure OpenAI provider support
This commit is contained in:
@@ -366,6 +366,21 @@ async function testApiKeyConnection(connection, effectiveProxy = null) {
|
||||
|
||||
try {
|
||||
switch (connection.provider) {
|
||||
case "azure": {
|
||||
const psd = connection.providerSpecificData || {};
|
||||
const endpoint = (psd.azureEndpoint || "").replace(/\/$/, "");
|
||||
const deployment = psd.deployment || "gpt-4";
|
||||
const apiVersion = psd.apiVersion || "2024-10-01-preview";
|
||||
const url = `${endpoint}/openai/deployments/${deployment}/chat/completions?api-version=${apiVersion}`;
|
||||
const headers = { "api-key": connection.apiKey, "Content-Type": "application/json" };
|
||||
if (psd.organization) headers["OpenAI-Organization"] = psd.organization;
|
||||
const res = await fetchWithConnectionProxy(url, {
|
||||
method: "POST", headers,
|
||||
body: JSON.stringify({ messages: [{ role: "user", content: "test" }], max_completion_tokens: 1 }),
|
||||
}, effectiveProxy);
|
||||
const valid = res.status !== 401 && res.status !== 403;
|
||||
return { valid, error: valid ? null : "Invalid API key or Azure configuration" };
|
||||
}
|
||||
case "openai": {
|
||||
const res = await fetchWithConnectionProxy("https://api.openai.com/v1/models", { headers: { Authorization: `Bearer ${connection.apiKey}` } }, effectiveProxy);
|
||||
return { valid: res.ok, error: res.ok ? null : "Invalid API key" };
|
||||
|
||||
@@ -116,7 +116,7 @@ export async function POST(request) {
|
||||
return NextResponse.json({ error: "Name is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
let providerSpecificData = null;
|
||||
let providerSpecificData = body.providerSpecificData || null;
|
||||
|
||||
if (isOpenAICompatibleProvider(provider)) {
|
||||
const node = await getProviderNodeById(provider);
|
||||
|
||||
@@ -63,6 +63,35 @@ export async function POST(request) {
|
||||
});
|
||||
}
|
||||
|
||||
if (provider === "azure") {
|
||||
const { providerSpecificData } = body;
|
||||
const endpoint = (providerSpecificData?.azureEndpoint || "").replace(/\/$/, "");
|
||||
const deployment = providerSpecificData?.deployment || "gpt-4";
|
||||
const apiVersion = providerSpecificData?.apiVersion || "2024-10-01-preview";
|
||||
const organization = providerSpecificData?.organization;
|
||||
|
||||
const url = `${endpoint}/openai/deployments/${deployment}/chat/completions?api-version=${apiVersion}`;
|
||||
const headers = {
|
||||
"api-key": apiKey,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
if (organization) headers["OpenAI-Organization"] = organization;
|
||||
|
||||
const azureRes = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
messages: [{ role: "user", content: "test" }],
|
||||
max_tokens: 1,
|
||||
}),
|
||||
});
|
||||
isValid = azureRes.status !== 401 && azureRes.status !== 403;
|
||||
return NextResponse.json({
|
||||
valid: isValid,
|
||||
error: isValid ? null : "Invalid API key or Azure configuration",
|
||||
});
|
||||
}
|
||||
|
||||
switch (provider) {
|
||||
case "openai":
|
||||
const openaiRes = await fetch("https://api.openai.com/v1/models", {
|
||||
|
||||
Reference in New Issue
Block a user