Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Apr 8, 2024
1 parent 9b93ddc commit f355748
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 7 deletions.
5 changes: 3 additions & 2 deletions aisploit/converter/gender.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import textwrap
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from ..core import BaseConverter, BaseChatModel
from ..core import BaseChatModelConverter, BaseChatModel

_template = ChatPromptTemplate.from_template(
textwrap.dedent(
Expand All @@ -22,8 +22,9 @@
)


class GenderConverter(BaseConverter):
class GenderConverter(BaseChatModelConverter):
def __init__(self, *, chat_model: BaseChatModel, prompt=_template) -> None:
super().__init__(chat_model)
self._chain = prompt | chat_model | StrOutputParser()

def _convert(self, prompt: str) -> str:
Expand Down
6 changes: 4 additions & 2 deletions aisploit/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .callbacks import BaseCallbackHandler, Callbacks, CallbackManager
from .classifier import BaseClassifier, Score
from .converter import BaseConverter
from .converter import BaseConverter, BaseChatModelConverter
from .dataset import BaseDataset, YamlDeserializable
from .job import BaseJob
from .model import BaseLLM, BaseChatModel, BaseModel, BaseEmbeddings
Expand All @@ -16,9 +16,11 @@
"BaseClassifier",
"Score",
"BaseConverter",
"BaseChatModelConverter",
"BaseDataset",
"YamlDeserializable",
"Dataset" "BaseJob",
"Dataset",
"BaseJob",
"BaseLLM",
"BaseChatModel",
"BaseModel",
Expand Down
18 changes: 18 additions & 0 deletions aisploit/core/converter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Union
from abc import ABC, abstractmethod
from langchain_core.prompt_values import StringPromptValue
from .model import BaseChatModel
from .prompt import BasePromptValue


Expand Down Expand Up @@ -47,3 +48,20 @@ def __repr__(self) -> str:
prefix = "custom"

return f"<{prefix}.{self.__class__.__name__}>"


class BaseChatModelConverter(BaseConverter, ABC):
def __init__(self, chat_model: BaseChatModel) -> None:
self._chat_model = chat_model

def __repr__(self) -> str:
"""Return a string representation of the converter.
Returns:
str: A string representation of the converter.
"""
prefix = "aisploit"
if not self.__module__.startswith(prefix):
prefix = "custom"

return f"<{prefix}.{self.__class__.__name__}(chat_model={self._chat_model.get_name()})>"
7 changes: 6 additions & 1 deletion aisploit/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from .distance import cosine_distance, euclidean_distance
from .smtp import SMTPClient

__all__ = ["cosine_distance", "euclidean_distance"]
__all__ = [
"cosine_distance",
"euclidean_distance",
"SMTPClient",
]
41 changes: 41 additions & 0 deletions aisploit/utils/smtp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import Optional, Union, Sequence
import smtplib
import ssl as ssl_lib


class SMTPClient:
def __init__(
self,
host: str,
port: Optional[int] = None,
ssl: bool = True,
ssl_context: Optional[ssl_lib.SSLContext] = None,
) -> None:
if not port:
port = smtplib.SMTP_SSL_PORT if ssl else smtplib.SMTP_PORT

self.host = host
self.port = port
self.ssl = ssl
self.ssl_context = ssl_context

self._smtp = self._create_SMTP()

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self._smtp.quit()

def _create_SMTP(self) -> Union[smtplib.SMTP_SSL, smtplib.SMTP]:
if self.ssl:
return smtplib.SMTP_SSL(
host=self.host, port=self.port, context=self.ssl_context
)
return smtplib.SMTP(host=self.host, port=self.port)

def login(self, user: str, password: str):
self._smtp.login(user, password)

def sendmail(self, from_addr: str, to_addrs: str | Sequence[str], msg: str):
self._smtp.sendmail(from_addr, to_addrs, msg)
3 changes: 1 addition & 2 deletions examples/converter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@
{
"data": {
"text/markdown": [
"Jane walked into the room. She greeted her colleagues and sat down at her desk. She opened her \n",
"laptop and began typing an email. She thought about her upcoming presentation and felt nervous."
"Jane walked into the room. She greeted her colleagues and sat down at her desk. She opened her laptop and began typing an email. She thought about her upcoming presentation and felt nervous."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ keywords = [
"genai",
"llm",
"ai",
"responsible-ai",
"pentest",
"cybersecurity",
]
Expand Down

0 comments on commit f355748

Please sign in to comment.