Skip to content

Commit

Permalink
AI chat: When a response contains the bot's own name it properly spli…
Browse files Browse the repository at this point in the history
…ts up the responses.
  • Loading branch information
mostlikely4r committed Nov 19, 2024
1 parent d57b764 commit d480db3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
5 changes: 2 additions & 3 deletions playerbot/PlayerbotLLMInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,11 @@ std::vector<std::string> PlayerbotLLMInterface::ParseResponse(const std::string&

subString += c;

if ((subString.size() > 1 && subString.back() == 'n' && subString[subString.size() - 2] == '\\') || (subString.size() > 100 && c == '.') || (subString.size() > 200 && c == ' ') || subString.size() > 250)
if (subString.back() == '|' || (subString.size() > 100 && c == '.') || (subString.size() > 200 && c == ' ') || subString.size() > 250)
{
if (subString.back() == 'n' && subString[subString.size() - 2] == '\\')
if (subString.back() == '|')
{
subString.pop_back();
subString.pop_back();
}
if(subString.size())
responses.push_back(subString);
Expand Down
31 changes: 24 additions & 7 deletions playerbot/strategy/actions/SayAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,17 @@ void ChatReplyAction::ChatReplyDo(Player* bot, uint32 type, uint32 guid1, uint32
{
PlayerbotAI* ai = bot->GetPlayerbotAI();
AiObjectContext* context = ai->GetAiObjectContext();
std::string botName = bot->GetName();
std::string playerName = player->GetName();

std::string llmContext = AI_VALUE(std::string, "manual string::llmcontext");

std::map<std::string, std::string> placeholders;
placeholders["<bot name>"] = bot->GetName();
placeholders["<bot name>"] = botName;
placeholders["<bot level>"] = std::to_string(bot->GetLevel());
placeholders["<bot class>"] = ai->GetChatHelper()->formatClass(bot->getClass());
placeholders["<bot race>"] = ai->GetChatHelper()->formatRace(bot->getRace());
placeholders["<player name>"] = player->GetName();
placeholders["<player name>"] = playerName;
placeholders["<player level>"] = std::to_string(player->GetLevel());
placeholders["<player class>"] = ai->GetChatHelper()->formatClass(player->getClass());
placeholders["<player race>"] = ai->GetChatHelper()->formatRace(player->getRace());
Expand Down Expand Up @@ -280,8 +282,6 @@ void ChatReplyAction::ChatReplyDo(Player* bot, uint32 type, uint32 guid1, uint32

json = BOT_TEXT2(json, placeholders);

std::string playerName = player->GetName();

uint32 type = CHAT_MSG_WHISPER;

switch (chatChannelSource)
Expand Down Expand Up @@ -312,7 +312,10 @@ void ChatReplyAction::ChatReplyDo(Player* bot, uint32 type, uint32 guid1, uint32
}
}

std::future<std::vector<WorldPacket>> futurePackets = std::async([type, bot, playerName, json] {
WorldSession* session = bot->GetSession();


std::future<std::vector<WorldPacket>> futurePackets = std::async([type, botName, playerName, json] {

WorldPacket packet_template(CMSG_MESSAGECHAT, 4096);

Expand All @@ -326,13 +329,27 @@ void ChatReplyAction::ChatReplyDo(Player* bot, uint32 type, uint32 guid1, uint32

std::string response = PlayerbotLLMInterface::Generate(json);

size_t pos;

if (sPlayerbotAIConfig.llmPreventTalkingForPlayer)
{
size_t pos = response.find(playerName + ":");
pos = response.find(playerName + ":");
if (pos != std::string::npos)
response = response.substr(0, pos) + sPlayerbotAIConfig.llmResponseEndPattern;
}

pos = 0;

while ((pos = response.find(botName + ":", pos)) != std::string::npos) {
response.replace(pos, botName.length() + 1, "|");
pos += 1;
}

while ((pos = response.find("/n", pos)) != std::string::npos) {
response.replace(pos, 2, "|");
pos += 1;
}

std::vector<std::string> lines = PlayerbotLLMInterface::ParseResponse(response, sPlayerbotAIConfig.llmResponseStartPattern, sPlayerbotAIConfig.llmResponseEndPattern);

std::vector<WorldPacket> packets;
Expand All @@ -345,7 +362,7 @@ void ChatReplyAction::ChatReplyDo(Player* bot, uint32 type, uint32 guid1, uint32

return packets; });

ai->SendDelayedPacket(bot->GetSession(), std::move(futurePackets));
ai->SendDelayedPacket(session, std::move(futurePackets));

SET_AI_VALUE(std::string, "manual string::llmcontext", llmContext);
}
Expand Down

0 comments on commit d480db3

Please sign in to comment.