1717#include < nlohmann/json.hpp>
1818#include < string>
1919#include < utility>
20+ #include < regex>
21+ #include < algorithm>
22+ #include < cctype>
2023
2124#include " behaviortree_cpp_v3/behavior_tree.h"
2225#include " hri/check_policy.hpp"
@@ -49,9 +52,8 @@ void CheckPolicy::on_tick()
4952 RCLCPP_DEBUG (node_->get_logger (), " CheckPolicy ticked" );
5053 RCLCPP_INFO (node_->get_logger (), " CheckPolicy ticked" );
5154 if (!image_) {
52- RCLCPP_ERROR (node_->get_logger (), " No image received" );
53- RCLCPP_INFO (node_->get_logger (), " No image received, setting to IDLE" );
54- setStatus (BT ::NodeStatus::IDLE );
55+ RCLCPP_ERROR (node_->get_logger (), " No image received yet" );
56+ goal_.prompt .clear ();
5557 return ;
5658 }
5759 RCLCPP_INFO (node_->get_logger (), " Image received, proceeding with CheckPolicy" );
@@ -61,6 +63,7 @@ void CheckPolicy::on_tick()
6163
6264 std::string prompt_ = text_;
6365 goal_.prompt = prompt_;
66+ goal_.images .clear ();
6467 goal_.images .push_back (*image_);
6568 goal_.reset = true ;
6669 goal_.sampling_config .temp = 0.0 ;
@@ -99,6 +102,61 @@ void CheckPolicy::image_callback(
99102 RCLCPP_INFO_ONCE (node_->get_logger (), " Image received in CheckPolicy" );
100103}
101104
105+ std::string trim_copy (const std::string & s)
106+ {
107+ auto start = std::find_if_not (s.begin (), s.end (),
108+ [](unsigned char c) { return std::isspace (c); });
109+ auto end = std::find_if_not (s.rbegin (), s.rend (),
110+ [](unsigned char c) { return std::isspace (c); }).base ();
111+
112+ if (start >= end) {
113+ return " " ;
114+ }
115+ return std::string (start, end);
116+ }
117+
118+ std::string sanitize_llm_output (std::string text)
119+ {
120+ // 1) quitar bloques <think>...</think>
121+ text = std::regex_replace (text, std::regex (R"( <think>.*?</think>)" ), " " );
122+
123+ // 2) quitar tags sueltos <think>, </think> y cualquier otro <...>
124+ text = std::regex_replace (text, std::regex (R"( </?think>)" ), " " );
125+ text = std::regex_replace (text, std::regex (R"( <[^>]+>)" ), " " );
126+
127+ // 3) si existe 'the guest is', quedarse desde ahí
128+ // std::string anchor = "the guest is";
129+ // auto pos = text.find(anchor);
130+ // if (pos != std::string::npos) {
131+ // text = text.substr(pos);
132+ // }
133+
134+ // 4) quitar saltos de línea
135+ text = std::regex_replace (text, std::regex (R"( [\r\n\t]+)" ), " " );
136+
137+ // 5) quitar comillas y paréntesis
138+ text = std::regex_replace (text, std::regex (R"( ["])" ), " " );
139+ text = std::regex_replace (text, std::regex (R"( [()])" ), " " );
140+ text = std::regex_replace (text, std::regex (R"( [!])" ), " " );
141+
142+ // 6) cambiar guiones por espacio
143+ text = std::regex_replace (text, std::regex (R"( -)" ), " " );
144+
145+ // 7) colapsar espacios múltiples
146+ text = std::regex_replace (text, std::regex (R"( \s{2,})" ), " " );
147+
148+ // 8) trim
149+ text = trim_copy (text);
150+
151+ // 9) quedarnos con una sola frase si hay varias
152+ // auto dot_pos = text.find('.');
153+ // if (dot_pos != std::string::npos) {
154+ // text = text.substr(0, dot_pos + 1);
155+ // }
156+
157+ return text;
158+ }
159+
102160BT ::NodeStatus CheckPolicy::on_success ()
103161{
104162 fprintf (stderr, " %s\n " , result_.result ->response .text .c_str ());
@@ -110,24 +168,18 @@ BT::NodeStatus CheckPolicy::on_success()
110168 if (result_.result ->response .text .empty () || result_.result ->response .text == " {}" ) {
111169 return BT ::NodeStatus::FAILURE ;
112170 }
113- std::string answer = result_.result ->response .text ;
114- setOutput (" output_text" , answer);
171+
172+ std::string answer = sanitize_llm_output (result_.result ->response .text );
173+
115174 RCLCPP_INFO (
116175 node_->get_logger (), " CheckPolicy extracted answer: %s" ,
117176 answer.c_str ());
118177
119- answer.erase (
120- std::remove_if (
121- answer.begin (), answer.end (),
122- [](unsigned char c) {return !std::isalnum (c);}), answer.end ());
123- std::transform (
124- answer.begin (), answer.end (), answer.begin (),
125- [](unsigned char c) {return std::tolower (c);});
126-
127178 if (answer.empty ()) {
128179 return BT ::NodeStatus::FAILURE ;
129180 }
130181
182+ setOutput (" output_text" , answer);
131183 return BT ::NodeStatus::SUCCESS ;
132184}
133185
0 commit comments