Skip to content

Commit e1bab01

Browse files
committed
init
0 parents  commit e1bab01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+4081
-0
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Sercan Altundas
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LICENSE.md.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## OpenAI Unity Package
2+
An unofficial OpenAI Unity Package that aims to help you use OpenAI API directly in Unity Game engine.
3+
4+
## How To Use
5+
6+
### Importing the Package
7+
In Unity 2019+ you can go to `Window > Package Manager > + > Add package from git URL` and paste the repository URL `https://github.com/srcnalt/OpenAI-Unity.git` then click on `Add` button.
8+
9+
### Creating OpenAI Account
10+
To be able to use OpenAI API you will need an OpenAI account. Go to https://openai.com/api and sign up for an account.
11+
12+
Once you created an account you can go to https://beta.openai.com/account/api-keys page and create a new secret key save it.
13+
14+
### Saving Your Credentials
15+
To be able to make requests to OpenAI endpoints you will need to use your API Key and optionally organization name.
16+
17+
To avoid keeping your API Key in your Unity project you can save it in your device's local.
18+
For this purpose this package uses a json file in User directory of your device.
19+
To create the file to save your credentials follow the steps below.
20+
- Create a folder called `.openai` in your user's home directory `C:User\UserName\` for Windows and `~\` for Linux and Mac.
21+
- Create a file called `auth.json` in `.openai` folder.
22+
- Add `api_key` and optionally `organization` fields, fill their values to your json object and save it.
23+
24+
Finally you should have `C:User\UserName\.openai\auth.json` file and the file content should look as the following.
25+
26+
```json
27+
{
28+
"api_key": "sk-...W6yi",
29+
"organization": "org-...L7W"
30+
}
31+
```
32+
33+
Remember that your API key is a secret! Do not share it with others or expose it in any client-side code (browsers, apps).
34+
Production requests must be routed through your own backend server where your API key can be securely loaded from an environment variable or key management service.
35+
If you are going to use OpenAI for production make sure to run it on the server side.
36+
37+
### Making Requests to OpenAPI
38+
You can create a async method to make a call to the `OpenAI` endpoints.
39+
All methods are asynchronous and can be directly accessed from the `OpenAIApi` instance.
40+
41+
```csharp
42+
private async void SendRequest()
43+
{
44+
var openai = new OpenAIApi();
45+
var request = new CreateCompletionRequest{
46+
Model="text-davinci-003",
47+
Prompt="Say this is a test",
48+
}
49+
var response = await openai.CreateCompletion(request);
50+
}
51+
```
52+
53+
### Sample Projects
54+
With this package you can find 2 sample scenes that you can import through Package Manager.
55+
- **ChatGPT sample:** A simple ChatGPT like chat example.
56+
- **DallE sample:** A DALL.E text to image generation example.
57+
58+
### Further Reading
59+
For further details on how to use all the request parameters please refer to OpenAI documentation.
60+
https://beta.openai.com/docs/api-reference/introduction

README.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Configuration.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.IO;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace OpenAI
7+
{
8+
public class Configuration
9+
{
10+
public string ApiKey { get; }
11+
public string Organization { get; }
12+
13+
public Configuration()
14+
{
15+
var path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
16+
var json = File.ReadAllText($"{path}/.openai/auth.json");
17+
var auth = JsonConvert.DeserializeObject<JToken>(json);
18+
19+
if (auth != null)
20+
{
21+
ApiKey = auth["api_key"]!.Value<string>();
22+
Organization = auth["organization"]!.Value<string>();
23+
}
24+
else
25+
{
26+
throw new Exception("auth.json could not be found.");
27+
}
28+
}
29+
}
30+
}

Runtime/Configuration.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)