Skip to content
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

Add OllamaChatClient ctor with string endpoint #5553

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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
Loading