-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModelIntegrationTests.cs
More file actions
72 lines (59 loc) · 2.59 KB
/
ModelIntegrationTests.cs
File metadata and controls
72 lines (59 loc) · 2.59 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
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
using MLXSharp;
using MLXSharp.Backends;
using Xunit;
namespace MLXSharp.Tests;
public sealed class ModelIntegrationTests
{
[Fact]
public async Task NativeBackendAnswersSimpleMathAsync()
{
TestEnvironment.EnsureInitialized();
EnsureAssets();
var options = CreateOptions();
using var backend = MlxNativeBackend.Create(options);
var request = new MlxTextRequest(
new[] { new ChatMessage(ChatRole.User, "Скільки буде 2+2?") },
new ChatOptions { Temperature = 0 });
var result = await backend.GenerateTextAsync(request, CancellationToken.None);
Assert.False(string.IsNullOrWhiteSpace(result.Text));
Assert.Contains("4", result.Text);
}
private static MlxClientOptions CreateOptions()
{
var libraryPath = Environment.GetEnvironmentVariable("MLXSHARP_LIBRARY");
var options = new MlxClientOptions
{
LibraryPath = string.IsNullOrWhiteSpace(libraryPath) ? null : libraryPath,
EnableNativeModelRunner = false,
};
var modelId = Environment.GetEnvironmentVariable("MLXSHARP_HF_MODEL_ID");
if (!string.IsNullOrWhiteSpace(modelId))
{
options.ChatModelId = modelId;
}
var modelDirectory = Environment.GetEnvironmentVariable("MLXSHARP_MODEL_PATH");
if (!string.IsNullOrWhiteSpace(modelDirectory))
{
options.NativeModelDirectory = modelDirectory;
}
var tokenizerPath = Environment.GetEnvironmentVariable("MLXSHARP_TOKENIZER_PATH");
if (!string.IsNullOrWhiteSpace(tokenizerPath))
{
options.TokenizerPath = tokenizerPath;
}
return options;
}
private static void EnsureAssets()
{
var modelPath = Environment.GetEnvironmentVariable("MLXSHARP_MODEL_PATH");
Assert.False(string.IsNullOrWhiteSpace(modelPath), "Native model bundle path is not configured. Set MLXSHARP_MODEL_PATH to a valid directory.");
Assert.True(System.IO.Directory.Exists(modelPath), $"Native model bundle not found at '{modelPath}'.");
var library = Environment.GetEnvironmentVariable("MLXSHARP_LIBRARY");
Assert.False(string.IsNullOrWhiteSpace(library), "Native libmlxsharp library is not configured. Set MLXSHARP_LIBRARY to the staged native library that ships with the official MLXSharp release.");
Assert.True(System.IO.File.Exists(library), $"Native libmlxsharp library not found at '{library}'.");
}
}