Skip to content

Bookmark API

api_delete_all_bookmark()

Removes all bookmarks from the database.

Returns:

Name Type Description
dict dict

A dictionary indicating the result of the delete operation.

Source code in moonshot/src/api/api_bookmark.py
def api_delete_all_bookmark() -> dict:
    """
    Removes all bookmarks from the database.

    Returns:
        dict: A dictionary indicating the result of the delete operation.
    """
    return Bookmark().delete_all_bookmark()

api_delete_bookmark(bookmark_name)

Removes a bookmark from the database using its name.

Parameters:

Name Type Description Default
bookmark_name str

The name of the bookmark to be removed.

required

Returns:

Name Type Description
dict dict

A dictionary containing the details of the deleted bookmark.

Source code in moonshot/src/api/api_bookmark.py
def api_delete_bookmark(bookmark_name: str) -> dict:
    """
    Removes a bookmark from the database using its name.

    Args:
        bookmark_name (str): The name of the bookmark to be removed.

    Returns:
        dict: A dictionary containing the details of the deleted bookmark.
    """
    return Bookmark().delete_bookmark(bookmark_name)

api_export_bookmarks(export_file_name='bookmarks')

Exports bookmarks to a specified file.

Parameters:

Name Type Description Default
export_file_name str

The name of the file to export the bookmarks to.

'bookmarks'

Returns:

Name Type Description
str str

The filepath of where the file is written.

Source code in moonshot/src/api/api_bookmark.py
def api_export_bookmarks(export_file_name: str = "bookmarks") -> str:
    """
    Exports bookmarks to a specified file.

    Args:
        export_file_name (str): The name of the file to export the bookmarks to.

    Returns:
        str: The filepath of where the file is written.
    """
    return Bookmark().export_bookmarks(export_file_name)

api_get_all_bookmarks()

Retrieves a list of all bookmarks from the database.

Returns:

Type Description
list[dict]

list[dict]: A list of bookmarks, each represented as a dictionary.

Source code in moonshot/src/api/api_bookmark.py
def api_get_all_bookmarks() -> list[dict]:
    """
    Retrieves a list of all bookmarks from the database.

    Returns:
        list[dict]: A list of bookmarks, each represented as a dictionary.
    """
    return Bookmark().get_all_bookmarks()

api_get_bookmark(bookmark_name)

Retrieves the details of a specific bookmark by its name.

Parameters:

Name Type Description Default
bookmark_name str

The name of the bookmark to retrieve.

required

Returns:

Name Type Description
dict dict

The bookmark details corresponding to the provided name.

Source code in moonshot/src/api/api_bookmark.py
def api_get_bookmark(bookmark_name: str) -> dict:
    """
    Retrieves the details of a specific bookmark by its name.

    Args:
        bookmark_name (str): The name of the bookmark to retrieve.

    Returns:
        dict: The bookmark details corresponding to the provided name.
    """
    return Bookmark().get_bookmark(bookmark_name)

api_insert_bookmark(name, prompt, prepared_prompt, response, context_strategy='', prompt_template='', attack_module='', metric='')

Inserts a new bookmark into the database.

This function constructs a BookmarkArguments object with the provided details and invokes the add_bookmark method of a Bookmark instance to persist the new bookmark.

Parameters:

Name Type Description Default
name str

The unique name of the bookmark.

required
prompt str

The associated prompt text for the bookmark.

required
prepared_prompt str

The prepared prompt text for the bookmark.

required
response str

The corresponding response text for the bookmark.

required
context_strategy str

The strategy used for context management in the bookmark. Defaults to "".

''
prompt_template str

The template used for generating the prompt. Defaults to "".

''
attack_module str

The attack module linked with the bookmark. Defaults to "".

''
metric str

The metric associated with the bookmark. Defaults to "".

''

Returns:

Name Type Description
dict dict

A dictionary containing the details of the newly inserted bookmark.

Source code in moonshot/src/api/api_bookmark.py
def api_insert_bookmark(
    name: str,
    prompt: str,
    prepared_prompt: str,
    response: str,
    context_strategy: str = "",
    prompt_template: str = "",
    attack_module: str = "",
    metric: str = "",
) -> dict:
    """
    Inserts a new bookmark into the database.

    This function constructs a BookmarkArguments object with the provided details and
    invokes the add_bookmark method of a Bookmark instance to persist the new bookmark.

    Args:
        name (str): The unique name of the bookmark.
        prompt (str): The associated prompt text for the bookmark.
        prepared_prompt (str): The prepared prompt text for the bookmark.
        response (str): The corresponding response text for the bookmark.
        context_strategy (str, optional): The strategy used for context management in the bookmark. Defaults to "".
        prompt_template (str, optional): The template used for generating the prompt. Defaults to "".
        attack_module (str, optional): The attack module linked with the bookmark. Defaults to "".
        metric (str, optional): The metric associated with the bookmark. Defaults to "".

    Returns:
        dict: A dictionary containing the details of the newly inserted bookmark.
    """
    # Create a new BookmarkArguments object
    bookmark_args = BookmarkArguments(
        id=0,  # id will be auto-generated by the database
        name=name,
        prompt=prompt,
        prepared_prompt=prepared_prompt,
        response=response,
        context_strategy=context_strategy,
        prompt_template=prompt_template,
        attack_module=attack_module,
        metric=metric,
        bookmark_time="",  # bookmark_time will be set to current time in add_bookmark method
    )
    return Bookmark().add_bookmark(bookmark_args)