in src/WebJobs.Extensions.OpenAI.AzureAISearch/AzureAISearchProvider.cs [245:276]
async Task IndexSectionsAsync(SearchClient searchClient, SearchableDocument document, CancellationToken cancellationToken = default)
{
int iteration = 0;
IndexDocumentsBatch<SearchDocument> batch = new();
for (int i = 0; i < document.Embeddings?.Response?.Count; i++)
{
batch.Actions.Add(new IndexDocumentsAction<SearchDocument>(
IndexActionType.MergeOrUpload,
new SearchDocument
{
["id"] = Guid.NewGuid().ToString("N"),
["text"] = document.Embeddings.Request![i],
["title"] = Path.GetFileNameWithoutExtension(document.Title),
["embeddings"] = document.Embeddings.Response[i].ToFloats().ToArray() ?? Array.Empty<float>(),
["timestamp"] = DateTime.UtcNow
}));
iteration++;
if (iteration % 1_000 is 0)
{
// Every one thousand documents, batch create.
await this.IndexDocumentsBatchAsync(searchClient, batch, cancellationToken);
batch = new();
}
}
if (batch is { Actions.Count: > 0 })
{
// Any remaining documents, batch create.
await this.IndexDocumentsBatchAsync(searchClient, batch, cancellationToken);
}
}