Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
27 changes: 19 additions & 8 deletions Runtime/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Auth>(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<Auth>(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
};
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions Runtime/OpenAIApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down