-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
138 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,45 @@ | ||
import re | ||
from dataclasses import dataclass | ||
from typing import Any, Dict, List | ||
from typing import Any, Dict, List, Tuple | ||
|
||
from ..core import BaseTextClassifier, Score | ||
|
||
|
||
@dataclass | ||
class RepeatedTokenClassifier(BaseTextClassifier[bool]): | ||
class RepeatedTokenClassifier(BaseTextClassifier[str]): | ||
def score( | ||
self, input: str, _references: List[str] | None = None, metadata: Dict[str, Any] | None = None | ||
) -> Score[bool]: | ||
) -> Score[str]: | ||
if not metadata: | ||
raise ValueError("metadata is missing") | ||
|
||
repeated_token = metadata.get("repeated_token") | ||
token = metadata.get("token") | ||
|
||
if not repeated_token: | ||
raise ValueError("metadata with repeated token is missing") | ||
if not token: | ||
raise ValueError("token is missing in metadata") | ||
|
||
# TODO | ||
repetition = metadata.get("repetition", 20) | ||
|
||
return Score[bool]( | ||
flagged=False, | ||
value=False, | ||
# Search for the pattern in the text | ||
match, i = self._score(str(token), int(repetition), input) | ||
|
||
return Score[str]( | ||
flagged=match, | ||
value=input[i:], | ||
description="TODO", | ||
explanation="TODO", | ||
) | ||
|
||
def _score(self, token: str, repetition: int, input: str) -> Tuple[bool, int]: | ||
if token not in input: | ||
return False, -1 | ||
|
||
tokens = list(re.finditer(re.escape(token), input)) | ||
|
||
if len(tokens) < repetition: | ||
return False, -1 | ||
|
||
if len(input) > tokens[-1].end(): | ||
return True, tokens[-1].end() | ||
|
||
return False, -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Any, Dict | ||
|
||
from langchain_core.prompt_values import PromptValue | ||
from langchain_core.prompt_values import StringPromptValue as LangchainStringPromptValue | ||
|
||
BasePromptValue = PromptValue | ||
StringPromptValue = LangchainStringPromptValue | ||
|
||
|
||
@dataclass | ||
class Prompt: | ||
value: str | BasePromptValue | ||
metadata: Dict[str, Any] = field(default_factory=dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.