|
| 1 | +namespace Aspire.Hosting; |
| 2 | + |
| 3 | +using Aspire.Hosting.Lifecycle; |
| 4 | +using Aspire.Hosting.ApplicationModel; |
| 5 | +using Aspire.Hosting.Ollama; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Provides extension methods for adding Ollama to the application model. |
| 9 | +/// </summary> |
| 10 | +public static class OllamaBuilderExtensions |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Adds an Ollama resource to the application. A container is used for local development. |
| 14 | + /// </summary> |
| 15 | + /// <example> |
| 16 | + /// Use in application host |
| 17 | + /// <code lang="csharp"> |
| 18 | + /// var builder = DistributedApplication.CreateBuilder(args); |
| 19 | + /// |
| 20 | + /// var ollama = builder.AddOllama("ollama"); |
| 21 | + /// var api = builder.AddProject<Projects.Api>("api") |
| 22 | + /// .WithReference(ollama); |
| 23 | + /// |
| 24 | + /// builder.Build().Run(); |
| 25 | + /// </code> |
| 26 | + /// </example> |
| 27 | + /// <remarks> |
| 28 | + /// This version the package defaults to the 0.3.4 tag of the ollama/ollama container image. |
| 29 | + /// The .NET client library uses the http port by default to communicate and this resource exposes that endpoint. |
| 30 | + /// </remarks> |
| 31 | + /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> |
| 32 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency</param> |
| 33 | + /// <param name="enableGpu">Whether to enable GPU support.</param> |
| 34 | + /// <param name="port">The host port of the http endpoint of Ollama resource.</param> |
| 35 | + /// <returns>A reference to the <see cref="IResourceBuilder{OllamaResource}"/>.</returns> |
| 36 | + public static IResourceBuilder<OllamaResource> AddOllama( |
| 37 | + this IDistributedApplicationBuilder builder, |
| 38 | + string name, |
| 39 | + bool enableGpu = false, |
| 40 | + int? port = null |
| 41 | + ) |
| 42 | + { |
| 43 | + builder.Services.TryAddLifecycleHook<OllamaLifecycleHook>(); |
| 44 | + |
| 45 | + var ollama = new OllamaResource(name); |
| 46 | + |
| 47 | + var resource = builder |
| 48 | + .AddResource(ollama) |
| 49 | + .WithImage(OllamaContainerImageTags.Image, OllamaContainerImageTags.Tag) |
| 50 | + .WithImageRegistry(OllamaContainerImageTags.Registry) |
| 51 | + .WithHttpEndpoint(port: port, targetPort: 11434, OllamaResource.PrimaryEndpointName) |
| 52 | + .ExcludeFromManifest() |
| 53 | + .PublishAsContainer(); |
| 54 | + |
| 55 | + if (enableGpu) |
| 56 | + { |
| 57 | + resource = resource.WithContainerRuntimeArgs("--gpus=all"); |
| 58 | + } |
| 59 | + |
| 60 | + return resource; |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Adds a model to the Ollama resource. |
| 65 | + /// </summary> |
| 66 | + /// <example> |
| 67 | + /// Use in application host |
| 68 | + /// <code lang="csharp"> |
| 69 | + /// var builder = DistributedApplication.CreateBuilder(args); |
| 70 | + /// |
| 71 | + /// var ollama = builder.AddOllama("ollama"); |
| 72 | + /// .AddModel("phi3") |
| 73 | + /// .WithDataVolume("ollama"); |
| 74 | + /// |
| 75 | + /// var api = builder.AddProject<Projects.Api>("api") |
| 76 | + /// .WithReference(ollama); |
| 77 | + /// |
| 78 | + /// builder.Build().Run(); |
| 79 | + /// </code> |
| 80 | + /// </example> |
| 81 | + /// <param name="builder">The Ollama resource builder.</param> |
| 82 | + /// <param name="modelName">The name of the model.</param> |
| 83 | + /// <remarks>This method will attempt to pull/download the model into the Ollama instance.</remarks> |
| 84 | + /// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns> |
| 85 | + public static IResourceBuilder<OllamaResource> AddModel( |
| 86 | + this IResourceBuilder<OllamaResource> builder, |
| 87 | + string modelName |
| 88 | + ) |
| 89 | + { |
| 90 | + builder.Resource.AddModel(modelName); |
| 91 | + return builder; |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Adds a named volume for the data folder to a Ollama container resource. |
| 96 | + /// </summary> |
| 97 | + /// <param name="builder">The resource builder.</param> |
| 98 | + /// <param name="name">The name of the volume. Defaults to an auto-generated name based on the resource name.</param> |
| 99 | + /// <param name="isReadOnly">A flag that indicates if this is a read-only volume.</param> |
| 100 | + /// <returns>The <see cref="IResourceBuilder{T}"/>.</returns> |
| 101 | + public static IResourceBuilder<OllamaResource> WithDataVolume( |
| 102 | + this IResourceBuilder<OllamaResource> builder, |
| 103 | + string? name = null, |
| 104 | + bool isReadOnly = false |
| 105 | + ) => builder.WithVolume(name ?? "ollama-aspire-data", "/root/.ollama", isReadOnly); |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Adds a bind mount for the data folder to an Ollama container resource. |
| 109 | + /// </summary> |
| 110 | + /// <param name="builder">The resource builder.</param> |
| 111 | + /// <param name="source">The source directory on the host to mount into the container.</param> |
| 112 | + /// <param name="isReadOnly">A flag that indicates if this is a read-only mount.</param> |
| 113 | + /// <returns>The <see cref="IResourceBuilder{T}"/>.</returns> |
| 114 | + public static IResourceBuilder<OllamaResource> WithDataBindMount( |
| 115 | + this IResourceBuilder<OllamaResource> builder, |
| 116 | + string source, |
| 117 | + bool isReadOnly = false |
| 118 | + ) => builder.WithBindMount(source, "/root/.ollama", isReadOnly); |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Adds an administration web UI Ollama to the application model using Attu. This version the package defaults to the main tag of the Open WebUI container image |
| 122 | + /// </summary> |
| 123 | + /// <example> |
| 124 | + /// Use in application host with an Ollama resource |
| 125 | + /// <code lang="csharp"> |
| 126 | + /// var builder = DistributedApplication.CreateBuilder(args); |
| 127 | + /// |
| 128 | + /// var ollama = builder.AddOllama("ollama") |
| 129 | + /// .WithOpenWebUI(); |
| 130 | + /// var api = builder.AddProject<Projects.Api>("api") |
| 131 | + /// .WithReference(ollama); |
| 132 | + /// |
| 133 | + /// builder.Build().Run(); |
| 134 | + /// </code> |
| 135 | + /// </example> |
| 136 | + /// <param name="builder">The Ollama resource builder.</param> |
| 137 | + /// <param name="configureContainer">Configuration callback for Open WebUI container resource.</param> |
| 138 | + /// <param name="containerName">The name of the container (Optional).</param> |
| 139 | + /// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns> |
| 140 | + /// <remarks>See https://openwebui.com for more information about Open WebUI</remarks> |
| 141 | + public static IResourceBuilder<T> WithOpenWebUI<T>( |
| 142 | + this IResourceBuilder<T> builder, |
| 143 | + Action<IResourceBuilder<OpenWebUIResource>>? configureContainer = null, |
| 144 | + string? containerName = null |
| 145 | + ) |
| 146 | + where T : OllamaResource |
| 147 | + { |
| 148 | + containerName ??= $"{builder.Resource.Name}-openwebui"; |
| 149 | + |
| 150 | + var openWebUI = new OpenWebUIResource(containerName); |
| 151 | + var resourceBuilder = builder |
| 152 | + .ApplicationBuilder.AddResource(openWebUI) |
| 153 | + .WithImage(OllamaContainerImageTags.OpenWebUIImage, OllamaContainerImageTags.OpenWebUITag) |
| 154 | + .WithImageRegistry(OllamaContainerImageTags.OpenWebUIRegistry) |
| 155 | + .WithHttpEndpoint(targetPort: 8080, name: "http") |
| 156 | + .WithVolume("open-webui", "/app/backend/data") |
| 157 | + .WithEnvironment(context => ConfigureOpenWebUIContainer(context, builder.Resource)) |
| 158 | + .ExcludeFromManifest(); |
| 159 | + |
| 160 | + configureContainer?.Invoke(resourceBuilder); |
| 161 | + |
| 162 | + return builder; |
| 163 | + } |
| 164 | + |
| 165 | + private static void ConfigureOpenWebUIContainer(EnvironmentCallbackContext context, OllamaResource resource) |
| 166 | + { |
| 167 | + context.EnvironmentVariables.Add("ENABLE_SIGNUP", "false"); |
| 168 | + context.EnvironmentVariables.Add("ENABLE_COMMUNITY_SHARING", "false"); // by default don't enable sharing |
| 169 | + context.EnvironmentVariables.Add("WEBUI_AUTH", "false"); // https://docs.openwebui.com/#quick-start-with-docker--recommended |
| 170 | + context.EnvironmentVariables.Add( |
| 171 | + "OLLAMA_BASE_URL", |
| 172 | + $"{resource.PrimaryEndpoint.Scheme}://{resource.PrimaryEndpoint.ContainerHost}:{resource.PrimaryEndpoint.Port}" |
| 173 | + ); |
| 174 | + } |
| 175 | +} |
0 commit comments