wanghuabing / UnrealOpenAIPlugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Complete Unreal Engine plugin for the OpenAI API

This plugin is a comprehensive Unreal Engine wrapper for the OpenAI API. It supports all OpenAI endpoints, including:

All requests are available in both C++ and Blueprints:

void AAPIOverview::CreateImage()
{
    auto* Provider = NewObject<UOpenAIProvider>();
    Provider->SetLogEnabled(false);
    Provider->OnRequestError().AddUObject(this, &ThisClass::OnRequestError);
    Provider->OnCreateImageCompleted().AddLambda(
        [](const FImageResponse& Response)
        {
            FString OutputString{};
            Algo::ForEach(Response.Data, [&](const FString& Data) { OutputString.Append(Data).Append(LINE_TERMINATOR); });
            UE_LOG(LogAPIOverview, Display, TEXT("%s"), *OutputString);
        });

    FOpenAIImage Image;
    Image.Prompt = "Tiger is eating pizza";
    Image.Size = UOpenAIFuncLib::OpenAIImageSizeToString(EImageSize::Size_512x512);
    Image.Response_Format = UOpenAIFuncLib::OpenAIImageFormatToString(EOpenAIImageFormat::URL);
    Image.N = 2;

    Provider->CreateImage(Image, Auth);
}

Supported Unreal Engine Versions

  • Unreal Engine 5.2

Installation

Marketplace link

C++

  1. Create a new C++ project in Unreal Engine.
  2. Create a Plugins folder at the root of your project.
  3. Add the OpenAI plugin to your Unreal Engine Plugins folder.
  4. The easiest way to add plugins is by adding the current plugin as a submodule. Use the following command in the root folder of your project:
git submodule add https://github.com/life-exe/UnrealOpenAIPlugin Plugins/OpenAI
  1. Alternatively, you can download the source code from the current repository and copy the files to your Plugins folder.
  2. When complete, your Unreal Engine project structure should resemble the following:

  1. Do the authentication steps
  2. Generate Visual Studio project files.
  3. Build your project and run Editor.
  4. In the Editor, navigate to Edit->Plugins. Find and activate the OpenAI plugin.
  5. Restart the editor.
  6. Start using the plugin.

Blueprints

  1. Create a blueprint project.
  2. Create a Plugins folder in the root of your project.
  3. Download precompiled plugin from the Releases.
  4. Unzip plugin to the Plugins folder, specifically to Plugins/OpenAI.
  5. Do the authentication steps
  6. Run the Editor <YourProjectName>.uproject
  7. In the Editor, navigate to Edit->Plugins. Find and activate the OpenAI plugin.
  8. Restart the editor.
  9. Start using the plugin.

Authentication

  1. Create an OpenAI account

  2. Generate and store your API Key: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  3. Record your Organization ID: org-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  4. At the root of your Unreal Engine project, create a file named OpenAIAuth.ini with the following content:

APIKey=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
OrganizationID=org-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  1. Once completed, your Unreal Engine project structure might look like this:

  1. Actually you can left OrganizationID empty. It doesn't affect on auth.

Finally, compile your project and launch the Unreal Editor.

Examples of Usage

Widget example

This provides a basic example of request usage with Blueprints.

  1. Make sure that plugin content activated:

  1. Open the plugin content folder.
  2. Open the level:

  1. Run the game.
  2. Test the API with the widget examples:

Feel free to modify requests in the widget code to test the API with different parameters:

Unreal Editor Chat-GPT

This is the Chat-GPT implementation with token streaming support.

  1. Open the plugin content folder.
  2. Navigate to the EditorWidgets folder.
  3. Right-click on the ChatGPT editor utility widget:

  1. Start chatting:

  1. You can save the chat history to a file using the Dump button. The specific location where the history is saved can be checked in the chat or logs:

Blueprint Nodes Overview

This blueprint demonstrates all available nodes.

  1. Open the plugin content folder:

  1. Open BP_APIOverview blueprint.
  2. Check the available nodes: functions and structs:

There are also several nodes that could be useful in other projects. Feel free to copy and paste the plugin code if you need them:

C++ example

  1. Open Plugins\OpenAI\Source\OpenAI\Private\Sample\APIOverview.cpp actor
  2. Uncomment the function that executes the request you want to test. Navigate to the function and adjust all the request parameters as needed.
  3. Compile your project and open Unreal Editor.
  4. Drop the AAPIOverview actor into any level.
  5. Run the game.
  6. Check the Output Log.

Plugin structure:

  • OpenAIModule - core classes.
  • OpenAIEditorModule - Chat-GPT editor utility widget.
  • OpenAITestRunnerModule - unit tests.

Documentation

I highly recommend reading the OpenAI documentation for a better understanding of how all requests work:

In addition plugin code has its own documentation generated with help of Doxygen:

You can generate the plugin documentation locally by following these steps:

  1. Update all submodules. You can use the batch script located at the root of the plugin folder: update_submodules.bat
  2. Ensure that Doxygen is installed on your system.
  3. Ensure that Python is installed on your system.
  4. Generate the documentation using the batch script at the root of the plugin folder: generate_docs.bat
  5. After the generation process, the documentation will be available in the Documentation folder at the root of the plugin: Documentation\html\index.html

Tests

Unit tests are available in the OpenAITestRunnerModule. You can initiate them using the Session Frontend:

Packaging

  1. Verify that your .uproject file contains the following:
{
    "Name": "OpenAI",
    "Enabled": true
}
  1. Package the project as you always do (kudos if you use a CI solution like Jenkins).
  2. You can handle authentication in your project as you see fit. However, if you choose to use a file-based method, such as in a plugin, please remember to include the OpenAIAuth.ini file in your packaged folder Build/Windows/<YourProjectName>/OpenAIAuth.ini If you are having problems loading the file, please check the logs to see where it might be located:
LogOpenAIFuncLib: Error: Failed loading file: C:/_Projects/UE5/OpenAICpp/Build/Windows/OpenAICpp/OpenAIAuth.ini

Limitations

  • Chat GPT-4 models are not available for everyone via the API. You need to request access for it:

  • OpenAI hosts a variety of different models. Please check the models that are compatible with the particular request.

Miscellaneous

I would appreciate bug reports and pull-request fixes.

Enjoy! 🚀️

About


Languages

Language:C++ 81.1%Language:C 17.8%Language:C# 0.9%Language:Batchfile 0.2%