-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pipeline base class and a simple pipeline
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
from pipeline.pipeline import SimplePipeline |
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 ABCMeta, abstractmethod | ||
from operator import itemgetter | ||
|
||
from langchain_core.output_parsers import StrOutputParser | ||
from domain import IrisMessage, IrisMessageRole | ||
|
||
|
||
class BasePipeline(metaclass=ABCMeta): | ||
""" Abstract class for all pipelines """ | ||
def __init__(self, name=None): | ||
self.name = name | ||
|
||
def __repr__(self): | ||
return f'{self.__class__.__name__} {self.name if self.name is not None else id(self)}' | ||
|
||
def __str__(self): | ||
return f'{self.__class__.__name__} {self.name if self.name is not None else id(self)}' | ||
|
||
@abstractmethod | ||
def run(self, *args, **kwargs) -> IrisMessage: | ||
""" Run the pipeline """ | ||
raise NotImplementedError | ||
|
||
|
||
class SimplePipeline(BasePipeline): | ||
def __init__(self, llm, name=None): | ||
super().__init__(name=name) | ||
self.llm = llm | ||
self.pipeline = {"query": itemgetter("query")} | llm | StrOutputParser() | ||
|
||
def run(self, *args, query: IrisMessage, **kwargs) -> IrisMessage: | ||
""" A simple pipeline that does not have any memory etc.""" | ||
if query is None: | ||
raise ValueError("IrisMessage must not be None") | ||
message = query.text | ||
response = self.pipeline.invoke({"query": message}) | ||
return IrisMessage(role=IrisMessageRole.ASSISTANT, text=response) |