fix(langchain): try/catch langchain import in service; Only langchain is installed with the [langchain] extra (#190)

This commit is contained in:
TomTom101
2024-05-31 10:19:27 +02:00
parent b19243ab75
commit 4b39efeee3
3 changed files with 17 additions and 10 deletions

View File

@@ -1,8 +1,6 @@
import sys
from typing import Union
from langchain_core.messages import AIMessageChunk
from langchain_core.runnables import Runnable
from loguru import logger
from pipecat.frames.frames import (Frame, LLMFullResponseEndFrame,
@@ -11,6 +9,15 @@ from pipecat.frames.frames import (Frame, LLMFullResponseEndFrame,
TextFrame)
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
try:
from langchain_core.messages import AIMessageChunk
from langchain_core.runnables import Runnable
except ModuleNotFoundError as e:
logger.exception(
"In order to use Langchain, you need to `pip install pipecat-ai[langchain]`. "
)
raise Exception(f"Missing module: {e}")
class LangchainProcessor(FrameProcessor):
def __init__(self, chain: Runnable, transcript_key: str = "input"):