UNPKG

@unified-llm/core

Version:
101 lines 4.39 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.AzureOpenAIProvider = void 0; // src/providers/azure/provider.ts const openai_1 = require("../openai"); const openai_2 = __importStar(require("openai")); class AzureOpenAIProvider extends openai_1.OpenAIProvider { constructor(auth, base) { var _a; /* 親には apiKey と model (= deployment) を渡す */ super({ ...base, model: auth.deployment }); const { apiKey } = base; // ← apiKey は統一してここから取得 const apiVersion = (_a = auth.apiVersion) !== null && _a !== void 0 ? _a : 'preview'; /* ── AAD 認証は旧 SDK (AzureOpenAI) 強制 ───────────────── */ if (auth.aadTokenProvider) { this.initAzureOpenAI_AAD(auth, apiVersion); return; } /* ── API-Key 認証パス ──────────────────────────────────── */ if (!apiKey) { throw new Error('AzureOpenAIProvider: apiKey is required unless aadTokenProvider is supplied'); } if (auth.useV1) { this.initOpenAI_Key(auth, apiKey, apiVersion); // /openai/v1 } else { this.initAzureOpenAI_Key(auth, apiKey, apiVersion); // /deployments/… } } /* ---------- /openai/v1 (+ API-Key) ---------- */ initOpenAI_Key(auth, apiKey, apiVersion) { this.client = new openai_2.default({ baseURL: `${auth.endpoint.replace(/\/$/, '')}/openai/v1/`, defaultQuery: { 'api-version': apiVersion }, apiKey, // 型合わせ defaultHeaders: { 'api-key': apiKey }, // Azure は "api-key" ヘッダー }); } /* ---------- /deployments/ (+ API-Key) ---------- */ initAzureOpenAI_Key(auth, apiKey, apiVersion) { this.client = new openai_2.AzureOpenAI({ endpoint: auth.endpoint, // ← camelCase: endpoint apiVersion, apiKey, deployment: auth.deployment, }); } /* ---------- /deployments/ (+ AAD) ---------- */ async initAzureOpenAI_AAD(auth, apiVersion) { if (!auth.aadTokenProvider) { throw new Error('AzureOpenAIProvider: aadTokenProvider is required for AAD authentication'); } const cred = await auth.aadTokenProvider(); this.client = new openai_2.AzureOpenAI({ endpoint: auth.endpoint, apiVersion, deployment: auth.deployment, azureADTokenProvider: async () => { const t = await cred.getToken('https://cognitiveservices.azure.com/.default'); if (!(t === null || t === void 0 ? void 0 : t.token)) throw new Error('AAD token acquisition failed'); return t.token; }, }); } } exports.AzureOpenAIProvider = AzureOpenAIProvider; //# sourceMappingURL=provider.js.map