|
|
@ -17,6 +17,8 @@ void UAIBaseManager::InitAIManager(UAIBaseConfig* AIConfig, bool DebugMode, AAct |
|
|
CurrentConfig = AIConfig; |
|
|
CurrentConfig = AIConfig; |
|
|
CurrentConfig->AddToRoot(); |
|
|
CurrentConfig->AddToRoot(); |
|
|
|
|
|
|
|
|
|
|
|
UAIBaseManager::AddRepeatSystemInstruction(); |
|
|
|
|
|
|
|
|
// Store the reference actor for world context
|
|
|
// Store the reference actor for world context
|
|
|
WorldReferenceActor = InWorldReferenceActor; |
|
|
WorldReferenceActor = InWorldReferenceActor; |
|
|
|
|
|
|
|
|
@ -122,7 +124,7 @@ void UAIBaseManager::SetNewState(EAvatarCoreAIState NewState, bool ForceState) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void UAIBaseManager::SendResponse(const FString& Response, bool NotifyDelay = false, FString OverrideInstruction = "") |
|
|
void UAIBaseManager::SendResponse(const FString& Response, bool NotifyDelay = false, bool TriggerResponse = true) |
|
|
{ |
|
|
{ |
|
|
AnswerCache.Empty(); |
|
|
AnswerCache.Empty(); |
|
|
ResponseID++; |
|
|
ResponseID++; |
|
|
@ -144,10 +146,10 @@ void UAIBaseManager::RepeatText(FString TextToRepeat, bool DoRephrase) |
|
|
ResponseID++; |
|
|
ResponseID++; |
|
|
FString Instruction; |
|
|
FString Instruction; |
|
|
if (DoRephrase) |
|
|
if (DoRephrase) |
|
|
Instruction = "Repeat the text in your own words."; |
|
|
Instruction = "Repeat the text in your own words: " + TextToRepeat; |
|
|
else |
|
|
else |
|
|
Instruction = "Repeat the text exactly word for word."; |
|
|
Instruction = "[REPEAT] " + TextToRepeat; |
|
|
SendResponse(TextToRepeat, false, Instruction); |
|
|
SendResponse(Instruction, false, true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void UAIBaseManager::ClearAI() |
|
|
void UAIBaseManager::ClearAI() |
|
|
@ -378,36 +380,100 @@ void UAIBaseManager::OnAIResponse(const FString& Chunk, bool IsFinal) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void UAIBaseManager::AddSystemInstruction(const FName Name, const FString NewSystemInstruction) |
|
|
void UAIBaseManager::AddSystemInstruction(const FName Name, const FString NewSystemInstruction, bool AddAsFirst = false) |
|
|
{ |
|
|
{ |
|
|
UAIBaseManager::RemoveSystemInstruction(Name); |
|
|
UAIBaseManager::RemoveSystemInstruction(Name); |
|
|
FSystemInstruction tmpEntry; |
|
|
FSystemInstruction tmpEntry; |
|
|
tmpEntry.Name = Name; |
|
|
tmpEntry.Name = Name; |
|
|
tmpEntry.Instruction = NewSystemInstruction; |
|
|
tmpEntry.Instruction = NewSystemInstruction; |
|
|
CurrentConfig->SystemPromps.Add(tmpEntry); |
|
|
|
|
|
|
|
|
if(AddAsFirst) |
|
|
|
|
|
{ |
|
|
|
|
|
TArray<FSystemInstruction> tmpSystemPrompts; |
|
|
|
|
|
tmpSystemPrompts.Add(tmpEntry); |
|
|
|
|
|
tmpSystemPrompts.Append(CurrentConfig->SystemPrompts); |
|
|
|
|
|
CurrentConfig->SystemPrompts = tmpSystemPrompts; |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
CurrentConfig->SystemPrompts.Add(tmpEntry); |
|
|
|
|
|
|
|
|
BroadcastAILog(FString::Printf(TEXT("AI Manager added System Instruction %s"), *Name.ToString())); |
|
|
BroadcastAILog(FString::Printf(TEXT("AI Manager added System Instruction %s"), *Name.ToString())); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UAIBaseManager::ClearAllSystemInstructios() |
|
|
void UAIBaseManager::ClearAllSystemInstructios() |
|
|
{ |
|
|
{ |
|
|
CurrentConfig->SystemPromps.Empty(); |
|
|
CurrentConfig->SystemPrompts.Empty(); |
|
|
BroadcastAILog(FString::Printf(TEXT("AI Manager wiped all System Instructions"))); |
|
|
BroadcastAILog(FString::Printf(TEXT("AI Manager wiped all System Instructions"))); |
|
|
|
|
|
UAIBaseManager::AddRepeatSystemInstruction(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void UAIBaseManager::AddRepeatSystemInstruction() |
|
|
|
|
|
{ |
|
|
|
|
|
UAIBaseManager::AddSystemInstruction(TEXT("Repeat Text"), TEXT("If the text starts with [REPEAT], repeat the text exactly word for word."), true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void UAIBaseManager::RemoveSystemInstruction(const FName Name) |
|
|
void UAIBaseManager::RemoveSystemInstruction(const FName Name) |
|
|
{ |
|
|
{ |
|
|
// Iterate in reverse to safely remove while iterating
|
|
|
// Iterate in reverse to safely remove while iterating
|
|
|
for (int32 i = CurrentConfig->SystemPromps.Num() - 1; i >= 0; --i) |
|
|
for (int32 i = CurrentConfig->SystemPrompts.Num() - 1; i >= 0; --i) |
|
|
{ |
|
|
{ |
|
|
if (CurrentConfig->SystemPromps[i].Name == Name) |
|
|
if (CurrentConfig->SystemPrompts[i].Name == Name) |
|
|
{ |
|
|
{ |
|
|
CurrentConfig->SystemPromps.RemoveAt(i); |
|
|
CurrentConfig->SystemPrompts.RemoveAt(i); |
|
|
BroadcastAILog(FString::Printf(TEXT("AI Manager removed System Instruction %s"), *Name.ToString())); |
|
|
BroadcastAILog(FString::Printf(TEXT("AI Manager removed System Instruction %s"), *Name.ToString())); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
FString UAIBaseManager::GetSystemInstructionPromptString(bool AsJsonString = false) |
|
|
|
|
|
{ |
|
|
|
|
|
FString prompt; |
|
|
|
|
|
|
|
|
|
|
|
if(AsJsonString) |
|
|
|
|
|
{ |
|
|
|
|
|
TArray<TSharedPtr<FJsonValue>> JsonArray; |
|
|
|
|
|
|
|
|
|
|
|
for (const FSystemInstruction& Item : CurrentConfig->SystemPrompts) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!Item.Instruction.IsEmpty()) |
|
|
|
|
|
{ |
|
|
|
|
|
// Each entry: { "Name": "Instruction" }
|
|
|
|
|
|
TSharedPtr<FJsonObject> Obj = MakeShared<FJsonObject>(); |
|
|
|
|
|
Obj->SetStringField(Item.Name.ToString(), Item.Instruction); |
|
|
|
|
|
JsonArray.Add(MakeShared<FJsonValueObject>(Obj)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Add a fallback if no entries exist
|
|
|
|
|
|
if (JsonArray.Num() == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
TSharedPtr<FJsonObject> DefaultObj = MakeShared<FJsonObject>(); |
|
|
|
|
|
DefaultObj->SetStringField(TEXT("Default"), TEXT("Just do exactly what the user wants you to do.")); |
|
|
|
|
|
JsonArray.Add(MakeShared<FJsonValueObject>(DefaultObj)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 2) Serialize array into a single JSON string
|
|
|
|
|
|
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&prompt); |
|
|
|
|
|
FJsonSerializer::Serialize(JsonArray, Writer); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
for (int32 Index = 0; Index < CurrentConfig->SystemPrompts.Num(); ++Index) |
|
|
|
|
|
{ |
|
|
|
|
|
FSystemInstruction& Item = CurrentConfig->SystemPrompts[Index]; |
|
|
|
|
|
if (!Item.Instruction.IsEmpty()) |
|
|
|
|
|
{ |
|
|
|
|
|
prompt += "# " + Item.Name.ToString() + TEXT("\r\n"); |
|
|
|
|
|
prompt += Item.Instruction; |
|
|
|
|
|
if(Index < CurrentConfig->SystemPrompts.Num() - 1) |
|
|
|
|
|
prompt += TEXT("\r\n\r\n"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return prompt; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void UAIBaseManager::ResetRequestTimeout() |
|
|
void UAIBaseManager::ResetRequestTimeout() |
|
|
{ |
|
|
{ |
|
|
UAIBaseManager::ClearRequestTimeout(); |
|
|
UAIBaseManager::ClearRequestTimeout(); |
|
|
|