-
-
Notifications
You must be signed in to change notification settings - Fork 301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Frank/ollama module #1099
Open
frankhaugen
wants to merge
11
commits into
testcontainers:develop
Choose a base branch
from
frankhaugen:frank/ollama-module
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Frank/ollama module #1099
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
85ab9b4
Initial create the module from template
frankhaugen 8f548e4
Added test project for Ollama module
frankhaugen fa48ecb
Correct any "wrongness" for setting up project and test
frankhaugen 49fa3a9
Added the nitty-gritty of the module
frankhaugen 952e5cf
Cleanup
frankhaugen 75a58a0
Merge branch 'develop' into frank/ollama-module
frankhaugen d1c1aae
#1097 Update Ollama configuration and remove test helpers
frankhaugen daab7ae
Merge remote-tracking branch 'origin/frank/ollama-module' into frank/…
frankhaugen 57461bc
Merge branch 'develop' into frank/ollama-module
frankhaugen ad1e944
Merge branch 'develop' into frank/ollama-module
frankhaugen 6a0f096
Merge branch 'develop' into frank/ollama-module
HofmeisterAn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
namespace Testcontainers.Ollama | ||
{ | ||
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
[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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Models are just like images and should be provided as a string rather than having an specific type. It will also reduce the amount of PRs just to keep it up-to-date |
||
|
||
/// <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; | ||
} | ||
|
||
/// <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); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override OllamaContainer Build() | ||
{ | ||
Validate(); | ||
return new OllamaContainer(DockerResourceConfiguration, TestcontainersSettings.Logger); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Validate() | ||
{ | ||
Guard.Argument(DockerResourceConfiguration.ModelName, nameof(DockerResourceConfiguration.ModelName)).NotNull().NotEmpty(); | ||
base.Validate(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override OllamaBuilder Init() | ||
{ | ||
return base.Init() | ||
.WithImage(new DockerImage(OllamaImage)) | ||
.WithPortBinding(DefaultPort, true) | ||
.WithVolumeMount(DefaultVolumeName, DefaultVolumePath) | ||
; | ||
} | ||
|
||
/// <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 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, new OllamaConfiguration(modelName: name))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
namespace Testcontainers.Ollama | ||
{ | ||
/// <inheritdoc cref="ContainerConfiguration" /> | ||
[PublicAPI] | ||
public sealed class OllamaConfiguration : ContainerConfiguration | ||
{ | ||
/// <summary> | ||
/// The OllamaConfiguration class represents the configuration for an Ollama container. | ||
/// </summary> | ||
public OllamaConfiguration(string modelName = null, string volumePath = null, string volumeName = null, int? port = null) | ||
{ | ||
ModelName = modelName ?? string.Empty; | ||
VolumePath = volumePath ?? OllamaBuilder.DefaultVolumePath; | ||
VolumeName = volumeName ?? OllamaBuilder.DefaultVolumeName; | ||
Port = port ?? OllamaBuilder.DefaultPort; | ||
} | ||
|
||
/// <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(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); | ||
VolumePath = BuildConfiguration.Combine(oldValue.VolumePath, newValue.VolumePath); | ||
VolumeName = BuildConfiguration.Combine(oldValue.VolumeName, newValue.VolumeName); | ||
Port = BuildConfiguration.Combine(oldValue.Port, newValue.Port); | ||
} | ||
|
||
/// <summary> | ||
/// Represents the configuration for the Ollama container. | ||
/// </summary> | ||
public string ModelName { get; set; } | ||
|
||
/// <summary> | ||
/// The OllamaConfiguration class represents the configuration for an Ollama container. | ||
/// </summary> | ||
public string VolumePath { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the volume associated with the Ollama container. | ||
/// </summary> | ||
public string VolumeName { get; set; } | ||
|
||
/// <summary> | ||
/// The <see cref="Port"/> class represents the configuration for an Ollama container port. | ||
/// </summary> | ||
public int Port { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Threading; | ||
|
||
namespace Testcontainers.Ollama | ||
{ | ||
/// <inheritdoc cref="DockerContainer" /> | ||
[PublicAPI] | ||
public sealed class OllamaContainer : DockerContainer | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="OllamaContainer" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The container configuration.</param> | ||
/// <param name="logger">The logger.</param> | ||
public OllamaContainer(OllamaConfiguration configuration, ILogger logger) | ||
: base(configuration, logger) | ||
{ | ||
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 Task Run(string modelName, CancellationToken ct = default) | ||
{ | ||
ModelName = modelName; | ||
if (State!= TestcontainersStates.Created && State != TestcontainersStates.Running) { | ||
ThrowIfResourceNotFound(); | ||
} | ||
|
||
return ExecAsync(new List<string>() { | ||
"ollama", "run", ModelName, | ||
}, 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(OllamaBuilder.DefaultPort)}/api"; | ||
|
||
/// <summary> | ||
/// Gets the name of the Docker image to use. | ||
/// </summary> | ||
public string ImageName { get; } | ||
|
||
/// <summary> | ||
/// Gets the name of the model to run. | ||
/// </summary> | ||
public string ModelName { get; private set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
namespace Testcontainers.Ollama | ||
{ | ||
/// <summary> | ||
/// A selection of OLLAMA models from the readme. | ||
/// </summary> | ||
/// <remarks> | ||
/// See: https://github.com/ollama/ollama?tab=readme-ov-file#model-library | ||
/// </remarks> | ||
public static class OllamaModels | ||
{ | ||
/// <summary> | ||
/// Llama 2: 7B parameters, Size: 3.8GB, Command: ollama run llama2 | ||
/// </summary> | ||
public const string Llama2 = "llama2"; | ||
|
||
/// <summary> | ||
/// Mistral: 7B parameters, Size: 4.1GB, Command: ollama run mistral | ||
/// </summary> | ||
public const string Mistral = "mistral"; | ||
|
||
/// <summary> | ||
/// Dolphin Phi: 2.7B parameters, Size: 1.6GB, Command: ollama run dolphin-phi | ||
/// </summary> | ||
public const string DolphinPhi = "dolphin-phi"; | ||
|
||
/// <summary> | ||
/// Phi-2: 2.7B parameters, Size: 1.7GB, Command: ollama run phi | ||
/// </summary> | ||
public const string Phi2 = "phi"; | ||
|
||
/// <summary> | ||
/// Neural Chat: 7B parameters, Size: 4.1GB, Command: ollama run neural-chat | ||
/// </summary> | ||
public const string NeuralChat = "neural-chat"; | ||
|
||
/// <summary> | ||
/// Starling: 7B parameters, Size: 4.1GB, Command: ollama run starling-lm | ||
/// </summary> | ||
public const string Starling = "starling-lm"; | ||
|
||
/// <summary> | ||
/// Code Llama: 7B parameters, Size: 3.8GB, Command: ollama run codellama | ||
/// </summary> | ||
public const string CodeLlama = "codellama"; | ||
|
||
/// <summary> | ||
/// Llama 2 Uncensored: 7B parameters, Size: 3.8GB, Command: ollama run llama2-uncensored | ||
/// </summary> | ||
public const string Llama2Uncensored = "llama2-uncensored"; | ||
|
||
/// <summary> | ||
/// Llama 2 13B: 13B parameters, Size: 7.3GB, Command: ollama run llama2:13b | ||
/// </summary> | ||
public const string Llama213B = "llama2:13b"; | ||
|
||
/// <summary> | ||
/// Llama 2 70B: 70B parameters, Size: 39GB, Command: ollama run llama2:70b | ||
/// </summary> | ||
public const string Llama270B = "llama2:70b"; | ||
|
||
/// <summary> | ||
/// Orca Mini: 3B parameters, Size: 1.9GB, Command: ollama run orca-mini | ||
/// </summary> | ||
public const string OrcaMini = "orca-mini"; | ||
|
||
/// <summary> | ||
/// Vicuna: 7B parameters, Size: 3.8GB, Command: ollama run vicuna | ||
/// </summary> | ||
public const string Vicuna = "vicuna"; | ||
|
||
/// <summary> | ||
/// LLaVA: 7B parameters, Size: 4.5GB, Command: ollama run llava | ||
/// </summary> | ||
public const string LLaVA = "llava"; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Volume mounting should be optional rather than the default, at least for now. We can align along with Java and Go implementation.