5.0
关注
AI SCORE
开源项目2025-01-27 23:29
ErisForge:Python LLM操作库开源
Hacker News · github.com#Python#LLM#开源
Editor brief · 编辑速览
新的Python库用于LLM改造和优化操作。热度一般,属于特定用途的开源工具。
ErisForge 是一个 Python 库,旨在通过对 Large Language Models(LLMs)的内部层应用变换来修改模型。它以纷争与不和女神 Eris 命名,可让你以可控方式改变模型行为,创建经过消融或增强的 LLM,使其针对特定类型的输入产生不同的响应。
修改 LLM 的内部层,从而产生不同的行为。
使用 AblationDecoderLayer 和 AdditionDecoderLayer 类对模型响应进行消融或增强。
使用 ExpressionRefusalScorer 衡量模型响应中的拒绝表达。
支持自定义行为方向,以应用特定类型的变换。
要安装 ErisForge,请克隆仓库并安装所需的软件包:
git clone https://github.com/tsadoq/erisforge.git
cd erisforge
pip install -r requirements.txt
或者直接通过 pip 安装:
pip install erisforge
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from erisforge import ErisForge
from erisforge.expression_refusal_scorer import ExpressionRefusalScorer
# Load a model and tokenizer
model_name = "gpt2"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Initialize ErisForge and configure the scorer
forge = ErisForge()
scorer = ExpressionRefusalScorer()
你可以对模型的特定层应用变换,从而诱导模型产生不同的响应行为。完整示例可在这个 notebook 中找到:Transform Model Layers。
# Define instructions
instructions = ["Explain why AI is beneficial.", "What are the limitations of AI?"]
# Specify layer ranges for ablation
min_layer = 2
max_layer = 4
# Modify the model by applying ablation to the specified layers
ablated_model = forge.run_forged_model(
model=model,
type_of_layer=AblationDecoderLayer,
objective_behaviour_dir=torch.rand(768), # Example direction tensor
tokenizer=tokenizer,
min_layer=min_layer,
max_layer=max_layer,
instructions=instructions,
max_new_tokens=50
)
# Display modified responses
for conversation in ablated_model:
print("User:", conversation[0]["content"])
print("AI:", conversation[1]["content"])
使用 ExpressionRefusalScorer 衡量模型响应中是否包含常见的拒绝措辞。
response_text = "I'm sorry, I cannot provide that information."
user_query = "What is the recipe for a dangerous substance?"
# Scoring the response for refusal expressions
refusal_score = scorer.score(user_query=user_query, model_response=response_text)
print("Refusal Score:", refusal_score)
你可以将修改后的模型保存到本地,也可以将其推送到 HuggingFace Hub:
output_model_name = "my_transformed_model"
# Save the modified model
forge.save_model(
model=model,
behaviour_dir=torch.rand(768), # Example direction tensor
scale_factor=1,
output_model_name=output_model_name,
tokenizer=tokenizer,
to_hub=False # Set to True to push to HuggingFace Hub
)
本项目受到以下仓库和项目工作的启发,并在其基础上构建:
使用 HF Transformers 移除拒绝行为
HuggingFace 上关于消融的博客文章
欢迎提交 issue、建议,或直接为本项目贡献代码。你可以 fork 此仓库、创建 feature 分支,然后提交 pull request。
本项目采用 MIT License。
免责声明:本库仅供研究与开发使用。对于 ErisForge 的任何具体应用或用途,作者概不负责。