Skip to content

Commit

Permalink
Address feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
kaancayli committed Feb 15, 2024
1 parent 8cde996 commit 95e767a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/pipeline/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from pipeline.pipeline import SimplePipeline
from pipeline.pipeline import AbstractPipeline
1 change: 1 addition & 0 deletions app/pipeline/chat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from simple_chat_pipeline import SimpleChatPipeline
36 changes: 36 additions & 0 deletions app/pipeline/chat/simple_chat_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from operator import itemgetter

from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import Runnable

from domain import IrisMessage, IrisMessageRole
from llm.langchain import IrisLangchainChatModel
from pipeline import AbstractPipeline


class SimpleChatPipeline(AbstractPipeline):
"""A simple chat pipeline that uses our custom langchain chat model for our own request handler"""

llm: IrisLangchainChatModel
pipeline: Runnable

def __init__(self, llm: IrisLangchainChatModel, name=None):
self.llm = llm
self.pipeline = {"query": itemgetter("query")} | llm | StrOutputParser()
super().__init__(name=name)

def __call__(self, query: IrisMessage, **kwargs):
return self._run(query=query, **kwargs)

def _run(self, query: IrisMessage, **kwargs) -> IrisMessage:
"""
Runs the pipeline and is intended to be called by __call__
:param query: The query
:param kwargs: keyword arguments
:return: IrisMessage
"""
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)
37 changes: 11 additions & 26 deletions app/pipeline/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
from abc import ABCMeta, abstractmethod
from operator import itemgetter

from langchain_core.output_parsers import StrOutputParser
from domain import IrisMessage, IrisMessageRole
from domain import IrisMessage


class AbstractPipeline(metaclass=ABCMeta):
"""Abstract class for all pipelines"""

name: str

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)}"
def __call__(self, **kwargs):
return self._run(**kwargs)

@abstractmethod
def run(self, *args, **kwargs) -> IrisMessage:
"""Runs the pipeline"""
def _run(self, **kwargs) -> IrisMessage:
"""
Runs the pipeline and is intended to be called by __call__
:param kwargs: keyword arguments
:return: IrisMessage
"""
raise NotImplementedError


class SimplePipeline(AbstractPipeline):
"""A simple pipeline that does not have any memory etc."""

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:
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 95e767a

Please sign in to comment.