Skip to content

Commit ef49d3b

Browse files
authored
Hardcoded Auth Key (#12)
* feat: let user set apikey and org in code * feat: set config if apikey is not null * chore: bump package version * chore: updated readme
1 parent 36ddc4c commit ef49d3b

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ private async void SendRequest()
5656
}
5757
```
5858

59+
You can also pass your API key into OpenAIApi ctor when creating an instance of it but again, this is not recommended!
60+
61+
```csharp
62+
var openai = new OpenAIApi("sk-Me8...6yi");
63+
```
64+
5965
### Sample Projects
6066
This package includes two sample scenes that you can import via the Package Manager:
6167

Runtime/Configuration.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,30 @@ public class Configuration
2020
}
2121
};
2222

23-
public Configuration()
23+
public Configuration(string apiKey = null, string organization = null)
2424
{
25-
var userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
26-
var authPath = $"{userPath}/.openai/auth.json";
27-
28-
if (File.Exists(authPath))
25+
if (apiKey == null)
2926
{
30-
var json = File.ReadAllText(authPath);
31-
Auth = JsonConvert.DeserializeObject<Auth>(json, jsonSerializerSettings);
27+
var userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
28+
var authPath = $"{userPath}/.openai/auth.json";
29+
30+
if (File.Exists(authPath))
31+
{
32+
var json = File.ReadAllText(authPath);
33+
Auth = JsonConvert.DeserializeObject<Auth>(json, jsonSerializerSettings);
34+
}
35+
else
36+
{
37+
Debug.LogError($"auth.json does not exist. Please check https://github.com/srcnalt/OpenAI-Unity#saving-your-credentials");
38+
}
3239
}
3340
else
3441
{
35-
Debug.LogError($"auth.json does not exist. Please check https://github.com/srcnalt/OpenAI-Unity#saving-your-credentials");
42+
Auth = new Auth()
43+
{
44+
ApiKey = apiKey,
45+
Organization = organization
46+
};
3647
}
3748
}
3849
}

Runtime/OpenAIApi.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ private Configuration Configuration
3232

3333
/// OpenAI API base path for requests.
3434
private const string BASE_PATH = "https://api.openai.com/v1";
35+
36+
public OpenAIApi(string apiKey = null, string organization = null)
37+
{
38+
if (apiKey != null)
39+
{
40+
configuration = new Configuration(apiKey, organization);
41+
}
42+
}
3543

3644
/// Used for serializing and deserializing PascalCase request object fields into snake_case format for JSON. Ignores null fields when creating JSON strings.
3745
private readonly JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.srcnalt.openai-unity",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"displayName": "OpenAI Unity",
55
"description": "An unofficial OpenAI Unity Package that aims to help you use OpenAI API directly in Unity Game engine.",
66
"unity": "2020.3",

0 commit comments

Comments
 (0)