Skip to content

Commit

Permalink
testcontainers#1097 Update Ollama configuration and remove test helpers
Browse files Browse the repository at this point in the history
This commit updates the Ollama configuration to allow more customization options and removes unnecessary test helpers. The OllamaConfiguration class was refactored to provide more configurable parameters such as the VolumePath and VolumeName. Additionally, the TestOutputHelperExtensions and TestOutputLogger classes were deleted as they were not providing any significant value.
  • Loading branch information
frankhaugen committed Feb 1, 2024
1 parent 952e5cf commit d1c1aae
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 124 deletions.
33 changes: 29 additions & 4 deletions src/Testcontainers.Ollama/OllamaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ namespace Testcontainers.Ollama
[PublicAPI]
public sealed class OllamaBuilder : ContainerBuilder<OllamaBuilder, OllamaContainer, OllamaConfiguration>
{
/// <summary>
/// Gets the default port of the Ollama API.
/// </summary>
public const int DefaultPort = 11434;

/// <summary>
/// Default image name and version tag.
/// </summary>
public const string OllamaImage = "ollama/ollama:0.1.22";

/// <summary>
/// Default volume path.
/// </summary>
public const string DefaultVolumePath = "/root/.ollama";

/// <summary>
/// Default volume name.
/// </summary>
public const string DefaultVolumeName = "ollama-volume";

/// <summary>
/// The default model name for the OllamaBuilder.
/// </summary>
public const string DefaultModelName = OllamaModels.Llama2;

/// <summary>
/// Initializes a new instance of the <see cref="OllamaBuilder" /> class.
/// </summary>
Expand Down Expand Up @@ -54,9 +79,9 @@ protected override void Validate()
protected override OllamaBuilder Init()
{
return base.Init()
.WithImage(new DockerImage(OllamaConfiguration.ImageName))
.WithPortBinding(OllamaConfiguration.DefaultPort, true)
.WithVolumeMount(OllamaConfiguration.DefaultVolumeName, OllamaConfiguration.DefaultVolumePath)
.WithImage(new DockerImage(OllamaImage))
.WithPortBinding(DefaultPort, true)
.WithVolumeMount(DefaultVolumeName, DefaultVolumePath)
;
}

Expand Down Expand Up @@ -90,7 +115,7 @@ protected override OllamaBuilder Merge(OllamaConfiguration oldValue, OllamaConfi
/// </remarks>
public OllamaBuilder WithModelName(string name)
{
return Merge(DockerResourceConfiguration, new OllamaConfiguration(DockerResourceConfiguration) {ModelName = name});
return Merge(DockerResourceConfiguration, new OllamaConfiguration(DockerResourceConfiguration, new OllamaConfiguration(modelName: name)));
}
}
}
45 changes: 21 additions & 24 deletions src/Testcontainers.Ollama/OllamaConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ namespace Testcontainers.Ollama
public sealed class OllamaConfiguration : ContainerConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// The OllamaConfiguration class represents the configuration for an Ollama container.
/// </summary>
/// <param name="modelName"></param>
/// <param name="schema"></param>
/// <param name="hostName"></param>
/// <param name="port"></param>
public OllamaConfiguration(string modelName = null, string schema = null, int? port = null)
public OllamaConfiguration(string modelName = null, string volumePath = null, string volumeName = null, int? port = null)
{
ModelName = modelName;
ModelName = modelName ?? string.Empty;
VolumePath = volumePath ?? OllamaBuilder.DefaultVolumePath;
VolumeName = volumeName ?? OllamaBuilder.DefaultVolumeName;
Port = port ?? OllamaBuilder.DefaultPort;
}

/// <summary>
Expand Down Expand Up @@ -55,31 +54,29 @@ public OllamaConfiguration(OllamaConfiguration oldValue, OllamaConfiguration new
: base(oldValue, newValue)
{
ModelName = BuildConfiguration.Combine(oldValue.ModelName, newValue.ModelName);
VolumePath = BuildConfiguration.Combine(oldValue.VolumePath, newValue.VolumePath);
VolumeName = BuildConfiguration.Combine(oldValue.VolumeName, newValue.VolumeName);
Port = BuildConfiguration.Combine(oldValue.Port, newValue.Port);
}

/// <summary>
/// Name of the model to use.
/// </summary>
public string ModelName { get; set; } = OllamaModels.Llama2;


/// <summary>
/// Gets the default port of the Ollama API.
/// Represents the configuration for the Ollama container.
/// </summary>
public const int DefaultPort = 11434;
public string ModelName { get; set; }

/// <summary>
/// Default image name.
/// The OllamaConfiguration class represents the configuration for an Ollama container.
/// </summary>
public const string ImageName = "ollama/ollama:latest";
public string VolumePath { get; set; }

/// <summary>
/// Default volume path.
/// Gets or sets the name of the volume associated with the Ollama container.
/// </summary>
public const string DefaultVolumePath = "/root/.ollama";
public string VolumeName { get; set; }

/// <summary>
/// Default volume name.
/// The <see cref="Port"/> class represents the configuration for an Ollama container port.
/// </summary>
public const string DefaultVolumeName = "ollama-volume";
public int Port { get; set; }
}
}
31 changes: 19 additions & 12 deletions src/Testcontainers.Ollama/OllamaContainer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Threading;

