Skip to content

Commit edcac76

Browse files
committed
🚀 feat: Support multiple Azure AI Search indexes and enhance search functionality
1 parent 7f6b32f commit edcac76

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

‎api/app/clients/tools/structured/AzureAISearch.js

+42-16
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ class AzureAISearch extends Tool {
3232
fields.AZURE_AI_SEARCH_SERVICE_ENDPOINT,
3333
'AZURE_AI_SEARCH_SERVICE_ENDPOINT',
3434
);
35-
this.indexName = this._initializeField(
35+
// Get the indexes as a comma-separated string
36+
this.indexNames = this._initializeField(
3637
fields.AZURE_AI_SEARCH_INDEX_NAME,
3738
'AZURE_AI_SEARCH_INDEX_NAME',
3839
);
39-
this.apiKey = this._initializeField(fields.AZURE_AI_SEARCH_API_KEY, 'AZURE_AI_SEARCH_API_KEY');
40+
this.apiKey = this._initializeField(
41+
fields.AZURE_AI_SEARCH_API_KEY,
42+
'AZURE_AI_SEARCH_API_KEY',
43+
);
4044
this.apiVersion = this._initializeField(
4145
fields.AZURE_AI_SEARCH_API_VERSION,
4246
'AZURE_AI_SEARCH_API_VERSION',
@@ -58,7 +62,7 @@ class AzureAISearch extends Tool {
5862
);
5963

6064
// Check for required fields
61-
if (!this.override && (!this.serviceEndpoint || !this.indexName || !this.apiKey)) {
65+
if (!this.override && (!this.serviceEndpoint || !this.indexNames || !this.apiKey)) {
6266
throw new Error(
6367
'Missing AZURE_AI_SEARCH_SERVICE_ENDPOINT, AZURE_AI_SEARCH_INDEX_NAME, or AZURE_AI_SEARCH_API_KEY environment variable.',
6468
);
@@ -68,12 +72,25 @@ class AzureAISearch extends Tool {
6872
return;
6973
}
7074

71-
// Create SearchClient
72-
this.client = new SearchClient(
73-
this.serviceEndpoint,
74-
this.indexName,
75-
new AzureKeyCredential(this.apiKey),
76-
{ apiVersion: this.apiVersion },
75+
// Split the indexNames by comma to support multiple indexes, trim whitespace,
76+
// convert to lowercase, and filter out any empty strings.
77+
const indexes = this.indexNames
78+
.split(',')
79+
.map(index => index.trim().toLowerCase())
80+
.filter(index => index.length > 0);
81+
82+
if (indexes.length === 0) {
83+
throw new Error('No valid index names provided in AZURE_AI_SEARCH_INDEX_NAME.');
84+
}
85+
86+
// Create a client for each index.
87+
this.clients = indexes.map(index =>
88+
new SearchClient(
89+
this.serviceEndpoint,
90+
index,
91+
new AzureKeyCredential(this.apiKey),
92+
{ apiVersion: this.apiVersion },
93+
),
7794
);
7895
}
7996

@@ -88,17 +105,26 @@ class AzureAISearch extends Tool {
88105
if (this.select) {
89106
searchOption.select = this.select.split(',');
90107
}
91-
const searchResults = await this.client.search(query, searchOption);
92-
const resultDocuments = [];
93-
for await (const result of searchResults.results) {
94-
resultDocuments.push(result.document);
95-
}
96-
return JSON.stringify(resultDocuments);
108+
109+
// Query all indexes concurrently
110+
const searchPromises = this.clients.map(async (client) => {
111+
const resultDocuments = [];
112+
const searchResults = await client.search(query, searchOption);
113+
for await (const result of searchResults.results) {
114+
resultDocuments.push(result.document);
115+
}
116+
return resultDocuments;
117+
});
118+
119+
// Wait for all search promises to complete and flatten the results
120+
const resultsByIndex = await Promise.all(searchPromises);
121+
const combinedResults = resultsByIndex.flat();
122+
return JSON.stringify(combinedResults);
97123
} catch (error) {
98124
logger.error('Azure AI Search request failed', error);
99125
return 'There was an error with Azure AI Search.';
100126
}
101127
}
102128
}
103129

104-
module.exports = AzureAISearch;
130+
module.exports = AzureAISearch;

0 commit comments

Comments
 (0)