Skip to content

Commit

Permalink
com.openai.unity 2.2.3 (#32)
Browse files Browse the repository at this point in the history
- #31 
- added `OPEN_AI_ORGANIZATION_ID` environment variable
- deprecated `Organization` use `OrganizationId` instead
  • Loading branch information
StephenHodgson authored Feb 10, 2023
1 parent f5bc655 commit 0de73b5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
16 changes: 9 additions & 7 deletions Runtime/AuthInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using System;
using System.Security.Authentication;
using UnityEngine;
using UnityEngine.Serialization;

namespace OpenAI
{
[Serializable]
internal class AuthInfo
{
public AuthInfo(string apiKey, string organization = null)
public AuthInfo(string apiKey, string organizationId = null)
{
if (!apiKey.Contains("sk-"))
{
Expand All @@ -18,14 +19,14 @@ public AuthInfo(string apiKey, string organization = null)

this.apiKey = apiKey;

if (organization != null)
if (organizationId != null)
{
if (!organization.Contains("org-"))
if (!organizationId.Contains("org-"))
{
throw new InvalidCredentialException($"{nameof(organization)} must start with 'org-'");
throw new InvalidCredentialException($"{nameof(organizationId)} must start with 'org-'");
}

this.organization = organization;
this.organizationId = organizationId;
}
}

Expand All @@ -35,8 +36,9 @@ public AuthInfo(string apiKey, string organization = null)
public string ApiKey => apiKey;

[SerializeField]
private string organization;
[FormerlySerializedAs("organization")]
private string organizationId;

public string Organization => organization;
public string OrganizationId => organizationId;
}
}
17 changes: 13 additions & 4 deletions Runtime/OpenAIAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public sealed class OpenAIAuthentication
private const string OPENAI_API_KEY = "OPENAI_API_KEY";
private const string OPENAI_SECRET_KEY = "OPENAI_SECRET_KEY";
private const string TEST_OPENAI_SECRET_KEY = "TEST_OPENAI_SECRET_KEY";
private const string OPEN_AI_ORGANIZATION_ID = "OPEN_AI_ORGANIZATION_ID";
private const string ORGANIZATION = "ORGANIZATION";

private readonly AuthInfo authInfo;
Expand All @@ -25,11 +26,14 @@ public sealed class OpenAIAuthentication
/// </summary>
public string ApiKey => authInfo.ApiKey;

[Obsolete("Use OrganizationId instead")]
public string Organization => authInfo.OrganizationId;

/// <summary>
/// For users who belong to multiple organizations, you can pass a header to specify which organization is used for an API request.
/// Usage from these API requests will count against the specified organization's subscription quota.
/// </summary>
public string Organization => authInfo.Organization;
public string OrganizationId => authInfo.OrganizationId;

/// <summary>
/// Allows implicit casting from a string, so that a simple string API key can be provided in place of an instance of <see cref="OpenAIAuthentication"/>.
Expand Down Expand Up @@ -90,15 +94,15 @@ private static OpenAIAuthentication LoadFromAsset()
/// <summary>
/// Attempts to load api keys from environment variables, as "OPENAI_KEY" (or "OPENAI_SECRET_KEY", for backwards compatibility)
/// </summary>
/// <param name="organization">
/// <param name="organizationId">
/// For users who belong to multiple organizations, you can pass a header to specify which organization is used for an API request.
/// Usage from these API requests will count against the specified organization's subscription quota.
/// </param>
/// <returns>
/// Returns the loaded <see cref="OpenAIAuthentication"/> any api keys were found,
/// or <see langword="null"/> if there were no matching environment vars.
/// </returns>
public static OpenAIAuthentication LoadFromEnv(string organization = null)
public static OpenAIAuthentication LoadFromEnv(string organizationId = null)
{
var apiKey = Environment.GetEnvironmentVariable(OPENAI_KEY);

Expand All @@ -117,7 +121,12 @@ public static OpenAIAuthentication LoadFromEnv(string organization = null)
apiKey = Environment.GetEnvironmentVariable(TEST_OPENAI_SECRET_KEY);
}

return string.IsNullOrEmpty(apiKey) ? null : new OpenAIAuthentication(apiKey, organization);
if (string.IsNullOrWhiteSpace(organizationId))
{
organizationId = Environment.GetEnvironmentVariable(OPEN_AI_ORGANIZATION_ID);
}

return string.IsNullOrEmpty(apiKey) ? null : new OpenAIAuthentication(apiKey, organizationId);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Runtime/OpenAIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public OpenAIClient(OpenAIAuthentication openAIAuthentication = null, Model mode
Client.DefaultRequestHeaders.Add("User-Agent", "com.openai.unity");
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", OpenAIAuthentication.ApiKey);

if (!string.IsNullOrWhiteSpace(OpenAIAuthentication.Organization))
if (!string.IsNullOrWhiteSpace(OpenAIAuthentication.OrganizationId))
{
Client.DefaultRequestHeaders.Add("OpenAI-Organization", OpenAIAuthentication.Organization);
Client.DefaultRequestHeaders.Add("OpenAI-Organization", OpenAIAuthentication.OrganizationId);
}

Version = 1;
Expand Down
24 changes: 12 additions & 12 deletions Tests/TestFixture_00_Authentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public void Test_01_GetAuthFromEnv()
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.ApiKey);
Assert.IsNotEmpty(auth.ApiKey);
Assert.IsNotNull(auth.Organization);
Assert.IsNotEmpty(auth.Organization);
Assert.IsNotNull(auth.OrganizationId);
Assert.IsNotEmpty(auth.OrganizationId);
}

[Test]
Expand All @@ -41,8 +41,8 @@ public void Test_02_GetAuthFromFile()
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("sk-test12", auth.ApiKey);
Assert.IsNotNull(auth.Organization);
Assert.AreEqual("org-testOrg", auth.Organization);
Assert.IsNotNull(auth.OrganizationId);
Assert.AreEqual("org-testOrg", auth.OrganizationId);
}

