Skip to content

Commit

Permalink
Android/WebGL Support (#15)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
srcnalt authored Feb 12, 2023
1 parent ef49d3b commit 5ee0efb
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 122 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ To do this, follow these steps:
"organization": "org-...L7W"
}
```

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");
```

**IMPORTANT:** Your API key is a secret.
Do not share it with others or expose it in any client-side code (e.g. browsers, apps).
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.
Expand All @@ -56,18 +63,20 @@ 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:

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

### Known Issues
- **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,
and UnityWebRequests with forms does not work as expected. For that reason C# Net.Http library is used however usage of the
library is blocked in WebGL due to security reasons.

- **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
downloaded using UnityWebRequest until you run it out of localhost, on a server.

### Further Reading
For more information on how to use the various request parameters,
please refer to the OpenAI documentation: https://beta.openai.com/docs/api-reference/introduction
2 changes: 1 addition & 1 deletion Runtime/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Configuration(string apiKey = null, string organization = null)
}
else
{
Debug.LogError($"auth.json does not exist. Please check https://github.com/srcnalt/OpenAI-Unity#saving-your-credentials");
Debug.LogError("API Key is null and auth.json does not exist. Please check https://github.com/srcnalt/OpenAI-Unity#saving-your-credentials");
}
}
else
Expand Down
32 changes: 16 additions & 16 deletions Runtime/DataTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public struct ListModelsResponse: IResponse
{
public ApiError Error { get; set; }
public string Object { get; set; }
public OpenAIModel[] Data { get; set; }
public List<OpenAIModel> Data { get; set; }
}

public class OpenAIModel
Expand All @@ -68,7 +68,7 @@ public class OpenAIModel
public long Created { get; set; }
public string Root { get; set; }
public string Parent { get; set; }
public Dictionary<string, object>[] Permission { get; set; }
public List<Dictionary<string, object>> Permission { get; set; }
}

public class OpenAIModelResponse : OpenAIModel, IResponse
Expand Down Expand Up @@ -105,7 +105,7 @@ public struct CreateCompletionResponse: IResponse
public string Object { get; set; }
public long Created { get; set; }
public string Model { get; set; }
public Choice[] Choices { get; set; }
public List<Choice> Choices { get; set; }
public Usage Usage { get; set; }
}
#endregion
Expand All @@ -126,7 +126,7 @@ public struct CreateEditResponse: IResponse
public ApiError Error { get; set; }
public string Object { get; set; }
public long Created { get; set; }
public Choice[] Choices { get; set; }
public List<Choice> Choices { get; set; }
public Usage Usage { get; set; }
}
#endregion
Expand Down Expand Up @@ -161,7 +161,7 @@ public struct CreateImageResponse: IResponse
{
public ApiError Error { get; set; }
public long Created { get; set; }
public ImageData[] Data { get; set; }
public List<ImageData> Data { get; set; }
}

public struct ImageData
Expand All @@ -183,15 +183,15 @@ public struct CreateEmbeddingsResponse: IResponse
{
public ApiError Error { get; set; }
public string Object { get; set; }
public EmbeddingData[] Data;
public List<EmbeddingData> Data;
public string Model { get; set; }
public Usage Usage { get; set; }
}

public struct EmbeddingData
{
public string Object { get; set; }
public float[] Embedding { get; set; }
public List<float> Embedding { get; set; }
public int Index { get; set; }
}
#endregion
Expand All @@ -201,7 +201,7 @@ public struct ListFilesResponse: IResponse
{
public ApiError Error { get; set; }
public string Object { get; set; }
public OpenAIFile[] Data { get; set; }
public List<OpenAIFile> Data { get; set; }
}

public struct DeleteResponse: IResponse
Expand Down Expand Up @@ -232,22 +232,22 @@ public class CreateFineTuneRequest
public bool ComputeClassificationMetrics { get; set; } = false;
public int? ClassificationNClasses { get; set; } = null;
public string ClassificationPositiveClass { get; set; }
public float[] ClassificationBetas { get; set; }
public List<float> ClassificationBetas { get; set; }
public string Suffix { get; set; }
}

public struct ListFineTunesResponse: IResponse
{
public ApiError Error { get; set; }
public string Object { get; set; }
public FineTune[] Data { get; set; }
public List<FineTune> Data { get; set; }
}

public struct ListFineTuneEventsResponse: IResponse
{
public ApiError Error { get; set; }
public string Object { get; set; }
public FineTuneEvent[] Data { get; set; }
public List<FineTuneEvent> Data { get; set; }
}

public class FineTune
Expand All @@ -261,10 +261,10 @@ public class FineTune
public string OrganizationId { get; set; }
public string Status { get; set; }
public Dictionary<string, object> Hyperparams { get; set; }
public OpenAIFile[] TrainingFiles { get; set; }
public OpenAIFile[] ValidationFiles { get; set; }
public OpenAIFile[] ResultFiles { get; set; }
public FineTuneEvent[] Events { get; set; }
public List<OpenAIFile> TrainingFiles { get; set; }
public List<OpenAIFile> ValidationFiles { get; set; }
public List<OpenAIFile> ResultFiles { get; set; }
public List<FineTuneEvent> Events { get; set; }
}

public class FineTuneResponse : FineTune, IResponse
Expand Down Expand Up @@ -293,7 +293,7 @@ public struct CreateModerationResponse: IResponse
public ApiError Error { get; set; }
public string Id { get; set; }
public string Model { get; set; }
public ModerationResult[] Results { get; set; }
public List<ModerationResult> Results { get; set; }
}

public struct ModerationResult
Expand Down
Loading

0 comments on commit 5ee0efb

Please sign in to comment.