From 1193d10fb495f4decadb5593f5c2134a3af7df97 Mon Sep 17 00:00:00 2001 From: 51616 Date: Thu, 28 Aug 2025 05:17:31 +0000 Subject: [PATCH] chat_to_str for chat-history related dataset --- src/ctx_to_lora/data/preprocessing_fn.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ctx_to_lora/data/preprocessing_fn.py b/src/ctx_to_lora/data/preprocessing_fn.py index fd81a61..66c2564 100644 --- a/src/ctx_to_lora/data/preprocessing_fn.py +++ b/src/ctx_to_lora/data/preprocessing_fn.py @@ -14,6 +14,17 @@ def closed_qa_prompting(prompt: str): return template.format(input=prompt) +def chat_to_str(messages: list[dict[str, str]]): + return "\n\n".join( + [ + "Message from: {role}\n{content}".format( + **{**m, "role": m["role"].capitalize()} + ) + for m in messages + ] + ) + + def get_preprocessing_fn( ds_name: str, is_eval: bool, @@ -266,7 +277,7 @@ def get_preprocessing_fn( # ctx → all history up to the last turn # q → last user input def f(sample): - chat_history = sample["messages"][:-2] + chat_history = chat_to_str(sample["messages"][:-2]) q = sample["messages"][-2]["content"] response = sample["messages"][-1]["content"] return {"context": chat_history, "prompt": q, "response": response}