Correctly escaping stopping strings? #130
-
Hi there, I noticed in the readme, and in example notebooks, a newline is escaped when used as a stopping string. Here's the example in the readme:
Is it always the case that newlines should be represented as "\n" when used as a stopping string? Is there any difference between single quotes One other question. Does token healing apply to stopping strings? Because I've seen unusual behavior where the model outputs my stopping string, but keeps generating. It only happens sometimes. So it made me think, maybe for short stopping strings (like a single character; in one case I have |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Good questions. First, you escape stopping strings the same way you escape strings in Python (double escaping when you are writing the guidance code inside another string). The literals in Guidance code are parsed with Python's As for token healing, no. Stop strings are tested against the growing string as decoded by the decoder. For OpenAI the endpoint handles this or Guidance depending on if you are using a regex or not. For Transformers you can see the regex stopping code here: If you find a scenario where something does not stop like you expect, please share as an issue. Thanks! |
Beta Was this translation helpful? Give feedback.
Good questions.
First, you escape stopping strings the same way you escape strings in Python (double escaping when you are writing the guidance code inside another string). The literals in Guidance code are parsed with Python's
ast.literal_eval
function, so anything that function can handle you can use:https://github.com/microsoft/guidance/blob/d2d37949c2a552995e2be5f8c3cdd0fe7f05ea79/guidance/_program_executor.py#L184-L188
As for token healing, no. Stop strings are tested against the growing string as decoded by the decoder. For OpenAI the endpoint handles this or Guidance depending on if you are using a regex or not. For Transformers you can see the regex stopping code here:
https://gi…