@azure/search-documents
Version:
Azure client library to use AI Search for node.js and browser.
100 lines (99 loc) • 4.14 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { isTokenCredential } from "@azure/core-auth";
import { bearerTokenAuthenticationPolicy, bearerTokenAuthenticationPolicyName, } from "@azure/core-rest-pipeline";
import { KnowledgeBaseRetrievalClient as GeneratedClient } from "./knowledgeBaseRetrieval/knowledgeBaseRetrievalClient.js";
import { logger } from "./logger.js";
import { createOdataMetadataPolicy } from "./odataMetadataPolicy.js";
import { createSearchApiKeyCredentialPolicy } from "./searchApiKeyCredentialPolicy.js";
import { KnownSearchAudience } from "./searchAudience.js";
import * as utils from "./serviceUtils.js";
import { tracingClient } from "./tracing.js";
/**
* Class used to perform operations against a knowledge base.
*/
export class KnowledgeRetrievalClient {
/// Maintenance note: when updating supported API versions,
/// the ContinuationToken logic will need to be updated below.
/**
* The service version to use when communicating with the service.
*/
serviceVersion = utils.defaultServiceVersion;
/**
* The endpoint of the search service
*/
endpoint;
/**
* The name of the knowledge base
*/
knowledgeBaseName;
/**
* @hidden
* A reference to the auto-generated KnowledgeRetrievalClient
*/
client;
/**
* A reference to the internal HTTP pipeline for use with raw requests
*/
pipeline;
/**
* Creates an instance of KnowledgeRetrievalClient.
*
* Example usage:
* ```ts snippet:ReadmeSampleKnowledgeRetrievalClient
* import { KnowledgeRetrievalClient, AzureKeyCredential } from "@azure/search-documents";
*
* const knowledgeRetrievalClient = new KnowledgeRetrievalClient(
* "<endpoint>",
* "<knowledgeBaseName>",
* new AzureKeyCredential("<apiKey>"),
* );
* ```
* @param endpoint - The endpoint of the search service
* @param knowledgeBaseName - The name of the knowledge base
* @param credential - Used to authenticate requests to the service.
* @param options - Used to configure the Search client.
*/
constructor(endpoint, knowledgeBaseName, credential, options = {}) {
this.endpoint = endpoint;
this.knowledgeBaseName = knowledgeBaseName;
const internalClientPipelineOptions = {
...options,
apiVersion: options.serviceVersion ?? utils.defaultServiceVersion,
...{
loggingOptions: {
logger: logger.info,
additionalAllowedHeaderNames: [
"elapsed-time",
"Location",
"OData-MaxVersion",
"OData-Version",
"Prefer",
"throttle-reason",
],
},
},
};
this.serviceVersion = options.serviceVersion ?? utils.defaultServiceVersion;
this.client = new GeneratedClient(endpoint, credential, knowledgeBaseName, internalClientPipelineOptions);
this.pipeline = this.client.pipeline;
// Replaced with a custom policy below
this.pipeline.removePolicy({ name: bearerTokenAuthenticationPolicyName });
if (isTokenCredential(credential)) {
const scope = options.audience
? `${options.audience}/.default`
: `${KnownSearchAudience.AzurePublicCloud}/.default`;
this.client.pipeline.addPolicy(bearerTokenAuthenticationPolicy({ credential, scopes: scope }));
}
else {
this.client.pipeline.addPolicy(createSearchApiKeyCredentialPolicy(credential));
}
this.client.pipeline.addPolicy(createOdataMetadataPolicy("none"));
}
async retrieve(retrievalRequest, options = {}) {
return tracingClient.withSpan("KnowledgeRetrievalClient-retrieve", options, async (updatedOptions) => {
return this.client.retrieve(retrievalRequest, updatedOptions);
});
}
}
//# sourceMappingURL=knowledgeRetrievalClient.js.map