Merge pull request #199 from pipecat-ai/aleix/langchain-changelog

move LangchainProcessor to processors/frameworks and update CHANGELOG
This commit is contained in:
Aleix Conchillo Flaqué
2024-06-01 07:46:51 +08:00
committed by GitHub
9 changed files with 31 additions and 25 deletions

View File

@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
the weather in Los Angeles, the LLM will call your function. the weather in Los Angeles, the LLM will call your function.
See https://platform.openai.com/docs/guides/function-calling See https://platform.openai.com/docs/guides/function-calling
- Added new `LangchainProcessor`.
- Added Cartesia TTS support (https://cartesia.ai/) - Added Cartesia TTS support (https://cartesia.ai/)
### Fixed ### Fixed

View File

@@ -1,14 +1,23 @@
import sys #
# Copyright (c) 2024, Daily
#
# SPDX-License-Identifier: BSD 2-Clause License
#
from typing import Union from typing import Union
from loguru import logger from pipecat.frames.frames import (
Frame,
from pipecat.frames.frames import (Frame, LLMFullResponseEndFrame, LLMFullResponseEndFrame,
LLMFullResponseStartFrame, LLMMessagesFrame, LLMFullResponseStartFrame,
LLMResponseEndFrame, LLMResponseStartFrame, LLMMessagesFrame,
TextFrame) LLMResponseEndFrame,
LLMResponseStartFrame,
TextFrame)
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
from loguru import logger
try: try:
from langchain_core.messages import AIMessageChunk from langchain_core.messages import AIMessageChunk
from langchain_core.runnables import Runnable from langchain_core.runnables import Runnable
@@ -38,26 +47,17 @@ class LangchainProcessor(FrameProcessor):
await self._ainvoke(text.strip()) await self._ainvoke(text.strip())
else: else:
await self.push_frame(frame) await self.push_frame(frame, direction)
async def _invoke(self, text: str):
response = await self._chain.ainvoke(
{self._transcript_key: text},
config={"configurable": {"session_id": self._participant_id}},
)
await self.push_frame(LLMFullResponseStartFrame())
await self.push_frame(TextFrame(response))
await self.push_frame(LLMFullResponseEndFrame())
@staticmethod @staticmethod
def __get_token_value(text: Union[str, AIMessageChunk]) -> str | None: def __get_token_value(text: Union[str, AIMessageChunk]) -> str:
match text: match text:
case str(): case str():
return text return text
case AIMessageChunk(): case AIMessageChunk():
return text.content return text.content
case _: case _:
return None return ""
async def _ainvoke(self, text: str): async def _ainvoke(self, text: str):
logger.debug(f"Invoking chain with {text}") logger.debug(f"Invoking chain with {text}")
@@ -72,8 +72,6 @@ class LangchainProcessor(FrameProcessor):
await self.push_frame(LLMResponseEndFrame()) await self.push_frame(LLMResponseEndFrame())
except GeneratorExit: except GeneratorExit:
logger.warning("Generator was closed prematurely") logger.warning("Generator was closed prematurely")
raise # Re-raise to ensure proper generator closure
except Exception as e: except Exception as e:
logger.error(f"An unknown error occurred: {e}") logger.error(f"An unknown error occurred: {e}")
raise
await self.push_frame(LLMFullResponseEndFrame()) await self.push_frame(LLMFullResponseEndFrame())

View File

View File

View File

@@ -1,7 +1,10 @@
import unittest #
# Copyright (c) 2024, Daily
#
# SPDX-License-Identifier: BSD 2-Clause License
#
from langchain.prompts import ChatPromptTemplate import unittest
from langchain_core.language_models import FakeStreamingListLLM
from pipecat.frames.frames import (LLMFullResponseEndFrame, from pipecat.frames.frames import (LLMFullResponseEndFrame,
LLMFullResponseStartFrame, StopTaskFrame, LLMFullResponseStartFrame, StopTaskFrame,
@@ -14,7 +17,10 @@ from pipecat.pipeline.task import PipelineTask
from pipecat.processors.aggregators.llm_response import ( from pipecat.processors.aggregators.llm_response import (
LLMAssistantResponseAggregator, LLMUserResponseAggregator) LLMAssistantResponseAggregator, LLMUserResponseAggregator)
from pipecat.processors.frame_processor import FrameProcessor from pipecat.processors.frame_processor import FrameProcessor
from pipecat.services.langchain import LangchainProcessor from pipecat.processors.frameworks.langchain import LangchainProcessor
from langchain.prompts import ChatPromptTemplate
from langchain_core.language_models import FakeStreamingListLLM
class TestLangchain(unittest.IsolatedAsyncioTestCase): class TestLangchain(unittest.IsolatedAsyncioTestCase):