This is the second part of the eShopSupport Series which covers the details of the eShopSupport GitHub repository.
DataIngestor Project
The DataIngestor is a console application that will process the data files created by the DataGenerator - it does not ingest the data into any of the databases. I mentioned in the last entry about the DataGenerator that you didn’t need to use the DataGenerator because there are two sets of generated files provided in the github repo: dev and test. What I failed to mention is those files are result of both the DataGenerator and the DataIngestor being run. So you also don’t have to use the DataIngestor.
The DataIngestor project is located under the src folder:
In this entry I’ll cover the details of what the DataIngestor application does, a few interesting things I found interesting and some thoughts on improvements.
What does it do?
Steve Sanderson mentions this project around 21 minutes into his NDC talk “How to add genuinely useful AI to your webapp (not just chatbots)”, with the subheading of “Parsing, Chunking, Embedding” … which pretty much sums up what this project does. It takes the files the DataGenerator project created and processes them into a format that can be used to seed the databases.
The purpose is to generate files that will be used by the applications to bootstrap their data when they start up. This may or may not be how you would do it for a production system, but it really makes open source demo projects more accessible to a wider experience level to easily get the application up and running with sample data. Here are the files it will create from the files the DataGenerator created:
NOTE: I will cover the population of the databases and processing of the manuals in a future blog entry, since it is done in the Backend project.
Like the DataGenerator, all the important logic is driven from the Program.cs file:
await new TicketIngestor().RunAsync(generatedDataPath, outputDir);
await new ProductCategoryIngestor().RunAsync(generatedDataPath, outputDir);
await new ProductIngestor().RunAsync(generatedDataPath, outputDir);
await new EvalQuestionIngestor().RunAsync(generatedDataPath, outputDir);
await new ManualIngestor().RunAsync(generatedDataPath, outputDir);
await new ManualZipIngestor().RunAsync(generatedDataPath, outputDir);
If you run the project from Visual Studio, it will look for the files to process in the location the DataGenerator writes it’s files:
It will output it’s files to the \seeddata\dev
directory, overwriting any files that were already there:
Tickets and Customers
The TicketIngestor reads in all the files in the /output/tickets/threads
directory (more information on these files), loops through them creating two things:
- a unique list of customer names
- a new Ticket data object that better matches the object to be saved to the database
Once all ticket threads are processed, there are two files created:
tickets.json - a json file containing the new Ticket object format and aggregates all generated tickets and their message threads:
customers.json - a json file containing a unique listing of all the customers in all the tickets generated by the DataGenerator:
[
{
"CustomerId": 227,
"FullName": "Trevor McAlister"
},
{
"CustomerId": 414,
"FullName": "Morgan J. Blackwell"
},
{
"CustomerId": 313,
"FullName": "Jordan Mallow"
},
{
"CustomerId": 128,
"FullName": "Jordan W. Mallory"
},
{
"CustomerId": 353,
"FullName": "Jacqueline Marsden"
},
// ...
}
Categories
The ProductCategoryIngestor reads in all the files in the /output/categories
directory (more information on these files). Like with tickets above, the ProductCategoryIngestor maps the generated category to a new ProductCategory object that will be saved in the database later. Plus an embedding is generated for the category name using the LocalTextEmbeddingGenerationService:
foreach (var filename in Directory.GetFiles(categoriesSourceDir, "*.json"))
{
var generated = (await JsonSerializer.DeserializeAsync<GeneratedCategory>(File.OpenRead(filename), inputOptions))!;
categories.Add(new ProductCategory
{
CategoryId = generated.CategoryId,
Name = generated.Name,
NameEmbeddingBase64 = ToBase64(await embeddingGenerator.GenerateEmbeddingAsync(generated.Name)),
});
}
Once all the categories have been processed and the embeddings retrieved, they are saved in a single categories.json file, which looks like this:
[
{
"CategoryId": 1,
"Name": "Solar-Powered Backpacks",
"NameEmbeddingBase64": "lR0wwGDRJ0ByLpI/ajImPgNisT92MQc/ody9P0EDg78Yfte\u002BLasLQPoHtT/9l0jAUBeavz7JeECqRV1AUKI/vszNMUDeqTRAx3G5vsHDtz\u002Bt4I1AmloewPOaDMDWIcC/Va9Kv56tML4hYw8/afWAv8pSNMCahMXAbGI\u002BP7ojY8Bayg/ABG7ev22iZb\u002BGt\u002BQ9/SCTPz3AD8A7pBzAm9erP/Jz/76C5gC/MvjSvwTWEz8njaa//Qe8vwyiTD5k8EPAY/rnP/WgNcAzL4g/AK0UwPWIqL6pqNW9Ss\u002Bjvysbqz\u002BKPwo/H/pCQMbzGkAYE\u002B293NgAQPQNwz/\u002BuPzAKZwVQJwjR0CUnGo9ZYUNwN\u002B/qz\u002BStyBA\u002BK3gPlCzsj6ep4c\u002BwEOuO1t0BEAb/zA/KK8EQFqzs7/Qgp6/dVSovxuIAsAxjfa/tf2KvhhOkL4iVTtATu72PzGye0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QM7EB8Ck3iPA9LcMPzyKHj/Xlt\u002B/xKWcv/jSjj65/grAD4igvok8gb/871E\u002BRzFnwKqnK0CBZg2/ZObkv2MbS0A6H9S/nj/TPxvz/79jq4W/khYcwLfKlD2WyxI/U3FrPt6FVb\u002Bjb/K\u002BuZYCP1TL274qSgi/tk2aQOx1jj/XcJY/ZW0nQAc5gL\u002BTbA1AQgjePwyVVECwe\u002Ba/jrOHvlsoIEB/mpO/DkCiv1T0qD8iVKk/\u002BrIbwMS32r/MEta/TE/8vxtjcD7wYtE/Di8RwIyOHr863spATGntvJGmL8HSGAA/xC4WvxG0FcAMujzAG1QXP0TvLkA7OXlAH/bJP5ZnqD9ECtK/S2MPQEhRKL6qsxE\u002B1/FKQEL0r74JNUBAg/dTv9GHPz91GR7AFlndP38HHj8sUNhAY2ASv\u002By9/75OlU5AJ6j\u002BvyEtnj8PNh0/kIqDvugjlr7vLI093HI5QGqBAb8nVW6/YAAqQMYGM79n1THANBp4Pyg/lr8NVwo\u002BvH5uP9l5jsAIEnu\u002B0Xy8P3ESMcCcTJC945\u002BowJiyWD1Sj\u002B6/DTM2v9nUTD7NoTrASNrdPo\u002B5E75ixku\u002Bqj9tv45PBcC0\u002BqO/ZFXEv2h8B0CorxG/LBvsvvNeCsDkkcO\u002B"
},
//...
]
NOTE: the brands that were in the generated files are not carried over to the category.json file.
Products
The ProductIngestor reads in all the files in the /output/products
directory (more information on these files). As it loops through each of the generated products it maps the information to the Product object and retrieves an embedding for the concatenated string $"{generated.Model}, {generated.Brand}"
. The code looks almost the same as the ProductCategoryIngestor except for the NameEmbedding being ReadOnlyMemory<float>
versus the NameEmbeddingBase64 being a string
in the ProductCategory.
foreach (var filename in Directory.GetFiles(productsSourceDir, "*.json"))
{
var generated = (await JsonSerializer.DeserializeAsync<GeneratedProduct>(File.OpenRead(filename), inputOptions))!;
products.Add(new Product
{
ProductId = generated.ProductId,
CategoryId = generated.CategoryId,
Brand = generated.Brand,
Model = generated.Model,
Description = generated.Description,
Price = generated.Price,
NameEmbedding = await embeddingGenerator.GenerateEmbeddingAsync($"{generated.Model}, {generated.Brand}"),
});
}
When you look at the products.json file, you will see the NameEmbedding is saved in the json file as a Base64 encoded string, even though it was not converted to a strind like in the ProductCategoryIngestor:
[
{
"ProductId": 1,
"CategoryId": 43,
"Brand": "CoolCampers",
"Model": "ThermoTent Pro 3000",
"Description": "Experience ultimate comfort with our climate-controlled tent featuring advanced thermal regulation tech. Perfect for any camping season.",
"Price": 499.99,
"NameEmbedding": "IBUxwI9cjkAHCKZA1co1wFSCXz8bFzhAycxjwA0o1T5Gg/K/b8Lgv24PNEBoHTzA7v8+QADo0bhjX7c/+dc7QKkv5j9qgTBAtHlOQAMPwT9dNjxAf1DIwC8LWz6Nk/G/2AFbvwjOJUCuAZXArACTv5Y0R8BONETBYVGiP1Wct8CoWbxAmm8EwGKB8b9lokDAVCHev8+YHUBekXrAzbx0v9zf0L8BbzFAthaewF8N1j0j10A/3ygWv7h3x7/nahTAiNzkvU0u178srue+SpwGwIwU676W50e++g4QQK5bE0Aykl5AVcBHP2AawL7cnhFAZdEMQHvakUDEcynB7KWdQFurkz+nTsy+NvquwAZKnb8QGrO+eE9Nv8XhBsA+jCFARb82QHdvnz8Qeo6/mXiTvxNVP8BJhULAnFzAv3tRDT9yXynABL0OwP7pXL9kzQrA/nFbP3SOhcDJVoBAypcTwBolk0AQQ+W/Jd+twJ1nnT1dhTvAjSksPzBY5r9sP8e/Qi2FQKTsrL6xrHJAbPwVQQh1Hr6J/GJA1U+TQBbdEcAgS1E/Hw6GwL6PT0DlIsG/o379wNoMaj/ryto/DhEtvw/B0z4ivyDAFF3tP61UJcBwEc4//JkIQIGqlr5HuZBAzLbqv8A6gj8uE6xAPAxVwGQgo7/gT4G+FDyaPt18lkBcfVFAZv3fQAC0FUBlMARB20gzv8dOhT8ItIQ/mdMUwMZuO0BwdkLAixOEQDbWC0CK4+++CqiuQHCuhEBeE0nBhhHnviiZ/j5ncy6+h0mXQFVkFM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9+/VZA/I53PjE7hr+tP2/A/k3JP4dnfj6bJRI+b4fLQHoWoT52kXq/vD/vPTc22T+397+/LE0UwDb+iT6GgTm/+Q3wQLi1FsAWmp6/2VRtwI9hbsC8nGg/WarWvuZBoMB23Ku/NDmhPvDl97/NfFNAZb+JvyzSrcCWIGzAiXPFwMN0lL+J0qk/C4GJQLOGhj8EFglA"
},
//...
]
This Base64 string conversion is done using the JsonConverter attribute on the property:
The code for the EmbeddingJsonConverter, looks like a useful utility to keep in mind:
class EmbeddingJsonConverter : JsonConverter<ReadOnlyMemory<float>>
{
public override ReadOnlyMemory<float> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
if (reader.TokenType != JsonTokenType.String)
{
throw new InvalidOperationException($"JSON deserialization failed because the value type was {reader.TokenType} but should be {JsonTokenType.String}");
}
var bytes = reader.GetBytesFromBase64();
var floats = MemoryMarshal.Cast<byte, float>(bytes);
return floats.ToArray(); // TODO: Can we avoid copying? The memory is already in the right format.
}
public override void Write(Utf8JsonWriter writer, ReadOnlyMemory<float> value, JsonSerializerOptions options)
{
var bytes = MemoryMarshal.AsBytes(value.Span);
writer.WriteBase64StringValue(bytes);
}
}
EvalQuestions
The EvalQuestionIngestor reads all the files in the /output/evalquestions
directory (more information on these files). As it loops through each of the generated files it deserializes them into a EvalQuestion object (notice it is in the Evaluator project not the Backend project like the others). Once all items have been deserialized, a new evalquestions.json file is created with all the evalquestions. Unlike the other json files created so far, these are not going to be objects stored in a database later - instead they are going to be used by the Evaluator project.
The end file looks like this:
[
{
"QuestionId": 1,
"ProductId": 44,
"Question": "can I use underwater?",
"Answer": "Not waterproof."
},
//...
]
Manuals
There are two steps for processing manuals: ManualIngestor and ManualZipIngestor.
The ManualIngestor processes all the pdf files from the /output/manuals/pdf
directory. The processing steps for each pdf are as follows:
- Open the Pdf document
- Get all the pages
- Chunk the pages into paragraphs around 200 tokens in length
- Get the embeddings for the paragraph chunks
- Create the object
The End result is a list of ManualChunk objects. These will end up in a vector db eventually.
NOTE: the embedding property on this object is a
byte[]
which the JsonSerializer will marshal as a base 64 string.
// Loop over each page in it
using var pdf = PdfDocument.Open(file);
foreach (var page in pdf.GetPages())
{
// [1] Parse (PDF page -> string)
var pageText = GetPageText(page);
// [2] Chunk (split into shorter strings on natural boundaries)
var paragraphs = TextChunker.SplitPlainTextParagraphs([pageText], 200);
// [3] Embed (map into semantic space)
var paragraphsWithEmbeddings = paragraphs.Zip(await embeddingGenerator.GenerateEmbeddingsAsync(paragraphs));
// [4] Save
chunks.AddRange(paragraphsWithEmbeddings.Select(p => new ManualChunk
{
ProductId = docId,
PageNumber = page.Number,
ChunkId = ++paragraphIndex,
Text = p.First,
Embedding = MemoryMarshal.AsBytes(p.Second.Span).ToArray()
}));
}
The end file looks like this:
[
{
"ChunkId": 1,
"ProductId": 1,
"PageNumber": 1,
"Text": "ThermoTent Pro 3000 (c) CoolCampers 1",
"Embedding": "lUSUwHvayUC6jt1AQ/llwNSMIb50PkhANbOIwAhzNj/9KRe/Y4Srv9RgVEB6moPA7fZeQFZ7CT/UZHI/yUVYQH5E8z8czB5AEFauQFhqFUDPujtAeWEEwUTVCr3BWDrAssSjv+kGIECBr7TALIcNwEgPH8AQ4H/BCVIGQH/X3sDSNQFB+KkFwNRgOsDOA4rARYEQwCkvL0BYJKrAKq6tvoq5279x1B9Ak2qvwOBzkb4UNtq+j5DSvXUnhr/itdu/Yh9ePxMj37+xddC+o7sCwGqHU78tlo6/Qj5MQGS1mD/I6ZNAfdoKQFhaqL5owxFAMn0WQE/hg0C9glTBLn+8QIhtyT+sDMC+GpmswIAWUcB9syw/sowjQOLTYMAx8wZA4YZZQCDrsjuz2De/oS04wOEAdsAH/SnAdotZv4lZhj8tarHAIP42wIH2McBnCR3ARJ9OP870u8AYjohAbQE+wNxZsEDMgS/AikCswIjHCb9gR4bA+EiGP6RNecDTC4K/1PShQDdzLz/oRqFAOtM6QeJS/D0wgA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x2S/z/YUw8/cVbWvy9LbkAbqgXAQpvfv+RKHEBN4FtAZRc+wCBy4L5u0KJAP+rEvnLzbD+HQLhAxt7dv9nnHUCgOXlAzp3kP7xUxcEA9Jk/CPT6PqThoL+isaI+4am2wG1dBMDaZV++cjcswBbrFEBxP2nA5t4aQIupmj+rM5lAgGzcPpRIdMDeTWtAMtFewCCrokB/VqLA7BPZPiKflj+flYtB3PcBwHY8SsA7fh1AoBrlO2/YAUBQb5RA4uVevmmyEMBp02/AK5kCQFa3Wr0wbE4/8KcLQUyuQz8plt6/SNOBPdpfakCfz9y/RwjQvx44Rz0eZLa/Za8eQYN5OsDe9R/AOZW5wHpLoMDp1gC/URUbwC0ckcBaXOK+E+DmvpZhC8Ch6ZZAWSQVwDGZlcCwpK3A5uPVwDaRX7+lBQNA5LPaQH5SLD9sPotA"
},
// ...
]
The ManualZipIngestor creates a zip file of all the files in the /output/manuals/pdf
. The pdfs in this zip file will eventually be put into blob storage.
Dependencies
The DataIngestor project uses a few dependencies to provide functionality with the file generation:
- PdfPig - used to work with Pdf files.
- SmartComponents.LocalEmbeddings.SemanticKernel - used to retrieve embeddings for names and chunks of the manual text. This uses the Onnx runtime behind the scene with the model.onnx and vocab.txt file you can find in the LocalEmbeddingsModel directory after you build the project.
How to set it up
In order to successfully run the DataIngestor, you need to first run the DataGenerator (more on getting the DataGenerator setup) in order to have the output
directory files.
The DataInjestor only needs a path to the generated files to run. If you run it in Visual Studio the default is set for you in the csproj file:
<StartArguments>$(SolutionDir)\seeddata\DataGenerator\output</StartArguments>
Points of Interest
These are some points in the code base that I found interesting and will be revisiting when writing my own code.
Usage of local embeddings
There are three usages of the LocalTextEmbeddingGenerationService in this project: ProductCategoryIngestor, ProductIngestor and ManualIngestor - all shown above. Another interesting note is they each map to different property types:
ProductCategory has a string NameEmbeddingBase64 property:
public class ProductCategory
{
...
public required string NameEmbeddingBase64 { get; set; }
}
Product has a ReadOnlyMemory
public class Product
{
...
[NotMapped, JsonConverter(typeof(EmbeddingJsonConverter))]
public required ReadOnlyMemory<float> NameEmbedding { get; set; }
}
ManualChunk has a byte[] Embedding property:
public class ManualChunk
{
...
public required byte[] Embedding { get; set; }
}
Serialization of embeddings
Another interesting point about the embeddings is the three different ways the get serialized:
ProductCategoryIngestor uses a utility method ToBase64()
:
private static string ToBase64(ReadOnlyMemory<float> embedding)
=> Convert.ToBase64String(MemoryMarshal.AsBytes(embedding.Span));
ProductInjestor uses the custom JsonConverter attribute for the EmbeddingJsonConverter
shown earlier
ManualIngestor uses the JsonSerializer that converts a byte array to a base 64 string
PdfPig usage
I’ve see PdfPig used in other projects before, however I have not seen the techniques used to get the words and text blocks used in this project:
private static string GetPageText(Page pdfPage)
{
var letters = pdfPage.Letters;
var words = NearestNeighbourWordExtractor.Instance.GetWords(letters);
var textBlocks = DocstrumBoundingBoxes.Instance.GetBlocks(words);
return string.Join(Environment.NewLine + Environment.NewLine,
textBlocks.Select(t => t.Text.ReplaceLineEndings(" ")));
}
IEnumerable.Zip usage
One of the great things about reading through open source projects is seeing the way others write code. In the DataIngestor project, the usage of the Enumerable.Zip Method is not something I see very often and seems like an efficient way to write that code:
ManualIngestor.cs
var paragraphsWithEmbeddings = paragraphs.Zip(await embeddingGenerator.GenerateEmbeddingsAsync(paragraphs));
Thoughts on Improvements
This is a pretty straightforward project and there are not too many things I see that need changed, however I did come up with a couple of items that would be nice to have.
Ability to Save to Databases
Due to the name of the project being DataIngestor, I would really like to have at least an option to configure the final steps to do the actual saving to the databases.
Reformat the Project in Visual Studio
I would move the Ingestors to an Ingestors folder (like the Generators being in a Generators folder in the DataGenerator project).
Things I still need to learn
- Is there a reason for the three different property types for embeddings?
- Does
IsAspireHost
in the csproj file, need to be true? If so, why?
Other Resources
- eShopSupport Github
- How to add genuinely useful AI to your webapp (not just chatbots) - Steve Sanderson
- smartcomponents Github
- Semantic Kernel Github
- Onnx Runtime
If you have a comment, please message me @haleyjason on twitter/X.