-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathChatTests.cs
More file actions
194 lines (162 loc) · 6.34 KB
/
ChatTests.cs
File metadata and controls
194 lines (162 loc) · 6.34 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using FuzzySharp;
using MaIN.Core.E2ETests.Helpers;
using MaIN.Core.Hub;
using MaIN.Domain.Entities;
using MaIN.Domain.Models;
using MaIN.Domain.Models.Abstract;
namespace MaIN.Core.E2ETests;
[Collection("E2ETests")]
public class ChatTests : IntegrationTestBase
{
public ChatTests() : base()
{
}
[Fact]
public async Task Should_AnswerQuestion_BasicChat()
{
var context = AIHub.Chat().WithModel(Models.Local.Qwen2_5_0_5b);
var result = await context
.WithMessage("Where the hedgehog goes at night?")
.CompleteAsync(interactive: true);
Assert.True(result.Done);
Assert.NotNull(result.Message);
Assert.NotEmpty(result.Message.Content);
}
[Fact]
public async Task Should_AnswerFileSubject_ChatWithFiles()
{
List<string> files = ["./Files/Nicolaus_Copernicus.pdf"];
var result = await AIHub.Chat()
.WithModel(Models.Local.Qwen2_5_0_5b)
.WithMessage("Who is described in the file? Reply with ONLY their full name. No explanation, no punctuation. Example: Isaak Newton")
.WithMemoryParams(new MemoryParams { AnswerTokens = 10 })
.WithFiles(files)
.CompleteAsync();
Assert.True(result.Done);
Assert.NotNull(result.Message);
Assert.NotEmpty(result.Message.Content);
var ratio = Fuzz.PartialRatio("nicolaus copernicus", result.Message.Content.ToLowerInvariant());
Assert.True(ratio > 50,
$"""
Fuzzy match failed!
Expected > 50, but got {ratio}.
Expected: 'nicolaus copernicus'
Actual: '{result.Message.Content}'
""");
}
[Fact]
public async Task Should_AnswerQuestion_FromExistingChat()
{
var result = AIHub.Chat()
.WithModel(Models.Local.Qwen2_5_0_5b);
await result.WithMessage("What do you think about math theories?")
.WithMemoryParams(new MemoryParams { AnswerTokens = 10 })
.CompleteAsync();
await result.WithMessage("And about physics?")
.CompleteAsync();
var chatNewContext = await AIHub.Chat().FromExisting(result.GetChatId());
var messages = chatNewContext.GetChatHistory();
Assert.Equal(4, messages.Count);
}
[Fact]
public async Task Should_AnswerGameFromImage_ChatWithImagesWithText()
{
List<string> images = ["./Files/gamex.jpg"];
var expectedAnswer = "call of duty";
var result = await AIHub.Chat()
.WithModel(Models.Local.Llama3_2_3b)
.WithMessage("What is the title of the game? Answer in 3 words.")
.WithMemoryParams(new MemoryParams { AnswerTokens = 10 })
.WithFiles(images)
.CompleteAsync();
Assert.True(result.Done);
Assert.NotNull(result.Message);
Assert.NotEmpty(result.Message.Content);
var ratio = Fuzz.PartialRatio(expectedAnswer, result.Message.Content.ToLowerInvariant());
Assert.True(ratio > 50,
$"""
Fuzzy match failed!
Expected > 50, but got {ratio}.
Expexted: '{expectedAnswer}'
Actual: '{result.Message.Content}'
""");
}
[Fact]
public async Task Should_AnswerAppleFromImage_ChatWithImagesWithVision()
{
List<string> images = ["./Files/apple.jpg"];
var expectedAnswer = "apple";
var result = await AIHub.Chat()
.WithModel(Models.Local.Gemma3_4b)
.WithMessage("What is this fruit? Answer in one word.")
.WithMemoryParams(new MemoryParams { AnswerTokens = 10 })
.WithFiles(images)
.CompleteAsync();
Assert.True(result.Done);
Assert.NotNull(result.Message);
Assert.NotEmpty(result.Message.Content);
var ratio = Fuzz.PartialRatio(expectedAnswer, result.Message.Content.ToLowerInvariant());
Assert.True(ratio > 50,
$"""
Fuzzy match failed!
Expected > 50, but got {ratio}.
Expexted: '{expectedAnswer}'
Actual: '{result.Message.Content}'
""");
}
[Fact(Skip = "Require powerful GPU")]
public async Task Should_GenerateImage_BasedOnPrompt()
{
Assert.True(NetworkHelper.PingHost("127.0.0.1", 5003, 5), "Please make sure ImageGen service is running on port 5003");
const string extension = "png";
var fluxModel = new GenericLocalModel("FLUX.1_Shnell");
ModelRegistry.RegisterOrReplace(fluxModel);
var result = await AIHub.Chat()
.WithModel(fluxModel.Id)
.WithMessage("Generate cat in Rome. Sightseeing, colloseum, ancient builidngs, Italy.")
.CompleteAsync();
if (string.IsNullOrWhiteSpace(extension) || extension.Contains("."))
{
throw new ArgumentException("Invalid file extension");
}
Assert.True(result.Done);
Assert.NotNull(result.Message.Image);
}
[Fact]
public async Task Should_AnswerFileSubject_ChatWithFiles_UsingStreams()
{
List<string> files = ["./Files/Nicolaus_Copernicus.pdf"];
var fileStreams = new List<FileStream>();
foreach (var path in files)
{
if (!File.Exists(path))
{
continue;
}
var fs = new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.Read);
fileStreams.Add(fs);
}
var expectedAnswer = "nicolaus copernicus";
var result = await AIHub.Chat()
.WithModel(Models.Local.Qwen2_5_0_5b)
.WithMessage("Who is described in the file? Reply with ONLY their full name. No explanation, no punctuation. Example: Isaak Newton")
.WithMemoryParams(new MemoryParams { AnswerTokens = 10 })
.WithFiles(fileStreams)
.CompleteAsync();
Assert.True(result.Done);
Assert.NotNull(result.Message);
Assert.NotEmpty(result.Message.Content);
var ratio = Fuzz.PartialRatio(expectedAnswer, result.Message.Content.ToLowerInvariant());
Assert.True(ratio > 50,
$"""
Fuzzy match failed!
Expected > 50, but got {ratio}.
Expected: '{expectedAnswer}'
Actual: '{result.Message.Content}'
""");
}
}