Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions client/cli/rag_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,17 @@ def session_command(argument, history, lock):
return history


def exchange_episode(store, history, last_session_id):
"""The training episode of the exchange at the end of the history.

It is the one its answer was recorded with, not the last one run: /undo
and a direct command both leave the last session pointing at an answer
that is no longer the one being rated."""

session_id = history[-1].get("session_id", last_session_id)
return store.latest_episode_id(session_id) if session_id else None


def main():
global TRACE_PROVIDER, TRACE_MODEL

Expand Down Expand Up @@ -521,7 +532,8 @@ def read_user_input():
else 2000)
entry = f"[tool result]\n{result[:kept]}"
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": entry})
history.append({"role": "assistant", "content": entry,
"session_id": None})
archive_entry({"role": "user", "content": user_input})
archive_entry({"role": "assistant", "content": entry})
save_history(history)
Expand Down Expand Up @@ -811,9 +823,10 @@ def read_user_input():

recorded = False

if TRAINING_STORE is not None and last_session_id:
if TRAINING_STORE is not None:
try:
episode_id = TRAINING_STORE.latest_episode_id(last_session_id)
episode_id = exchange_episode(TRAINING_STORE, history,
last_session_id)

if episode_id:
TRAINING_STORE.record_verdict(episode_id, user_input[1:])
Expand Down Expand Up @@ -862,9 +875,10 @@ def read_user_input():

episode_id = None

if TRAINING_STORE is not None and last_session_id:
if TRAINING_STORE is not None:
try:
episode_id = TRAINING_STORE.latest_episode_id(last_session_id)
episode_id = exchange_episode(TRAINING_STORE, history,
last_session_id)

if episode_id:
TRAINING_STORE.record_correction(episode_id, text)
Expand Down Expand Up @@ -1649,7 +1663,8 @@ def keep_cut_turn(response, reason):

if reply:
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": reply})
history.append({"role": "assistant", "content": reply,
"session_id": last_session_id})
save_history(history)

if task_result.status == TaskStatus.INTERRUPTED:
Expand Down Expand Up @@ -1811,7 +1826,8 @@ def keep_cut_turn(response, reason):
archive_entry({"role": "user", "content": user_record})

if full_response:
history.append({"role": "assistant", "content": full_response})
history.append({"role": "assistant", "content": full_response,
"session_id": last_session_id})
archive_entry({"role": "assistant", "content": full_response})

if len(history) > MAX_HISTORY:
Expand Down
27 changes: 27 additions & 0 deletions client/tests/test_operator_verdicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
and stayed a positive example.
"""

import dataclasses
import tempfile
import unittest
from pathlib import Path
Expand Down Expand Up @@ -47,6 +48,32 @@ def test_the_latest_episode_is_found(self):
self.assertEqual(self.store.latest_episode_id(), "episode_b")
self.assertIsNone(self.store.latest_episode_id("another-session"))

def test_a_verdict_goes_to_the_episode_of_the_exchange_it_rates(self):
from cli.rag_chat import exchange_episode

for name in ("kept", "undone"):
self.store.save_episode(dataclasses.replace(
answered(f"episode_{name}"), session_id=f"session_{name}"))

# /undo dropped the last exchange: the history now ends on the one
# before, while the last session run is still the undone one.

history = [{"role": "user", "content": "q"},
{"role": "assistant", "content": "a", "session_id": "session_kept"}]

self.assertEqual(exchange_episode(self.store, history, "session_undone"),
"episode_kept")

# A direct command's answer has no episode, and a history saved before
# answers carried their session falls back to the last one.

history[-1]["session_id"] = None
self.assertIsNone(exchange_episode(self.store, history, "session_undone"))

del history[-1]["session_id"]
self.assertEqual(exchange_episode(self.store, history, "session_undone"),
"episode_undone")

def test_a_bad_answer_is_never_an_sft_target(self):
self.store.save_episode(answered("episode_a"))
self.assertEqual(len(SFTDatasetBuilder(self.store).build().samples), 1)
Expand Down
Loading