-
Notifications
You must be signed in to change notification settings - Fork 170
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
first commit for rd #251
Closed
Closed
first commit for rd #251
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from abc import abstractmethod | ||
from typing import Iterator, Optional | ||
|
||
from ..providers.prompt import PromptProvider | ||
from ..providers.llm import LLMProvider | ||
from ..providers.logging import LoggingDatabaseConnection, log_execution_to_db | ||
from r2r.core import BasicDocument, GenerationConfig | ||
from r2r.pipelines import Pipeline | ||
|
||
class EntityExtractionPipeline(Pipeline): | ||
def __init__( | ||
self, | ||
llm: LLMProvider, | ||
prompt_provider: PromptProvider, | ||
logging_connection: Optional[LoggingDatabaseConnection] = None, | ||
*args, | ||
**kwargs, | ||
): | ||
self.llm = llm | ||
self.prompt_provider = prompt_provider | ||
super().__init__(logging_connection=logging_connection, **kwargs) | ||
|
||
@abstractmethod | ||
def preprocess_text(self, text: str) -> str: | ||
pass | ||
|
||
@abstractmethod | ||
def extract_entities(self, text: str, generation_config: GenerationConfig) -> list[str]: | ||
pass | ||
|
||
@abstractmethod | ||
def postprocess_entities(self, entities: list[str]) -> list[str]: | ||
pass | ||
|
||
@abstractmethod | ||
def run(self, documents: Iterator[BasicDocument]) -> Iterator[BasicDocument]: | ||
pass |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import Iterator | ||
|
||
from r2r.core import BasicDocument, EntityExtractionPipeline, GenerationConfig | ||
from r2r.pipelines import BasicPromptProvider | ||
|
||
class BasicEntityExtractionPipeline(EntityExtractionPipeline): | ||
BASIC_SYSTEM_PROMPT = "You are a helpful assistant." | ||
BASIC_TASK_PROMPT = """ | ||
## Task: | ||
Extract the named entities from the following text document, and return them in a comma-separated list. | ||
|
||
## Response: | ||
""" | ||
def __init__(self, llm, logging_connection=None, *args, **kwargs): | ||
super().__init__(prompt_provider=BasicPromptProvider(BasicEntityExtractionPipeline.BASIC_SYSTEM_PROMPT, BasicEntityExtractionPipeline.BASIC_TASK_PROMPT), logging_connection=logging_connection, **kwargs) | ||
self.llm = llm | ||
|
||
def preprocess_text(self, text: str) -> str: | ||
# Optional - Implement text preprocessing logic here | ||
return text | ||
|
||
def extract_entities(self, text: str, generation_config: GenerationConfig) -> list[str]: | ||
# entities = self.com | ||
self._check_pipeline_initialized() | ||
messages = [ | ||
{ | ||
"role": "system", | ||
"content": self.prompt_provider.get_prompt("system_prompt"), | ||
|
||
}, | ||
{ | ||
"role": "user", | ||
"content": self.prompt_provider.get_prompt("task_prompt"), | ||
}, | ||
] | ||
entities_list = self.llm.get_completion(text, generation_config) | ||
if not "," in entities_list: | ||
entities = [] | ||
else: | ||
entities = entities_list.split(",") | ||
return entities | ||
|
||
def postprocess_entities(self, entities: list[str]) -> list[str]: | ||
# Implement entity postprocessing logic here | ||
return [entity.upper() for entity in entities] | ||
|
||
def run(self, documents: Iterator[BasicDocument]) -> Iterator[BasicDocument]: | ||
for document in documents: | ||
preprocessed_text = self.preprocess_text(document.text) | ||
entities = self.extract_entities(preprocessed_text) | ||
postprocessed_entities = self.postprocess_entities(entities) | ||
document.metadata["entities"] = postprocessed_entities | ||
yield document |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the Pipeline class is in .pipeline
Should it be
from .pipeline import Pipeline