@@ -2162,35 +2162,58 @@ struct server_context {
21622162
21632163 if (slot.has_new_line ) {
21642164 // require that each new line has a whitespace prefix (i.e. indentation) of at least slot.params.n_indent
2165- if (slot.params .n_indent > 0 ) {
2165+ if (slot.params .n_indent >= 0 ) {
21662166 // check the current indentation
2167- // TODO: improve by not doing it more than once for each new line
2168- if (slot.last_nl_pos > 0 ) {
2169- size_t pos = slot.last_nl_pos ;
2170-
2171- int n_indent = 0 ;
2172- while (pos < slot.generated_text .size () && (slot.generated_text [pos] == ' ' || slot.generated_text [pos] == ' \t ' )) {
2173- n_indent++;
2174- pos++;
2167+ int n_indent = 0 ;
2168+
2169+ size_t pos = slot.last_nl_pos ;
2170+
2171+ while (pos < slot.generated_text .size () && (slot.generated_text [pos] == ' ' || slot.generated_text [pos] == ' \t ' || slot.generated_text [pos] == ' \n ' )) {
2172+ n_indent++;
2173+
2174+ if (slot.generated_text [pos] == ' \n ' ) {
2175+ n_indent = 0 ;
2176+ slot.last_nl_pos = pos + 1 ;
21752177 }
21762178
2177- if (pos < slot.generated_text .size () && n_indent < slot.params .n_indent ) {
2179+ pos++;
2180+ }
2181+
2182+ if (0 < pos && pos < slot.generated_text .size ()) {
2183+ if (n_indent < slot.params .n_indent ) {
21782184 slot.stop = STOP_TYPE_LIMIT;
21792185 slot.has_next_token = false ;
21802186
21812187 // cut the last line
2182- slot.generated_text .erase (pos, std::string::npos);
2188+ // slot.generated_text.erase(pos, std::string::npos);
21832189
21842190 SLT_DBG (slot, " stopped by indentation limit, n_decoded = %d, n_indent = %d\n " , slot.n_decoded , n_indent);
21852191 }
21862192 }
21872193
2194+ // SLT_ERR(slot, "n_indent = %d (%d), generated_text.size() = %d, n_decoded = %d, last_nl_pos = %d\n", n_indent, slot.params.n_indent, slot.generated_text.size(), slot.n_decoded, slot.last_nl_pos);
2195+
21882196 // find the next new line
21892197 {
2190- const size_t pos = slot.generated_text .find (' \n ' , slot.last_nl_pos );
2198+ size_t pos = slot.generated_text .find (' \n ' , slot.last_nl_pos );
21912199
2192- if (pos != std::string::npos) {
2200+ while (pos != std::string::npos) {
21932201 slot.last_nl_pos = pos + 1 ;
2202+
2203+ // detect end of paragraph at current indent level
2204+ if (slot.generated_text [slot.last_nl_pos - 2 ] == ' \n ' && n_indent <= slot.params .n_indent ) {
2205+ slot.stop = STOP_TYPE_LIMIT;
2206+ slot.has_next_token = false ;
2207+
2208+ // cut the last line
2209+ slot.generated_text .erase (pos, std::string::npos);
2210+
2211+ SLT_DBG (slot, " stopped by reaching end of paragraph, n_decoded = %d, n_indent = %d\n " , slot.n_decoded , n_indent);
2212+
2213+ break ;
2214+ }
2215+
2216+ pos = slot.generated_text .find (' \n ' , slot.last_nl_pos );
21942217 }
21952218 }
21962219 }
0 commit comments