Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
UE5.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
lucoiso committed May 24, 2024
1 parent 870b864 commit 8fd23c0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 68 deletions.
4 changes: 2 additions & 2 deletions HttpGPT.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 22,
"VersionName": "1.5.9",
"Version": 23,
"VersionName": "1.5.10",
"FriendlyName": "HttpGPT - GPT Integration",
"Description": "HttpGPT is an Unreal Engine plugin that facilitates integration with OpenAI's GPT based services (ChatGPT and DALL-E) through asynchronous REST requests, making it easy for developers to communicate with these services. HttpGPT also includes new Editor Tools to integrate Chat GPT and DALL-E image generation directly in the Engine.",
"Category": "Game Features",
Expand Down
64 changes: 32 additions & 32 deletions Source/HttpGPTChatModule/Private/Tasks/HttpGPTChatRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ bool UHttpGPTChatRequest::CanActivateTask() const

if (HttpGPT::Internal::HasEmptyParam(Messages))
{
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Can't activate task: Invalid Messages."), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Can't activate task: Invalid Messages."), *FString(__FUNCTION__), GetUniqueID());
return false;
}

Expand Down Expand Up @@ -116,7 +116,7 @@ FString UHttpGPTChatRequest::SetRequestContent()
return FString();
}

UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Mounting content"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Mounting content"), *FString(__FUNCTION__), GetUniqueID());

const TSharedPtr<FJsonObject> JsonRequest = MakeShared<FJsonObject>();
JsonRequest->SetStringField("model", UHttpGPTHelper::ModelToName(GetChatOptions().Model).ToString().ToLower());
Expand Down Expand Up @@ -157,7 +157,7 @@ FString UHttpGPTChatRequest::SetRequestContent()

if (UHttpGPTHelper::ModelSupportsChat(GetChatOptions().Model))
{
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Selected model supports Chat API. Mounting section history."), *FString(__func__),
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Selected model supports Chat API. Mounting section history."), *FString(__FUNCTION__),
GetUniqueID());

TArray<TSharedPtr<FJsonValue>> MessagesJson;
Expand All @@ -182,7 +182,7 @@ FString UHttpGPTChatRequest::SetRequestContent()
else
{
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Selected model does not supports Chat API. Using last message as prompt content."),
*FString(__func__), GetUniqueID());
*FString(__FUNCTION__), GetUniqueID());
JsonRequest->SetStringField("prompt", Messages.Top().Content);
}

Expand All @@ -206,8 +206,8 @@ void UHttpGPTChatRequest::OnProgressUpdated(const FString& Content, int32 BytesS

TArray<FString> Deltas = GetDeltasFromContent(Content);

UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Progress Updated"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Content: %s; Bytes Sent: %d; Bytes Received: %d"), *FString(__func__), GetUniqueID(),
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Progress Updated"), *FString(__FUNCTION__), GetUniqueID());
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Content: %s; Bytes Sent: %d; Bytes Received: %d"), *FString(__FUNCTION__), GetUniqueID(),
*Deltas.Top(), BytesSent, BytesReceived);

DeserializeStreamedResponse(Deltas);
Expand Down Expand Up @@ -245,7 +245,7 @@ void UHttpGPTChatRequest::OnProgressCompleted(const FString& Content, const bool

if (!bWasSuccessful || HttpGPT::Internal::HasEmptyParam(Content))
{
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Request failed"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Request failed"), *FString(__FUNCTION__), GetUniqueID());
AsyncTask(ENamedThreads::GameThread, [this]
{
RequestFailed.Broadcast();
Expand All @@ -254,8 +254,8 @@ void UHttpGPTChatRequest::OnProgressCompleted(const FString& Content, const bool
return;
}

UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Process Completed"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Content: %s"), *FString(__func__), GetUniqueID(), *Content);
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Process Completed"), *FString(__FUNCTION__), GetUniqueID());
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Content: %s"), *FString(__FUNCTION__), GetUniqueID(), *Content);

