I am researching and figuring out how to train easier and better with ML agents using other APIs. This is just preliminary idea and get it to final stage of the code. If anyone want to try it, please contact me and I will provide code, and see if it is easier for you. When research is done, I will add this to Unity Asset Store.
The summarized idea of the C# script for Unity, OctoAIIntegration.cs(OpenAIIntegration.cs), is designed to facilitate communication between a Unity application and an external AI service provided by OctoAI via its API.
The script sends natural language prompts to the OctoAI API and handles the responses asynchronously. Here’s a breakdown of its main components and functionality:
-
Using Directives
-
using UnityEngine; includes the base Unity namespace which provides access to all the fundamental Unity functionalities.
-
using UnityEngine.Networking; includes the Unity Networking namespace that allows sending and receiving network requests.
-
using System.Collections; includes support for coroutines, which are used for asynchronous operations.
-
OctoAIIntegration Class
-
Inherits from MonoBehaviour, making it a component that can be attached to Unity GameObjects. This allows it to interact with Unity’s update loop and scene elements.
-
Class Variables
-
apiURL: A string containing the URL to the OctoAI/OpenAI API endpoint responsible for processing chat completion requests.
-
apiKey: Your personal API key for authenticating with the OctoAI/OpenAI service. You need to replace “YOUR_API_KEY_HERE” with the actual key provided by OctoAI.
-
SendRequestToOctoAI(OpenAI) Method
-
Public method that takes a string prompt and a callback function of type System.Action. This method initiates the process of sending a request to the OctoAI/OpenAI API by starting the coroutine SendRequestCoroutine with the given prompt and callback.
-
SendRequestCoroutine IEnumerator
-
A coroutine method that asynchronously sends a request to the OctoAI/OpenAI API.
-
It constructs a request body with necessary parameters such as model, prompt, and max_tokens. The model specifies which AI model to use for the completion, prompt is the text input for which you seek a completion, and max_tokens limits the length of the generated response.
-
requestBodyJson: The request body is serialized into a JSON string using JsonUtility.ToJson.
-
UnityWebRequest: This object is used to configure and send the HTTP POST request. The request URL is the apiURL, and the serialized JSON is the body of the request.
-
SetRequestHeader is called to add necessary HTTP headers, including Content-Type as “application/json” and Authorization with the Bearer token (your apiKey).
-
uploadHandler is assigned a new UploadHandlerRaw initialized with the byte array of the JSON request body, allowing the data to be sent as the request body.
-
downloadHandler is assigned a new DownloadHandlerBuffer, which will store the API response.
-
The coroutine then waits (yield return) for the request to complete.
-
Upon completion, if there are no connection or protocol errors (UnityWebRequest.Result.ConnectionError, UnityWebRequest.Result.ProtocolError), it invokes the callback function with the response text as its argument. This allows the calling context to process the API’s response.
-
If an error occurs, it logs the error using Debug.LogError.
Summary
This script is designed to abstract the complexity of communicating with the OctoAI/OpenAI API, handling asynchronous network operations within Unity. It allows Unity developers to send prompts to OctoAI, receive generated text completions, and then use these completions within their Unity projects, such as for controlling NPCs, generating dynamic dialogues, or other creative applications that benefit from natural language processing.