guidance-ai / guidance

A guidance language for controlling large language models.
MIT License
19.14k stars 1.04k forks source link

reset() does not work as expected in connection with role blocks #1071

Closed dominikmau closed 2 weeks ago

dominikmau commented 3 weeks ago

reset() does not completely reset the model object when used in connection with role blocks

Code:

from guidance import models, gen, user, assistant
from guidance.chat import mistral_7b_instruct_llamacpp_template

mistral = models.LlamaCpp(
    'model/Mistral-Small.gguf',
    n_gpu_layers=-1,
    offload_kqv=True,
    n_ctx=4096,
    chat_template=mistral_7b_instruct_llamacpp_template,
    echo=False
)

with user():
    mistral += "hi"

with assistant():
    mistral += gen()

print(mistral)
mistral.reset(clear_variables=True)
print(mistral)
print(mistral.__dict__)

Output:

 [INST] hi [/INST]  I'm here to help! How can I assist you today?</s>

</s>

{'engine': <guidance.models.llama_cpp._llama_cpp.LlamaCppEngine object at 0x000001E52D8CCFD0>, 'chat_template': <guidance.chat.Mistral7BInstructChatTemplate object at 0x000001E52CFB9810>, 'echo': False, 'token_count': 14, 'max_display_rate': 0.2, 'opened_blocks': {<guidance.library._block.ContextBlock object at 0x000001E5123A0090>: (807, '<||_#NODISP_||></s><||_/NODISP_||><||_html:</div></div>_||>')}, '_variables': {}, '_variables_log_probs': {}, '_cache_state': {}, '_state': '', '_event_queue': None, '_event_parent': None, '_last_display': 0, '_last_event_stream': 1730491113.106941}

Guidance Version: 0.1.16

hudson-ai commented 2 weeks ago

Hi @dominikmau sorry you're running into an issue here. Thanks for bringing it to our attention. I think I know what's going on here and will open a PR.