-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBasicOpenAIMemoryVectorDatabaseTest.cs
More file actions
139 lines (117 loc) · 6.21 KB
/
BasicOpenAIMemoryVectorDatabaseTest.cs
File metadata and controls
139 lines (117 loc) · 6.21 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using OpenAI;
using OpenAI.Embeddings;
using Build5Nines.SharpVector.OpenAI;
using System.ClientModel;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.ClientModel.Primitives;
using System.IO;
using System;
namespace Build5Nines.SharpVector.OpenAI.Tests
{
[TestClass]
public class BasicMemoryVectorDatabaseTest
{
private Mock<EmbeddingClient>? _mockEmbeddingClient;
private BasicOpenAIMemoryVectorDatabase? _database;
[TestInitialize]
public void Setup()
{
_mockEmbeddingClient = new Mock<EmbeddingClient>();
// Mock the OpenAI EmbeddingClient to return a deterministic embedding vector
// GenerateEmbeddingAsync(string input, EmbeddingGenerationOptions? options = null, CancellationToken cancellationToken = default)
// returns ClientResult<OpenAIEmbedding>. We create one using the Model Factory helpers.
var embeddingVector = new float[] { 0.1f, 0.2f, 0.3f }; // small deterministic vector for tests
var openAiEmbedding = OpenAIEmbeddingsModelFactory.OpenAIEmbedding(index: 0, vector: embeddingVector);
// Create minimal concrete PipelineResponse implementation to satisfy ClientResult.FromValue without relying on Moq for abstract type
var response = new TestPipelineResponse();
var clientResult = ClientResult.FromValue(openAiEmbedding, response);
_mockEmbeddingClient
.Setup(c => c.GenerateEmbeddingAsync(
It.IsAny<string>(),
It.IsAny<EmbeddingGenerationOptions?>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(clientResult);
_database = new BasicOpenAIMemoryVectorDatabase(_mockEmbeddingClient.Object);
}
// Minimal headers implementation for TestPipelineResponse
internal class EmptyPipelineResponseHeaders : PipelineResponseHeaders
{
public override IEnumerator<KeyValuePair<string, string>> GetEnumerator() => (new List<KeyValuePair<string,string>>()).GetEnumerator();
public override bool TryGetValue(string name, out string? value) { value = null; return false; }
public override bool TryGetValues(string name, out IEnumerable<string>? values) { values = null; return false; }
}
// Minimal PipelineResponse implementation
internal class TestPipelineResponse : PipelineResponse
{
private Stream? _contentStream = Stream.Null;
private readonly EmptyPipelineResponseHeaders _headers = new EmptyPipelineResponseHeaders();
public override int Status => 200;
public override string ReasonPhrase => "OK";
public override Stream? ContentStream { get => _contentStream; set => _contentStream = value; }
protected override PipelineResponseHeaders HeadersCore => _headers;
public override BinaryData Content => BinaryData.FromBytes(Array.Empty<byte>());
public override BinaryData BufferContent(CancellationToken cancellationToken = default) => Content;
public override ValueTask<BinaryData> BufferContentAsync(CancellationToken cancellationToken = default) => ValueTask.FromResult(Content);
public override void Dispose() { _contentStream?.Dispose(); }
}
[TestMethod]
public void TestInitialization()
{
Assert.IsNotNull(_database);
}
[TestMethod]
public async Task Test_SaveLoad_01()
{
var filename = "openai_test_saveload_01.b59vdb";
#pragma warning disable CS8604 // Possible null reference argument.
await _database.SaveToFileAsync(filename);
#pragma warning restore CS8604 // Possible null reference argument.
await _database.LoadFromFileAsync(filename);
}
[TestMethod]
public async Task Test_SaveLoad_TestIds_01()
{
_database.AddText("Sample text for testing IDs.", "111");
_database.AddText("Another sample text for testing IDs.", "222");
var results = _database.Search("testing IDs");
Assert.AreEqual(2, results.Texts.Count());
var filename = "openai_test_saveload_testids_01.b59vdb";
#pragma warning disable CS8604 // Possible null reference argument.
await _database.SaveToFileAsync(filename);
#pragma warning restore CS8604 // Possible null reference argument.
await _database.LoadFromFileAsync(filename);
_database.AddText("A new text after loading to check ID assignment.", "333");
var newResults = _database.Search("testing IDs");
Assert.AreEqual(3, newResults.Texts.Count());
var texts = newResults.Texts.OrderBy(x => x.Metadata).ToArray();
Assert.AreEqual("111", texts[0].Metadata);
Assert.AreEqual("222", texts[1].Metadata);
Assert.AreEqual("333", texts[2].Metadata);
}
[TestMethod]
public async Task Test_SaveLoad_TestIds_02()
{
_database.AddText("Sample text for testing IDs.", "111");
_database.AddText("Another sample text for testing IDs.", "222");
var results = _database.Search("testing IDs");
Assert.AreEqual(2, results.Texts.Count());
var filename = "openai_test_saveload_testids_02.b59vdb";
#pragma warning disable CS8604 // Possible null reference argument.
await _database.SaveToFileAsync(filename);
#pragma warning restore CS8604 // Possible null reference argument.
var newdb = new BasicOpenAIMemoryVectorDatabase(_mockEmbeddingClient.Object);
await newdb.LoadFromFileAsync(filename);
newdb.AddText("A new text after loading to check ID assignment.", "333");
var newResults = newdb.Search("testing IDs");
Assert.AreEqual(3, newResults.Texts.Count());
var texts = newResults.Texts.OrderBy(x => x.Metadata).ToArray();
Assert.AreEqual("111", texts[0].Metadata);
Assert.AreEqual("222", texts[1].Metadata);
Assert.AreEqual("333", texts[2].Metadata);
}
}
}