UNPKG

@azure-rest/ai-inference

Version:
61 lines 3.09 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { __rest } from "tslib"; import { getClient } from "@azure-rest/core-client"; import { logger } from "./logger.js"; import { isKeyCredential } from "@azure/core-auth"; import { tracingPolicy } from "./tracingPolicy.js"; /** * Initialize a new instance of `ModelClient` * @param endpointParam - The parameter endpointParam * @param credentials - uniquely identify client credential * @param options - the parameter for all optional parameters */ export default function createClient(endpointParam, credentials, _a = {}) { var _b, _c, _d, _e, _f, _g, _h, _j; var { apiVersion = "2024-05-01-preview" } = _a, options = __rest(_a, ["apiVersion"]); const endpointUrl = (_c = (_b = options.endpoint) !== null && _b !== void 0 ? _b : options.baseUrl) !== null && _c !== void 0 ? _c : `${endpointParam}`; const userAgentInfo = `azsdk-js-ai-inference-rest/1.0.0-beta.6`; const userAgentPrefix = options.userAgentOptions && options.userAgentOptions.userAgentPrefix ? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}` : `${userAgentInfo}`; options = Object.assign(Object.assign({}, options), { userAgentOptions: { userAgentPrefix, }, loggingOptions: { logger: (_e = (_d = options.loggingOptions) === null || _d === void 0 ? void 0 : _d.logger) !== null && _e !== void 0 ? _e : logger.info, }, credentials: { scopes: (_g = (_f = options.credentials) === null || _f === void 0 ? void 0 : _f.scopes) !== null && _g !== void 0 ? _g : ["https://ml.azure.com/.default"], apiKeyHeaderName: (_j = (_h = options.credentials) === null || _h === void 0 ? void 0 : _h.apiKeyHeaderName) !== null && _j !== void 0 ? _j : "api-key", } }); const client = getClient(endpointUrl, credentials, options); client.pipeline.removePolicy({ name: "ApiVersionPolicy" }); client.pipeline.addPolicy({ name: "InferenceTracingPolicy", sendRequest: (req, next) => { return tracingPolicy().sendRequest(req, next); }, }); client.pipeline.addPolicy({ name: "ClientApiVersionPolicy", sendRequest: (req, next) => { // Use the apiVersion defined in request url directly // Append one if there is no apiVersion and we have one at client options const url = new URL(req.url); if (!url.searchParams.get("api-version") && apiVersion) { req.url = `${req.url}${Array.from(url.searchParams.keys()).length > 0 ? "&" : "?"}api-version=${apiVersion}`; } return next(req); }, }); if (isKeyCredential(credentials)) { client.pipeline.addPolicy({ name: "customKeyCredentialPolicy", async sendRequest(request, next) { request.headers.set("Authorization", "Bearer " + credentials.key); return next(request); }, }); } return client; } //# sourceMappingURL=modelClient.js.map