tryAGI / tryAGI/LangChain.Databases

review way to map metadata

Open
#4 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

todo
Dominant language
C#
Stars
12
Forks
8
PR merge metrics
No merged PRs in 30d

Description

https://github.com/tryAGI/LangChain.Databases/blob/709070b0226b59fe8eef116395b0e4ad1bd5df97/src/SemanticKernel/src/SemanticKernelMemoryStoreCollection.cs#L18


using Microsoft.SemanticKernel.Memory;

namespace LangChain.Databases.SemanticKernel;

public class SemanticKernelMemoryStoreCollection(IMemoryStore store,
    string name = VectorCollection.DefaultName,
    string? id = null)
    : VectorCollection(name, id), IVectorCollection
{
    public async Task<IReadOnlyCollection<string>> AddAsync(IReadOnlyCollection<Vector> items, CancellationToken cancellationToken = default)
    {
        items = items ?? throw new ArgumentNullException(nameof(items));

        List<string> list = [];
        foreach (var item in items)
        {
            string? metadata = null;
            //TODO: review way to map metadata
            if (item.Metadata != null)
                metadata = string.Join("#", item.Metadata.Select(kv => kv.Key + "&" + kv.Value));

            var record = new MemoryRecord
            (
                new MemoryRecordMetadata
                (
                    isReference: false,
                    id: item.Id,
                    text: item.Text,
                    description: string.Empty,
                    externalSourceName: item.Text,
                    additionalMetadata: metadata ?? string.Empty
                ),
                item.Embedding,
                null,
                null
            );

            var insert = await store.UpsertAsync(Name, record, cancellationToken).ConfigureAwait(false);
            list.Add(insert);
        }
        return list;
    }

    public async Task<bool> DeleteAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default)
    {
        await store.RemoveBatchAsync(Name, ids, cancellationToken).ConfigureAwait(false);
        return true;
    }

    public async Task<Vector?> GetAsync(string id, CancellationToken cancellationToken = default)
    {
        var record = await store.GetAsync(Name, id, cancellationToken: cancellationToken).ConfigureAwait(false);

        Dictionary<string, object>? metadata = null;
        if (record?.Metadata?.AdditionalMetadata != null)
            metadata = record.Metadata.AdditionalMetadata
                .Split('#')
                .Select(part => part.Split('&'))
                .ToDictionary(split => split[0], split => (object)split[1]);

        return record != null ? new Vector { Id = id, Text = record.Metadata.ExternalSourceName, Metadata = metadata } : null;
    }

    public async Task<bool> IsEmptyAsync(CancellationToken cancellationToken = default)
    {
        var collections = store.GetCollectionsAsync(cancellationToken);
        return !(await collections.CountAsync(cancellationToken).ConfigureAwait(false) > 0);
    }

    public async Task<VectorSearchResponse> SearchAsync(VectorSearchRequest request, VectorSearchSettings? settings = null, CancellationToken cancellationToken = default)
    {
        request = request ?? throw new ArgumentNullException(nameof(request));
        settings ??= new VectorSearchSettings();
        var results = await store.GetNearestMatchesAsync(Name, request.Embeddings.First(), limit: settings.NumberOfResults, cancellationToken: cancellationToken)
            .ToListAsync(cancellationToken).ConfigureAwait(false);
        return new VectorSearchResponse { Items = results.Select(x => new Vector { Text = x.Item1.Metadata.ExternalSourceName }).ToList() };
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/SemanticKernel/src/SemanticKernelMemoryStoreCollection.cs, focusing on the metadata handling in AddAsync and GetAsync. Review how Vector metadata is encoded into AdditionalMetadata and decoded again, then establish the expected round-trip behavior before changing the mapping. Done means the metadata representation is consistently handled in both methods.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
database
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.