Skip to content

Commit

Permalink
Add OllamaChatClient ctor with string endpoint (#5553)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Oct 22, 2024
1 parent 8fbeca0 commit 2dd959f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/Libraries/Microsoft.Extensions.AI.Ollama/OllamaChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public sealed class OllamaChatClient : IChatClient
/// <summary>The <see cref="HttpClient"/> to use for sending requests.</summary>
private readonly HttpClient _httpClient;

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

/// <summary>Initializes a new instance of the <see cref="OllamaChatClient"/> class.</summary>
/// <param name="endpoint">The endpoint URI where Ollama is hosted.</param>
/// <param name="modelId">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public class OllamaChatClientTests
[Fact]
public void Ctor_InvalidArgs_Throws()
{
Assert.Throws<ArgumentNullException>("endpoint", () => new OllamaChatClient(null!));
Assert.Throws<ArgumentException>("modelId", () => new OllamaChatClient(new("http://localhost"), " "));
Assert.Throws<ArgumentNullException>("endpoint", () => new OllamaChatClient((Uri)null!));
Assert.Throws<ArgumentException>("modelId", () => new OllamaChatClient("http://localhost", " "));
}

[Fact]
public void GetService_SuccessfullyReturnsUnderlyingClient()
{
using OllamaChatClient client = new(new("http://localhost"));
using OllamaChatClient client = new("http://localhost");

Assert.Same(client, client.GetService<OllamaChatClient>());
Assert.Same(client, client.GetService<IChatClient>());
Expand Down Expand Up @@ -94,7 +94,7 @@ public async Task BasicRequestResponse_NonStreaming()

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

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

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

using VerbatimHttpHandler handler = new(Input, Output);
using HttpClient httpClient = new(handler);
using IChatClient client = new OllamaChatClient(new("http://localhost:11434"), httpClient: httpClient);
using IChatClient client = new OllamaChatClient("http://localhost:11434", httpClient: httpClient);

List<ChatMessage> messages =
[
Expand Down Expand Up @@ -342,7 +342,7 @@ public async Task FunctionCallContent_NonStreaming()

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

using VerbatimHttpHandler handler = new(Input, Output);
using HttpClient httpClient = new(handler) { Timeout = Timeout.InfiniteTimeSpan };
using IChatClient client = new OllamaChatClient(new("http://localhost:11434"), "llama3.1", httpClient)
using IChatClient client = new OllamaChatClient("http://localhost:11434", "llama3.1", httpClient)
{
ToolCallJsonSerializerOptions = TestJsonSerializerContext.Default.Options,
};
Expand Down

0 comments on commit 2dd959f

Please sign in to comment.