pyrit.models.Message#

class Message(message_pieces: Sequence[MessagePiece], *, skip_validation: bool | None = False)[source]#

Bases: object

Represents a message in a conversation, for example a prompt or a response to a prompt.

This is a single request to a target. It can contain multiple message pieces.

Parameters:

message_pieces (Sequence[MessagePiece]) – The list of message pieces.

__init__(message_pieces: Sequence[MessagePiece], *, skip_validation: bool | None = False)[source]#

Methods

__init__(message_pieces, *[, skip_validation])

flatten_to_message_pieces(messages)

from_prompt(*, prompt, role)

from_system_prompt(system_prompt)

get_all_values(messages)

Return all converted values across the provided messages.

get_piece([n])

Return the nth message piece.

get_value([n])

Return the converted value of the nth message piece.

get_values()

Return the converted values of all message pieces.

is_error()

Returns True if any of the message pieces have an error response.

set_response_not_in_database()

Set that the prompt is not in the database.

validate()

Validates the request response.

Attributes

conversation_id

Return the conversation ID of the first request piece (they should all be the same).

role

Return the role of the first request piece (they should all be the same).

sequence

Return the sequence of the first request piece (they should all be the same).

property conversation_id: str#

Return the conversation ID of the first request piece (they should all be the same).

static flatten_to_message_pieces(messages: Sequence[Message]) MutableSequence[MessagePiece][source]#
classmethod from_prompt(*, prompt: str, role: Literal['system', 'user', 'assistant', 'tool', 'developer']) Message[source]#
classmethod from_system_prompt(system_prompt: str) Message[source]#
static get_all_values(messages: Sequence[Message]) list[str][source]#

Return all converted values across the provided messages.

get_piece(n: int = 0) MessagePiece[source]#

Return the nth message piece.

get_value(n: int = 0) str[source]#

Return the converted value of the nth message piece.

get_values() list[str][source]#

Return the converted values of all message pieces.

is_error() bool[source]#

Returns True if any of the message pieces have an error response.

property role: Literal['system', 'user', 'assistant', 'tool', 'developer']#

Return the role of the first request piece (they should all be the same).

property sequence: int#

Return the sequence of the first request piece (they should all be the same).

set_response_not_in_database()[source]#

Set that the prompt is not in the database.

This is needed when we’re scoring prompts or other things that have not been sent by PyRIT

validate()[source]#

Validates the request response.