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

Do no dispose of httpclient after each call. #23

Merged
merged 2 commits into from
Apr 12, 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
48 changes: 41 additions & 7 deletions Anthropic.SDK.Tests/HttpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,45 @@ public async Task TestBasicHttpClientFailure()
[TestMethod]
public async Task TestBasicHttpClientPass()
{
var client = new AnthropicClient();
client.HttpClient = CustomHttpClientPass();
var messages = new List<Message>();
messages.Add(new Message()
var client = new AnthropicClient
{
Role = RoleType.User,
Content = "Write me a sonnet about the Statue of Liberty"
});
HttpClient = CustomHttpClientPass()
};
var messages = new List<Message>
{
new Message()
{
Role = RoleType.User,
Content = "Write me a sonnet about the Statue of Liberty"
}
};
var parameters = new MessageParameters()
{
Messages = messages,
MaxTokens = 512,
Model = AnthropicModels.Claude3Opus,
Stream = false,
Temperature = 1.0m,
};
var res = await client.Messages.GetClaudeMessageAsync(parameters);

}

[TestMethod]
public async Task MultipleCallsWithCustomHttpClient()
{
var client = new AnthropicClient
{
HttpClient = new HttpClient()
};
var messages = new List<Message>
{
new()
{
Role = RoleType.User,
Content = "Write me a sonnet about the Statue of Liberty"
}
};
var parameters = new MessageParameters()
{
Messages = messages,
Expand All @@ -81,6 +112,9 @@ public async Task TestBasicHttpClientPass()
Temperature = 1.0m,
};
var res = await client.Messages.GetClaudeMessageAsync(parameters);

res = await client.Messages.GetClaudeMessageAsync(parameters);


}

Expand Down
22 changes: 16 additions & 6 deletions Anthropic.SDK/EndpointBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public abstract class EndpointBase
/// </summary>
protected readonly AnthropicClient Client;

private HttpClient _client;

/// <summary>
/// Constructor of the api endpoint base, to be called from the constructor of any derived classes.
/// </summary>
Expand All @@ -44,6 +46,12 @@ internal EndpointBase(AnthropicClient client)
protected string Url => string.Format(Client.ApiUrlFormat, Client.ApiVersion, Endpoint);


private HttpClient innerClient
{
get { return _client ??= GetClient(); }
}


/// <summary>
/// Gets an HTTPClient with the appropriate authorization and other headers set
/// </summary>
Expand All @@ -64,7 +72,11 @@ protected HttpClient GetClient()
client.DefaultRequestHeaders.Add("anthropic-version", Client.AnthropicVersion);
client.DefaultRequestHeaders.Add("anthropic-beta", Client.AnthropicBetaVersion);
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);

client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));


return client;
}

Expand Down Expand Up @@ -96,12 +108,10 @@ protected async Task<T> HttpRequestMessages<T>(string url = null, HttpMethod ver
{
if (string.IsNullOrEmpty(url))
url = this.Url;

//var client = GetClient();

using var client = GetClient();

client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = null;
string resultAsString = null;
HttpRequestMessage req = new HttpRequestMessage(verb, url);
Expand All @@ -121,7 +131,7 @@ protected async Task<T> HttpRequestMessages<T>(string url = null, HttpMethod ver
}
}

response = await client.SendAsync(req,
response = await innerClient.SendAsync(req,
streaming ? HttpCompletionOption.ResponseHeadersRead : HttpCompletionOption.ResponseContentRead, ctx);

if (response.IsSuccessStatusCode)
Expand Down
Loading