Created
May 9, 2026 05:51
-
-
Save pingkunga/a2badd85dc615ffeecd143bb841dc3aa to your computer and use it in GitHub Desktop.
OllamaApiClientExtensions that use Reflection to modified setting of httpClient
This file contains hidden or 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
| using System.Reflection; | |
| using OllamaSharp; | |
| namespace OllamaSharp.Custom.Extensions; | |
| /// <summary> | |
| /// Extension methods for OllamaApiClient providing enhanced configuration capabilities, | |
| /// specifically focusing on timeout management for long-running LLM operations. | |
| /// </summary> | |
| public static class OllamaApiClientExtensions | |
| { | |
| // Cache the FieldInfo to avoid repeated reflection overhead | |
| private static readonly FieldInfo? HttpClientField = typeof(OllamaApiClient) | |
| .GetField("_client", BindingFlags.NonPublic | BindingFlags.Instance); | |
| /// <summary> | |
| /// Sets the timeout for the OllamaApiClient's underlying HttpClient. | |
| /// </summary> | |
| /// <param name="client">The OllamaApiClient instance.</param> | |
| /// <param name="timeout">The timeout duration to set.</param> | |
| /// <returns>The same OllamaApiClient instance for method chaining.</returns> | |
| /// <exception cref="ArgumentNullException">Thrown when client is null.</exception> | |
| /// <exception cref="ArgumentOutOfRangeException">Thrown when timeout is zero or negative.</exception> | |
| /// <exception cref="InvalidOperationException">Thrown when the internal HttpClient cannot be accessed.</exception> | |
| public static OllamaApiClient SetTimeout(this OllamaApiClient client, TimeSpan timeout) | |
| { | |
| ArgumentNullException.ThrowIfNull(client); | |
| if (timeout <= TimeSpan.Zero) | |
| throw new ArgumentOutOfRangeException(nameof(timeout), "Timeout must be greater than zero."); | |
| if (GetInternalClient(client) is not HttpClient httpClient) | |
| throw new InvalidOperationException("Unable to access the internal HttpClient of OllamaApiClient. The library structure may have changed."); | |
| httpClient.Timeout = timeout; | |
| return client; | |
| } | |
| /// <summary> | |
| /// Gets the current timeout setting from the OllamaApiClient's underlying HttpClient. | |
| /// </summary> | |
| public static TimeSpan? GetTimeout(this OllamaApiClient client) | |
| { | |
| ArgumentNullException.ThrowIfNull(client); | |
| return GetInternalClient(client)?.Timeout; | |
| } | |
| /// <summary> | |
| /// Configures the timeout using a functional approach. | |
| /// </summary> | |
| public static OllamaApiClient ConfigureTimeout(this OllamaApiClient client, Func<TimeSpan?, TimeSpan> configure) | |
| { | |
| ArgumentNullException.ThrowIfNull(client); | |
| ArgumentNullException.ThrowIfNull(configure); | |
| var newTimeout = configure(client.GetTimeout()); | |
| return client.SetTimeout(newTimeout); | |
| } | |
| /// <summary> | |
| /// Sets a 2-minute timeout for quick responses. | |
| /// </summary> | |
| public static OllamaApiClient WithQuickTimeout(this OllamaApiClient client) => client.SetTimeout(TimeSpan.FromMinutes(2)); | |
| /// <summary> | |
| /// Sets a 5-minute timeout for standard operations. | |
| /// </summary> | |
| public static OllamaApiClient WithStandardTimeout(this OllamaApiClient client) => client.SetTimeout(TimeSpan.FromMinutes(5)); | |
| /// <summary> | |
| /// Sets a 10-minute timeout for long generations. | |
| /// </summary> | |
| public static OllamaApiClient WithLongTimeout(this OllamaApiClient client) => client.SetTimeout(TimeSpan.FromMinutes(10)); | |
| /// <summary> | |
| /// Sets a 30-minute timeout for heavy models or complex tasks. | |
| /// </summary> | |
| public static OllamaApiClient WithExtendedTimeout(this OllamaApiClient client) => client.SetTimeout(TimeSpan.FromMinutes(30)); | |
| /// <summary> | |
| /// Internal helper to extract the HttpClient using cached reflection data. | |
| /// </summary> | |
| private static HttpClient? GetInternalClient(OllamaApiClient client) | |
| => HttpClientField?.GetValue(client) as HttpClient; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment