Skip to content

Commit c306535

Browse files
committed
feat: allow disabling semantic search
1 parent f37ef35 commit c306535

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

infra/main.bicep

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ module searchService 'core/search/search-services.bicep' = {
358358
sku: {
359359
name: searchServiceSkuName
360360
}
361-
semanticSearch: 'free'
361+
semanticSearch: searchServiceSkuName == 'standard' ? 'free' : 'disabled'
362362
}
363363
}
364364

@@ -526,6 +526,7 @@ output AZURE_OPENAI_EMBEDDING_MODEL string = embeddingModelName
526526
output AZURE_SEARCH_INDEX string = searchIndexName
527527
output AZURE_SEARCH_SERVICE string = searchService.outputs.name
528528
output AZURE_SEARCH_SERVICE_RESOURCE_GROUP string = searchServiceResourceGroup.name
529+
output AZURE_SEARCH_SEMANTIC_RANKER string = searchServiceSkuName == 'standard' ? 'enabled' : 'disabled'
529530

530531
output AZURE_STORAGE_ACCOUNT string = storage.outputs.name
531532
output AZURE_STORAGE_CONTAINER string = storageContainerName

packages/indexer/src/lib/indexer.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class Indexer {
3737
this.blobStorage = new BlobStorage(logger, azure);
3838
}
3939

