Skip to content
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

Implementation of MongoDBAtlasVectorCollection #280

Merged
merged 9 commits into from
May 2, 2024
73 changes: 73 additions & 0 deletions src/Databases/Mongo/src/MongoVectorCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using LangChain.Databases.Mongo.Client;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;

namespace LangChain.Databases.Mongo;

public class MongoVectorCollection(

Check warning on line 7 in src/Databases/Mongo/src/MongoVectorCollection.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Rename type name MongoVectorCollection so that it does not end in 'Collection' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)

Check warning on line 7 in src/Databases/Mongo/src/MongoVectorCollection.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Rename type name MongoVectorCollection so that it does not end in 'Collection' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)

Check warning on line 7 in src/Databases/Mongo/src/MongoVectorCollection.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Rename type name MongoVectorCollection so that it does not end in 'Collection' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)

Check warning on line 7 in src/Databases/Mongo/src/MongoVectorCollection.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Rename type name MongoVectorCollection so that it does not end in 'Collection' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)

Check warning on line 7 in src/Databases/Mongo/src/MongoVectorCollection.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Rename type name MongoVectorCollection so that it does not end in 'Collection' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)

Check warning on line 7 in src/Databases/Mongo/src/MongoVectorCollection.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Rename type name MongoVectorCollection so that it does not end in 'Collection' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)

Check warning on line 7 in src/Databases/Mongo/src/MongoVectorCollection.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Rename type name MongoVectorCollection so that it does not end in 'Collection' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)

Check warning on line 7 in src/Databases/Mongo/src/MongoVectorCollection.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Rename type name MongoVectorCollection so that it does not end in 'Collection' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)

Check warning on line 7 in src/Databases/Mongo/src/MongoVectorCollection.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Rename type name MongoVectorCollection so that it does not end in 'Collection' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)

Check warning on line 7 in src/Databases/Mongo/src/MongoVectorCollection.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Rename type name MongoVectorCollection so that it does not end in 'Collection' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)
IMongoContext mongoContext,
string indexName,
string name = VectorCollection.DefaultName,
string? id = null)
: VectorCollection(name, id), IVectorCollection
{
private IMongoCollection<Vector> _mongoCollection = mongoContext.GetCollection<Vector>(name);

public async Task<IReadOnlyCollection<string>> AddAsync(IReadOnlyCollection<Vector> items, CancellationToken cancellationToken = default)
{
await _mongoCollection.InsertManyAsync(items, cancellationToken: cancellationToken).ConfigureAwait(false);
return items.Select(i => i.Id).ToList();
}

public async Task<bool> DeleteAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default)
{
var filter = Builders<Vector>.Filter.In(i => i.Id, ids);
var result = await _mongoCollection.DeleteManyAsync(filter, cancellationToken).ConfigureAwait(false);
return result.IsAcknowledged;
}

public async Task<Vector?> GetAsync(string id, CancellationToken cancellationToken = default)
{
var filter = Builders<Vector>.Filter.Eq(i => i.Id, id);
var result = await _mongoCollection.FindAsync(filter, cancellationToken: cancellationToken).ConfigureAwait(false);
return result.FirstOrDefault(cancellationToken: cancellationToken);
}

public async Task<bool> IsEmptyAsync(CancellationToken cancellationToken = default)
{
return await _mongoCollection.CountDocumentsAsync(FilterDefinition<Vector>.Empty, cancellationToken: cancellationToken).ConfigureAwait(false) == 0;
HavenDV marked this conversation as resolved.
Show resolved Hide resolved
}

public async Task<VectorSearchResponse> SearchAsync(VectorSearchRequest request, VectorSearchSettings? settings = null, CancellationToken cancellationToken = default)
{
request = request ?? throw new ArgumentNullException(nameof(request));
settings ??= new VectorSearchSettings();

var options = new VectorSearchOptions<Vector>()
{
IndexName = indexName,
NumberOfCandidates = settings.NumberOfResults * 10
};
var projectionDefinition = Builders<Vector>.Projection
.Exclude(a => a.Distance)
.Meta("score", "vectorSearchScore");

var results = await _mongoCollection.Aggregate()
.VectorSearch(nameof(Vector.Embedding), request.Embeddings.First(), settings.NumberOfResults, options)
.Project(projectionDefinition)
.ToListAsync(cancellationToken)
.ConfigureAwait(false);


return new VectorSearchResponse
{
Items = results.Select(result =>
{
var output = BsonSerializer.Deserialize<Vector>(result);
output.Distance = (float)result["score"].ToDouble();
return output;
})
.ToArray(),
};
}
}