forked from srcnalt/OpenAI-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.cs
50 lines (46 loc) · 1.66 KB
/
Configuration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.IO;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace OpenAI
{
public class Configuration
{
public Auth Auth { get; }
/// 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()
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new CustomNamingStrategy()
}
};
public Configuration(string apiKey = null, string organization = null)
{
if (apiKey == null)
{
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("API Key is null and auth.json does not exist. Please check https://github.com/srcnalt/OpenAI-Unity#saving-your-credentials");
}
}
else
{
Auth = new Auth()
{
ApiKey = apiKey,
Organization = organization
};
}
}
}
}