namespace Testcontainers.Ollama
{
/// <inheritdoc cref="DockerContainer" />
Expand All @@ -12,32 +14,37 @@ public sealed class OllamaContainer : DockerContainer
public OllamaContainer(OllamaConfiguration configuration, ILogger logger)
: base(configuration, logger)
{
ModelName = configuration.ModelName;
ImageName = OllamaConfiguration.ImageName;
Configuration = configuration;
}

public OllamaConfiguration Configuration { get; private set; }

public Task Run(CancellationToken ct = default)
{
return Run(Configuration.ModelName, ct);
}

/// <summary>
/// Starts the Ollama container.
/// </summary>
public async Task StartOllamaAsync()
public Task Run(string modelName, CancellationToken ct = default)
{
if (State!= TestcontainersStates.Created && State != TestcontainersStates.Running) {
throw new InvalidOperationException("Cannot start a container that has not been created.");
ModelName = modelName;
if (State!= TestcontainersStates.Created && State != TestcontainersStates.Running) {
ThrowIfResourceNotFound();
}
Task.WaitAll(ExecAsync(new List<string>()
{

return ExecAsync(new List<string>() {
"ollama", "run", ModelName,
}));

await Task.CompletedTask;
}, ct);
}

/// <summary>
/// Gets the base URL of the Ollama API.
/// </summary>
/// <returns>The base URL of the Ollama API.</returns>
/// <example>http://localhost:5000/api</example>
public string GetBaseUrl() => $"http://{Hostname}:{GetMappedPublicPort(OllamaConfiguration.DefaultPort)}/api";
public string GetBaseUrl() => $"http://{Hostname}:{GetMappedPublicPort(OllamaBuilder.DefaultPort)}/api";

/// <summary>
/// Gets the name of the Docker image to use.
Expand All @@ -47,6 +54,6 @@ public async Task StartOllamaAsync()
/// <summary>
/// Gets the name of the model to run.
/// </summary>
public string ModelName { get; }
public string ModelName { get; private set; }
}
}
22 changes: 0 additions & 22 deletions tests/Testcontainers.Commons/TestOutputHelperExtensions.cs

This file was deleted.

49 changes: 0 additions & 49 deletions tests/Testcontainers.Commons/TestOutputLogger.cs

This file was deleted.

1 change: 0 additions & 1 deletion tests/Testcontainers.Commons/Testcontainers.Commons.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0"/>
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj"/>
Expand Down
15 changes: 4 additions & 11 deletions tests/Testcontainers.Ollama.Tests/OllamaContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ namespace Testcontainers.Ollama.Tests
{
public class OllamaContainerTests : IAsyncLifetime
{
private readonly ITestOutputHelper _testOutputHelper;
private OllamaContainer _ollamaContainer;

public OllamaContainerTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

public async Task InitializeAsync()
{
TestcontainersSettings.Logger = new TestOutputLogger(nameof(OllamaContainerTests), _testOutputHelper);
_ollamaContainer = new OllamaBuilder().Build();
_ollamaContainer = new OllamaBuilder()
.OllamaConfig(new OllamaConfiguration(OllamaModels.Llama2))
.Build();
await _ollamaContainer.StartAsync();
await _ollamaContainer.StartOllamaAsync();
await _ollamaContainer.Run();
}

public async Task DisposeAsync()
Expand All @@ -41,8 +36,6 @@ public async Task OllamaContainerReturnsSuccessful()
var response = await client.SendChat(chatRequest, stream => { });
response = response.ToList();

_testOutputHelper.WriteJson(response);

Assert.True(response.Any());
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/Testcontainers.Ollama.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
global using System.Threading.Tasks;
global using DotNet.Testcontainers.Commons;
global using Xunit;
global using Xunit.Abstractions;
global using System.Collections.Generic;
global using System.Linq;
global using DotNet.Testcontainers.Configurations;
Expand Down

0 comments on commit d1c1aae

Please sign in to comment.