Show / Hide Table of Contents

Class KnowledgeCollection

Represents a collection of knowledge items with associated relevance scores.

Inheritance
object
KnowledgeCollection
Implements
IEnumerable<(double Score, Knowledge Knowledge)>
IEnumerable
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
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 Source

Count

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.

| Edit this page View Source

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 Source

Add(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.

| Edit this page View Source

AddDocument(string, string)

Declaration
public void AddDocument(string id, string content)
Parameters
Type Name Description
string id
string content
| Edit this page View Source

Clear()

Removes all knowledge items from the collection.

Declaration
public void Clear()
| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

Implements

IEnumerable<T>
IEnumerable
  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX