Skip to content

Commit 2dd959f

Browse files
authored
Add OllamaChatClient ctor with string endpoint (#5553)
1 parent 8fbeca0 commit 2dd959f

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/Libraries/Microsoft.Extensions.AI.Ollama/OllamaChatClient.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ public sealed class OllamaChatClient : IChatClient
3030
/// <summary>The <see cref="HttpClient"/> to use for sending requests.</summary>
3131
private readonly HttpClient _httpClient;
3232

33+
/// <summary>Initializes a new instance of the <see cref="OllamaChatClient"/> class.</summary>
34+
/// <param name="endpoint">The endpoint URI where Ollama is hosted.</param>
35+
/// <param name="modelId">
36+
/// The id of the model to use. This may also be overridden per request via <see cref="ChatOptions.ModelId"/>.
37+
/// Either this parameter or <see cref="ChatOptions.ModelId"/> must provide a valid model id.
38+
/// </param>
39+
/// <param name="httpClient">An <see cref="HttpClient"/> instance to use for HTTP operations.</param>
40+
public OllamaChatClient(string endpoint, string? modelId = null, HttpClient? httpClient = null)
41+
: this(new Uri(Throw.IfNull(endpoint)), modelId, httpClient)
42+
{
43+
}
44+
3345
/// <summary>Initializes a new instance of the <see cref="OllamaChatClient"/> class.</summary>
3446
/// <param name="endpoint">The endpoint URI where Ollama is hosted.</param>
3547
/// <param name="modelId">

test/Libraries/Microsoft.Extensions.AI.Ollama.Tests/OllamaChatClientTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public class OllamaChatClientTests
2222
[Fact]
2323
public void Ctor_InvalidArgs_Throws()
2424
{
25-
Assert.Throws<ArgumentNullException>("endpoint", () => new OllamaChatClient(null!));
26-
Assert.Throws<ArgumentException>("modelId", () => new OllamaChatClient(new("http://localhost"), " "));
25+
Assert.Throws<ArgumentNullException>("endpoint", () => new OllamaChatClient((Uri)null!));
26+
Assert.Throws<ArgumentException>("modelId", () => new OllamaChatClient("http://localhost", " "));
2727
}
2828

2929
[Fact]
3030
public void GetService_SuccessfullyReturnsUnderlyingClient()
3131
{
32-
using OllamaChatClient client = new(new("http://localhost"));
32+
using OllamaChatClient client = new("http://localhost");
3333

3434
Assert.Same(client, client.GetService<OllamaChatClient>());
3535
Assert.Same(client, client.GetService<IChatClient>());
@@ -94,7 +94,7 @@ public async Task BasicRequestResponse_NonStreaming()
9494

9595
using VerbatimHttpHandler handler = new(Input, Output);
9696
using HttpClient httpClient = new(handler);
97-
using OllamaChatClient client = new(new("http://localhost:11434"), "llama3.1", httpClient);
97+
using OllamaChatClient client = new("http://localhost:11434", "llama3.1", httpClient);
9898
var response = await client.CompleteAsync("hello", new()
9999
{
100100
MaxOutputTokens = 10,
@@ -152,7 +152,7 @@ public async Task BasicRequestResponse_Streaming()
152152

153153
using VerbatimHttpHandler handler = new(Input, Output);
154154
using HttpClient httpClient = new(handler);
155-
using IChatClient client = new OllamaChatClient(new("http://localhost:11434"), "llama3.1", httpClient);
155+
using IChatClient client = new OllamaChatClient("http://localhost:11434", "llama3.1", httpClient);
156156

157157
List<StreamingChatCompletionUpdate> updates = [];
158158
await foreach (var update in client.CompleteStreamingAsync("hello", new()
@@ -238,7 +238,7 @@ public async Task MultipleMessages_NonStreaming()
238238

239239
using VerbatimHttpHandler handler = new(Input, Output);
240240
using HttpClient httpClient = new(handler);
241-
using IChatClient client = new OllamaChatClient(new("http://localhost:11434"), httpClient: httpClient);
241+
using IChatClient client = new OllamaChatClient("http://localhost:11434", httpClient: httpClient);
242242

243243
List<ChatMessage> messages =
244244
[
@@ -342,7 +342,7 @@ public async Task FunctionCallContent_NonStreaming()
342342

343343
using VerbatimHttpHandler handler = new(Input, Output);
344344
using HttpClient httpClient = new(handler) { Timeout = Timeout.InfiniteTimeSpan };
345-
using IChatClient client = new OllamaChatClient(new("http://localhost:11434"), "llama3.1", httpClient)
345+
using IChatClient client = new OllamaChatClient("http://localhost:11434", "llama3.1", httpClient)
346346
{
347347
ToolCallJsonSerializerOptions = TestJsonSerializerContext.Default.Options,
348348
};
@@ -434,7 +434,7 @@ public async Task FunctionResultContent_NonStreaming()
434434

435435
using VerbatimHttpHandler handler = new(Input, Output);
436436
using HttpClient httpClient = new(handler) { Timeout = Timeout.InfiniteTimeSpan };
437-
using IChatClient client = new OllamaChatClient(new("http://localhost:11434"), "llama3.1", httpClient)
437+
using IChatClient client = new OllamaChatClient("http://localhost:11434", "llama3.1", httpClient)
438438
{
439439
ToolCallJsonSerializerOptions = TestJsonSerializerContext.Default.Options,
440440
};

0 commit comments

Comments
 (0)