// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Http.h"
#include "Json.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 SendChatCompletionRequest();
void ProcessResponseSimple(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
//void ProcessResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
};
.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CurlApiTest.h"
#include "CurlRequest.h"
#include "Http.h"
#include "Json.h"
#include "Misc/Paths.h"
#include "Misc/ConfigCacheIni.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();
SendChatCompletionRequest();
}
// Called every frame
void ACurlApiTest::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ACurlApiTest::SendChatCompletionRequest()
{
FString ApiKey = TEXT("sk-から始まる秘密鍵");
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().BindUObject(this, &ACurlApiTest::ProcessResponseSimple);
/*
HttpRequest->OnProcessRequestComplete().BindLambda([this](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
ProcessResponse(Request, Response, bWasSuccessful);
});
*/
HttpRequest->ProcessRequest();
}
void ACurlApiTest::ProcessResponseSimple(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful && Response.IsValid())
{
FString ResponseContent = Response->GetContentAsString();
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(ResponseContent);
if (FJsonSerializer::Deserialize(Reader, JsonObject) && JsonObject.IsValid())
{
FString MessageContent = JsonObject->GetArrayField("choices")[0]->AsObject()->GetObjectField("message")->GetStringField("content");
UE_LOG(LogTemp, Warning, TEXT("Response Message: %s"), *MessageContent);
}
else
{
UE_LOG(LogTemp, Error, TEXT("Failed to parse JSON response"));
}
}
else
{
UE_LOG(LogTemp, Error, TEXT("Request failed"));
}
}
Running D:/Program Files/Epic Games/UE_5.3/Engine/Build/BatchFiles/Build.bat -projectfiles -project="D:/Sandbox/UE53CratePlugin/UE5_VS/UE5_VS.uproject" -game -rocket -progress
Using bundled DotNet SDK version: 6.0.302
Running UnrealBuildTool: dotnet "..\..\Engine\Binaries\DotNET\UnrealBuildTool\UnrealBuildTool.dll" -projectfiles -project="D:/Sandbox/UE53CratePlugin/UE5_VS/UE5_VS.uproject" -game -rocket -progress
Log file: C:\Users\furcr\AppData\Local\UnrealBuildTool\Log_GPF.txt
Generating VisualStudio2019 project files:
Discovering modules, targets and source code for project...
Visual Studio 2019 does not support .NET 6.0 C# projects, these projects will not be added to the generated solution.
Please generate the Visual Studio 2022 solution if .NET 6.0 C# project support is required.
Microsoft platform targets must be compiled with Visual Studio 2022 17.4 (MSVC 14.34.x) or later for the installed engine. Please update Visual Studio 2022 and ensure no configuration is forcing WindowsTargetRules.Compiler to VisualStudio2019. The current compiler version was detected as: 14.29.30154