Files
EvoSynth/jailbreak_toolbox/models/base_model.py
dongdongunique f3af94df0b first commit
2025-12-10 00:54:02 +08:00

34 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from abc import ABC, abstractmethod
from typing import Any, Dict
class BaseModel(ABC):
"""
所有模型的抽象基类。
定义了所有模型无论是本地白盒还是远程API黑盒都必须遵守的接口。
"""
def __init__(self, **kwargs):
"""允许在配置文件中传入任意模型特定的参数。"""
pass
@abstractmethod
def query(self, text_input: str, image_input: Any = None) -> str:
"""
向模型发送查询并获取响应的核心方法。
对于纯文本模型image_input 将被忽略。
这是所有模型都必须实现的功能。
"""
pass
def get_gradients(self, inputs) -> Dict:
"""
(可选) 获取梯度,用于白盒攻击。
如果模型不支持如黑盒API模型则直接抛出异常。
"""
raise NotImplementedError(f"{self.__class__.__name__} does not support gradient access.")
def get_embeddings(self, inputs) -> Any:
"""
(可选) 获取嵌入向量,用于白盒或灰盒攻击。
如果模型不支持,则直接抛出异常。
"""
raise NotImplementedError(f"{self.__class__.__name__} does not support embedding access.")