Metadata¶
The Build5Nines.SharpVector
vector database enables semantic search for Text
that is stored in the database. Being able to semantically search text is an extremely useful way to lookup more information related to the text. For this purpose, Metadata
is stored alongside the Text
within the vector database. This way, when Text
is found when performing a semantic search, then the matching Metadata
is also retrieved.
Adding Metadata¶
The .AddText
and .AddTextAsync
methods access 2 arguments:
text
: TheText
that is added to the vector database and has vector embeddings generated for.metadata
: This is additional data / information that is stored alongside theText
.
C# | |
---|---|
1 2 3 |
|
JSON and String Metadata¶
When using the BasicMemoryVectorDatabase
class, the Metadata
values will always be of type String
. This enables you to store a variety of values here, including:
- JSON data: You can serialize any data to a JSON string for storage in the
Metadata
associated with a text item in the database. String
value: You can store any other string value as theMetadata
associated with a text item in the database. This could be a URL, Filename, or other information.
OpenAI and Ollama Support
When working with the OpenAI BasicOpenAIMemoryVectorDatabase
and Ollama BasicOllamaMemoryVectorDatabase
, the Metadata
data type is also String
.
Here are some examples of storing string
metadata and retrieving it from the database:
C# | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
C# | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Custom Metadata Type¶
The MemoryVectorDatabase<TMetadata>
generic class allows you to create a vector database that uses your own custom class as the metadata by defining that class using generics. This enables you to store a native .NET object as the metadata alongside the text in the vector database.
Here's an example of using the MemoryVectorDatabase<TMetadata>
with a .NET class for the Metadata
:
C# | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
This will offer better performance with scenarios that require more complex metadata since you no longer need to handle serialization to/from JSON.
OpenAI and Ollama Support
The OpenAIMemoryVectorDatabase<TMetadata>
and OllamaMemoryVectorDatabase<TMetadata>
generic classes can also be used to define your own Metadata
type when working with OpenAI and Ollama embeddings.