-
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.
- Loading branch information
Showing
4 changed files
with
49 additions
and
27 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 +1 @@ | ||
from pipeline.pipeline import SimplePipeline | ||
from pipeline.pipeline import AbstractPipeline |
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 simple_chat_pipeline import SimpleChatPipeline |
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,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) |
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,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) |