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
10 changes: 5 additions & 5 deletions src/askui/models/shared/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,18 @@ def _add_message(self, message: MessageParam) -> None:
Args:
message: Message to add
"""
# Report to reporter
self._reporter.add_message(
self.current_speaker.name, message.model_dump(mode="json")
)

if not self._truncation_strategy:
logger.error("No truncation strategy, cannot add message")
return

# Add to truncation strategy
self._truncation_strategy.append_message(message)

# Report to reporter
self._reporter.add_message(
self.current_speaker.name, message.model_dump(mode="json")
)

@tracer.start_as_current_span("_handle_continue_conversation")
def _handle_continue_conversation(self, result: SpeakerResult) -> bool:
"""Handle speaker result status and determine if loop should continue.
Expand Down
28 changes: 20 additions & 8 deletions src/askui/models/shared/truncation_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
from askui.models.shared.token_counter import SimpleTokenCounter
from askui.models.shared.tools import ToolCollection
from askui.prompts.truncation import SUMMARIZE_INSTRUCTION_PROMPT
from askui.reporting import Reporter

if TYPE_CHECKING:
from askui.callbacks.conversation_callback import ConversationCallback
from askui.models.shared.conversation import Conversation
from askui.reporting import Reporter

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -81,6 +81,7 @@ def _summarize_message_history(
system: SystemPrompt | None = None,
tools: ToolCollection | None = None,
provider_options: dict[str, Any] | None = None,
reporter: Reporter | None = None,
) -> MessageParam:
"""Ask the VLM to summarize the conversation history.

Expand All @@ -99,6 +100,7 @@ def _summarize_message_history(
Required for cache hits on the prefix.
provider_options: Provider-specific options (e.g. ``betas``)
used by the regular conversation calls.
reporter: Reporter to log errors during summarization to

Returns:
The raw VLM response message.
Expand All @@ -121,13 +123,21 @@ def _summarize_message_history(
)
)

return vlm_provider.create_message(
messages=messages_to_summarize,
max_tokens=2048,
system=system,
tools=tools,
provider_options=provider_options,
)
try:
return vlm_provider.create_message(
messages=messages_to_summarize,
max_tokens=2048,
system=system,
tools=tools,
provider_options=provider_options,
)
except Exception as e:
# catch e.g. BadRequestError
error_msg = f"Truncation Failed with error: {e}"
logger.exception(error_msg)
if reporter:
reporter.add_message("TruncationStrategy", error_msg)
raise
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should it not be raise e?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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



def _extract_summary_text(response: MessageParam) -> str:
Expand Down Expand Up @@ -405,6 +415,7 @@ def truncate(self) -> None:
system=system,
tools=tools,
provider_options=provider_options,
reporter=self.reporter,
)
if self.reporter:
self.reporter.add_message(
Expand Down Expand Up @@ -731,6 +742,7 @@ def truncate(self) -> None:
system=system,
tools=tools,
provider_options=provider_options,
reporter=self.reporter,
)
if self.reporter:
self.reporter.add_message(
Expand Down
Loading