{"id":13341924,"url":"https://github.com/srcnalt/OpenAI-Unity","last_synced_at":"2025-03-11T23:30:30.229Z","repository":{"id":65276489,"uuid":"586360591","full_name":"srcnalt/OpenAI-Unity","owner":"srcnalt","description":"An unofficial OpenAI Unity Package that aims to help you use OpenAI API directly in Unity Game engine.","archived":false,"fork":false,"pushed_at":"2024-11-27T20:01:07.000Z","size":1133,"stargazers_count":773,"open_issues_count":31,"forks_count":168,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-09T23:26:16.873Z","etag":null,"topics":["chatgpt","dalle","openai","openai-api","unity","unity3d","whisper"],"latest_commit_sha":null,"homepage":"https://neocortex.link","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/srcnalt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"srcnalt"}},"created_at":"2023-01-07T21:18:17.000Z","updated_at":"2025-03-07T20:38:00.000Z","dependencies_parsed_at":"2023-02-19T17:21:33.110Z","dependency_job_id":"b7d0f855-68ef-4077-9f26-1a5111b9eb60","html_url":"https://github.com/srcnalt/OpenAI-Unity","commit_stats":{"total_commits":80,"total_committers":1,"mean_commits":80.0,"dds":0.0,"last_synced_commit":"95503471ac85848ba0d85b597419b1303fbb2c01"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srcnalt%2FOpenAI-Unity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srcnalt%2FOpenAI-Unity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srcnalt%2FOpenAI-Unity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srcnalt%2FOpenAI-Unity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/srcnalt","download_url":"https://codeload.github.com/srcnalt/OpenAI-Unity/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243129523,"owners_count":20241029,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatgpt","dalle","openai","openai-api","unity","unity3d","whisper"],"created_at":"2024-07-29T19:26:41.009Z","updated_at":"2025-03-11T23:30:30.221Z","avatar_url":"https://github.com/srcnalt.png","language":"C#","funding_links":["https://github.com/sponsors/srcnalt"],"categories":["SDK, Libraries, Frameworks","Packages \u0026 Services"],"sub_categories":["C#"],"readme":"## 📣 Check out [Neocortex](https://neocortex.link?source=unity-openai-github) for easy Smart NPC integration!\n \n[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/sgt3v.svg?style=social\u0026label=Follow%20%40sgt3v)](https://twitter.com/sgt3v)\n\n\n## OpenAI Unity Package\nAn unofficial Unity package that allows you to use the OpenAI API directly in the Unity game engine.\n\n## How To Use\n\n### Youtube Tutorials\nYou can find dedicated video tutorials for how to use this package in this YouTube playlist.\n\nhttps://www.youtube.com/playlist?list=PLrE-FZIEEls1-c7QifZYzeq50Id08FcJo\n\n\u003cimg width=\"655\" alt=\"image\" src=\"https://github.com/srcnalt/OpenAI-Unity/assets/3163281/cf0004cf-ff2d-4dad-955e-259bd5a77786\"\u003e\n\n### Importing the Package\nTo import the package, follow these steps:\n- Open Unity 2019 or later\n- Go to `Window \u003e Package Manager`\n- Click the `+` button and select `Add package from git URL`\n- Paste the repository URL https://github.com/srcnalt/OpenAI-Unity.git and click `Add`\n\n### Setting Up Your OpenAI Account\nTo use the OpenAI API, you need to have an OpenAI account. Follow these steps to create an account and generate an API key:\n\n- Go to https://openai.com/api and sign up for an account\n- Once you have created an account, go to https://beta.openai.com/account/api-keys\n- Create a new secret key and save it\n\n### Saving Your Credentials\nTo make requests to the OpenAI API, you need to use your API key and organization name (if applicable). To avoid exposing your API key in your Unity project, you can save it in your device's local storage.\n\nTo do this, follow these steps:\n\n- Create a folder called .openai in your home directory (e.g. `C:User\\UserName\\` for Windows or `~\\` for Linux or Mac)\n- Create a file called `auth.json` in the `.openai` folder\n- Add an api_key field and a organization field (if applicable) to the auth.json file and save it\n- Here is an example of what your auth.json file should look like:\n\n```json\n{\n    \"api_key\": \"sk-...W6yi\",\n    \"organization\": \"org-...L7W\"\n}\n```\n\nYou can also pass your API key into `OpenAIApi` ctor when creating an instance of it but again, this is not recommended!\n\n```csharp\nvar openai = new OpenAIApi(\"sk-Me8...6yi\");\n```\n\n**IMPORTANT:** Your API key is a secret. \nDo not share it with others or expose it in any client-side code (e.g. browsers, apps). \nIf 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.\n\n### Making Requests to OpenAPI\nYou can use the `OpenAIApi` class to make async requests to the OpenAI API.\n\nAll methods are asynchronous and can be accessed directly from an instance of the `OpenAIApi` class.\n\nHere is an example of how to make a request:\n\n```csharp\nprivate async void SendRequest()\n{\n    var req = new CreateChatCompletionRequest\n    {\n        Model = \"gpt-3.5-turbo\",\n        Messages = new List\u003cChatMessage\u003e()\n        {\n            new ChatMessage()\n            {\n                Role = \"user\",\n                Content = \"Hello!\"\n            }\n        }\n    };\n    var res = await openai.CreateChatCompletion(req);\n}\n```\n\nTo make a stream request, you can use the `CreateCompletionAsync` and `CreateChatCompletionAsync` methods. \nThese methods are going to set `Stream` property of the request to `true` and return responses through an onResponse callback.\nIn this case text responses are stored in `Delta` property of the `Choices` field in the Chat Completion.\n\n```csharp\nvar req = new CreateChatCompletionRequest{\n    Model = \"gpt-3.5-turbo\",\n    Messages = new List\u003cChatMessage\u003e\n    {\n        new ChatMessage()\n        {\n            Role = \"user\",\n            Content = \"Write a 100 word long short story in La Fontaine style.\"\n        }\n    },\n    Temperature = 0.7f,\n};\n    \nopenai.CreateChatCompletionAsync(req, \n    (responses) =\u003e {\n        var result = string.Join(\"\", responses.Select(response =\u003e response.Choices[0].Delta.Content));\n        Debug.Log(result);\n    }, \n    () =\u003e {\n        Debug.Log(\"completed\");\n    }, \n    new CancellationTokenSource()\n);\n```\n\n### Sample Projects\nThis package includes two sample scenes that you can import via the Package Manager:\n\n- **ChatGPT sample:** A simple ChatGPT like chat example.\n- **DallE sample:** A DALL.E text to image generation example.\n\n### Known Issues\n- **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\ndownloaded using UnityWebRequest until you run it out of localhost, on a server.\n\n- **Streamed Response is just blank in WebGL Build:** Unity 2020 WebGL has a bug where stream responses return empty. You can update and try with a newer version of Unity.\n\n### Supported Unity Versions for WebGL Builds\nThe following table shows the supported Unity versions for WebGL builds: \n\n| Unity Version | Windows | Linux | MacOS | WebGL | Android | IOS | Oculus 2|\n| --- | --- | --- | --- | --- | --- | --- | --- |\n| 2022.3.x | ✅ | ⚠️ | ⚠️ | ⚠️ | ✅ | ✅ | ✅ | ✅ |\n| 2021.3.x | ✅ | ⚠️ | ⚠️ | ⚠️ | ✅ | ✅ | ✅ | ⚠️ |\n| 2020.3.x | ✅ | ⚠️ | ⚠️ | ⚠️ | ✅ | ✅ | ✅ | ⚠️ |\n| 2019.4.x | ✅ | ⚠️ | ⚠️ | ⚠️ | ⚠️ | ⚠️ | ⚠️ | ⚠️ |\n\n|  |  |  |\n| --- | --- | --- |\n| ✅ = Supported | ⚠️ = Not Tested | ⛔ = Not Supported |\n\nPlease report any issues you encounter with builds.\n\n### Further Reading\nFor more information on how to use the various request parameters, please refer to the OpenAI documentation: https://platform.openai.com/docs/api-reference\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrcnalt%2FOpenAI-Unity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsrcnalt%2FOpenAI-Unity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrcnalt%2FOpenAI-Unity/lists"}