Class KnowledgeCollection
Represents a collection of knowledge items with associated relevance scores.
Inherited Members
Namespace: VulcanAI.Knowledge
Assembly: VulcanAI.dll
Syntax
public class KnowledgeCollection : IEnumerable<(double Score, Knowledge Knowledge)>, IEnumerable
Remarks
This class maintains a sorted collection of knowledge items where each item has an associated relevance score. The collection is always kept sorted in descending order by score. It supports serialization to and from JSON format. The collection is thread-safe for read operations, but write operations should be synchronized externally if used in a multi-threaded context.
Properties
| Edit this page View SourceCount
Gets the number of knowledge items in the collection.
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| int | The total count of knowledge items. |
this[int]
Gets the knowledge item at the specified index.
Declaration
public (double Score, Knowledge Knowledge) this[int index] { get; }
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | The zero-based index of the item to retrieve. |
Property Value
| Type | Description |
|---|---|
| (double Score, Knowledge Knowledge) | A tuple containing the score and knowledge item at the specified index. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when the index is out of range. |
Methods
| Edit this page View SourceAdd(Knowledge, double)
Adds a new knowledge item to the collection with the specified relevance score.
Declaration
public void Add(Knowledge knowledge, double score)
Parameters
| Type | Name | Description |
|---|---|---|
| Knowledge | knowledge | The knowledge item to add. |
| double | score | The relevance score of the knowledge item. |
Remarks
The item is inserted in the correct position to maintain the collection's sorted order by score in descending order. If the score is NaN or infinity, it will be treated as 0.0.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when knowledge is null. |
| ArgumentException | Thrown when score is not a valid number. |
AddDocument(string, string)
Declaration
public void AddDocument(string id, string content)
Parameters
| Type | Name | Description |
|---|---|---|
| string | id | |
| string | content |
Clear()
Removes all knowledge items from the collection.
Declaration
public void Clear()
DeserializeFromJson(string)
Deserializes a knowledge collection from a JSON string.
Declaration
public static KnowledgeCollection DeserializeFromJson(string json)
Parameters
| Type | Name | Description |
|---|---|---|
| string | json | The JSON string to deserialize. |
Returns
| Type | Description |
|---|---|
| KnowledgeCollection | A new instance of KnowledgeCollection containing the deserialized items. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when json is null. |
| JsonException | Thrown when the JSON string is invalid. |
GetEnumerator()
Returns an enumerator that iterates through the collection.
Declaration
public IEnumerator<(double Score, Knowledge Knowledge)> GetEnumerator()
Returns
| Type | Description |
|---|---|
| IEnumerator<(double Score, Knowledge Knowledge)> | An enumerator that can be used to iterate through the collection. |
GetTopItems(int)
Gets a slice of the knowledge collection with the highest relevance scores.
Declaration
public KnowledgeCollection GetTopItems(int count)
Parameters
| Type | Name | Description |
|---|---|---|
| int | count | The maximum number of items to return. |
Returns
| Type | Description |
|---|---|
| KnowledgeCollection | A new collection containing the top items by score. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when count is negative. |
SerializeToJson()
Serializes the knowledge collection to a JSON string.
Declaration
public string SerializeToJson()
Returns
| Type | Description |
|---|---|
| string | A JSON string representation of the knowledge collection. |
Remarks
The JSON output is formatted with indentation for better readability. The serialization includes all knowledge items with their scores.