Skip to content

Commit 5ee0efb

Browse files
authored
Android/WebGL Support (#15)
* feat: unity web req impl * feat: null and length check for results * feat: make form uploads with net.http only * chore: message updated * feat: arrays turned into list to prevent WebGL error * feat: samples are updated * feat: samples are updated * fix: minor fixes * feat: readme updated * chore: version bumped
1 parent ef49d3b commit 5ee0efb

15 files changed

+230
-122
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ To do this, follow these steps:
3333
"organization": "org-...L7W"
3434
}
3535
```
36+
37+
You can also pass your API key into `OpenAIApi` ctor when creating an instance of it but again, this is not recommended!
38+
39+
```csharp
40+
var openai = new OpenAIApi("sk-Me8...6yi");
41+
```
42+
3643
**IMPORTANT:** Your API key is a secret.
3744
Do not share it with others or expose it in any client-side code (e.g. browsers, apps).
3845
If you are using OpenAI for production, make sure to run it on the server side, where your API key can be securely loaded from an environment variable or key management service.
@@ -56,18 +63,20 @@ private async void SendRequest()
5663
}
5764
```
5865

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-
6566
### Sample Projects
6667
This package includes two sample scenes that you can import via the Package Manager:
6768

6869
- **ChatGPT sample:** A simple ChatGPT like chat example.
6970
- **DallE sample:** A DALL.E text to image generation example.
7071

72+
### Known Issues
73+
- **Some Endpoints are not available in WebGL Project:** Some of the endpoints such as image edits, image variations, file and fine tune creations depend on multipart form uploads,
74+
and UnityWebRequests with forms does not work as expected. For that reason C# Net.Http library is used however usage of the
75+
library is blocked in WebGL due to security reasons.
76+
77+
- **Can't See the Image Result in WebGL Builds:** Due to CORS policy of OpenAI image storage in local WebGL builds you will get the generated image's URL however it will not be
78+
downloaded using UnityWebRequest until you run it out of localhost, on a server.
79+
7180
### Further Reading
7281
For more information on how to use the various request parameters,
7382
please refer to the OpenAI documentation: https://beta.openai.com/docs/api-reference/introduction

Runtime/Configuration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public Configuration(string apiKey = null, string organization = null)
3434
}
3535
else
3636
{
37-
Debug.LogError($"auth.json does not exist. Please check https://github.com/srcnalt/OpenAI-Unity#saving-your-credentials");
37+
Debug.LogError("API Key is null and auth.json does not exist. Please check https://github.com/srcnalt/OpenAI-Unity#saving-your-credentials");
3838
}
3939
}
4040
else

Runtime/DataTypes.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public struct ListModelsResponse: IResponse
5757
{
5858
public ApiError Error { get; set; }
5959
public string Object { get; set; }
60-
public OpenAIModel[] Data { get; set; }
60+
public List<OpenAIModel> Data { get; set; }
6161
}
6262

6363
public class OpenAIModel
@@ -68,7 +68,7 @@ public class OpenAIModel
6868
public long Created { get; set; }
6969
public string Root { get; set; }
7070
public string Parent { get; set; }
71-
public Dictionary<string, object>[] Permission { get; set; }
71+
public List<Dictionary<string, object>> Permission { get; set; }
7272
}
7373

7474
public class OpenAIModelResponse : OpenAIModel, IResponse
@@ -105,7 +105,7 @@ public struct CreateCompletionResponse: IResponse
105105
public string Object { get; set; }
106106
public long Created { get; set; }
107107
public string Model { get; set; }
108-
public Choice[] Choices { get; set; }
108+
public List<Choice> Choices { get; set; }
109109
public Usage Usage { get; set; }
110110
}
111111
#endregion
@@ -126,7 +126,7 @@ public struct CreateEditResponse: IResponse
126126
public ApiError Error { get; set; }
127127
public string Object { get; set; }
128128
public long Created { get; set; }
129-
public Choice[] Choices { get; set; }
129+
public List<Choice> Choices { get; set; }
130130
public Usage Usage { get; set; }
131131
}
132132
#endregion
@@ -161,7 +161,7 @@ public struct CreateImageResponse: IResponse
161161
{
162162
public ApiError Error { get; set; }
163163
public long Created { get; set; }
164-
public ImageData[] Data { get; set; }
164+
public List<ImageData> Data { get; set; }
165165
}
166166

167167
public struct ImageData
@@ -183,15 +183,15 @@ public struct CreateEmbeddingsResponse: IResponse
183183
{
184184
public ApiError Error { get; set; }
185185
public string Object { get; set; }
186-
public EmbeddingData[] Data;
186+
public List<EmbeddingData> Data;
187187
public string Model { get; set; }
188188
public Usage Usage { get; set; }
189189
}
190190

191191
public struct EmbeddingData
192192
{
193193
public string Object { get; set; }
194-
public float[] Embedding { get; set; }
194+
public List<float> Embedding { get; set; }
195195
public int Index { get; set; }
196196
}
197197
#endregion
@@ -201,7 +201,7 @@ public struct ListFilesResponse: IResponse
201201
{
202202
public ApiError Error { get; set; }
203203
public string Object { get; set; }
204-
public OpenAIFile[] Data { get; set; }
204+
public List<OpenAIFile> Data { get; set; }
205205
}
206206

207207
public struct DeleteResponse: IResponse
@@ -232,22 +232,22 @@ public class CreateFineTuneRequest
232232
public bool ComputeClassificationMetrics { get; set; } = false;
233233
public int? ClassificationNClasses { get; set; } = null;
234234
public string ClassificationPositiveClass { get; set; }
235-
public float[] ClassificationBetas { get; set; }
235+
public List<float> ClassificationBetas { get; set; }
236236
public string Suffix { get; set; }
237237
}
238238

239239
public struct ListFineTunesResponse: IResponse
240240
{
241241
public ApiError Error { get; set; }
242242
public string Object { get; set; }
243-
public FineTune[] Data { get; set; }
243+
public List<FineTune> Data { get; set; }
244244
}
245245

246246
public struct ListFineTuneEventsResponse: IResponse
247247
{
248248
public ApiError Error { get; set; }
249249
public string Object { get; set; }
250-
public FineTuneEvent[] Data { get; set; }
250+
public List<FineTuneEvent> Data { get; set; }
251251
}
252252

253253
public class FineTune
@@ -261,10 +261,10 @@ public class FineTune
261261
public string OrganizationId { get; set; }
262262
public string Status { get; set; }
263263
public Dictionary<string, object> Hyperparams { get; set; }
264-
public OpenAIFile[] TrainingFiles { get; set; }
265-
public OpenAIFile[] ValidationFiles { get; set; }
266-
public OpenAIFile[] ResultFiles { get; set; }
267-
public FineTuneEvent[] Events { get; set; }
264+
public List<OpenAIFile> TrainingFiles { get; set; }
265+
public List<OpenAIFile> ValidationFiles { get; set; }
266+
public List<OpenAIFile> ResultFiles { get; set; }
267+
public List<FineTuneEvent> Events { get; set; }
268268
}
269269

270270
public class FineTuneResponse : FineTune, IResponse
@@ -293,7 +293,7 @@ public struct CreateModerationResponse: IResponse
293293
public ApiError Error { get; set; }
294294
public string Id { get; set; }
295295
public string Model { get; set; }
296-
public ModerationResult[] Results { get; set; }
296+
public List<ModerationResult> Results { get; set; }
297297
}
298298

299299
public struct ModerationResult

0 commit comments

Comments
 (0)