diff --git a/README.md b/README.md index 7b9fced..6c77aaf 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,12 @@ private async void SendRequest() } ``` +You can also pass your API key into OpenAIApi ctor when creating an instance of it but again, this is not recommended! + +```csharp +var openai = new OpenAIApi("sk-Me8...6yi"); +``` + ### Sample Projects This package includes two sample scenes that you can import via the Package Manager: diff --git a/Runtime/Configuration.cs b/Runtime/Configuration.cs index 8445801..a19074a 100644 --- a/Runtime/Configuration.cs +++ b/Runtime/Configuration.cs @@ -20,19 +20,30 @@ public class Configuration } }; - public Configuration() + public Configuration(string apiKey = null, string organization = null) { - var userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - var authPath = $"{userPath}/.openai/auth.json"; - - if (File.Exists(authPath)) + if (apiKey == null) { - var json = File.ReadAllText(authPath); - Auth = JsonConvert.DeserializeObject(json, jsonSerializerSettings); + var userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + var authPath = $"{userPath}/.openai/auth.json"; + + if (File.Exists(authPath)) + { + var json = File.ReadAllText(authPath); + Auth = JsonConvert.DeserializeObject(json, jsonSerializerSettings); + } + else + { + Debug.LogError($"auth.json does not exist. Please check https://github.com/srcnalt/OpenAI-Unity#saving-your-credentials"); + } } else { - Debug.LogError($"auth.json does not exist. Please check https://github.com/srcnalt/OpenAI-Unity#saving-your-credentials"); + Auth = new Auth() + { + ApiKey = apiKey, + Organization = organization + }; } } } diff --git a/Runtime/OpenAIApi.cs b/Runtime/OpenAIApi.cs index ee9f0ee..f06ef22 100644 --- a/Runtime/OpenAIApi.cs +++ b/Runtime/OpenAIApi.cs @@ -32,6 +32,14 @@ private Configuration Configuration /// OpenAI API base path for requests. private const string BASE_PATH = "https://api.openai.com/v1"; + + public OpenAIApi(string apiKey = null, string organization = null) + { + if (apiKey != null) + { + configuration = new Configuration(apiKey, organization); + } + } /// Used for serializing and deserializing PascalCase request object fields into snake_case format for JSON. Ignores null fields when creating JSON strings. private readonly JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings() diff --git a/package.json b/package.json index 839a905..23f1835 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.srcnalt.openai-unity", - "version": "0.1.4", + "version": "0.1.5", "displayName": "OpenAI Unity", "description": "An unofficial OpenAI Unity Package that aims to help you use OpenAI API directly in Unity Game engine.", "unity": "2020.3",