in src/WebJobs.Extensions.OpenAI.CosmosDBSearch/CosmosDBSearchProvider.cs [212:260]
async Task UpsertVectorAsync(MongoClient cosmosClient, SearchableDocument document)
{
List<BsonDocument> list = new();
for (int i = 0; i < document.Embeddings?.Response?.Count; i++)
{
BsonDocument vectorDocument =
new()
{
{ "id", Guid.NewGuid().ToString("N") },
{
this.cosmosDBSearchConfigOptions.Value.TextKey,
document.Embeddings.Request![i]
},
{ "title", Path.GetFileNameWithoutExtension(document.Title) },
{
this.cosmosDBSearchConfigOptions.Value.EmbeddingKey,
new BsonArray(
document
.Embeddings.Response[i].ToFloats().ToArray()
.Select(e => new BsonDouble(Convert.ToDouble(e)))
)
},
{ "timestamp", DateTime.UtcNow }
};
list.Add(vectorDocument);
}
try
{
// Insert the documents into the collection
await cosmosClient
.GetDatabase(this.databaseName)
.GetCollection<BsonDocument>(this.collectionName)
.InsertManyAsync(list);
this.logger.LogInformation(
"""
Indexed {Count} sections
""",
list.Count
);
}
catch (MongoException ex)
{
this.logger.LogError(ex, "CosmosDBSearchProvider:UpsertVectorAsync error");
throw;
}
}