Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
frankhaugen committed Jan 25, 2024
1 parent 49fa3a9 commit 952e5cf
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 218 deletions.
158 changes: 83 additions & 75 deletions src/Testcontainers.Ollama/OllamaBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,88 +1,96 @@
namespace Testcontainers.Ollama;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class OllamaBuilder : ContainerBuilder<OllamaBuilder, OllamaContainer, OllamaConfiguration>
namespace Testcontainers.Ollama
{
/// <summary>
/// Initializes a new instance of the <see cref="OllamaBuilder" /> class.
/// </summary>
public OllamaBuilder()
: this(new OllamaConfiguration())
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class OllamaBuilder : ContainerBuilder<OllamaBuilder, OllamaContainer, OllamaConfiguration>
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}
/// <summary>
/// Initializes a new instance of the <see cref="OllamaBuilder" /> class.
/// </summary>
public OllamaBuilder()
: this(new OllamaConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

/// <summary>
/// Initializes a new instance of the <see cref="OllamaBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private OllamaBuilder(OllamaConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}
/// <summary>
/// Initializes a new instance of the <see cref="OllamaBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private OllamaBuilder(OllamaConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}

/// <inheritdoc />
protected override OllamaConfiguration DockerResourceConfiguration { get; }
/// <inheritdoc />
protected override OllamaConfiguration DockerResourceConfiguration { get; }

/// <summary>
/// Sets the Testcontainers.Ollama config.
/// </summary>
/// <param name="config">The Testcontainers.Ollama config.</param>
/// <returns>A configured instance of <see cref="OllamaBuilder" />.</returns>
public OllamaBuilder OllamaConfig(OllamaConfiguration config)
{
return Merge(DockerResourceConfiguration, config);
}
/// <summary>
/// Sets the Testcontainers.Ollama config.
/// </summary>
/// <param name="config">The Testcontainers.Ollama config.</param>
/// <returns>A configured instance of <see cref="OllamaBuilder" />.</returns>
public OllamaBuilder OllamaConfig(OllamaConfiguration config)
{
return Merge(DockerResourceConfiguration, config);
}

/// <inheritdoc />
public override OllamaContainer Build()
{
Validate();
return new OllamaContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}
/// <inheritdoc />
public override OllamaContainer Build()
{
Validate();
return new OllamaContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}

/// <inheritdoc />
protected override void Validate()
{
Guard.Argument(DockerResourceConfiguration.Port, nameof(DockerResourceConfiguration.Port)).ThrowIf(info => info.Value is < 1 or > 65535, info => new ArgumentOutOfRangeException(info.Name, info.Value, $"The port must be between 1 and 65535."));
Guard.Argument(DockerResourceConfiguration.ModelName, nameof(DockerResourceConfiguration.ModelName)).NotNull().NotEmpty();
Guard.Argument(DockerResourceConfiguration.ImageName, nameof(DockerResourceConfiguration.ImageName)).NotNull().NotEmpty();
Guard.Argument(DockerResourceConfiguration.HostName, nameof(DockerResourceConfiguration.HostName)).NotNull().NotEmpty();
Guard.Argument(DockerResourceConfiguration.Schema, nameof(DockerResourceConfiguration.Schema)).NotNull().NotEmpty();

base.Validate();
}
/// <inheritdoc />
protected override void Validate()
{
Guard.Argument(DockerResourceConfiguration.ModelName, nameof(DockerResourceConfiguration.ModelName)).NotNull().NotEmpty();
base.Validate();
}

/// <inheritdoc />
protected override OllamaBuilder Init()
{
return base.Init()
.WithName("ollama-container")
.WithImage(new DockerImage(DockerResourceConfiguration.ImageName))
.WithHostname(DockerResourceConfiguration.HostName)
.WithPortBinding(DockerResourceConfiguration.Port, DockerResourceConfiguration.Port)
.WithVolumeMount("ollama", "/root/.ollama")
.WithExposedPort(DockerResourceConfiguration.Port)
;
}
/// <inheritdoc />
protected override OllamaBuilder Init()
{
return base.Init()
.WithImage(new DockerImage(OllamaConfiguration.ImageName))
.WithPortBinding(OllamaConfiguration.DefaultPort, true)
.WithVolumeMount(OllamaConfiguration.DefaultVolumeName, OllamaConfiguration.DefaultVolumePath)
;
}

/// <inheritdoc />
protected override OllamaBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new OllamaConfiguration(resourceConfiguration));
}
/// <inheritdoc />
protected override OllamaBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new OllamaConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override OllamaBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new OllamaConfiguration(resourceConfiguration));
}
/// <inheritdoc />
protected override OllamaBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new OllamaConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override OllamaBuilder Merge(OllamaConfiguration oldValue, OllamaConfiguration newValue)
{
return new OllamaBuilder(new OllamaConfiguration(oldValue, newValue));
/// <inheritdoc />
protected override OllamaBuilder Merge(OllamaConfiguration oldValue, OllamaConfiguration newValue)
{
return new OllamaBuilder(new OllamaConfiguration(oldValue, newValue));
}

/// <summary>
/// Sets the name of the model to run.
/// </summary>
/// <param name="name">The name of the model to run.</param>
/// <returns>A configured instance of <see cref="OllamaBuilder" />.</returns>
/// <exception cref="ArgumentNullException">The name of the model to run is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">The name of the model to run is empty.</exception>
/// <remarks>
/// The name of the model to run is required.
/// </remarks>
public OllamaBuilder WithModelName(string name)
{
return Merge(DockerResourceConfiguration, new OllamaConfiguration(DockerResourceConfiguration) {ModelName = name});
}
}
}
142 changes: 76 additions & 66 deletions src/Testcontainers.Ollama/OllamaConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,75 +1,85 @@
namespace Testcontainers.Ollama;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class OllamaConfiguration : ContainerConfiguration
namespace Testcontainers.Ollama
{
/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// </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, string hostName = null, int? port = null)
/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class OllamaConfiguration : ContainerConfiguration
{
ModelName = modelName;
Schema = schema;
HostName = hostName;
Port = port ?? Port;
}
/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// </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)
{
ModelName = modelName;
}

/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public OllamaConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}
/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public OllamaConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public OllamaConfiguration(IContainerConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}
/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public OllamaConfiguration(IContainerConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public OllamaConfiguration(OllamaConfiguration resourceConfiguration)
: this(new OllamaConfiguration(), resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}
/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public OllamaConfiguration(OllamaConfiguration resourceConfiguration)
: this(new OllamaConfiguration(), resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public OllamaConfiguration(OllamaConfiguration oldValue, OllamaConfiguration newValue)
: base(oldValue, newValue)
{
ModelName = BuildConfiguration.Combine(oldValue.ModelName, newValue.ModelName);
Schema = BuildConfiguration.Combine(oldValue.Schema, newValue.Schema);
HostName = BuildConfiguration.Combine(oldValue.HostName, newValue.HostName);
Port = BuildConfiguration.Combine(oldValue.Port, newValue.Port);
}
/// <summary>
/// Initializes a new instance of the <see cref="OllamaConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public OllamaConfiguration(OllamaConfiguration oldValue, OllamaConfiguration newValue)
: base(oldValue, newValue)
{
ModelName = BuildConfiguration.Combine(oldValue.ModelName, newValue.ModelName);
}

public string ModelName { get; set; } = OllamaModels.Llama2;
public string Schema { get; set; } = "http";
public string HostName { get; set; } = "localhost";
public int Port { get; set; } = 11434;
/// <summary>
/// Name of the model to use.
/// </summary>
public string ModelName { get; set; } = OllamaModels.Llama2;

/// <summary>
/// Gets the name of the Docker image to use.
/// </summary>
public string ImageName { get; } = "ollama/ollama:latest";
/// <summary>
/// Gets the default port of the Ollama API.
/// </summary>
public const int DefaultPort = 11434;

/// <summary>
/// Default image name.
/// </summary>
public const string ImageName = "ollama/ollama:latest";

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

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

0 comments on commit 952e5cf

Please sign in to comment.