dungeon_assistant
dungeon_assistant
The dungeon_assistant
module contains the DungeonAssistant
class that interfaces with the OpenAI API and performs the duties of the game's referee and guide (game master or dungeon master in some tabletop RPGs).
DungeonAssistant
DungeonAssistant(adventure: Adventure = None, openai_model: OpenAIModelVersion = OpenAIModelVersion.DEFAULT)
The DungeonAssistant
is the primary interface between the player, the game world's rules engine, and optionally the OpenAI API.
Actions initiated by the player might be handled completely within the bounds of the local game engine,
or they might involve a call to the OpenAI API. The DungeonAssistant
class is responsible for determining
which actions are handled locally and which are handled by the OpenAI API.
Attributes:
-
adventure
(Adventure
) –The adventure the
DungeonAssistant
is running. -
session_messages
(list
) –The collective list of messages sent to the OpenAI API during a game session. Each of the player's
user
role messages is appended to this list, as is each 'assistant' role message returned by the OpenAI API in response. -
session_is_started
(bool
) –Indicates whether the game session has started. The game session starts upon first call to the
start_session()
method.
format_user_message
Format the given string as an OpenAI 'user' role message.
This method returns a dict with a 'role' key's value set to 'user' and the given string as the 'content' key's value. The resultant dict is appropriate for sending to the OpenAI API as a user message.
Example:
The string returned by this method is in the following format and is appropriate for sending to the OpenAI API as a user message:
Parameters:
-
message_string
(str
) –The string to format as an OpenAI user message. It must be a regular string and not a JSON string.
Returns:
-
dict
(dict
) –A version of the user message as a Python
dict
.
send_player_message
Send a message from the player to the Dungeon Assistant and return the response.
start_session
start_session() -> str
Start a gaming session with the dungeon assistant in the current adventure.
If this the first session in the adventure, the adventure is marked as started and the dungeon assistant's (system) init message is sent to the OpenAI API as is player's (user) init message. If it's not the first session of the adventure, only the system and user init messages are sent.
Returns:
-
str
(str
) –The response from the Dungeon Assistant (in this case, the OpenAI API) when initiating a game session. This string is appropriate for display to the player.
Raises:
-
ValueError
–If there is no active adventure. Call set_active_adventure() with a valid adventure before calling start_session().