if (!GetChatOptions().bStream)
{
Expand Down Expand Up @@ -283,7 +283,7 @@ void UHttpGPTChatRequest::OnProgressCompleted(const FString& Content, const bool
}
else
{
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Request failed"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Request failed"), *FString(__FUNCTION__), GetUniqueID());
AsyncTask(ENamedThreads::GameThread, [this]
{
FScopeLock Lock(&Mutex);
Expand Down Expand Up @@ -342,16 +342,16 @@ void UHttpGPTChatRequest::DeserializeSingleResponse(const FString& Content)

Response.bSuccess = true;

Response.ID = *JsonResponse->GetStringField("id");
Response.Object = *JsonResponse->GetStringField("object");
Response.Created = JsonResponse->GetNumberField("created");
Response.ID = *JsonResponse->GetStringField(TEXT("id"));
Response.Object = *JsonResponse->GetStringField(TEXT("object"));
Response.Created = JsonResponse->GetNumberField(TEXT("created"));

const TArray<TSharedPtr<FJsonValue>> ChoicesArr = JsonResponse->GetArrayField("choices");
const TArray<TSharedPtr<FJsonValue>> ChoicesArr = JsonResponse->GetArrayField(TEXT("choices"));

for (auto Iterator = ChoicesArr.CreateConstIterator(); Iterator; ++Iterator)
{
const TSharedPtr<FJsonObject> ChoiceObj = (*Iterator)->AsObject();
const int32 ChoiceIndex = ChoiceObj->GetIntegerField("index");
const int32 ChoiceIndex = ChoiceObj->GetIntegerField(TEXT("index"));

FHttpGPTChatChoice* Choice = Response.Choices.FindByPredicate([this, ChoiceIndex](const FHttpGPTChatChoice& Element)
{
Expand All @@ -365,54 +365,54 @@ void UHttpGPTChatRequest::DeserializeSingleResponse(const FString& Content)
Choice = &Response.Choices.Add_GetRef(NewChoice);
}

if (const TSharedPtr<FJsonObject>* MessageObj; ChoiceObj->TryGetObjectField("message", MessageObj))
if (const TSharedPtr<FJsonObject>* MessageObj; ChoiceObj->TryGetObjectField(TEXT("message"), MessageObj))
{
if (FString RoleStr; (*MessageObj)->TryGetStringField("role", RoleStr))
if (FString RoleStr; (*MessageObj)->TryGetStringField(TEXT("role"), RoleStr))
{
Choice->Message.Role = RoleStr == "user" ? EHttpGPTChatRole::User : EHttpGPTChatRole::Assistant;
}

if (FString ContentStr; (*MessageObj)->TryGetStringField("content", ContentStr))
if (FString ContentStr; (*MessageObj)->TryGetStringField(TEXT("content"), ContentStr))
{
Choice->Message.Content = ContentStr;
}

if (const TSharedPtr<FJsonObject>* FunctionObj; (*MessageObj)->TryGetObjectField("function_call", FunctionObj))
if (const TSharedPtr<FJsonObject>* FunctionObj; (*MessageObj)->TryGetObjectField(TEXT("function_call"), FunctionObj))
{
if (FString FunctionNameStr; (*FunctionObj)->TryGetStringField("name", FunctionNameStr))
if (FString FunctionNameStr; (*FunctionObj)->TryGetStringField(TEXT("name"), FunctionNameStr))
{
Choice->Message.FunctionCall.Name = *FunctionNameStr;
}
if (FString FunctionArgumentsStr; (*FunctionObj)->TryGetStringField("arguments", FunctionArgumentsStr))
if (FString FunctionArgumentsStr; (*FunctionObj)->TryGetStringField(TEXT("arguments"), FunctionArgumentsStr))
{
Choice->Message.FunctionCall.Arguments = FunctionArgumentsStr;
}
}
}
else if (const TSharedPtr<FJsonObject>* DeltaObj; ChoiceObj->TryGetObjectField("delta", DeltaObj))
else if (const TSharedPtr<FJsonObject>* DeltaObj; ChoiceObj->TryGetObjectField(TEXT("delta"), DeltaObj))
{
if (FString RoleStr; (*DeltaObj)->TryGetStringField("role", RoleStr))
if (FString RoleStr; (*DeltaObj)->TryGetStringField(TEXT("role"), RoleStr))
{
Choice->Message.Role = UHttpGPTHelper::NameToRole(*RoleStr);
}
else if (FString ContentStr; (*DeltaObj)->TryGetStringField("content", ContentStr))
else if (FString ContentStr; (*DeltaObj)->TryGetStringField(TEXT("content"), ContentStr))
{
Choice->Message.Content += ContentStr;
}

if (const TSharedPtr<FJsonObject>* FunctionObj; (*DeltaObj)->TryGetObjectField("function_call", FunctionObj))
if (const TSharedPtr<FJsonObject>* FunctionObj; (*DeltaObj)->TryGetObjectField(TEXT("function_call"), FunctionObj))
{
if (FString FunctionNameStr; (*FunctionObj)->TryGetStringField("name", FunctionNameStr))
if (FString FunctionNameStr; (*FunctionObj)->TryGetStringField(TEXT("name"), FunctionNameStr))
{
Choice->Message.FunctionCall.Name = *FunctionNameStr;
}
if (FString FunctionArgumentsStr; (*FunctionObj)->TryGetStringField("arguments", FunctionArgumentsStr))
if (FString FunctionArgumentsStr; (*FunctionObj)->TryGetStringField(TEXT("arguments"), FunctionArgumentsStr))
{
Choice->Message.FunctionCall.Arguments += FunctionArgumentsStr;
}
}
}
else if (FString MessageText; ChoiceObj->TryGetStringField("text", MessageText))
else if (FString MessageText; ChoiceObj->TryGetStringField(TEXT("text"), MessageText))
{
Choice->Message.Role = EHttpGPTChatRole::Assistant;
Choice->Message.Content += MessageText;
Expand All @@ -423,16 +423,16 @@ void UHttpGPTChatRequest::DeserializeSingleResponse(const FString& Content)
Choice->Message.Content.RemoveAt(0);
}

if (FString FinishReasonStr; ChoiceObj->TryGetStringField("finish_reason", FinishReasonStr))
if (FString FinishReasonStr; ChoiceObj->TryGetStringField(TEXT("finish_reason"), FinishReasonStr))
{
Choice->FinishReason = *FinishReasonStr;
}
}

if (const TSharedPtr<FJsonObject>* UsageObj; JsonResponse->TryGetObjectField("usage", UsageObj))
if (const TSharedPtr<FJsonObject>* UsageObj; JsonResponse->TryGetObjectField(TEXT("usage"), UsageObj))
{
Response.Usage = FHttpGPTChatUsage((*UsageObj)->GetNumberField("prompt_tokens"), (*UsageObj)->GetNumberField("completion_tokens"),
(*UsageObj)->GetNumberField("total_tokens"));
Response.Usage = FHttpGPTChatUsage((*UsageObj)->GetNumberField(TEXT("prompt_tokens")), (*UsageObj)->GetNumberField(TEXT("completion_tokens")),
(*UsageObj)->GetNumberField(TEXT("total_tokens")));
}
}

Expand Down
40 changes: 22 additions & 18 deletions Source/HttpGPTCommonModule/Private/Tasks/HttpGPTBaseTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void UHttpGPTBaseTask::Activate()
{
Super::Activate();

UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Activating task"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Activating task"), *FString(__FUNCTION__), GetUniqueID());

bIsTaskActive = true;
if (!CommonOptions.Endpoint.EndsWith(TEXT("/")))
Expand All @@ -39,7 +39,7 @@ void UHttpGPTBaseTask::Activate()

if (!CanActivateTask())
{
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Failed to activate task."), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Failed to activate task."), *FString(__FUNCTION__), GetUniqueID());
RequestFailed.Broadcast();
SetReadyToDestroy();
return;
Expand Down Expand Up @@ -71,7 +71,7 @@ void UHttpGPTBaseTask::StopHttpGPTTask()
return;
}

UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Stopping task"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Stopping task"), *FString(__FUNCTION__), GetUniqueID());

bIsTaskActive = false;

Expand All @@ -93,7 +93,7 @@ void UHttpGPTBaseTask::SetReadyToDestroy()
return;
}

UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Setting task as Ready to Destroy"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Setting task as Ready to Destroy"), *FString(__FUNCTION__), GetUniqueID());

#if WITH_EDITOR
if (bIsEditorTask)
Expand Down Expand Up @@ -132,7 +132,7 @@ void UHttpGPTBaseTask::PrePIEEnded(bool bIsSimulating)
return;
}

UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Trying to finish task due to PIE end"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Trying to finish task due to PIE end"), *FString(__FUNCTION__), GetUniqueID());

bEndingPIE = true;
StopHttpGPTTask();
Expand All @@ -143,7 +143,7 @@ bool UHttpGPTBaseTask::CanActivateTask() const
{
if (HttpGPT::Internal::HasEmptyParam(GetCommonOptions().APIKey))
{
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Can't activate task: Invalid API Key."), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Can't activate task: Invalid API Key."), *FString(__FUNCTION__), GetUniqueID());
return false;
}

Expand All @@ -164,7 +164,7 @@ void UHttpGPTBaseTask::InitializeRequest()
{
FScopeLock Lock(&Mutex);

UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Initializing request object"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Initializing request object"), *FString(__FUNCTION__), GetUniqueID());

HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetURL(GetEndpointURL());
Expand All @@ -182,11 +182,15 @@ void UHttpGPTBaseTask::BindRequestCallbacks()
return;
}

UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Binding callbacks"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Binding callbacks"), *FString(__FUNCTION__), GetUniqueID());

if (CanBindProgress())
{
#if ENGINE_MAJOR_VERSION > 5 || (ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 4)
HttpRequest->OnRequestProgress64().BindLambda([this](const FHttpRequestPtr& Request, int32 BytesSent, int32 BytesReceived)
#else
HttpRequest->OnRequestProgress().BindLambda([this](const FHttpRequestPtr& Request, int32 BytesSent, int32 BytesReceived)
#endif
{
const FScopeTryLock Lock(&Mutex);

Expand Down Expand Up @@ -226,7 +230,7 @@ void UHttpGPTBaseTask::SendRequest()

if (!HttpRequest.IsValid())
{
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Failed to send request: Request object is invalid"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Failed to send request: Request object is invalid"), *FString(__FUNCTION__), GetUniqueID());

AsyncTask(ENamedThreads::GameThread, [this]
{
Expand All @@ -237,12 +241,12 @@ void UHttpGPTBaseTask::SendRequest()
return;
}

UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Sending request"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Sending request"), *FString(__FUNCTION__), GetUniqueID());

if (HttpRequest->ProcessRequest())
{
UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Request sent"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Request content body:\n%s"), *FString(__func__), GetUniqueID(), *ContentString);
UE_LOG(LogHttpGPT, Display, TEXT("%s (%d): Request sent"), *FString(__FUNCTION__), GetUniqueID());
UE_LOG(LogHttpGPT_Internal, Display, TEXT("%s (%d): Request content body:\n%s"), *FString(__FUNCTION__), GetUniqueID(), *ContentString);

AsyncTask(ENamedThreads::GameThread, [this]
{
Expand All @@ -251,7 +255,7 @@ void UHttpGPTBaseTask::SendRequest()
}
else
{
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Failed to initialize the request process"), *FString(__func__), GetUniqueID());
UE_LOG(LogHttpGPT, Error, TEXT("%s (%d): Failed to initialize the request process"), *FString(__FUNCTION__), GetUniqueID());
AsyncTask(ENamedThreads::GameThread, [this]
{
RequestFailed.Broadcast();
Expand All @@ -262,18 +266,18 @@ void UHttpGPTBaseTask::SendRequest()

const bool UHttpGPTBaseTask::CheckError(const TSharedPtr<FJsonObject>& JsonObject, FHttpGPTCommonError& OutputError) const
{
if (JsonObject->HasField("error"))
if (JsonObject->HasField(TEXT("error")))
{
const TSharedPtr<FJsonObject> ErrorObj = JsonObject->GetObjectField("error");
if (FString ErrorMessage; ErrorObj->TryGetStringField("message", ErrorMessage))
const TSharedPtr<FJsonObject> ErrorObj = JsonObject->GetObjectField(TEXT("error"));
if (FString ErrorMessage; ErrorObj->TryGetStringField(TEXT("message"), ErrorMessage))
{
OutputError.Message = *ErrorMessage;
}
if (FString ErrorCode; ErrorObj->TryGetStringField("code", ErrorCode))
if (FString ErrorCode; ErrorObj->TryGetStringField(TEXT("code"), ErrorCode))
{
OutputError.Code = *ErrorCode;
}
if (FString ErrorType; ErrorObj->TryGetStringField("type", ErrorType))
if (FString ErrorType; ErrorObj->TryGetStringField(TEXT("type"), ErrorType))
{
OutputError.Type = *ErrorType;
}
Expand Down
10 changes: 5 additions & 5 deletions Source/HttpGPTEditorModule/Private/Chat/SHttpGPTChatView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void SHttpGPTChatView::Construct(const FArguments& InArgs)
{
SetSessionID(InArgs._SessionID);

ModelsComboBox = SNew(STextComboBox).OptionsSource(&AvailableModels).ToolTipText(FText::FromString(TEXT("GPT Model")));
SAssignNew(ModelsComboBox, STextComboBox).OptionsSource(&AvailableModels).ToolTipText(FText::FromString(TEXT("GPT Model")));

for (const FName& ModelName : UHttpGPTHelper::GetAvailableGPTModels())
{
Expand Down Expand Up @@ -230,7 +230,7 @@ FString SHttpGPTChatView::GetDefaultSystemContext() const
}

FString SupportedModels;
for (const FTextDisplayStringPtr& Model : AvailableModels)
for (const TSharedPtr<FString>& Model : AvailableModels)
{
SupportedModels.Append(*Model.Get() + ", ");
}
Expand Down Expand Up @@ -298,16 +298,16 @@ void SHttpGPTChatView::LoadChatHistory()
const TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(FileContent);
if (FJsonSerializer::Deserialize(Reader, JsonParsed))
{
const TArray<TSharedPtr<FJsonValue>> SessionData = JsonParsed->GetArrayField("Data");
const TArray<TSharedPtr<FJsonValue>> SessionData = JsonParsed->GetArrayField(TEXT("Data"));
for (const TSharedPtr<FJsonValue>& Item : SessionData)
{
if (const TSharedPtr<FJsonObject> MessageItObj = Item->AsObject())
{
if (FString RoleString; MessageItObj->TryGetStringField("role", RoleString))
if (FString RoleString; MessageItObj->TryGetStringField(TEXT("role"), RoleString))
{
const EHttpGPTChatRole Role = UHttpGPTHelper::NameToRole(*RoleString);

if (FString Message; MessageItObj->TryGetStringField("content", Message))
if (FString Message; MessageItObj->TryGetStringField(TEXT("content"), Message))
{
if (Role == EHttpGPTChatRole::System && Message == GetDefaultSystemContext())
{
Expand Down
2 changes: 1 addition & 1 deletion Source/HttpGPTEditorModule/Private/Chat/SHttpGPTChatView.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SHttpGPTChatView final : public SCompoundWidget
TSharedPtr<class SEditableTextBox> InputTextBox;

TSharedPtr<class STextComboBox> ModelsComboBox;
TArray<FTextDisplayStringPtr> AvailableModels;
TArray<TSharedPtr<FString>> AvailableModels;

TWeakObjectPtr<class UHttpGPTChatRequest> RequestReference;
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class SHttpGPTImageGenView final : public SCompoundWidget
TSharedPtr<class SEditableTextBox> InputTextBox;

TSharedPtr<class STextComboBox> ImageNumComboBox;
TArray<FTextDisplayStringPtr> ImageNum;
TArray<TSharedPtr<FString>> ImageNum;

TSharedPtr<class STextComboBox> ImageSizeComboBox;
TArray<FTextDisplayStringPtr> ImageSize;
TArray<TSharedPtr<FString>> ImageSize;
};
Loading

0 comments on commit 8fd23c0

Please sign in to comment.