[Test]
Expand All @@ -63,8 +63,8 @@ public void Test_04_GetAuthFromConfiguration()
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.ApiKey);
Assert.IsNotEmpty(auth.ApiKey);
Assert.IsNotNull(auth.Organization);
Assert.IsNotEmpty(auth.Organization);
Assert.IsNotNull(auth.OrganizationId);
Assert.IsNotEmpty(auth.OrganizationId);
}

[Test]
Expand All @@ -76,18 +76,18 @@ public void Test_05_Authentication()
var shouldBeDefaultAuth = api.OpenAIAuthentication;
Assert.IsNotNull(shouldBeDefaultAuth);
Assert.IsNotNull(shouldBeDefaultAuth.ApiKey);
Assert.IsNotNull(shouldBeDefaultAuth.Organization);
Assert.IsNotNull(shouldBeDefaultAuth.OrganizationId);
Assert.AreEqual(defaultAuth.ApiKey, shouldBeDefaultAuth.ApiKey);
Assert.AreEqual(defaultAuth.Organization, shouldBeDefaultAuth.Organization);
Assert.AreEqual(defaultAuth.OrganizationId, shouldBeDefaultAuth.OrganizationId);

OpenAIAuthentication.Default = new OpenAIAuthentication("sk-testAA", "org-testAA");
api = new OpenAIClient();
var shouldBeManualAuth = api.OpenAIAuthentication;
Assert.IsNotNull(shouldBeManualAuth);
Assert.IsNotNull(shouldBeManualAuth.ApiKey);
Assert.IsNotNull(shouldBeManualAuth.Organization);
Assert.IsNotNull(shouldBeManualAuth.OrganizationId);
Assert.AreEqual(manualAuth.ApiKey, shouldBeManualAuth.ApiKey);
Assert.AreEqual(manualAuth.Organization, shouldBeManualAuth.Organization);
Assert.AreEqual(manualAuth.OrganizationId, shouldBeManualAuth.OrganizationId);

OpenAIAuthentication.Default = defaultAuth;
}
Expand Down Expand Up @@ -138,8 +138,8 @@ public void Test_08_ParseKey()
public void Test_09_GetOrganization()
{
var auth = new OpenAIAuthentication("sk-testAA", "org-testAA");
Assert.IsNotNull(auth.Organization);
Assert.AreEqual("org-testAA", auth.Organization);
Assert.IsNotNull(auth.OrganizationId);
Assert.AreEqual("org-testAA", auth.OrganizationId);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestFixture_07_FineTuning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public IEnumerator Test_08_DeleteFineTunedModel()
foreach (var model in models)
{
if (model.OwnedBy == api.OpenAIAuthentication.Organization)
if (model.OwnedBy == api.OpenAIAuthentication.OrganizationId)
{
Debug.Log(model);
var result = await api.ModelsEndpoint.DeleteFineTuneModelAsync(model);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "OpenAI",
"description": "A OpenAI package for the Unity Game Engine to use GPT-3 and Dall-E though their RESTful API (currently in beta).\n\nIndependently developed, this is not an official library and I am not affiliated with OpenAI.\n\nAn OpenAI API account is required.",
"keywords": [],
"version": "2.2.2",
"version": "2.2.3",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.openai.unity#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.openai.unity/releases",
Expand Down

0 comments on commit 0de73b5

Please sign in to comment.