Skip to content

Commit

Permalink
Add pipeline base class and a simple pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
kaancayli committed Feb 15, 2024
1 parent 626a92c commit b18b16e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/pipeline/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from pipeline.pipeline import SimplePipeline
37 changes: 37 additions & 0 deletions app/pipeline/pipeline.py
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)

0 comments on commit b18b16e

Please sign in to comment.