-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
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) | ||
{ | ||
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)); | ||
|
||
foreach (var id in ids) | ||
{ | ||
await client.DeleteAsync(Name, new Id(id), cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Task<IReadOnlyCollection<string>> AddAsync( | ||
IReadOnlyCollection<Vector> items, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
items = items ?? throw new ArgumentNullException(nameof(items)); | ||
|
||
return Task.FromResult<IReadOnlyCollection<string>>([]); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Task<VectorSearchResponse> SearchAsync( | ||
VectorSearchRequest request, | ||
VectorSearchSettings? settings = default, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
request = request ?? throw new ArgumentNullException(nameof(request)); | ||
settings ??= new VectorSearchSettings(); | ||
|
||
var response = await client.SearchAsync<MyDoc>(s => s | ||
Check failure on line 64 in src/Databases/Elasticsearch/src/ElasticsearchVectorCollection.cs GitHub Actions / Build and test / Build, test and publish
Check warning on line 64 in src/Databases/Elasticsearch/src/ElasticsearchVectorCollection.cs GitHub Actions / Build and test / Build, test and publish
Check warning on line 64 in src/Databases/Elasticsearch/src/ElasticsearchVectorCollection.cs GitHub Actions / Build and test / Build, test and publish
Check warning on line 64 in src/Databases/Elasticsearch/src/ElasticsearchVectorCollection.cs GitHub Actions / Build and test / Build, test and publish
|
||
.Index("my_index") | ||
.From(0) | ||
.Size(10) | ||
.Query(q => q | ||
.Knn() | ||
.Term(t => t.User, "flobernd") | ||
Check failure on line 70 in src/Databases/Elasticsearch/src/ElasticsearchVectorCollection.cs GitHub Actions / Build and test / Build, test and publish
|
||
) | ||
); | ||
|
||
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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
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) | ||
{ | ||
try | ||
{ | ||
var collection = await client.GetCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false) ?? throw new InvalidOperationException("Collection not found"); | ||
Check failure on line 17 in src/Databases/Elasticsearch/src/ElasticsearchVectorDatabase.cs GitHub Actions / Build and test / Build, test and publish
|
||
|
||
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) | ||
{ | ||
await foreach (var name in client.ListCollectionsAsync(cancellationToken).ConfigureAwait(false)) | ||
Check failure on line 33 in src/Databases/Elasticsearch/src/ElasticsearchVectorDatabase.cs GitHub Actions / Build and test / Build, test and publish
|
||
{ | ||
if (name == collectionName) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task CreateCollectionAsync(string collectionName, int dimensions, CancellationToken cancellationToken = default) | ||
{ | ||
await client.CreateCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); | ||
Check failure on line 47 in src/Databases/Elasticsearch/src/ElasticsearchVectorDatabase.cs GitHub Actions / Build and test / Build, test and publish
|
||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<IReadOnlyList<string>> ListCollectionsAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return await client.ListCollectionsAsync(cancellationToken).ToListAsync(cancellationToken: cancellationToken).ConfigureAwait(false); | ||
Check failure on line 53 in src/Databases/Elasticsearch/src/ElasticsearchVectorDatabase.cs GitHub Actions / Build and test / Build, test and publish
|
||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<IVectorCollection> GetOrCreateCollectionAsync(string collectionName, int dimensions, CancellationToken cancellationToken = default) | ||
{ | ||
if (!await IsCollectionExistsAsync(collectionName, cancellationToken).ConfigureAwait(false)) | ||
{ | ||
await client.CreateCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); | ||
Check failure on line 61 in src/Databases/Elasticsearch/src/ElasticsearchVectorDatabase.cs GitHub Actions / Build and test / Build, test and publish
|
||
} | ||
|
||
return await GetCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task DeleteCollectionAsync(string collectionName, CancellationToken cancellationToken = default) | ||
{ | ||
await client.DeleteCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false); | ||
Check failure on line 70 in src/Databases/Elasticsearch/src/ElasticsearchVectorDatabase.cs GitHub Actions / Build and test / Build, test and publish
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net4.6.2;netstandard2.0;net6.0;net8.0</TargetFrameworks> | ||
<NoWarn>$(NoWarn)</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Label="NuGet"> | ||
<Description>Elasticsearch support for LangChain.</Description> | ||
<PackageTags>$(PackageTags);elasticsearch</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Label="Usings"> | ||
<Using Include="System.Net.Http" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Elastic.Clients.Elasticsearch" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Abstractions\src\LangChain.Databases.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ public enum SupportedDatabase | |
Postgres, | ||
Redis, | ||
Mongo, | ||
Elasticsearch, | ||
} |