213 lines
7.9 KiB
Python
213 lines
7.9 KiB
Python
import os
|
|
from typing import List, Dict, Any
|
|
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSequenceClassification
|
|
from transformers import Trainer, TrainingArguments
|
|
from sentence_transformers import SentenceTransformer
|
|
import torch
|
|
from datasets import Dataset
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
class LLM:
|
|
def __init__(self, model_name: str = "ethics_finetuned_llama3"):
|
|
self.model_name = model_name
|
|
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
# Load the main language model
|
|
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
self.model = AutoModelForCausalLM.from_pretrained(model_name).to(self.device)
|
|
|
|
# Load the ethics evaluation model
|
|
self.ethics_model = AutoModelForSequenceClassification.from_pretrained("ethics_classifier").to(self.device)
|
|
|
|
# Load the embedding model for vector representations
|
|
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2').to(self.device)
|
|
|
|
def generate(self, prompt: str, max_length: int = 100) -> str:
|
|
"""
|
|
Generate text based on a given prompt.
|
|
|
|
Args:
|
|
prompt (str): The input prompt for text generation.
|
|
max_length (int): The maximum length of the generated text.
|
|
|
|
Returns:
|
|
str: The generated text.
|
|
"""
|
|
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
|
|
outputs = self.model.generate(**inputs, max_length=max_length)
|
|
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
def summarize(self, text: str, max_length: int = 150) -> str:
|
|
"""
|
|
Generate a summary of the given text.
|
|
|
|
Args:
|
|
text (str): The text to be summarized.
|
|
max_length (int): The maximum length of the summary.
|
|
|
|
Returns:
|
|
str: The generated summary.
|
|
"""
|
|
prompt = f"Summarize the following text:\n\n{text}\n\nSummary:"
|
|
return self.generate(prompt, max_length)
|
|
|
|
def evaluate_ethics(self, decision: str, standards: List[str]) -> Dict[str, Any]:
|
|
"""
|
|
Evaluate an AI decision against ethical standards.
|
|
|
|
Args:
|
|
decision (str): The AI decision to be evaluated.
|
|
standards (List[str]): List of ethical standards to evaluate against.
|
|
|
|
Returns:
|
|
Dict[str, Any]: A dictionary containing the ethical evaluation results.
|
|
"""
|
|
# Combine decision and standards into a single input
|
|
input_text = f"Decision: {decision}\n\nEthical Standards:\n" + "\n".join(standards)
|
|
|
|
inputs = self.tokenizer(input_text, return_tensors="pt", truncation=True, max_length=512).to(self.device)
|
|
outputs = self.ethics_model(**inputs)
|
|
|
|
# Assuming the ethics_model outputs a score between 0 and 1
|
|
ethics_score = torch.sigmoid(outputs.logits).item()
|
|
|
|
evaluation = self.generate(f"Evaluate the ethics of this AI decision: {decision}")
|
|
|
|
return {
|
|
"score": ethics_score,
|
|
"evaluation": evaluation
|
|
}
|
|
|
|
def generate_feedback(self, scenario: str, user_decision: str) -> str:
|
|
"""
|
|
Generate feedback for a user's decision in an ethics scenario.
|
|
|
|
Args:
|
|
scenario (str): The ethics scenario presented to the user.
|
|
user_decision (str): The user's decision for the scenario.
|
|
|
|
Returns:
|
|
str: Generated feedback on the user's decision.
|
|
"""
|
|
prompt = f"Ethics Scenario: {scenario}\n\nUser's Decision: {user_decision}\n\nProvide feedback on the ethical implications of this decision:"
|
|
return self.generate(prompt, max_length=200)
|
|
|
|
def evaluate_contribution(self, contribution: str) -> float:
|
|
"""
|
|
Evaluate the impact of a user's contribution to the knowledge base.
|
|
|
|
Args:
|
|
contribution (str): The user's contribution to evaluate.
|
|
|
|
Returns:
|
|
float: A score representing the impact of the contribution (0 to 1).
|
|
"""
|
|
prompt = f"Evaluate the following contribution to an AI ethics knowledge base. Provide a score from 0 to 1, where 1 is highly valuable:\n\n{contribution}\n\nScore:"
|
|
score_text = self.generate(prompt, max_length=10)
|
|
try:
|
|
return float(score_text.strip())
|
|
except ValueError:
|
|
return 0.5 # Default to neutral if parsing fails
|
|
|
|
def translate(self, content: str, target_language: str) -> str:
|
|
"""
|
|
Translate content to the target language.
|
|
|
|
Args:
|
|
content (str): The content to be translated.
|
|
target_language (str): The target language for translation.
|
|
|
|
Returns:
|
|
str: The translated content.
|
|
"""
|
|
prompt = f"Translate the following text to {target_language}:\n\n{content}\n\nTranslation:"
|
|
return self.generate(prompt, max_length=len(content) * 2)
|
|
|
|
def embed(self, text: str) -> List[float]:
|
|
"""
|
|
Generate an embedding for the given text.
|
|
|
|
Args:
|
|
text (str): The text to embed.
|
|
|
|
Returns:
|
|
List[float]: The embedding vector.
|
|
"""
|
|
return self.embedding_model.encode(text).tolist()
|
|
|
|
def fine_tune(self, dataset_path: str, output_dir: str, num_train_epochs: int = 3):
|
|
"""
|
|
Fine-tune the model on a given dataset.
|
|
|
|
Args:
|
|
dataset_path (str): Path to the dataset for fine-tuning.
|
|
output_dir (str): Directory to save the fine-tuned model.
|
|
num_train_epochs (int): Number of training epochs.
|
|
"""
|
|
# Load and preprocess the dataset
|
|
dataset = self._load_dataset(dataset_path)
|
|
|
|
# Set up training arguments
|
|
training_args = TrainingArguments(
|
|
output_dir=output_dir,
|
|
num_train_epochs=num_train_epochs,
|
|
per_device_train_batch_size=8,
|
|
per_device_eval_batch_size=8,
|
|
warmup_steps=500,
|
|
weight_decay=0.01,
|
|
logging_dir='./logs',
|
|
)
|
|
|
|
# Initialize trainer
|
|
trainer = Trainer(
|
|
model=self.model,
|
|
args=training_args,
|
|
train_dataset=dataset['train'],
|
|
eval_dataset=dataset['validation']
|
|
)
|
|
|
|
# Start fine-tuning
|
|
trainer.train()
|
|
|
|
# Save the fine-tuned model
|
|
self.model.save_pretrained(output_dir)
|
|
self.tokenizer.save_pretrained(output_dir)
|
|
|
|
def _load_dataset(self, dataset_path: str) -> Dataset:
|
|
"""
|
|
Load and preprocess the dataset for fine-tuning.
|
|
|
|
Args:
|
|
dataset_path (str): Path to the dataset file.
|
|
|
|
Returns:
|
|
Dataset: Processed dataset ready for fine-tuning.
|
|
"""
|
|
# Load raw data (assuming it's a text file with one example per line)
|
|
with open(dataset_path, 'r') as f:
|
|
data = f.readlines()
|
|
|
|
# Preprocess data
|
|
processed_data = [self.tokenizer(text.strip(), truncation=True, padding='max_length', max_length=512) for text in data]
|
|
|
|
# Split into train and validation sets
|
|
train_data, val_data = train_test_split(processed_data, test_size=0.1)
|
|
|
|
# Create datasets
|
|
train_dataset = Dataset.from_dict({k: [d[k] for d in train_data] for k in train_data[0].keys()})
|
|
val_dataset = Dataset.from_dict({k: [d[k] for d in val_data] for k in val_data[0].keys()})
|
|
|
|
return {'train': train_dataset, 'validation': val_dataset}
|
|
|
|
@classmethod
|
|
def load(cls, model_path: str):
|
|
"""
|
|
Load a pre-trained or fine-tuned model.
|
|
|
|
Args:
|
|
model_path (str): Path to the model directory.
|
|
|
|
Returns:
|
|
LLM: An instance of the LLM class with the loaded model.
|
|
"""
|
|
return cls(model_name=model_path) |