Add support for universal LLMContext to LangchainProcessor

This commit is contained in:
Paul Kompfner
2025-09-23 10:00:48 -04:00
parent a19b9f70c0
commit 97868175e6
2 changed files with 14 additions and 14 deletions

View File

@@ -23,13 +23,8 @@ from pipecat.frames.frames import LLMMessagesUpdateFrame
from pipecat.pipeline.pipeline import Pipeline from pipecat.pipeline.pipeline import Pipeline
from pipecat.pipeline.runner import PipelineRunner from pipecat.pipeline.runner import PipelineRunner
from pipecat.pipeline.task import PipelineParams, PipelineTask from pipecat.pipeline.task import PipelineParams, PipelineTask
from pipecat.processors.aggregators.llm_response import ( from pipecat.processors.aggregators.llm_context import LLMContext
LLMAssistantContextAggregator, from pipecat.processors.aggregators.llm_response_universal import LLMContextAggregatorPair
LLMUserContextAggregator,
)
from pipecat.processors.aggregators.openai_llm_context import (
OpenAILLMContext,
)
from pipecat.processors.frameworks.langchain import LangchainProcessor from pipecat.processors.frameworks.langchain import LangchainProcessor
from pipecat.runner.types import RunnerArguments from pipecat.runner.types import RunnerArguments
from pipecat.runner.utils import create_transport from pipecat.runner.utils import create_transport
@@ -106,19 +101,18 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
) )
lc = LangchainProcessor(history_chain) lc = LangchainProcessor(history_chain)
context = OpenAILLMContext() context = LLMContext()
tma_in = LLMUserContextAggregator(context=context) context_aggregator = LLMContextAggregatorPair(context)
tma_out = LLMAssistantContextAggregator(context=context)
pipeline = Pipeline( pipeline = Pipeline(
[ [
transport.input(), # Transport user input transport.input(), # Transport user input
stt, stt,
tma_in, # User responses context_aggregator.user(), # User responses
lc, # Langchain lc, # Langchain
tts, # TTS tts, # TTS
transport.output(), # Transport bot output transport.output(), # Transport bot output
tma_out, # Assistant spoken responses context_aggregator.assistant(), # Assistant spoken responses
] ]
) )

View File

@@ -12,6 +12,7 @@ from loguru import logger
from pipecat.frames.frames import ( from pipecat.frames.frames import (
Frame, Frame,
LLMContextFrame,
LLMFullResponseEndFrame, LLMFullResponseEndFrame,
LLMFullResponseStartFrame, LLMFullResponseStartFrame,
TextFrame, TextFrame,
@@ -64,11 +65,16 @@ class LangchainProcessor(FrameProcessor):
""" """
await super().process_frame(frame, direction) await super().process_frame(frame, direction)
if isinstance(frame, OpenAILLMContextFrame): if isinstance(frame, (LLMContextFrame, OpenAILLMContextFrame)):
# Messages are accumulated on the context as a list of messages. # Messages are accumulated on the context as a list of messages.
# The last one by the human is the one we want to send to the LLM. # The last one by the human is the one we want to send to the LLM.
logger.debug(f"Got transcription frame {frame}") logger.debug(f"Got transcription frame {frame}")
text: str = frame.context.messages[-1]["content"] messages = (
frame.context.messages
if isinstance(frame, OpenAILLMContextFrame)
else frame.context.get_messages()
)
text: str = messages[-1]["content"]
await self._ainvoke(text.strip()) await self._ainvoke(text.strip())
else: else: