Skip to content

Commit

Permalink
Update tests and readme due to removal of publishable API keys
Browse files Browse the repository at this point in the history
  • Loading branch information
OkGoDoIt committed Dec 22, 2020
1 parent 0f661e0 commit 729ca97
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 54 deletions.
72 changes: 24 additions & 48 deletions OpenAI_Tests/AuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,28 @@ public class AuthTests
[SetUp]
public void Setup()
{
File.WriteAllText(".openai", "OPENAI_KEY=pk-test12" + Environment.NewLine + "OPENAI_SECRET_KEY: sk-test34");
File.WriteAllText(".openai", "OPENAI_KEY=pk-test12");
Environment.SetEnvironmentVariable("OPENAI_KEY", "pk-test-env");
Environment.SetEnvironmentVariable("OPENAI_SECRET_KEY", "sk-test-env");
//Environment.SetEnvironmentVariable("OPENAI_SECRET_KEY", "sk-test-env");
}

[Test]
public void GetAuthFromEnv()
{
var auth = OpenAI_API.APIAuthentication.LoadFromEnv();
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.APIKey);
Assert.IsNotEmpty(auth.APIKey);
Assert.AreEqual("pk-test-env", auth.APIKey);

Assert.IsNotNull(auth.Secretkey);
Assert.IsNotEmpty(auth.Secretkey);
Assert.AreEqual("sk-test-env", auth.Secretkey);

Assert.IsNotNull(auth.ApiKey);
Assert.IsNotEmpty(auth.ApiKey);
Assert.AreEqual("pk-test-env", auth.ApiKey);
}

[Test]
public void GetAuthFromFile()
{
var auth = OpenAI_API.APIAuthentication.LoadFromPath();
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.APIKey);
Assert.AreEqual("pk-test12", auth.APIKey);
Assert.IsNotNull(auth.Secretkey);
Assert.AreEqual("sk-test34", auth.Secretkey);
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("pk-test12", auth.ApiKey);
}


Expand All @@ -55,10 +48,8 @@ public void GetDefault()
var auth = OpenAI_API.APIAuthentication.Default;
var envAuth = OpenAI_API.APIAuthentication.LoadFromEnv();
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.APIKey);
Assert.AreEqual(envAuth.APIKey, auth.APIKey);
Assert.IsNotNull(auth.Secretkey);
Assert.AreEqual(envAuth.Secretkey, auth.Secretkey);
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual(envAuth.ApiKey, auth.ApiKey);
}


Expand All @@ -67,57 +58,42 @@ public void GetDefault()
public void testHelper()
{
OpenAI_API.APIAuthentication defaultAuth = OpenAI_API.APIAuthentication.Default;
OpenAI_API.APIAuthentication manualAuth = new OpenAI_API.APIAuthentication("pk-testAA", "sk-testBB");
OpenAI_API.APIAuthentication manualAuth = new OpenAI_API.APIAuthentication("pk-testAA");
OpenAI_API.OpenAIAPI api = new OpenAI_API.OpenAIAPI();
OpenAI_API.APIAuthentication shouldBeDefaultAuth = api.Auth;
Assert.IsNotNull(shouldBeDefaultAuth);
Assert.IsNotNull(shouldBeDefaultAuth.APIKey);
Assert.AreEqual(defaultAuth.APIKey, shouldBeDefaultAuth.APIKey);
Assert.IsNotNull(shouldBeDefaultAuth.Secretkey);
Assert.AreEqual(defaultAuth.Secretkey, shouldBeDefaultAuth.Secretkey);
Assert.IsNotNull(shouldBeDefaultAuth.ApiKey);
Assert.AreEqual(defaultAuth.ApiKey, shouldBeDefaultAuth.ApiKey);

OpenAI_API.APIAuthentication.Default = new OpenAI_API.APIAuthentication("pk-testAA", "sk-testBB");
OpenAI_API.APIAuthentication.Default = new OpenAI_API.APIAuthentication("pk-testAA");
api = new OpenAI_API.OpenAIAPI();
OpenAI_API.APIAuthentication shouldBeManualAuth = api.Auth;
Assert.IsNotNull(shouldBeManualAuth);
Assert.IsNotNull(shouldBeManualAuth.APIKey);
Assert.AreEqual(manualAuth.APIKey, shouldBeManualAuth.APIKey);
Assert.IsNotNull(shouldBeManualAuth.Secretkey);
Assert.AreEqual(manualAuth.Secretkey, shouldBeManualAuth.Secretkey);
Assert.IsNotNull(shouldBeManualAuth.ApiKey);
Assert.AreEqual(manualAuth.ApiKey, shouldBeManualAuth.ApiKey);
}

