@@ -2162,35 +2162,48 @@ struct server_context {
2162
2162
2163
2163
if (slot.has_new_line ) {
2164
2164
// 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 ) {
2166
2166
// check the current indentation
2167
+ int n_indent = 0 ;
2168
+
2167
2169
// TODO: improve by not doing it more than once for each new line
2168
2170
if (slot.last_nl_pos > 0 ) {
2169
2171
size_t pos = slot.last_nl_pos ;
2170
2172
2171
- int n_indent = 0 ;
2172
- while (pos < slot.generated_text .size () && (slot.generated_text [pos] == ' ' || slot.generated_text [pos] == ' \t ' )) {
2173
+ while (pos < slot.generated_text .size () && (slot.generated_text [pos] == ' ' || slot.generated_text [pos] == ' \t ' || slot.generated_text [pos] == ' \n ' )) {
2173
2174
n_indent++;
2174
2175
pos++;
2176
+
2177
+ if (slot.generated_text [pos] == ' \n ' ) {
2178
+ n_indent = 0 ;
2179
+ slot.last_nl_pos = pos + 1 ;
2180
+ }
2175
2181
}
2176
2182
2177
2183
if (pos < slot.generated_text .size () && n_indent < slot.params .n_indent ) {
2178
2184
slot.stop = STOP_TYPE_LIMIT;
2179
2185
slot.has_next_token = false ;
2180
2186
2181
- // cut the last line
2182
- slot.generated_text .erase (pos, std::string::npos);
2183
-
2184
2187
SLT_DBG (slot, " stopped by indentation limit, n_decoded = %d, n_indent = %d\n " , slot.n_decoded , n_indent);
2185
2188
}
2186
2189
}
2187
2190
2191
+ // 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);
2192
+
2188
2193
// find the next new line
2189
2194
{
2190
2195
const size_t pos = slot.generated_text .find (' \n ' , slot.last_nl_pos );
2191
2196
2192
2197
if (pos != std::string::npos) {
2193
2198
slot.last_nl_pos = pos + 1 ;
2199
+
2200
+ // detect end of paragraph: '\n\n'
2201
+ if (slot.generated_text .size () > slot.last_nl_pos && slot.generated_text [slot.last_nl_pos ] == ' \n ' && n_indent <= slot.params .n_indent && slot.n_decoded > 2 ) {
2202
+ slot.stop = STOP_TYPE_LIMIT;
2203
+ slot.has_next_token = false ;
2204
+
2205
+ SLT_DBG (slot, " stopped by reaching end of paragraph, n_decoded = %d, n_indent = %d\n " , slot.n_decoded , n_indent);
2206
+ }
2194
2207
}
2195
2208
}
2196
2209
}
0 commit comments