Update Gemini adapter to be able to handle LLMSpecificMessages containing Google-formatted messages
This commit is contained in:
@@ -15,7 +15,12 @@ from loguru import logger
|
|||||||
|
|
||||||
from pipecat.adapters.base_llm_adapter import BaseLLMAdapter
|
from pipecat.adapters.base_llm_adapter import BaseLLMAdapter
|
||||||
from pipecat.adapters.schemas.tools_schema import AdapterType, ToolsSchema
|
from pipecat.adapters.schemas.tools_schema import AdapterType, ToolsSchema
|
||||||
from pipecat.processors.aggregators.llm_context import LLMContext, LLMContextMessage
|
from pipecat.processors.aggregators.llm_context import (
|
||||||
|
LLMContext,
|
||||||
|
LLMContextMessage,
|
||||||
|
LLMSpecificMessage,
|
||||||
|
LLMStandardMessage,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from google.genai.types import (
|
from google.genai.types import (
|
||||||
@@ -118,10 +123,7 @@ class GeminiLLMAdapter(BaseLLMAdapter[GeminiLLMInvocationParams]):
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class ConvertedMessages:
|
class ConvertedMessages:
|
||||||
"""Container for converted messages.
|
"""Container for Google-formatted messages converted from universal context."""
|
||||||
|
|
||||||
Holds the converted messages in a format suitable for Gemini's API.
|
|
||||||
"""
|
|
||||||
|
|
||||||
messages: List[Content]
|
messages: List[Content]
|
||||||
system_instruction: Optional[str] = None
|
system_instruction: Optional[str] = None
|
||||||
@@ -150,14 +152,13 @@ class GeminiLLMAdapter(BaseLLMAdapter[GeminiLLMInvocationParams]):
|
|||||||
|
|
||||||
# Process each message, preserving Google-formatted messages and converting others
|
# Process each message, preserving Google-formatted messages and converting others
|
||||||
for message in universal_context_messages:
|
for message in universal_context_messages:
|
||||||
if isinstance(message, Content):
|
if isinstance(message, LLMSpecificMessage):
|
||||||
# Keep existing Google-formatted messages (e.g., function calls/responses)
|
# Assume that LLMSpecificMessage wraps a message in Google format
|
||||||
# TODO: this branch is probably not needed anymore, since LLMContext contains a universal format
|
messages.append(message.message)
|
||||||
messages.append(message)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Convert standard format to Google format
|
# Convert standard format to Google format
|
||||||
converted = self._from_universal_context_message(message)
|
converted = self._from_standard_message(message)
|
||||||
if isinstance(converted, Content):
|
if isinstance(converted, Content):
|
||||||
# Regular (non-system) message
|
# Regular (non-system) message
|
||||||
messages.append(converted)
|
messages.append(converted)
|
||||||
@@ -183,7 +184,7 @@ class GeminiLLMAdapter(BaseLLMAdapter[GeminiLLMInvocationParams]):
|
|||||||
|
|
||||||
return self.ConvertedMessages(messages=messages, system_instruction=system_instruction)
|
return self.ConvertedMessages(messages=messages, system_instruction=system_instruction)
|
||||||
|
|
||||||
def _from_universal_context_message(self, message: LLMContextMessage) -> Content | str:
|
def _from_standard_message(self, message: LLMStandardMessage) -> Content | str:
|
||||||
"""Convert universal context message to Google Content object.
|
"""Convert universal context message to Google Content object.
|
||||||
|
|
||||||
Handles conversion of text, images, and function calls to Google's
|
Handles conversion of text, images, and function calls to Google's
|
||||||
|
|||||||
Reference in New Issue
Block a user