From 729ca97a136484952766f569048eba7f799c7733 Mon Sep 17 00:00:00 2001 From: okgodoit Date: Tue, 22 Dec 2020 18:54:34 -0500 Subject: [PATCH] Update tests and readme due to removal of publishable API keys --- OpenAI_Tests/AuthTests.cs | 72 +++++++++++++-------------------------- README.md | 11 +++--- 2 files changed, 29 insertions(+), 54 deletions(-) diff --git a/OpenAI_Tests/AuthTests.cs b/OpenAI_Tests/AuthTests.cs index 10b06fc..3cec040 100644 --- a/OpenAI_Tests/AuthTests.cs +++ b/OpenAI_Tests/AuthTests.cs @@ -9,9 +9,9 @@ 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] @@ -19,14 +19,9 @@ 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] @@ -34,10 +29,8 @@ 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); } @@ -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); } @@ -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); } } diff --git a/README.md b/README.md index 0983bb0..826cf83 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 StreamCompletionEnumerableAsync(CompletionRequest request)