mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 01:10:14 +01:00
* Update azure_cognitive_search.md * Updated: Azure Cognitive Search Plugin to Azure AI Search Plugin. Update Docs: Azure Cognitive Search Plugin to Azure AI Search Plugin. Updated:.env.example Azure Cognitive Search to Azure AI Search Updated: mkdocs.yml link Updated: SDK Azure 11.3.2 to 12.0.0 * fix:.env AZURE- to AZURE_ * Update azure_ai_search.md * Updated:(api/package.json, package-lock.json): updated for new version the plugin (@azure/search-documents) * fix:Resolved incorrect file name AzureAISearch * fix:.env Azure AI Search * fix:"-" to "_" * Update Docs: Azure AI Search ith an improved tutorial featuring images and easier-to-understand instructions fix: Change name of plugin "Azure Ai Search" to "Azure AI Search" i * Update:Version of REST API versions (Azure AI Search) * Update azure_ai_search.md * Update azure_ai_search.md * Update azure_ai_search.md * fix: docs Azure AI Seach Images were not appearing. * fix:Updated to the new repository with working APIs * Update: Added Compatibility for Previous Environment Variable Names in AzureAISearch Plugin * Update: Added Compatibility for Previous Environment Variable Names in AzureAISearch Plugin * Update: Added Compatibility for Previous Environment Variable Names in AzureAISearch Plugin * Update: Added Compatibility for Previous Environment Variable Names in AzureAISearch Plugin * Update: o AzureAiSearch.js * Atualizar o AzureAISearch.js * Update/fix:EnvironmentVariablesForDeprecation * fix:The file is outdated and needs to be updated. * fix:The file is outdated and needs to be updated. * update: translation portuguese brazilian * Refactor:Improve Readability and Cleanliness of AzureAISearch Class * Update AzureAiSearch.js * Update AzureAISearch.js * fix:getServiceEndpoint * fix: getServiceEndpoint * fix:AzureAISearch * fix: Fix functions erros AzureAISearch * fix: Update API_VERSION * fix: AzureAISearch files outdated
80 lines
3 KiB
JavaScript
80 lines
3 KiB
JavaScript
const { StructuredTool } = require('langchain/tools');
|
|
const { z } = require('zod');
|
|
const { SearchClient, AzureKeyCredential } = require('@azure/search-documents');
|
|
|
|
class AzureAISearch extends StructuredTool {
|
|
// Constants for default values
|
|
static DEFAULT_API_VERSION = '2023-11-01';
|
|
static DEFAULT_QUERY_TYPE = 'simple';
|
|
static DEFAULT_TOP = 5;
|
|
|
|
// Helper function for initializing properties
|
|
_initializeField(field, envVar, defaultValue) {
|
|
return field || process.env[envVar] || defaultValue;
|
|
}
|
|
|
|
constructor(fields = {}) {
|
|
super();
|
|
|
|
// Initialize properties using helper function
|
|
this.serviceEndpoint = this._initializeField(fields.AZURE_AI_SEARCH_SERVICE_ENDPOINT, 'AZURE_AI_SEARCH_SERVICE_ENDPOINT');
|
|
this.indexName = this._initializeField(fields.AZURE_AI_SEARCH_INDEX_NAME, 'AZURE_AI_SEARCH_INDEX_NAME');
|
|
this.apiKey = this._initializeField(fields.AZURE_AI_SEARCH_API_KEY, 'AZURE_AI_SEARCH_API_KEY');
|
|
this.apiVersion = this._initializeField(fields.AZURE_AI_SEARCH_API_VERSION, 'AZURE_AI_SEARCH_API_VERSION', AzureAISearch.DEFAULT_API_VERSION);
|
|
this.queryType = this._initializeField(fields.AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE, 'AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE', AzureAISearch.DEFAULT_QUERY_TYPE);
|
|
this.top = this._initializeField(fields.AZURE_AI_SEARCH_SEARCH_OPTION_TOP, 'AZURE_AI_SEARCH_SEARCH_OPTION_TOP', AzureAISearch.DEFAULT_TOP);
|
|
this.select = this._initializeField(fields.AZURE_AI_SEARCH_SEARCH_OPTION_SELECT, 'AZURE_AI_SEARCH_SEARCH_OPTION_SELECT');
|
|
|
|
// Check for required fields
|
|
if (!this.serviceEndpoint || !this.indexName || !this.apiKey) {
|
|
throw new Error('Missing AZURE_AI_SEARCH_SERVICE_ENDPOINT, AZURE_AI_SEARCH_INDEX_NAME, or AZURE_AI_SEARCH_API_KEY environment variable.');
|
|
}
|
|
|
|
// Create SearchClient
|
|
this.client = new SearchClient(
|
|
this.serviceEndpoint,
|
|
this.indexName,
|
|
new AzureKeyCredential(this.apiKey),
|
|
{ apiVersion: this.apiVersion }
|
|
);
|
|
|
|
// Define schema
|
|
this.schema = z.object({
|
|
query: z.string().describe('Search word or phrase to Azure AI Search'),
|
|
});
|
|
}
|
|
|
|
// Simplified getter methods
|
|
get name() {
|
|
return 'azure-ai-search';
|
|
}
|
|
|
|
get description() {
|
|
return 'Use the \'azure-ai-search\' tool to retrieve search results relevant to your input';
|
|
}
|
|
|
|
// Improved error handling and logging
|
|
async _call(data) {
|
|
const { query } = data;
|
|
try {
|
|
const searchOption = {
|
|
queryType: this.queryType,
|
|
top: this.top,
|
|
};
|
|
if (this.select) {
|
|
searchOption.select = this.select.split(',');
|
|
}
|
|
const searchResults = await this.client.search(query, searchOption);
|
|
const resultDocuments = [];
|
|
for await (const result of searchResults.results) {
|
|
resultDocuments.push(result.document);
|
|
}
|
|
return JSON.stringify(resultDocuments);
|
|
} catch (error) {
|
|
console.error(`Azure AI Search request failed: ${error.message}`);
|
|
return 'There was an error with Azure AI Search.';
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = AzureAISearch;
|