[Test]
public void GetKey()
{
var auth = new OpenAI_API.APIAuthentication("pk-testAA", "sk-testBB");
Assert.IsNotNull(auth.GetKey());
Assert.AreEqual("sk-testBB", auth.GetKey());

auth = new OpenAI_API.APIAuthentication("pk-testAA", null);
Assert.IsNotNull(auth.GetKey());
Assert.AreEqual("pk-testAA", auth.GetKey());
var auth = new OpenAI_API.APIAuthentication("pk-testAA");
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("pk-testAA", auth.ApiKey);
}

[Test]
public void ParseKey()
{
var auth = new OpenAI_API.APIAuthentication("pk-testAA");
Assert.IsNotNull(auth.APIKey);
Assert.IsNull(auth.Secretkey);
Assert.AreEqual("pk-testAA", auth.APIKey);
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("pk-testAA", auth.ApiKey);
auth = "pk-testCC";
Assert.IsNotNull(auth.APIKey);
Assert.IsNull(auth.Secretkey);
Assert.AreEqual("pk-testCC", auth.APIKey);
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("pk-testCC", auth.ApiKey);

auth = new OpenAI_API.APIAuthentication("sk-testBB");
Assert.IsNotNull(auth.Secretkey);
Assert.IsNull(auth.APIKey);
Assert.AreEqual("sk-testBB", auth.Secretkey);
auth = "sk-testDD";
Assert.IsNotNull(auth.Secretkey);
Assert.IsNull(auth.APIKey);
Assert.AreEqual("sk-testDD", auth.Secretkey);
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("sk-testBB", auth.ApiKey);
}

}
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,18 @@ Install-Package OpenAI
### Authentication
There are 3 ways to provide your API keys, in order of precedence:
1. Pass keys directly to `APIAuthentication(string key)` constructor
2. Set environment vars for OPENAI_KEY and/or OPENAI_SECRET_KEY
2. Set environment var for OPENAI_KEY
3. Include a config file in the local directory or in your user directory named `.openai` and containing one or both lines:
```shell
OPENAI_KEY=pk-aaaabbbbbccccddddd
OPENAI_SECRET_KEY=sk-aaaabbbbbccccddddd
OPENAI_KEY=sk-aaaabbbbbccccddddd
```

You use the `APIAuthentication` when you initialize the API as shown:
```csharp
// for example
OpenAIAPI api = new OpenAIAPI("sk-mysecretkeyhere"); // shorthand
OpenAIAPI api = new OpenAIAPI("sk-mykeyhere"); // shorthand
// or
OpenAIAPI api = new OpenAIAPI(new APIAuthentication("pk-publishkey","sk-secretkey")); // specify manually
OpenAIAPI api = new OpenAIAPI(new APIAuthentication("sk-secretkey")); // create object manually
// or
OpenAIAPI api = new OpenAIAPI(APIAuthentication LoadFromEnv()); // use env vars
// or
Expand All @@ -74,7 +73,7 @@ You can create your `CompletionRequest` ahead of time or use one of the helper o
#### Streaming
Streaming allows you to get results are they are generated, which can help your application feel more responsive, especially on slow models like Davinci.

Using the new C# 8.0 async interators:
Using the new C# 8.0 async iterators:
```csharp
IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(CompletionRequest request)

Expand Down

0 comments on commit 729ca97

Please sign in to comment.