Skip to content

Commit b457650

Browse files
committed
server : improve infill stop criteria
1 parent 6ab2e47 commit b457650

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

examples/server/server.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,35 +2162,48 @@ 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+
int n_indent = 0;
2168+
21672169
// TODO: improve by not doing it more than once for each new line
21682170
if (slot.last_nl_pos > 0) {
21692171
size_t pos = slot.last_nl_pos;
21702172

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')) {
21732174
n_indent++;
21742175
pos++;
2176+
2177+
if (slot.generated_text[pos] == '\n') {
2178+
n_indent = 0;
2179+
slot.last_nl_pos = pos + 1;
2180+
}
21752181
}
21762182

21772183
if (pos < slot.generated_text.size() && n_indent < slot.params.n_indent) {
21782184
slot.stop = STOP_TYPE_LIMIT;
21792185
slot.has_next_token = false;
21802186

2181-
// cut the last line
2182-
slot.generated_text.erase(pos, std::string::npos);
2183-
21842187
SLT_DBG(slot, "stopped by indentation limit, n_decoded = %d, n_indent = %d\n", slot.n_decoded, n_indent);
21852188
}
21862189
}
21872190

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+
21882193
// find the next new line
21892194
{
21902195
const size_t pos = slot.generated_text.find('\n', slot.last_nl_pos);
21912196

21922197
if (pos != std::string::npos) {
21932198
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+
}
21942207
}
21952208
}
21962209
}

0 commit comments

Comments
 (0)