Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding NoOpTarget #79

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyrit/prompt_target/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pyrit.prompt_target.prompt_target import PromptTarget
from pyrit.prompt_target.azure_openai_chat_target import AzureOpenAIChatTarget
from pyrit.prompt_target.no_op_target import NoOpTarget


__all__ = ["AzureOpenAIChatTarget", "PromptTarget"]
__all__ = ["AzureOpenAIChatTarget", "PromptTarget", "NoOpTarget"]
40 changes: 40 additions & 0 deletions pyrit/prompt_target/no_op_target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

from pyrit.memory import FileMemory, MemoryInterface
from pyrit.models import ChatMessage
from pyrit.prompt_target import PromptTarget


class NoOpTarget(PromptTarget):
"""
The NoOpTarget takes prompts, adds them to memory and prints them, but doesn't send them anywhere

This can be useful in various situations, for example, if operators want to generate prompts
but enter them manually.
"""

def __init__(self, *, memory: MemoryInterface = None) -> None:
self.memory = memory if memory else FileMemory()

def set_system_prompt(self, prompt: str, conversation_id: str, normalizer_id: str) -> None:
messages = self.memory.get_memories_with_conversation_id(conversation_id=conversation_id)

if messages:
raise RuntimeError("Conversation already exists, system prompt needs to be set at the beginning")

self.memory.add_chat_message_to_memory(
conversation=ChatMessage(role="system", content=prompt),
conversation_id=conversation_id,
normalizer_id=normalizer_id,
)

def send_prompt(self, normalized_prompt: str, conversation_id: str, normalizer_id: str) -> str:
msg = ChatMessage(role="user", content=normalized_prompt)
print(msg)
rlundeen2 marked this conversation as resolved.
Show resolved Hide resolved

self.memory.add_chat_message_to_memory(
conversation=msg, conversation_id=conversation_id, normalizer_id=normalizer_id
)

return ""
6 changes: 3 additions & 3 deletions tests/test_prompt_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_set_system_prompt(azure_openai_target: AzureOpenAIChatTarget):
assert chats[0].content == "system prompt"


def test_complete_chat_user_no_system(azure_openai_target: AzureOpenAIChatTarget, openai_mock_return: ChatCompletion):
def test_send_prompt_user_no_system(azure_openai_target: AzureOpenAIChatTarget, openai_mock_return: ChatCompletion):
with patch("openai.resources.chat.Completions.create") as mock:
mock.return_value = openai_mock_return
azure_openai_target.send_prompt(
Expand All @@ -70,7 +70,7 @@ def test_complete_chat_user_no_system(azure_openai_target: AzureOpenAIChatTarget
assert chats[1].role == "assistant"


def test_complete_chat_user_with_system(azure_openai_target: AzureOpenAIChatTarget, openai_mock_return: ChatCompletion):
def test_send_prompt_with_system(azure_openai_target: AzureOpenAIChatTarget, openai_mock_return: ChatCompletion):
with patch("openai.resources.chat.Completions.create") as mock:
mock.return_value = openai_mock_return

Expand All @@ -86,7 +86,7 @@ def test_complete_chat_user_with_system(azure_openai_target: AzureOpenAIChatTarg
assert chats[1].role == "user"


def test_complete_chat_user_with_system_calls_chat_complete(
def test_send_prompt_with_system_calls_chat_complete(
azure_openai_target: AzureOpenAIChatTarget, openai_mock_return: ChatCompletion
):
with patch("openai.resources.chat.Completions.create") as mock:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_prompt_target_no_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import pathlib

import pytest

from pyrit.memory import FileMemory
from pyrit.prompt_target import NoOpTarget


@pytest.fixture
def memory(tmp_path: pathlib.Path):
return FileMemory(filepath=tmp_path / "target_no_op_test.json.memory")


def test_set_system_prompt(memory: FileMemory):
no_op = NoOpTarget(memory=memory)

no_op.set_system_prompt(prompt="system prompt", conversation_id="1", normalizer_id="2")

chats = no_op.memory.get_memories_with_conversation_id(conversation_id="1")
assert len(chats) == 1, f"Expected 1 chat, got {len(chats)}"
assert chats[0].role == "system"
assert chats[0].content == "system prompt"


def test_send_prompt_user_no_system(memory: FileMemory):
no_op = NoOpTarget(memory=memory)

no_op.send_prompt(
normalized_prompt="hi, I am a victim chatbot, how can I help?", conversation_id="1", normalizer_id="2"
)

chats = no_op.memory.get_memories_with_conversation_id(conversation_id="1")
assert len(chats) == 1, f"Expected 1 chat, got {len(chats)}"
assert chats[0].role == "user"
Loading