Skip to content

Commit

Permalink
feat: require api keys (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Meyerowitz authored Aug 2, 2023
1 parent b4bf206 commit 282be98
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- name: Run Tests
env:
CB_PROJECT_ID: ${{ secrets.CB_PROJECT_ID }}
CB_API_KEY: ${{ secrets.CB_API_KEY }}
run: dotnet test

release:
Expand Down
2 changes: 1 addition & 1 deletion Commonbase.Examples/ChatCompletion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static class ChatCompletionExample
{
public static async Task RunAsync()
{
CommonbaseClient client = new();
CommonbaseClient client = new(apiKey: Program.CB_API_KEY!);

string systemMessage = "You help people with geography.";
ChatMessage[] messages = new[] {
Expand Down
7 changes: 5 additions & 2 deletions Commonbase.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ public class Program
// Set a CB_PROJECT_ID environment variable to run these examples without writing any code.
// You can also hard-code your project ID here if you prefer.
public static string? CB_PROJECT_ID = Environment.GetEnvironmentVariable("CB_PROJECT_ID");
public static string? CB_API_KEY = Environment.GetEnvironmentVariable("CB_API_KEY");

public static async Task Main()
{
if (string.IsNullOrWhiteSpace(CB_PROJECT_ID))
if (string.IsNullOrWhiteSpace(CB_PROJECT_ID) || string.IsNullOrWhiteSpace(CB_API_KEY))
{
Console.Error.WriteLine("Please set the CB_PROJECT_ID environment variable to your Commonbase Project ID.");
Console.Error.WriteLine(
"Please set the CB_PROJECT_ID and CB_API_KEY environment variables to run these examples."
);
return;
}

Expand Down
3 changes: 2 additions & 1 deletion Commonbase.Examples/StreamingCompletion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ public static class StreamingCompletionExample
{
public static async Task RunAsync()
{
CommonbaseClient client = new();
CommonbaseClient client = new(apiKey: Program.CB_API_KEY!);

string prompt = "Write me an essay about artificial intelligence.";

var stream = client.StreamCompletionAsync(
Expand Down
3 changes: 2 additions & 1 deletion Commonbase.Examples/TextCompletion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ public static class TextCompletionExample
{
public static async Task RunAsync()
{
CommonbaseClient client = new();
CommonbaseClient client = new(apiKey: Program.CB_API_KEY!);

string prompt = "Hello, what is your name?";

Console.WriteLine("\n=======================================================");
Expand Down
11 changes: 10 additions & 1 deletion Commonbase.Tests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ public ClientTests()
{
Client = new CommonbaseClient(new ClientOptions
{
ProjectId = Environment.GetEnvironmentVariable("CB_PROJECT_ID")
ApiKey = Environment.GetEnvironmentVariable("CB_API_KEY")!,
ProjectId = Environment.GetEnvironmentVariable("CB_PROJECT_ID")!
});
}

[Fact]
public void ShouldThrowArgumentExceptionOnNoApiKey()
{
Assert.Throws<ArgumentException>(
() => new CommonbaseClient(apiKey: "")
);
}

[Fact]
public async void ShouldFailOnInvalidProjectId()
{
Expand Down
17 changes: 14 additions & 3 deletions Commonbase/CommonbaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace Commonbase;

public record ClientOptions(string? ProjectId = null, string? ApiKey = null);
public record ClientOptions
{
public required string ApiKey { get; init; }
public string? ProjectId { get; init; }
}

public class CommonbaseClient
{
Expand All @@ -21,9 +25,15 @@ static CommonbaseClient()
private HttpClient HttpClient;

private ClientOptions clientOptions;
public CommonbaseClient(ClientOptions? options = null)
public CommonbaseClient(string apiKey) : this(new ClientOptions { ApiKey = apiKey }) { }
public CommonbaseClient(ClientOptions options)
{
clientOptions = options ?? new ClientOptions();
if (string.IsNullOrWhiteSpace(options.ApiKey))
{
throw new ArgumentException("Api Key must not be null or empty.", nameof(options.ApiKey));
}

clientOptions = options;
HttpClient = new HttpClient();
}

Expand Down Expand Up @@ -59,6 +69,7 @@ private async Task<HttpResponseMessage> SendCompletionRequestAsync(

request.Content = body;

request.Headers.Add("Authorization", clientOptions.ApiKey);
request.Headers.Add("User-Agent", $"commonbase-dotnet/{ClientVersion}");

if (stream)
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@ dotnet package add Commonbase

## Usage

A project ID is required for all Commonbase requests. You can find your project ID
in the [Commonbase Dashboard](https://commonbase.com/).
A Project ID and API Key are required for all Commonbase requests. You can find your Project ID
and generate an API Key in the [Commonbase Dashboard](https://commonbase.com/).

To create text and chat completions, use `CommonbaseClient.CreateCompletionAsync`:
To create a completion, configure a `CommonbaseClient` with your API Key and provide your Project
ID and prompt to `CreateCompletionAsync`:

```c#
using Commonbase;

CommonbaseClient client = new();
CommonbaseClient client = new(apiKey: "API_KEY");

var response = await client.CreateCompletionAsync(
prompt: "Hello!",
projectId: "<your_project_id>"
projectId: "PROJECT_ID"
);

Console.WriteLine(response.BestResult);
```

To stream a completion as it is generated, use `CommonbaseClient.StreamCompletionAsync`.
To stream a completion as it is generated, use `StreamCompletionAsync`.

For more examples, see [/Commonbase.Examples](https://github.com/commonbaseapp/commonbase-dotnet/tree/main/Commonbase.Examples).

0 comments on commit 282be98

Please sign in to comment.