40-
async createSearchIndex(indexName: string) {
40+
async createSearchIndex(indexName: string, useSemanticRanker = false) {
4141
this.logger.debug(`Ensuring search index "${indexName}" exists`);
4242

4343
const searchIndexClient = this.azure.searchIndex;
@@ -72,21 +72,25 @@ export class Indexer {
7272
},
7373
],
7474
},
75-
semanticSearch: {
76-
defaultConfigurationName: 'semantic-search-config',
77-
configurations: [
78-
{
79-
name: 'semantic-search-config',
80-
prioritizedFields: {
81-
contentFields: [
75+
...(useSemanticRanker
76+
? {
77+
semanticSearch: {
78+
defaultConfigurationName: 'semantic-search-config',
79+
configurations: [
8280
{
83-
name: 'content',
81+
name: 'semantic-search-config',
82+
prioritizedFields: {
83+
contentFields: [
84+
{
85+
name: 'content',
86+
},
87+
],
88+
},
8489
},
8590
],
8691
},
87-
},
88-
],
89-
},
92+
}
93+
: {}),
9094
fields: [
9195
{
9296
name: 'id',

packages/indexer/src/plugins/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface AppConfig {
88
azureStorageContainer: string;
99
azureSearchService: string;
1010
azureSearchIndex: string;
11+
azureSearchSemanticRanker: string;
1112
azureOpenAiService: string;
1213
azureOpenAiEmbeddingDeployment: string;
1314
azureOpenAiEmbeddingModel: string;
@@ -28,6 +29,7 @@ export default fp(
2829
azureStorageAccount: process.env.AZURE_STORAGE_ACCOUNT || '',
2930
azureStorageContainer: process.env.AZURE_STORAGE_CONTAINER || '',
3031
azureSearchService: process.env.AZURE_SEARCH_SERVICE || '',
32+
azureSearchSemanticRanker: process.env.AZURE_SEARCH_SEMANTIC_RANKER || 'disabled',
3133
azureSearchIndex: process.env.AZURE_SEARCH_INDEX || '',
3234
azureOpenAiService: process.env.AZURE_OPENAI_SERVICE || '',
3335
azureOpenAiEmbeddingDeployment: process.env.AZURE_OPENAI_EMBEDDING_DEPLOYMENT || '',

packages/indexer/src/routes/indexes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const root: FastifyPluginAsyncJsonSchemaToTs = async (fastify, _options): Promis
3333
handler: async function (request, reply) {
3434
const { name } = request.body;
3535
try {
36-
await fastify.indexer.createSearchIndex(name);
36+
await fastify.indexer.createSearchIndex(name, fastify.config.azureSearchSemanticRanker !== 'enabled');
3737
reply.code(204);
3838
} catch (_error: unknown) {
3939
const error = _error as Error;

packages/search/src/plugins/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface AppConfig {
88
azureStorageContainer: string;
99
azureSearchService: string;
1010
azureSearchIndex: string;
11+
azureSearchSemanticRanker: string;
1112
azureOpenAiService: string;
1213
azureOpenAiChatGptDeployment: string;
1314
azureOpenAiChatGptModel: string;
@@ -32,6 +33,7 @@ export default fp(
3233
azureStorageContainer: process.env.AZURE_STORAGE_CONTAINER || '',
3334
azureSearchService: process.env.AZURE_SEARCH_SERVICE || '',
3435
azureSearchIndex: process.env.AZURE_SEARCH_INDEX || '',
36+
azureSearchSemanticRanker: process.env.AZURE_SEARCH_SEMANTIC_RANKER || 'disabled',
3537
azureOpenAiService: process.env.AZURE_OPENAI_SERVICE || '',
3638
azureOpenAiChatGptDeployment: process.env.AZURE_OPENAI_CHATGPT_DEPLOYMENT || '',
3739
azureOpenAiChatGptModel: process.env.AZURE_OPENAI_CHATGPT_MODEL || 'gpt-4o-mini',

packages/search/src/routes/root.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Readable } from 'node:stream';
55
import { type FastifyPluginAsync } from 'fastify';
66
import { type JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts';
77
import { type SchemaTypes } from '../plugins/schemas.js';
8+
import { type ApproachContext } from '../lib/index.js';
89

910
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1011

@@ -74,21 +75,26 @@ const root: FastifyPluginAsync = async (_fastify, _options): Promise<void> => {
7475
}
7576

7677
const { messages, context, stream } = request.body;
78+
let approachContext: ApproachContext = (context as any) ?? {};
79+
if (this.config.azureSearchSemanticRanker !== 'enabled') {
80+
approachContext = { ...approachContext, semantic_ranker: false };
81+
}
82+
7783
try {
7884
if (stream) {
7985
const buffer = new Readable();
8086
// Dummy implementation needed
8187
buffer._read = () => {};
8288
reply.type('application/x-ndjson').send(buffer);
8389

84-
const chunks = await chatApproach.runWithStreaming(messages, (context as any) ?? {});
90+
const chunks = await chatApproach.runWithStreaming(messages, approachContext);
8591
for await (const chunk of chunks) {
8692
buffer.push(JSON.stringify(chunk) + '\n');
8793
}
8894
// eslint-disable-next-line unicorn/no-null
8995
buffer.push(null);
9096
} else {
91-
return await chatApproach.run(messages, (context as any) ?? {});
97+
return await chatApproach.run(messages, approachContext);
9298
}
9399
} catch (_error: unknown) {
94100
const error = _error as Error & { error?: any; status?: number };
@@ -121,21 +127,26 @@ const root: FastifyPluginAsync = async (_fastify, _options): Promise<void> => {
121127
}
122128

123129
const { messages, context, stream } = request.body;
130+
let approachContext: ApproachContext = (context as any) ?? {};
131+
if (this.config.azureSearchSemanticRanker !== 'enabled') {
132+
approachContext = { ...approachContext, semantic_ranker: false };
133+
}
134+
124135
try {
125136
if (stream) {
126137
const buffer = new Readable();
127138
// Dummy implementation needed
128139
buffer._read = () => {};
129140
reply.type('application/x-ndjson').send(buffer);
130141

131-
const chunks = await askApproach.runWithStreaming(messages[0].content, (context as any) ?? {});
142+
const chunks = await askApproach.runWithStreaming(messages[0].content, approachContext);
132143
for await (const chunk of chunks) {
133144
buffer.push(JSON.stringify(chunk) + '\n');
134145
}
135146
// eslint-disable-next-line unicorn/no-null
136147
buffer.push(null);
137148
} else {
138-
return await askApproach.run(messages[0].content, (context as any) ?? {});
149+
return await askApproach.run(messages[0].content, approachContext);
139150
}
140151
} catch (_error: unknown) {
141152
const error = _error as Error & { error?: any; status?: number };

0 commit comments

Comments
 (0)