-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added Elasticsearch database template. (#352)
* feat: Added Elasticsearch database. * fix: Disabled.
- Loading branch information
Showing
9 changed files
with
343 additions
and
101 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
106 changes: 106 additions & 0 deletions
106
src/Databases/Elasticsearch/src/ElasticsearchVectorCollection.cs
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,106 @@ | ||
using Elastic.Clients.Elasticsearch; | ||
|
||
namespace LangChain.Databases.Elasticsearch; | ||
|
||
/// <summary> | ||
/// Elasticsearch vector collection. | ||
/// </summary> | ||
public class ElasticsearchVectorCollection( | ||
ElasticsearchClient client, | ||
string name = VectorCollection.DefaultName, | ||
string? id = null) | ||
: VectorCollection(name, id), IVectorCollection | ||
{ | ||
/// <inheritdoc /> | ||
public async Task<Vector?> GetAsync(string id, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
// var record = await client.GetAsync(Name, new Id(id), cancellationToken: cancellationToken).ConfigureAwait(false); | ||
// if (record == null) | ||
// { | ||
// return null; | ||
// } | ||
// | ||
// return new Vector | ||
// { | ||
// Text = string.Empty, | ||
// Metadata = new Dictionary<string, object>(), | ||
// }; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<bool> DeleteAsync( | ||
IEnumerable<string> ids, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
ids = ids ?? throw new ArgumentNullException(nameof(ids)); | ||
|
||
throw new NotImplementedException(); | ||
// foreach (var id in ids) | ||
// { | ||
// await client.DeleteAsync(Name, new Id(id), cancellationToken).ConfigureAwait(false); | ||
// } | ||
// | ||
// return true; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<IReadOnlyCollection<string>> AddAsync( | ||
IReadOnlyCollection<Vector> items, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
items = items ?? throw new ArgumentNullException(nameof(items)); | ||
|
||
throw new NotImplementedException(); | ||
//return Task.FromResult<IReadOnlyCollection<string>>([]); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<VectorSearchResponse> SearchAsync( | ||
VectorSearchRequest request, | ||
VectorSearchSettings? settings = default, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
request = request ?? throw new ArgumentNullException(nameof(request)); | ||
settings ??= new VectorSearchSettings(); | ||
|
||
throw new NotImplementedException(); | ||
// var response = await client.SearchAsync<MyDoc>(s => s | ||
// .Index("my_index") | ||
// .From(0) | ||
// .Size(10) | ||
// .Query(q => q | ||
// .Knn() | ||
// .Term(t => t.User, "flobernd") | ||
// ) | ||
// ); | ||
// | ||
// if (response.IsValidResponse) | ||
// { | ||
// var doc = response.Documents.FirstOrDefault(); | ||
// } | ||
// | ||
// return Task.FromResult(new VectorSearchResponse | ||
// { | ||
// Items = Array.Empty<string>() | ||
// .Select(record => | ||
// { | ||
// return new Vector | ||
// { | ||
// Id = string.Empty, | ||
// Text = string.Empty, | ||
// Metadata = new Dictionary<string, object>(), | ||
// Embedding = [], | ||
// Distance = 0.0F, | ||
// }; | ||
// }) | ||
// .ToArray(), | ||
// }); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Task<bool> IsEmptyAsync(CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/Databases/Elasticsearch/src/ElasticsearchVectorDatabase.cs
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,78 @@ | ||
using Elastic.Clients.Elasticsearch; | ||
|
||
namespace LangChain.Databases.Elasticsearch; | ||
|
||
/// <summary> | ||
/// Elasticsearch vector store. | ||
/// </summary> | ||
public class ElasticsearchVectorDatabase( | ||
ElasticsearchClient client) | ||
: IVectorDatabase | ||
{ | ||
/// <inheritdoc /> | ||
public async Task<IVectorCollection> GetCollectionAsync(string collectionName, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
// try | ||
// { | ||
// // var collection = await client.GetCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false) ?? throw new InvalidOperationException("Collection not found"); | ||
// // | ||
// // return new ElasticsearchVectorCollection( | ||
// // client, | ||
// // name: collection.Name, | ||
// // id: collection.Id); | ||
// } | ||
// catch (Exception exception) | ||
// { | ||
// throw new InvalidOperationException("Collection not found", innerException: exception); | ||
// } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<bool> IsCollectionExistsAsync(string collectionName, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
// await foreach (var name in client.ListCollectionsAsync(cancellationToken).ConfigureAwait(false)) | ||
// { | ||
// if (name == collectionName) | ||
// { | ||
// return true; | ||
// } | ||
// } | ||
// | ||
// return false; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task CreateCollectionAsync(string collectionName, int dimensions, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
//await client.CreateCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<IReadOnlyList<string>> ListCollectionsAsync(CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
//return await client.ListCollectionsAsync(cancellationToken).ToListAsync(cancellationToken: cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<IVectorCollection> GetOrCreateCollectionAsync(string collectionName, int dimensions, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
// if (!await IsCollectionExistsAsync(collectionName, cancellationToken).ConfigureAwait(false)) | ||
// { | ||
// await client.CreateCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); | ||
// } | ||
// | ||
// return await GetCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task DeleteCollectionAsync(string collectionName, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
//await client.DeleteCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Databases/Elasticsearch/src/LangChain.Databases.Elasticsearch.csproj
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,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net4.6.2;netstandard2.0;net6.0;net8.0</TargetFrameworks> | ||
<NoWarn>$(NoWarn);CS9113;CS1998</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Label="NuGet"> | ||
<Description>Elasticsearch support for LangChain.</Description> | ||
<PackageTags>$(PackageTags);elasticsearch</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Elastic.Clients.Elasticsearch" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Abstractions\src\LangChain.Databases.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
|
@@ -10,5 +10,6 @@ public enum SupportedDatabase | |
Postgres, | ||
Redis, | ||
Mongo, | ||
Elasticsearch, | ||
DuckDb | ||
} |
Oops, something went wrong.