-
Notifications
You must be signed in to change notification settings - Fork 6
Add semantic search configs for Azure Knowledgebase #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MohamedSabthar
merged 4 commits into
ballerina-platform:main
from
SasinduDilshara:add-azure-knowledgebase
Nov 3, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,12 @@ type IndexSchemaInfo record { | |
| map<search:SearchField> allFields; | ||
| }; | ||
|
|
||
| # Details for semantic configuration in Azure AI Search. | ||
| public type SemanticConfigurationDetails record {| | ||
| # Name of the semantic configuration | ||
| string name; | ||
| |}; | ||
|
|
||
| # Represents the Azure Search Knowledge Base implementation. | ||
| public distinct isolated class AiSearchKnowledgeBase { | ||
| *ai:KnowledgeBase; | ||
|
|
@@ -85,11 +91,12 @@ public distinct isolated class AiSearchKnowledgeBase { | |
| private final string apiKey; | ||
| private final boolean verbose; | ||
| private final ai:Chunker|ai:AUTO|ai:DISABLE chunker; | ||
| private final ai:EmbeddingProvider embeddingModel; | ||
| private final ai:EmbeddingProvider? embeddingModel; | ||
| private final string contentFieldName; | ||
| private final string keyFieldName; | ||
| private final string[] vectorFieldNames; | ||
| private final map<search:SearchField> allFields; | ||
| private final SemanticConfigurationDetails? semanticConfigurationDetails; | ||
|
|
||
| # Initializes a new `AiSearchKnowledgeBase` instance. | ||
| # | ||
|
|
@@ -107,17 +114,20 @@ public distinct isolated class AiSearchKnowledgeBase { | |
| # This configuration is only required when the `index` parameter is | ||
| # provided as an `search:SearchIndex` | ||
| # + indexClientConnectionConfig - Connection configuration for the Azure AI index client. | ||
| # + semanticConfigurationDetails - Optional semantic configuration details for semantic search. | ||
| # + return - An instance of `AiSearchKnowledgeBase` or an `ai:Error` if initialization fails | ||
| public isolated function init(string serviceUrl, string apiKey, | ||
| string|search:SearchIndex index, ai:EmbeddingProvider embeddingModel, | ||
| string|search:SearchIndex index, ai:EmbeddingProvider? embeddingModel = (), | ||
| ai:Chunker|ai:AUTO|ai:DISABLE chunker = ai:AUTO, boolean verbose = false, | ||
| string apiVersion = AI_AZURE_KNOWLEDGE_BASE_API_VERSION, string contentFieldName = CONTENT_FIELD_NAME, | ||
| search:ConnectionConfig searchClientConnectionConfig = {}, | ||
| index:ConnectionConfig indexClientConnectionConfig = {}) returns ai:Error? { | ||
| index:ConnectionConfig indexClientConnectionConfig = {}, | ||
| SemanticConfigurationDetails? semanticConfigurationDetails = ()) returns ai:Error? { | ||
|
||
| self.chunker = chunker; | ||
| self.embeddingModel = embeddingModel; | ||
| self.verbose = verbose; | ||
| self.contentFieldName = contentFieldName; | ||
| self.semanticConfigurationDetails = semanticConfigurationDetails.cloneReadOnly(); | ||
|
|
||
| // Initialize service client for management operations | ||
| self.apiKey = apiKey; | ||
|
|
@@ -189,14 +199,22 @@ public distinct isolated class AiSearchKnowledgeBase { | |
| return error ai:Error("Failed to chunk documents before ingestion", chunks); | ||
| } | ||
|
|
||
| ai:Embedding[]|error embeddings = self.embeddingModel->batchEmbed(chunks); | ||
| if embeddings is error { | ||
| ai:Embedding[]? embeddings = (); | ||
| ai:EmbeddingProvider? embeddingProvider = self.embeddingModel; | ||
| if embeddingProvider is ai:EmbeddingProvider { | ||
| logIfVerboseEnabled(self.verbose, | ||
| string `Generating embeddings for ${chunks.length().toString()} chunks using embedding model.`); | ||
| ai:Embedding[]|error? embeddingResults = embeddingProvider->batchEmbed(chunks); | ||
| if embeddingResults is error { | ||
| logIfVerboseEnabled(self.verbose, | ||
| string `Failed to generate embeddings for documents: ${embeddingResults.message()}}`, embeddingResults); | ||
| return error ai:Error("Failed to generate embeddings for documents", embeddingResults); | ||
| } | ||
|
|
||
| embeddings = embeddingResults; | ||
| logIfVerboseEnabled(self.verbose, | ||
| string `Failed to generate embeddings for documents: ${embeddings.message()}}`, embeddings); | ||
| return error ai:Error("Failed to generate embeddings for documents", embeddings); | ||
| string `Generated embeddings for ${embeddings == () ? 0: embeddings.length().toString()} chunks.`); | ||
| } | ||
| logIfVerboseEnabled(self.verbose, | ||
| string `Generated embeddings for ${embeddings.length().toString()} chunks.`); | ||
|
|
||
| index:IndexDocumentsResult|error uploadResult = self.uploadDocuments(self.indexClient, chunks, self.index, | ||
| embeddings, {[API_KEY_HEADER_NAME]: self.apiKey}, {api\-version: self.apiVersion}); | ||
|
|
@@ -236,13 +254,17 @@ public distinct isolated class AiSearchKnowledgeBase { | |
|
|
||
| lock { | ||
| ai:TextChunk queryChunk = {content: query, 'type: CONTENT_TYPE_TEXT_CHUNK}; | ||
| ai:Embedding queryEmbedding = check self.embeddingModel->embed(queryChunk); | ||
| ai:Embedding? queryEmbedding = (); | ||
| ai:EmbeddingProvider? embeddingProvider = self.embeddingModel; | ||
| if embeddingProvider is ai:EmbeddingProvider { | ||
| queryEmbedding = check embeddingProvider->embed(queryChunk); | ||
| } | ||
|
|
||
| // Create vector search request using Azure AI Search's integrated vectorization | ||
| int vectorFieldLength = self.vectorFieldNames.length(); | ||
| index:VectorQuery[]? vectorQuery = (); | ||
|
|
||
| if vectorFieldLength != 0 { | ||
| if vectorFieldLength != 0 && queryEmbedding is ai:Embedding { | ||
| ai:Vector|ai:Error vectors = generateVectorFromEmbedding(queryEmbedding); | ||
| if vectors is ai:Error { | ||
| return vectors; | ||
|
|
@@ -258,9 +280,16 @@ public distinct isolated class AiSearchKnowledgeBase { | |
| ]; | ||
| } | ||
|
|
||
| SemanticConfigurationDetails? semanticConfig = self.semanticConfigurationDetails is SemanticConfigurationDetails | ||
| ? self.semanticConfigurationDetails : (); | ||
| index:QueryType queryType = semanticConfig is SemanticConfigurationDetails | ||
| ? "semantic" : "simple"; | ||
|
|
||
| index:SearchRequest searchRequest = { | ||
| search: query, | ||
| 'select: "*", | ||
| queryType: queryType, | ||
| semanticConfiguration: semanticConfig is SemanticConfigurationDetails ? semanticConfig.name : (), | ||
| vectorQueries: vectorQuery ?: [], | ||
| top: maxLimit == -1 ? () : <int:Signed32>maxLimit | ||
| }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.