Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop adding separator when completing a partial assistant response #57

Merged
merged 2 commits into from
May 22, 2024

Conversation

psmitsu
Copy link

@psmitsu psmitsu commented Apr 23, 2024

Hello, thank you for a great plugin!

Sometimes it is convenient to prepare the "assistant" response before sending the whole prompt to an LLM. E.g. I can type manually:

> You are a helpful assistant.
Hello!

======
Hello! What a

and run :Mchat here. I would expect the LLM to continue Hello! What a with nice day! for example.

In chat mode, this can be done by creating a prompt like that:

run = function(messages,config)
  local prompt = '<|start_header_id|>system<|end_header_id|>\n' .. config.system
  for _, msg in ipairs(messages) do
    prompt = prompt
    .. '<|eot_id|>\n\n<|start_header_id|>'
    .. (msg.role == 'user' and 'user' or 'assistant')
    .. '<|end_header_id|>\n'
    .. msg.content
  end
  --- allow editing ai response
  if messages[#messages].role == 'user' then
    prompt = prompt .. '<|eot_id|>\n\n<|start_header_id|>assistant<|end_header_id|>\n'
  end

  return { prompt = prompt }
end,

However, doing as described above will make the assistant response be written after a separator as if it is a new message:

> You are a helpful assistant.
Hello!

======
Hello! What a
======
 lovely day it is. How can I assist you today?

Hello! What a is what I typed, and lovely day... is the llm's "assistant" response.

This PR adds a simple change to chat processing s.t. a separator is not added when the last message belongs to the assistant. I.e. if the last message belongs to the assistant, then new messages from LLM should be concatenated with it.

@gsuuon
Copy link
Owner

gsuuon commented May 8, 2024

Hi, this is is a great idea thanks for the PR! Sorry for the late response - there's one change needed here and I'll get this merged.

Copy link
Owner

@gsuuon gsuuon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part will re-add the leading separator if the ending response includes the entire text (e.g. with the openai handler). Also feel free to fix the seperator -> separator typo!

@psmitsu
Copy link
Author

psmitsu commented May 22, 2024

@gsuuon I've changed re-adding separator in on_finish and fixed the typo 😃

@gsuuon gsuuon merged commit 05bbd07 into gsuuon:main May 22, 2024
1 of 2 checks passed
@gsuuon
Copy link
Owner

gsuuon commented May 22, 2024

Awesome contribution, thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants