プラグインの作り方はこの記事から
furcraeaUEOpenAI ってプラグイン作って
D:\Sandbox\UE53CratePlugin\UE53CreatePlugin\Plugins\furcraeaUEOpenAI\Source\furcraeaUEOpenAI\furcraeaUEOpenAI.Build.cs のPublicDependencyModuleNamesに
“HTTP”, “Json”,
を追加
// Some copyright should be here...
using UnrealBuildTool;
public class furcraeaUEOpenAI : ModuleRules
{
public furcraeaUEOpenAI(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
PublicIncludePaths.AddRange(
new string[] {
// ... add public include paths required here ...
}
);
PrivateIncludePaths.AddRange(
new string[] {
// ... add other private include paths required here ...
}
);
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core","HTTP", "Json",
// ... add other public dependencies that you statically link with here ...
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
// ... add private dependencies that you statically link with here ...
}
);
DynamicallyLoadedModuleNames.AddRange(
new string[]
{
// ... add any modules that your module loads dynamically here ...
}
);
}
}
C++クラス作成で
でActorを選択
パブリック押して名前入力
.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CurlApiTest.generated.h"
UCLASS()
class FURCRAEAUEOPENAI_API ACurlApiTest : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACurlApiTest();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable, Category = "Http")
void CurlSendRequest3();
};
.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CurlApiTest.h"
// Sets default values
ACurlApiTest::ACurlApiTest()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ACurlApiTest::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACurlApiTest::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ACurlApiTest::CurlSendRequest3()
{
FString ApiKey = TEXT("sk-からはじまるKEY");
TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetURL("https://api.openai.com/v1/chat/completions");
HttpRequest->SetVerb("POST");
HttpRequest->SetHeader("Content-Type", "application/json");
HttpRequest->SetHeader("Authorization", "Bearer " + ApiKey);
// JSON Body
FString Content = TEXT(R"(
{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "Say this is a test!"}],
"temperature": 0.7
}
)");
HttpRequest->SetContentAsString(Content);
//
HttpRequest->OnProcessRequestComplete().BindLambda([](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful && Response.IsValid())
{
FString ResponseContent = Response->GetContentAsString();
UE_LOG(LogTemp, Warning, TEXT("%s"), *ResponseContent);
}
else
{
UE_LOG(LogTemp, Error, TEXT("Request failed"));
}
});
HttpRequest->ProcessRequest();
}
BP_CurlApiTestを作成
現在のレベルに置く
レベルを保存
新規ブループリントから Widget BluePrint >User Widgetで作成 WBP_ApiTest
ボタン配置
ボタンに下記ノード作成
作成BluePrintからゲームモード「GM_ApiTest_GameMode」を作成。
ワールドセッティングからレベルに割り当て
で再生
作ったボタンおしてみると!応答が返ってきた!!