Skip to content

Commit

Permalink
feat: Added constructor with ApiKey.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Aug 6, 2024
1 parent 6ae8ba5 commit eac2ea0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ Includes [tokenizer](https://github.com/tryAGI/Tiktoken) and some helper methods
```csharp
using Anthropic;

using var api = new AnthropicApi();
api.AuthorizeUsingApiKey(apiKey);
api.SetHeaders();
using var api = new AnthropicApi(apiKey);

var response = await api.CreateMessageAsync(
model: CreateMessageRequestModel.Claude3Haiku20240307,
model: CreateMessageRequestModel.Claude35Sonnet20240620,
messages: [
"What's the weather like today?",
"Sure! Could you please provide me with your location?".AsAssistantMessage(),
Expand Down Expand Up @@ -85,9 +83,7 @@ public class WeatherService : IWeatherFunctions
```csharp
using Anthropic;

using var api = new AnthropicApi();
api.AuthorizeUsingApiKey(apiKey);
api.SetHeaders();
using var api = new AnthropicApi(apiKey);

var service = new WeatherService();
var tools = service.AsTools();
Expand Down
15 changes: 13 additions & 2 deletions ...libs/Anthropic/AnthropicApi.SetHeaders.cs → ...ropic/OpenAiApi.AdditionalConstructors.cs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
using System.Net.Http.Headers;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Anthropic;

public partial class AnthropicApi
{
/// <inheritdoc cref="AnthropicApi(HttpClient?, Uri?)"/>
public AnthropicApi(
string apiKey,
HttpClient? httpClient = null,
Uri? baseUri = null) : this(httpClient, baseUri)
{
AuthorizeUsingApiKey(apiKey);
SetHeaders();
}

/// <summary>
///
/// </summary>
Expand All @@ -14,4 +25,4 @@ public void SetHeaders()
_httpClient.DefaultRequestHeaders.Add("User-Agent", "tryAGI/Anthropic");
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
}
}
6 changes: 1 addition & 5 deletions src/tests/Anthropic.IntegrationTests/Tests.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ private static AnthropicApi GetAuthorizedApi()
Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY") ??
throw new AssertInconclusiveException("ANTHROPIC_API_KEY environment variable is not found.");

var api = new AnthropicApi();
api.AuthorizeUsingApiKey(apiKey);
api.SetHeaders();

return api;
return new AnthropicApi(apiKey);
}
}
10 changes: 5 additions & 5 deletions src/tests/Anthropic.IntegrationTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public async Task Complete()
using var api = GetAuthorizedApi();

var response = await api.CreateMessageAsync(
model: CreateMessageRequestModel.Claude3Haiku20240307,
model: CreateMessageRequestModel.Claude35Sonnet20240620,
messages: ["Once upon a time"],
maxTokens: 250,
metadata: null,
Expand All @@ -20,7 +20,7 @@ public async Task Complete()
topK: 0,
topP: 0,
stream: false);
response.Model.Should().Be(CreateMessageRequestModel.Claude3Haiku20240307.ToValueString());
response.Model.Should().Be(CreateMessageRequestModel.Claude35Sonnet20240620.ToValueString());
response.Content.Value2.Should().NotBeNullOrEmpty();
response.Content.Value2!.First().Text?.Text.Should().NotBeNullOrEmpty();
response.StopReason.Should().Be(StopReason.EndTurn);
Expand All @@ -32,7 +32,7 @@ public async Task CompleteHistory()
using var api = GetAuthorizedApi();

var response = await api.CreateMessageAsync(
model: CreateMessageRequestModel.Claude3Haiku20240307,
model: CreateMessageRequestModel.Claude35Sonnet20240620,
messages: [
"What's the weather like today?",
"Sure! Could you please provide me with your location?".AsAssistantMessage(),
Expand All @@ -48,7 +48,7 @@ public async Task CompleteHistory()
topK: 0,
topP: 0,
stream: false);
response.Model.Should().Be(CreateMessageRequestModel.Claude3Haiku20240307.ToValueString());
response.Model.Should().Be(CreateMessageRequestModel.Claude35Sonnet20240620.ToValueString());
response.Content.Value2.Should().NotBeNullOrEmpty();
response.Content.Value2!.First().Text?.Text.Should().NotBeNullOrEmpty();
response.StopReason.Should().Be(StopReason.EndTurn);
Expand Down Expand Up @@ -127,7 +127,7 @@ public async Task Streaming()

var enumerable = api.CreateMessageAsStreamAsync(new CreateMessageRequest
{
Model = CreateMessageRequestModel.Claude3Haiku20240307,
Model = CreateMessageRequestModel.Claude35Sonnet20240620,
Messages = ["Once upon a time"],
MaxTokens = 250,
});
Expand Down

0 comments on commit eac2ea0

Please sign in to comment.