feat: Azure Cognitive Search Plugin (#815)

* feat(AzureCognitiveSearchPlugin)

* feat(tools/AzureCognitiveSearch.js): Add a new plugin (not structured
  version)
* feat(tools/structured/AzureCognitiveSearch.js): Add a new plugin (structured version)
* feat(tools/manifest.json, tools/index.js, tools/util/handleTools.js):
  Add configurations for the plugin
* feat(api/package.json, package-lock.json): Installed a new package for the
  plugin (@azure/search-documents)
* feat(.env.example): Add new environment variables for the plugin

Here is the link to the corresponding discussion page:
https://github.com/danny-avila/LibreChat/discussions/567

* docs(AzureCognitiveSearchPlugin)

* docs(features/plugins/azure_cognitive_search.md): Add a new document
  for the plugin

* (fix:.env.example)

* reverted extra whitespaces removed by the editor

* docs(mkdocs.yml)

* Add the Azure Cognitive Search Plugin's documentation item to
mkdocs.yml.
This commit is contained in:
Naosuke Yokoe 2023-08-19 20:11:31 +09:00 committed by GitHub
parent 3c7f67fa76
commit 61dcb4d307
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 498 additions and 12 deletions

View file

@ -0,0 +1,111 @@
const { Tool } = require('langchain/tools');
const { SearchClient, AzureKeyCredential } = require('@azure/search-documents');
class AzureCognitiveSearch extends Tool {
constructor(fields = {}) {
super();
this.serviceEndpoint =
fields.AZURE_COGNITIVE_SEARCH_SERVICE_ENDPOINT || this.getServiceEndpoint();
this.indexName = fields.AZURE_COGNITIVE_SEARCH_INDEX_NAME || this.getIndexName();
this.apiKey = fields.AZURE_COGNITIVE_SEARCH_API_KEY || this.getApiKey();
this.apiVersion = fields.AZURE_COGNITIVE_SEARCH_API_VERSION || this.getApiVersion();
this.queryType = fields.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_QUERY_TYPE || this.getQueryType();
this.top = fields.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_TOP || this.getTop();
this.select = fields.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_SELECT || this.getSelect();
this.client = new SearchClient(
this.serviceEndpoint,
this.indexName,
new AzureKeyCredential(this.apiKey),
{
apiVersion: this.apiVersion,
},
);
}
/**
* The name of the tool.
* @type {string}
*/
name = 'azure-cognitive-search';
/**
* A description for the agent to use
* @type {string}
*/
description =
'Use the \'azure-cognitive-search\' tool to retrieve search results relevant to your input';
getServiceEndpoint() {
const serviceEndpoint = process.env.AZURE_COGNITIVE_SEARCH_SERVICE_ENDPOINT || '';
if (!serviceEndpoint) {
throw new Error('Missing AZURE_COGNITIVE_SEARCH_SERVICE_ENDPOINT environment variable.');
}
return serviceEndpoint;
}
getIndexName() {
const indexName = process.env.AZURE_COGNITIVE_SEARCH_INDEX_NAME || '';
if (!indexName) {
throw new Error('Missing AZURE_COGNITIVE_SEARCH_INDEX_NAME environment variable.');
}
return indexName;
}
getApiKey() {
const apiKey = process.env.AZURE_COGNITIVE_SEARCH_API_KEY || '';
if (!apiKey) {
throw new Error('Missing AZURE_COGNITIVE_SEARCH_API_KEY environment variable.');
}
return apiKey;
}
getApiVersion() {
return process.env.AZURE_COGNITIVE_SEARCH_API_VERSION || '2020-06-30';
}
getQueryType() {
return process.env.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_QUERY_TYPE || 'simple';
}
getTop() {
if (process.env.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_TOP) {
return Number(process.env.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_TOP);
} else {
return 5;
}
}
getSelect() {
if (process.env.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_SELECT) {
return process.env.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_SELECT.split(',');
} else {
return null;
}
}
async _call(query) {
try {
const searchOption = {
queryType: this.queryType,
top: this.top,
};
if (this.select) {
searchOption.select = this.select;
}
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 Cognitive Search request failed: ${error}`);
return 'There was an error with Azure Cognitive Search.';
}
}
}
module.exports = AzureCognitiveSearch;

View file

@ -7,6 +7,8 @@ const StableDiffusionAPI = require('./StableDiffusion');
const WolframAlphaAPI = require('./Wolfram');
const StructuredWolfram = require('./structured/Wolfram');
const SelfReflectionTool = require('./SelfReflection');
const AzureCognitiveSearch = require('./AzureCognitiveSearch');
const StructuredACS = require('./structured/AzureCognitiveSearch');
const availableTools = require('./manifest.json');
module.exports = {
@ -20,4 +22,6 @@ module.exports = {
WolframAlphaAPI,
StructuredWolfram,
SelfReflectionTool,
AzureCognitiveSearch,
StructuredACS,
};

View file

@ -102,5 +102,28 @@
"description": "You can use Zapier with your API Key from Zapier."
}
]
},
{
"name": "Azure Cognitive Search",
"pluginKey": "azure-cognitive-search",
"description": "Use Azure Cognitive Search to find information",
"icon": "https://i.imgur.com/E7crPze.png",
"authConfig": [
{
"authField": "AZURE_COGNITIVE_SEARCH_SERVICE_ENDPOINT",
"label": "Azur Cognitive Search Endpoint",
"description": "You need to provide your Endpoint for Azure Cognitive Search."
},
{
"authField": "AZURE_COGNITIVE_SEARCH_INDEX_NAME",
"label": "Azur Cognitive Search Index Name",
"description": "You need to provide your Index Name for Azure Cognitive Search."
},
{
"authField": "AZURE_COGNITIVE_SEARCH_API_KEY",
"label": "Azur Cognitive Search API Key",
"description": "You need to provideq your API Key for Azure Cognitive Search."
}
]
}
]

View file

@ -0,0 +1,116 @@
const { StructuredTool } = require('langchain/tools');
const { z } = require('zod');
const { SearchClient, AzureKeyCredential } = require('@azure/search-documents');
class AzureCognitiveSearch extends StructuredTool {
constructor(fields = {}) {
super();
this.serviceEndpoint =
fields.AZURE_COGNITIVE_SEARCH_SERVICE_ENDPOINT || this.getServiceEndpoint();
this.indexName = fields.AZURE_COGNITIVE_SEARCH_INDEX_NAME || this.getIndexName();
this.apiKey = fields.AZURE_COGNITIVE_SEARCH_API_KEY || this.getApiKey();
this.apiVersion = fields.AZURE_COGNITIVE_SEARCH_API_VERSION || this.getApiVersion();
this.queryType = fields.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_QUERY_TYPE || this.getQueryType();
this.top = fields.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_TOP || this.getTop();
this.select = fields.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_SELECT || this.getSelect();
this.client = new SearchClient(
this.serviceEndpoint,
this.indexName,
new AzureKeyCredential(this.apiKey),
{
apiVersion: this.apiVersion,
},
);
this.schema = z.object({
query: z.string().describe('Search word or phrase to Azure Cognitive Search'),
});
}
/**
* The name of the tool.
* @type {string}
*/
name = 'azure-cognitive-search';
/**
* A description for the agent to use
* @type {string}
*/
description =
'Use the \'azure-cognitive-search\' tool to retrieve search results relevant to your input';
getServiceEndpoint() {
const serviceEndpoint = process.env.AZURE_COGNITIVE_SEARCH_SERVICE_ENDPOINT || '';
if (!serviceEndpoint) {
throw new Error('Missing AZURE_COGNITIVE_SEARCH_SERVICE_ENDPOINT environment variable.');
}
return serviceEndpoint;
}
getIndexName() {
const indexName = process.env.AZURE_COGNITIVE_SEARCH_INDEX_NAME || '';
if (!indexName) {
throw new Error('Missing AZURE_COGNITIVE_SEARCH_INDEX_NAME environment variable.');
}
return indexName;
}
getApiKey() {
const apiKey = process.env.AZURE_COGNITIVE_SEARCH_API_KEY || '';
if (!apiKey) {
throw new Error('Missing AZURE_COGNITIVE_SEARCH_API_KEY environment variable.');
}
return apiKey;
}
getApiVersion() {
return process.env.AZURE_COGNITIVE_SEARCH_API_VERSION || '2020-06-30';
}
getQueryType() {
return process.env.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_QUERY_TYPE || 'simple';
}
getTop() {
if (process.env.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_TOP) {
return Number(process.env.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_TOP);
} else {
return 5;
}
}
getSelect() {
if (process.env.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_SELECT) {
return process.env.AZURE_COGNITIVE_SEARCH_SEARCH_OPTION_SELECT.split(',');
} else {
return null;
}
}
async _call(data) {
const { query } = data;
try {
const searchOption = {
queryType: this.queryType,
top: this.top,
};
if (this.select) {
searchOption.select = this.select;
}
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 Cognitive Search request failed: ${error}`);
return 'There was an error with Azure Cognitive Search.';
}
}
}
module.exports = AzureCognitiveSearch;

View file

@ -15,6 +15,8 @@ const {
OpenAICreateImage,
StableDiffusionAPI,
StructuredSD,
AzureCognitiveSearch,
StructuredACS,
} = require('../');
const { loadSpecs } = require('./loadSpecs');
@ -78,6 +80,7 @@ const loadTools = async ({ user, model, functions = null, tools = [], options =
wolfram: functions ? StructuredWolfram : WolframAlphaAPI,
'dall-e': OpenAICreateImage,
'stable-diffusion': functions ? StructuredSD : StableDiffusionAPI,
'azure-cognitive-search': functions ? StructuredACS : AzureCognitiveSearch,
};
const customConstructors = {