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

added generative text explainer #2516

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@
Tokens)
from responsibleai_text.utils.question_answering import QAPredictor

try:
from interpret_text.generative.lime_tools.explainers import \
LocalExplanationSentenceEmbedder
except ImportError as e:
print("Could not import LocalExplanationSentenceEmbedder: ", e)

try:
from interpret_text.generative.model_lib.openai_tooling import ChatOpenAI
except ImportError as e:
print("Could not import ChatOpenAI: ", e)
mohsinposts marked this conversation as resolved.
Show resolved Hide resolved

try:
from sentence_transformers import SentenceTransformer
except ImportError as e:
print("Could not import SentenceTransformer: ", e)


CONTEXT = QuestionAnsweringFields.CONTEXT
QUESTIONS = QuestionAnsweringFields.QUESTIONS
SEP = Tokens.SEP
Expand Down Expand Up @@ -74,10 +91,13 @@ def __init__(self, model: Any, evaluation_examples: pd.DataFrame,
"""
self._model = model
self._target_column = target_column
if not isinstance(target_column, list):
if not isinstance(target_column, (list, type(None))):
target_column = [target_column]
self._evaluation_examples = \
evaluation_examples.drop(columns=target_column)
if target_column is None:
self._evaluation_examples = evaluation_examples
else:
self._evaluation_examples = \
evaluation_examples.drop(columns=target_column)
self._is_run = False
self._is_added = False
self._features = list(self._evaluation_examples.columns)
Expand Down Expand Up @@ -131,6 +151,43 @@ def compute(self):
eval_examples.append(question + SEP + context)
self._explanation = [explainer_start(eval_examples),
explainer_end(eval_examples)]
elif self._task_type == ModelTask.GENERATIVE_TEXT:
context = self._evaluation_examples[CONTEXT]
questions = self._evaluation_examples[QUESTIONS]
eval_examples = []
for context, question in zip(context, questions):
eval_examples.append(question + SEP + context)

sentence_embedder = SentenceTransformer('all-MiniLM-L6-v2')
explainer = LocalExplanationSentenceEmbedder(
sentence_embedder=sentence_embedder,
perturbation_model="removal",
partition_fn="sentences",
progress_bar=None)
max_completion = 50 # Define max tokens for the completion

api_settings = {
"api_type": self._model.model.api_type,
"api_base": self._model.model.api_base,
"api_version": self._model.model.api_version,
"api_key": self._model.model.api_key
}
model_wrapped = ChatOpenAI(
engine=self._model.model.engine,
encoding="cl100k_base",
api_settings=api_settings)
completions = model_wrapped.sample(
eval_examples, max_new_tokens=max_completion)

explanation = []
for i, completion in enumerate(completions):
attribution, parts = explainer.attribution(model_wrapped,
eval_examples[i],
completion,
)
explanation.append((attribution, parts))

self._explanation = explanation
else:
raise ValueError("Unknown task type: {}".format(self._task_type))

Expand Down
4 changes: 4 additions & 0 deletions responsibleai_text/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
'bert_score',
'nltk',
'rouge_score'
],
"generative_text": [
'interpret_text',
'sentence_transformers'
]
}
setuptools.setup(
Expand Down
Loading