Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
dclipca committed Jan 18, 2025
1 parent 0eed711 commit d1e14e2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 72 deletions.
64 changes: 22 additions & 42 deletions SpongeEngine.LMStudioSharp.Tests/Common/LmStudioTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,29 @@ namespace SpongeEngine.LMStudioSharp.Tests.Common
{
public abstract class LmStudioTestBase : IDisposable, IAsyncLifetime
{
protected readonly LmStudioSharpClient Client;
protected readonly ITestOutputHelper Output;
protected readonly LmStudioSharpClient Client;
protected readonly WireMockServer Server;
protected readonly ILogger Logger;
protected readonly string BaseUrl;
protected bool ServerAvailable;
protected Model? DefaultModel;

protected LmStudioTestBase(ITestOutputHelper output)
{
Server = WireMockServer.Start();
BaseUrl = Server.Urls[0];
Logger = LoggerFactory
.Create(builder => builder.AddXUnit(output))
.CreateLogger(GetType());



Output = output;
Logger = LoggerFactory
.Create(builder => builder.AddXUnit(output))
.CreateLogger(GetType());

Server = WireMockServer.Start();
Client = new LmStudioSharpClient(new LmStudioClientOptions()
{
BaseUrl = BaseUrl,
HttpClient = new HttpClient
{
BaseUrl = Server.Urls[0],
HttpClient = new HttpClient
{
BaseAddress = new Uri(TestConfig.NativeApiBaseUrl),
},
JsonSerializerOptions = new JsonSerializerOptions()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
}
},
Logger = LoggerFactory
.Create(builder => builder.AddXUnit(output))
.CreateLogger(GetType()),
});
}

Expand All @@ -56,33 +45,24 @@ public async Task InitializeAsync()
{
try
{
ServerAvailable = await Client.IsAvailableAsync();
if (ServerAvailable)
{
Output.WriteLine("LM Studio server is available");
Output.WriteLine("LM Studio server is available");

ModelsResponse modelsResponse = await Client.ListModelsAsync();
if (modelsResponse.Data.Any())
{
DefaultModel = new Model
{
Id = modelsResponse.Data[0].Id,
Object = modelsResponse.Data[0].Object,
// Map other properties as needed
};
Output.WriteLine($"Found model: {DefaultModel.Id}");
}
else
ModelsResponse modelsResponse = await Client.ListModelsAsync();
if (modelsResponse.Data.Any())
{
DefaultModel = new Model
{
Output.WriteLine($"modelsResponse: {JsonSerializer.Serialize(modelsResponse)}");
Output.WriteLine("No models available");
throw new SkipException("No models available in LM Studio");
}
Id = modelsResponse.Data[0].Id,
Object = modelsResponse.Data[0].Object,
// Map other properties as needed
};
Output.WriteLine($"Found model: {DefaultModel.Id}");
}
else
{
Output.WriteLine("LM Studio server is not available");
throw new SkipException("LM Studio server is not available");
Output.WriteLine($"modelsResponse: {JsonSerializer.Serialize(modelsResponse)}");
Output.WriteLine("No models available");
throw new SkipException("No models available in LM Studio");
}
}
catch (Exception ex) when (ex is not SkipException)
Expand Down
12 changes: 1 addition & 11 deletions SpongeEngine.LMStudioSharp.Tests/Integration/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ namespace SpongeEngine.LMStudioSharp.Tests.Integration
[Trait("API", "Native")]
public class Tests : LmStudioTestBase
{
public Tests(ITestOutputHelper output) : base(output) { }
public Tests(ITestOutputHelper output) : base(output) {}

[SkippableFact]
[Trait("Category", "Integration")]
public async Task Complete_WithSimplePrompt_ShouldReturnResponse()
{
Skip.If(!ServerAvailable, "LM Studio server is not available");

// Arrange
var request = new CompletionRequest
{
Expand All @@ -42,8 +40,6 @@ public async Task Complete_WithSimplePrompt_ShouldReturnResponse()
[Trait("Category", "Integration")]
public async Task StreamCompletion_ShouldStreamTokens()
{
Skip.If(!ServerAvailable, "LM Studio server is not available");

var request = new CompletionRequest
{
Model = "test-model",
Expand Down Expand Up @@ -89,8 +85,6 @@ public async Task StreamCompletion_ShouldStreamTokens()
[Trait("Category", "Integration")]
public async Task Complete_WithStopSequence_ShouldReturnResponse()
{
Skip.If(!ServerAvailable, "LM Studio server is not available");

// Arrange
var request = new CompletionRequest
{
Expand All @@ -115,8 +109,6 @@ public async Task Complete_WithStopSequence_ShouldReturnResponse()
[Trait("Category", "Integration")]
public async Task Complete_WithDifferentTemperatures_ShouldWork()
{
Skip.If(!ServerAvailable, "LM Studio server is not available");

// Test various temperature settings
var temperatures = new[] { 0.1f, 0.7f, 1.5f };
foreach (var temp in temperatures)
Expand Down Expand Up @@ -148,8 +140,6 @@ public async Task Complete_WithDifferentTemperatures_ShouldWork()
[Trait("Category", "Integration")]
public async Task ChatComplete_ShouldReturnResponse()
{
Skip.If(!ServerAvailable, "LM Studio server is not available");

// Arrange
var request = new ChatRequest
{
Expand Down
7 changes: 1 addition & 6 deletions SpongeEngine.LMStudioSharp.Tests/Unit/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ namespace SpongeEngine.LMStudioSharp.Tests.Unit
{
public class Tests : LmStudioTestBase
{
public LmStudioSharpClient Client { get; }

public Tests(ITestOutputHelper output) : base(output)
{
Client = new LmStudioSharpClient(new LmStudioClientOptions());
}
public Tests(ITestOutputHelper output) : base(output) {}

[Fact]
public async Task ListModelsAsync_ShouldReturnModels()
Expand Down
13 changes: 0 additions & 13 deletions SpongeEngine.LMStudioSharp/LmStudioSharpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,6 @@ public async IAsyncEnumerable<string> StreamChatAsync(ChatRequest request, [Enum
}
}

public async Task<bool> IsAvailableAsync(CancellationToken cancellationToken = default)
{
try
{
var response = await Options.HttpClient.GetAsync(MODELS_ENDPOINT, cancellationToken);
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}

private async Task<HttpResponseMessage> PostAsJsonAsync<T>(string endpoint, T content, CancellationToken cancellationToken)
{
var json = JsonSerializer.Serialize(content, Options.JsonSerializerOptions);
Expand Down

0 comments on commit d1e14e2

Please sign in to comment.