gsuuon / model.nvim

Neovim plugin for interacting with LLM's and building editor integrated prompts.
MIT License
293 stars 21 forks source link

stop adding separator when completing a partial assistant response #57

Closed psmitsu closed 1 month ago

psmitsu commented 2 months ago

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 commented 2 months ago

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.

psmitsu commented 1 month ago

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

gsuuon commented 1 month ago

Awesome contribution, thanks again!