-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding IVectorDatabase implementation for mongo
- Loading branch information
Showing
7 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using LangChain.Databases.Mongo.Client; | ||
using MongoDB.Driver; | ||
|
||
namespace LangChain.Databases.Mongo | ||
{ | ||
public class MongoVectorDatabase( | ||
string connectionString, | ||
string schema = MongoVectorDatabase.DefaultSchema) | ||
: IVectorDatabase | ||
{ | ||
private const string DefaultSchema = "langchain"; | ||
|
||
private readonly IMongoDbClient _client = new MongoDbClient( | ||
new MongoContext( | ||
new DatabaseConfiguration | ||
{ | ||
ConnectionString = connectionString, | ||
DatabaseName = schema, | ||
})); | ||
|
||
/// <inheritdoc /> | ||
public async Task<IVectorCollection> GetCollectionAsync(string collectionName, CancellationToken cancellationToken = default) | ||
{ | ||
if (!await IsCollectionExistsAsync(collectionName, cancellationToken).ConfigureAwait(false)) | ||
{ | ||
throw new InvalidOperationException($"Collection '{collectionName}' does not exist."); | ||
} | ||
|
||
var context = new MongoContext(new DatabaseConfiguration | ||
{ | ||
ConnectionString = connectionString, | ||
DatabaseName = schema, | ||
}); | ||
|
||
return new MongoVectorCollection(context, "idx_"+collectionName, name: collectionName); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task DeleteCollectionAsync(string collectionName, CancellationToken cancellationToken = default) | ||
{ | ||
await _client.DropCollectionAsync(collectionName).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<IVectorCollection> GetOrCreateCollectionAsync(string collectionName, int dimensions, CancellationToken cancellationToken = default) | ||
{ | ||
if (!await IsCollectionExistsAsync(collectionName, cancellationToken).ConfigureAwait(false)) | ||
{ | ||
await CreateCollectionAsync(collectionName, dimensions, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
return await GetCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<bool> IsCollectionExistsAsync(string collectionName, CancellationToken cancellationToken = default) | ||
{ | ||
return await _client.CollectionExistsAsync(collectionName).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task CreateCollectionAsync(string collectionName, int dimensions, CancellationToken cancellationToken = default) | ||
{ | ||
var collection = await _client.CreateCollection<Vector>(collectionName).ConfigureAwait(false); | ||
var indexName = await collection.Indexes.CreateOneAsync(new CreateIndexModel<Vector>( | ||
Builders<Vector>.IndexKeys.Ascending(v => v.Embedding) | ||
.Ascending(v => v.Text), new CreateIndexOptions | ||
{ | ||
Background = true, | ||
}), cancellationToken: cancellationToken).ConfigureAwait(false); | ||
return; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<IReadOnlyList<string>> ListCollectionsAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return await _client.GetCollections().ConfigureAwait(false); | ||
} | ||
} | ||
} |