-
Notifications
You must be signed in to change notification settings - Fork 470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce support for extracting and processing grounding metadata from GoogleLLMService. #1030
Open
filipi87
wants to merge
1
commit into
main
Choose a base branch
from
gemini_grounding_metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+235
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# | ||
# Copyright (c) 2024, Daily | ||
# | ||
# SPDX-License-Identifier: BSD 2-Clause License | ||
# | ||
|
||
import asyncio | ||
import os | ||
import sys | ||
from pathlib import Path | ||
|
||
import aiohttp | ||
from dotenv import load_dotenv | ||
from loguru import logger | ||
|
||
from pipecat.audio.vad.silero import SileroVADAnalyzer | ||
from pipecat.frames.frames import Frame | ||
from pipecat.pipeline.pipeline import Pipeline | ||
from pipecat.pipeline.runner import PipelineRunner | ||
from pipecat.pipeline.task import PipelineParams, PipelineTask | ||
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext | ||
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor | ||
from pipecat.services.cartesia import CartesiaTTSService | ||
from pipecat.services.deepgram import DeepgramSTTService | ||
from pipecat.services.google import GoogleLLMService, LLMSearchResponseFrame | ||
from pipecat.transports.services.daily import DailyParams, DailyTransport | ||
|
||
sys.path.append(str(Path(__file__).parent.parent)) | ||
from runner import configure | ||
|
||
load_dotenv(override=True) | ||
|
||
logger.remove(0) | ||
logger.add(sys.stderr, level="DEBUG") | ||
|
||
# Function handlers for the LLM | ||
search_tool = {"google_search_retrieval": {}} | ||
tools = [search_tool] | ||
|
||
system_instruction = """ | ||
You are an expert at providing the most recent news from any place. Your responses will be converted to audio, so avoid using special characters or overly complex formatting. | ||
|
||
Always use the google search API to retrieve the latest news. You must also use it to check which day is today. | ||
|
||
You can: | ||
- Use the Google search API to check the current date. | ||
- Provide the most recent and relevant news from any place by using the google search API. | ||
- Answer any questions the user may have, ensuring your responses are accurate and concise. | ||
|
||
Start each interaction by asking the user about which place they would like to know the information. | ||
""" | ||
|
||
|
||
class LLMSearchLoggerProcessor(FrameProcessor): | ||
async def process_frame(self, frame: Frame, direction: FrameDirection): | ||
await super().process_frame(frame, direction) | ||
|
||
if isinstance(frame, LLMSearchResponseFrame): | ||
print(f"LLMSearchLoggerProcessor: {frame}") | ||
|
||
await self.push_frame(frame) | ||
|
||
|
||
async def main(): | ||
async with aiohttp.ClientSession() as session: | ||
(room_url, token) = await configure(session) | ||
|
||
transport = DailyTransport( | ||
room_url, | ||
token, | ||
"Latest news!", | ||
DailyParams( | ||
audio_out_enabled=True, | ||
vad_enabled=True, | ||
vad_analyzer=SileroVADAnalyzer(), | ||
vad_audio_passthrough=True, | ||
), | ||
) | ||
|
||
stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY")) | ||
|
||
tts = CartesiaTTSService( | ||
api_key=os.getenv("CARTESIA_API_KEY"), | ||
voice_id="79a125e8-cd45-4c13-8a67-188112f4dd22", # British Lady | ||
) | ||
|
||
# Initialize the Gemini Multimodal Live model | ||
llm = GoogleLLMService( | ||
api_key=os.getenv("GOOGLE_API_KEY"), | ||
system_instruction=system_instruction, | ||
tools=tools, | ||
) | ||
|
||
context = OpenAILLMContext( | ||
[ | ||
{ | ||
"role": "user", | ||
"content": "Start by greeting the user warmly, introducing yourself, and mentioning the current day. Be friendly and engaging to set a positive tone for the interaction.", | ||
} | ||
], | ||
) | ||
context_aggregator = llm.create_context_aggregator(context) | ||
|
||
llm_search_logger = LLMSearchLoggerProcessor() | ||
|
||
pipeline = Pipeline( | ||
[ | ||
transport.input(), | ||
stt, | ||
context_aggregator.user(), | ||
llm, | ||
llm_search_logger, | ||
tts, | ||
transport.output(), | ||
context_aggregator.assistant(), | ||
] | ||
) | ||
|
||
task = PipelineTask(pipeline, PipelineParams(allow_interruptions=True)) | ||
|
||
@transport.event_handler("on_first_participant_joined") | ||
async def on_first_participant_joined(transport, participant): | ||
await task.queue_frames([context_aggregator.user().get_context_frame()]) | ||
|
||
runner = PipelineRunner() | ||
await runner.run(task) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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,2 @@ | ||
from .frames import LLMSearchResponseFrame | ||
from .google import * |
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,33 @@ | ||
# | ||
# Copyright (c) 2024–2025, Daily | ||
# | ||
# SPDX-License-Identifier: BSD 2-Clause License | ||
# | ||
|
||
from dataclasses import dataclass, field | ||
from typing import List, Optional | ||
|
||
from pipecat.frames.frames import DataFrame | ||
|
||
|
||
@dataclass | ||
class LLMSearchResult: | ||
text: str | ||
confidence: Optional[float] = None | ||
|
||
|
||
@dataclass | ||
class LLMSearchOrigin: | ||
site_uri: Optional[str] = None | ||
site_title: Optional[str] = None | ||
results: List[LLMSearchResult] = field(default_factory=list) | ||
|
||
|
||
@dataclass | ||
class LLMSearchResponseFrame(DataFrame): | ||
search_result: Optional[str] = None | ||
rendered_content: Optional[str] = None | ||
origins: List[LLMSearchOrigin] = field(default_factory=list) | ||
|
||
def __str__(self): | ||
return f"LLMSearchResponseFrame(search_result={self.search_result}, origins={self.origins})" |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One nice improvement would be to create an RTVI message so we can share this response with the RTVI clients. What do you think? I can create a follow-up PR to implement the message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like:
RTVIBotLLMSearchResponse
.What do you think ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have included it in this PR: #1051