doc/code/memory/2_basic_memory_programming.ipynb (88 lines of code) (raw):

{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# 2. Basic Memory Programming Usage\n", "\n", "The `pyrit.memory` module provides functionality to keep track of the conversation history, scoring, data, and more. You can use memory to read and write data. Here is an example that retrieves a normalized conversation:" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None: user: Hi, chat bot! This is my initial prompt.\n", "None: assistant: Nice to meet you! This is my response.\n", "None: user: Wonderful! This is my second prompt to the chat bot!\n" ] } ], "source": [ "from uuid import uuid4\n", "\n", "from pyrit.memory.duckdb_memory import DuckDBMemory\n", "from pyrit.models import PromptRequestPiece, PromptRequestResponse\n", "\n", "conversation_id = str(uuid4())\n", "\n", "message_list = [\n", " PromptRequestPiece(\n", " role=\"user\", original_value=\"Hi, chat bot! This is my initial prompt.\", conversation_id=conversation_id\n", " ),\n", " PromptRequestPiece(\n", " role=\"assistant\", original_value=\"Nice to meet you! This is my response.\", conversation_id=conversation_id\n", " ),\n", " PromptRequestPiece(\n", " role=\"user\",\n", " original_value=\"Wonderful! This is my second prompt to the chat bot!\",\n", " conversation_id=conversation_id,\n", " ),\n", "]\n", "\n", "memory = DuckDBMemory()\n", "\n", "memory.add_request_response_to_memory(request=PromptRequestResponse([message_list[0]]))\n", "memory.add_request_response_to_memory(request=PromptRequestResponse([message_list[1]]))\n", "memory.add_request_response_to_memory(request=PromptRequestResponse([message_list[2]]))\n", "\n", "\n", "entries = memory.get_conversation(conversation_id=conversation_id)\n", "\n", "for entry in entries:\n", " print(entry)\n", "\n", "\n", "# Cleanup memory resources\n", "memory.dispose_engine()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.10" } }, "nbformat": 4, "nbformat_minor": 5 }