agentsociety.agent.prompt¶
Module Contents¶
Classes¶
A class to handle the formatting of prompts based on a template, with support for system prompts and variable extraction. |
API¶
- class agentsociety.agent.prompt.FormatPrompt(template: str, format_prompt: Optional[str] = None, system_prompt: Optional[str] = None, memory: Optional[agentsociety.memory.Memory] = None)¶
A class to handle the formatting of prompts based on a template, with support for system prompts and variable extraction.
Attributes:
template(str): The template string containing placeholders.system_prompt(Optional[str]): An optional system prompt to add to the dialog.variables(List[str]): A list of variable names extracted from the template.formatted_string(str): The formatted string derived from the template and provided variables.bound_objects(Dict[str, Any]): Dictionary of objects bound to the prompt for use in expressions.
Initialization
Description:
Initializes the FormatPrompt with a template, an optional system prompt, and bound objects.
Args:
template(str): The string template with variable placeholders.system_prompt(Optional[str], optional): An optional system prompt. Defaults to None.**bound_objects: Named objects to bind to the prompt for use in expressions.
- _extract_variables() list[str]¶
Description:
Extracts variable names from the template string using regular expressions.
Looks for simple variables in the format {variable_name}.
Returns:
List[str]: A list of variable names found within the template.
- _is_safe_expression(expr: str) bool¶
Description:
Checks if an expression is safe to evaluate.
Only allows attribute access and dictionary access.
Supports both single and double quotes for dictionary keys.
Supports numeric index access.
Args:
expr(str): The expression to check.
Returns:
bool: True if the expression is safe, False otherwise.
- async _eval_expr(expr: str, context: dict) Any¶
Description:
Evaluates expressions using eval with safety checks.
Supports expressions like profile.xxx, status.xxx, context.xxx
Also supports square bracket notation like profile.xxx[“yyy”]
For profile and status, uses async get method from memory
Supports nested dictionary access with square brackets
Args:
expr(str): The expression to evaluate.context(dict): The evaluation context.
Returns:
Any: The result of the expression evaluation.
- async format(context: Optional[dict] = None, **kwargs) str¶
Description:
Formats the template string using the provided context and keyword arguments.
Supports simple variables {var} for direct substitution.
Supports complex expressions ${expression} in three formats:
${profile.xxx}: Retrieves values from memory.profile
${status.xxx}: Retrieves values from memory.status
${context.xxx}: Retrieves values from the provided context dictionary
Args:
context(Optional[dict]): Dictionary containing context values.**kwargs: Variable names and their corresponding values to format the template.
Returns:
str: The formatted string.
Raises:
KeyError: If a placeholder in the template does not have a corresponding key in kwargs.ValueError: If an expression has an invalid format.