Open skehlet opened 1 month ago
A longer series of messages might be more helpful, and show the </s>
:
messages = [
SystemMessage(content="You're a helpful assistant that answers with super short poems. "),
HumanMessage(content="What is your name?"),
AIMessage(content="I am Haiku, your poetic guide."),
HumanMessage(content="What do you do?"),
AIMessage(content="I distill thoughts, in verse, concise,\nTo bring you wisdom, nice and precise."),
HumanMessage(content="What is your favorite color?"),
]
yields:
{"max_tokens": 1000, "prompt": "<s>[INST] You're a helpful assistant that answers with super short poems. \n\nWhat is your name? [/INST]I am Haiku, your poetic guide.</s>[INST] What do you do? [/INST]I distill thoughts, in verse, concise,\nTo bring you wisdom, nice and precise.</s>[INST] What is your favorite color? [/INST]"}
Hello! I am using Mistral through Bedrock with LangChain and noticed, after using the model for a while with
ChatMessageHistory
, it would begin to hallucinate and have trouble stopping. I dug into it and it seems similar to langchain-aws#31 that the prompt getting sent to the model doesn't quite match the AWS documentation. Notably, there is no use of<s>
or</s>
, and the system prompt isn't put inside the first human message.I updated langchain_aws/chat_models/bedrock.py as follows:
Here is a simple program:
Prompt before (obtained by adding some print statements to the
_stream
function here):After:
Sorry, this simple program doesn't demonstrate the hallucinating, the program I have is much longer and for work and unfortunately I can't share it. But it does demonstrate the prompt updates.
With this update to the prompt, the model seems to be working great and I no longer have the issues with it hallucinating or not stopping.
Note about wrapping the system prompt in \<\>/\<\ >: I reviewed the mistral docs, llama_index's implementation, as well as chujiezheng's chat_templates. Note that Mistral doesn't seem to advertise any special handling for system prompts. The llama_index implementation just follows Llama's use of \<\>, but chujiezheng doesn't. I tried both ways and it didn't seem to matter, so for the above, I left it out.
I hope this is helpful. If my understanding of how
<s>
and</s>
works isn't correct, I apologize, but it really seems necessary according to the docs, this other really helpful read (How to Prompt Mistral AI models, and Why) and I'm pretty sure nothing is adding it. Thank you for your consideration addressing this issue.