Fix continue from prefix

This commit is contained in:
2026-01-10 13:50:57 +01:00
parent 39e0c81ef1
commit 5821391309

View File

@@ -5,6 +5,7 @@ os.chdir("/usr/local/lib")
from llama_cpp import Llama, LogitsProcessorList, llama_cpp from llama_cpp import Llama, LogitsProcessorList, llama_cpp
from llm_engine_utils import LlmEngine from llm_engine_utils import LlmEngine
from llm_engine_utils.iterators import skip_prefix
from pathlib import Path from pathlib import Path
from transformers import AutoTokenizer from transformers import AutoTokenizer
from typing import Iterator from typing import Iterator
@@ -40,6 +41,8 @@ class GemmaLlmEngine(LlmEngine):
stream=True, stream=True,
logits_processor=logits_processor_list logits_processor=logits_processor_list
) )
def content_generator():
for output in stream: for output in stream:
choice = output["choices"][0] choice = output["choices"][0]
if choice.get('finish_reason'): if choice.get('finish_reason'):
@@ -50,6 +53,8 @@ class GemmaLlmEngine(LlmEngine):
break break
yield text yield text
yield from skip_prefix(content_generator(), prefix)
def token_count(self, system: str, context: str) -> int: def token_count(self, system: str, context: str) -> int:
return len(self._format_messages(system, context, None)) return len(self._format_messages(system, context, None))
@@ -65,7 +70,6 @@ class GemmaLlmEngine(LlmEngine):
{ {
"role": "assistant", "role": "assistant",
"content": prefix, "content": prefix,
"prefix": True,
}, },
] if prefix else [ ] if prefix else [
{ {
@@ -76,5 +80,6 @@ class GemmaLlmEngine(LlmEngine):
return self._tokenizer.apply_chat_template( return self._tokenizer.apply_chat_template(
messages, messages,
tokenize=False, tokenize=False,
add_generation_prompt=prefix is None or len(prefix) == 0 add_generation_prompt=not prefix,
continue_final_message=bool(prefix)
).removeprefix("<bos>") ).removeprefix("<bos>")