Spaces:
Build error
Build error
File size: 19,721 Bytes
64772a4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
from dataclasses import dataclass
from enum import Enum
from typing import (
Any,
List,
Dict,
Mapping,
Optional,
Union,
)
# Import Literal with Python 3.7 fallback
from typing_extensions import Literal
from aleph_alpha_client import Text
from aleph_alpha_client.prompt import ControlTokenOverlap, Image, Prompt, PromptItem
class ExplanationPostprocessing(Enum):
"""
Available types of explanation postprocessing.
Square:
Square each score
Absolute:
Take the absolute value of each score
"""
Square = "square"
Absolute = "absolute"
def to_json(self) -> str:
return self.value
@dataclass(frozen=True)
class CustomGranularity:
"""
Allows for passing a custom delimiter to determine the granularity to
to explain the prompt by. The text of the prompt will be split by the
delimiter you provide.
Parameters:
delimiter (str, required):
String to split the text in the prompt by for generating
explanations for your prompt.
"""
delimiter: str
def to_json(self) -> Mapping[str, Any]:
return {"type": "custom", "delimiter": self.delimiter}
class PromptGranularity(Enum):
Token = "token"
Word = "word"
Sentence = "sentence"
Paragraph = "paragraph"
def to_json(self) -> Mapping[str, Any]:
return {"type": self.value}
def prompt_granularity_to_json(
prompt_granularity: Union[PromptGranularity, str, CustomGranularity],
) -> Mapping[str, Any]:
# we allow str for backwards compatibility
# This was previously possible because PromptGranularity was not an Enum
if isinstance(prompt_granularity, str):
return {"type": prompt_granularity}
return prompt_granularity.to_json()
class TargetGranularity(Enum):
"""
How many explanations should be returned in the output.
Complete:
Return one explanation for the entire target. Helpful in many cases to determine which parts of the prompt contribute overall to the given completion.
Token:
Return one explanation for each token in the target.
"""
Complete = "complete"
Token = "token"
def to_json(self) -> str:
return self.value
@dataclass(frozen=True)
class ExplanationRequest:
"""
Describes an Explanation request you want to make agains the API.
Parameters:
prompt (Prompt, required)
Prompt you want to generate explanations for a target completion.
target (str, required)
The completion string to be explained based on model probabilities.
contextual_control_threshold (float, default None)
If set to None, attention control parameters only apply to those tokens that have
explicitly been set in the request.
If set to a non-None value, we apply the control parameters to similar tokens as well.
Controls that have been applied to one token will then be applied to all other tokens
that have at least the similarity score defined by this parameter.
The similarity score is the cosine similarity of token embeddings.
control_factor (float, default None):
The amount to adjust model attention by.
For Explanation, you want to supress attention, and the API will default to 0.1.
Values between 0 and 1 will supress attention.
A value of 1 will have no effect.
Values above 1 will increase attention.
control_token_overlap (ControlTokenOverlap, default None)
What to do if a control partially overlaps with a text or image token.
If set to "partial", the factor will be adjusted proportionally with the amount
of the token it overlaps. So a factor of 2.0 of a control that only covers 2 of
4 token characters, would be adjusted to 1.5.
If set to "complete", the full factor will be applied as long as the control
overlaps with the token at all.
control_log_additive (bool, default None)
True: apply control by adding the log(control_factor) to attention scores.
False: apply control by (attention_scores - - attention_scores.min(-1)) * control_factor
If None, the API will default to True
prompt_granularity (Union[PromptGranularity, str, CustomGranularity], default None)
At which granularity should the target be explained in terms of the prompt.
If you choose, for example, "sentence" then we report the importance score of each
sentence in the prompt towards generating the target output.
If you do not choose a granularity then we will try to find the granularity that
brings you closest to around 30 explanations. For large documents, this would likely
be sentences. For short prompts this might be individual words or even tokens.
If you choose a custom granularity then you must provide a custom delimiter. We then
split your prompt by that delimiter. This might be helpful if you are using few-shot
prompts that contain stop sequences.
We currently support providing the prompt_granularity as PromptGranularity (recommended)
or CustomGranularity (if needed) or str (deprecated). Note that supplying plain strings
only makes sense if you choose one of the values defined in the PromptGranularity enum.
All other strings will be rejected by the API. In future versions we might cut support
for plain str values.
For image prompt items, the granularities determine into how many tiles we divide
the image for the explanation.
"token" -> 12x12
"word" -> 6x6
"sentence" -> 3x3
"paragraph" -> 1
target_granularity (TargetGranularity, default None)
How many explanations should be returned in the output.
"complete" -> Return one explanation for the entire target. Helpful in many cases to determine which parts of the prompt contribute overall to the given completion.
"token" -> Return one explanation for each token in the target.
If None, API will default to "complete"
postprocessing (ExplanationPostprocessing, default None)
Optionally apply postprocessing to the difference in cross entropy scores for each token.
"none": Apply no postprocessing.
"absolute": Return the absolute value of each value.
"square": Square each value
normalize (bool, default None)
Return normalized scores. Minimum score becomes 0 and maximum score becomes 1. Applied after any postprocessing
"""
prompt: Prompt
target: str
contextual_control_threshold: Optional[float] = None
control_factor: Optional[float] = None
control_token_overlap: Optional[ControlTokenOverlap] = None
control_log_additive: Optional[bool] = None
prompt_granularity: Optional[
Union[PromptGranularity, str, CustomGranularity]
] = None
target_granularity: Optional[TargetGranularity] = None
postprocessing: Optional[ExplanationPostprocessing] = None
normalize: Optional[bool] = None
def to_json(self) -> Mapping[str, Any]:
payload: Dict[str, Any] = {
"prompt": self.prompt.to_json(),
"target": self.target,
}
if self.contextual_control_threshold is not None:
payload["contextual_control_threshold"] = self.contextual_control_threshold
if self.control_token_overlap is not None:
payload["control_token_overlap"] = self.control_token_overlap.to_json()
if self.postprocessing is not None:
payload["postprocessing"] = self.postprocessing.to_json()
if self.control_log_additive is not None:
payload["control_log_additive"] = self.control_log_additive
if self.prompt_granularity is not None:
payload["prompt_granularity"] = prompt_granularity_to_json(
self.prompt_granularity
)
if self.target_granularity is not None:
payload["target_granularity"] = self.target_granularity.to_json()
if self.postprocessing is not None:
payload["postprocessing"] = self.postprocessing.to_json()
if self.normalize is not None:
payload["normalize"] = self.normalize
if self.control_factor is not None:
payload["control_factor"] = self.control_factor
return payload
@dataclass(frozen=True)
class TextScore:
start: int
length: int
score: float
@staticmethod
def from_json(score: Any) -> "TextScore":
return TextScore(
start=score["start"],
length=score["length"],
score=score["score"],
)
@dataclass(frozen=True)
class TextScoreWithRaw:
start: int
length: int
score: float
text: str
@staticmethod
def from_text_score(score: TextScore, prompt: Text) -> "TextScoreWithRaw":
return TextScoreWithRaw(
start=score.start,
length=score.length,
score=score.score,
text=prompt.text[score.start : score.start + score.length],
)
@dataclass(frozen=True)
class ImageScore:
left: float
top: float
width: float
height: float
score: float
@staticmethod
def from_json(score: Any) -> "ImageScore":
return ImageScore(
left=score["rect"]["left"],
top=score["rect"]["top"],
width=score["rect"]["width"],
height=score["rect"]["height"],
score=score["score"],
)
@dataclass(frozen=True)
class TargetScore:
start: int
length: int
score: float
@staticmethod
def from_json(score: Any) -> "TargetScore":
return TargetScore(
start=score["start"],
length=score["length"],
score=score["score"],
)
@dataclass(frozen=True)
class TargetScoreWithRaw:
start: int
length: int
score: float
text: str
@staticmethod
def from_target_score(score: TargetScore, target: str) -> "TargetScoreWithRaw":
return TargetScoreWithRaw(
start=score.start,
length=score.length,
score=score.score,
text=target[score.start : score.start + score.length],
)
@dataclass(frozen=True)
class TokenScore:
score: float
@staticmethod
def from_json(score: Any) -> "TokenScore":
return TokenScore(
score=score,
)
@dataclass(frozen=True)
class ImagePromptItemExplanation:
"""
Explains the importance of an image prompt item.
The amount of items in the "scores" array depends on the granularity setting.
Each score object contains the top-left corner of a rectangular area in the image prompt.
The coordinates are all between 0 and 1 in terms of the total image size
"""
scores: List[ImageScore]
@staticmethod
def from_json(item: Dict[str, Any]) -> "ImagePromptItemExplanation":
return ImagePromptItemExplanation(
scores=[ImageScore.from_json(score) for score in item["scores"]]
)
def in_pixels(self, prompt_item: PromptItem) -> "ImagePromptItemExplanation":
if not isinstance(prompt_item, Image):
raise ValueError
(original_image_width, original_image_height) = prompt_item.dimensions()
return ImagePromptItemExplanation(
[
ImageScore(
left=int(score.left * original_image_width),
width=int(score.width * original_image_width),
top=int(score.top * original_image_height),
height=int(score.height * original_image_height),
score=score.score,
)
for score in self.scores
]
)
@dataclass(frozen=True)
class TextPromptItemExplanation:
"""
Explains the importance of a text prompt item.
The amount of items in the "scores" array depends on the granularity setting.
Each score object contains an inclusive start character and a length of the substring plus
a floating point score value.
"""
scores: List[Union[TextScore, TextScoreWithRaw]]
@staticmethod
def from_json(item: Dict[str, Any]) -> "TextPromptItemExplanation":
return TextPromptItemExplanation(
scores=[TextScore.from_json(score) for score in item["scores"]]
)
def with_text(self, prompt: Text) -> "TextPromptItemExplanation":
return TextPromptItemExplanation(
scores=[
TextScoreWithRaw.from_text_score(score, prompt)
if isinstance(score, TextScore)
else score
for score in self.scores
]
)
@dataclass(frozen=True)
class TargetPromptItemExplanation:
"""
Explains the importance of text in the target string that came before the currently
to-be-explained target token. The amount of items in the "scores" array depends on the
granularity setting.
Each score object contains an inclusive start character and a length of the substring plus
a floating point score value.
"""
scores: List[Union[TargetScore, TargetScoreWithRaw]]
@staticmethod
def from_json(item: Dict[str, Any]) -> "TargetPromptItemExplanation":
return TargetPromptItemExplanation(
scores=[TargetScore.from_json(score) for score in item["scores"]]
)
def with_text(self, prompt: str) -> "TargetPromptItemExplanation":
return TargetPromptItemExplanation(
scores=[
TargetScoreWithRaw.from_target_score(score, prompt)
if isinstance(score, TargetScore)
else score
for score in self.scores
]
)
@dataclass(frozen=True)
class TokenPromptItemExplanation:
"""Explains the importance of a request prompt item of type "token_ids".
Will contain one floating point importance value for each token in the same order as in the original prompt.
"""
scores: List[TokenScore]
@staticmethod
def from_json(item: Dict[str, Any]) -> "TokenPromptItemExplanation":
return TokenPromptItemExplanation(
scores=[TokenScore.from_json(score) for score in item["scores"]]
)
@dataclass(frozen=True)
class Explanation:
"""
Explanations for a given portion of the target.
Parameters:
target (str, required)
If target_granularity was set to "complete", then this will be the entire target. If it was set to "token", this will be a single target token.
items (List[Union[TextPromptItemExplanation, TargetPromptItemExplanation, TokenPromptItemExplanation, ImagePromptItemExplanation], required)
Contains one item for each prompt item (in order), and the last item refers to the target.
"""
target: str
items: List[
Union[
TextPromptItemExplanation,
TargetPromptItemExplanation,
TokenPromptItemExplanation,
ImagePromptItemExplanation,
]
]
def prompt_item_from_json(
item: Any,
) -> Union[
TextPromptItemExplanation,
ImagePromptItemExplanation,
TargetPromptItemExplanation,
TokenPromptItemExplanation,
]:
if item["type"] == "text":
return TextPromptItemExplanation.from_json(item)
elif item["type"] == "target":
return TargetPromptItemExplanation.from_json(item)
elif item["type"] == "image":
return ImagePromptItemExplanation.from_json(item)
elif item["type"] == "token_ids":
return TokenPromptItemExplanation.from_json(item)
else:
raise NotImplementedError("Unsupported explanation type")
@staticmethod
def from_json(json: Dict[str, Any]) -> "Explanation":
return Explanation(
target=json["target"],
items=[Explanation.prompt_item_from_json(item) for item in json["items"]],
)
def with_image_prompt_items_in_pixels(self, prompt: Prompt) -> "Explanation":
return Explanation(
target=self.target,
items=[
item.in_pixels(prompt.items[item_index])
if isinstance(item, ImagePromptItemExplanation)
else item
for item_index, item in enumerate(self.items)
],
)
def with_text_from_prompt(self, prompt: Prompt, target: str) -> "Explanation":
items: List[
Union[
TextPromptItemExplanation,
ImagePromptItemExplanation,
TargetPromptItemExplanation,
TokenPromptItemExplanation,
]
] = []
for item_index, item in enumerate(self.items):
if isinstance(item, TextPromptItemExplanation):
# separate variable to fix linting error
prompt_item = prompt.items[item_index]
if isinstance(prompt_item, Text):
items.append(item.with_text(prompt_item))
else:
items.append(item)
elif isinstance(item, TargetPromptItemExplanation):
items.append(item.with_text(target))
else:
items.append(item)
return Explanation(
target=self.target,
items=items,
)
@dataclass(frozen=True)
class ExplanationResponse:
"""
The top-level response data structure that will be returned from an explanation request.
Parameters:
model_version (str, required)
Version of the model used to generate the explanation.
explanations (List[Explanation], required)
This array will contain one explanation object for each portion of the target.
"""
model_version: str
explanations: List[Explanation]
@staticmethod
def from_json(json: Dict[str, Any]) -> "ExplanationResponse":
return ExplanationResponse(
model_version=json["model_version"],
explanations=[
Explanation.from_json(explanation)
for explanation in json["explanations"]
],
)
def with_image_prompt_items_in_pixels(
self, prompt: Prompt
) -> "ExplanationResponse":
mapped_explanations = [
explanation.with_image_prompt_items_in_pixels(prompt)
for explanation in self.explanations
]
return ExplanationResponse(self.model_version, mapped_explanations)
def with_text_from_prompt(
self, request: ExplanationRequest
) -> "ExplanationResponse":
mapped_explanations = [
explanation.with_text_from_prompt(request.prompt, request.target)
for explanation in self.explanations
]
return ExplanationResponse(self.model_version, mapped_explanations)
|