-
Notifications
You must be signed in to change notification settings - Fork 1.5k
openinference pipecat instrumentation example #3221
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
Draft
vipyne
wants to merge
2
commits into
main
Choose a base branch
from
vp-pipecat-instrumentation-refactor
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.
+194
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,179 @@ | ||
| # | ||
| # Copyright (c) 2024–2025, Daily | ||
| # | ||
| # SPDX-License-Identifier: BSD 2-Clause License | ||
| # | ||
|
|
||
|
|
||
| import os | ||
| from datetime import datetime | ||
|
|
||
| from arize.otel import register as register_arize | ||
| from dotenv import load_dotenv | ||
| from loguru import logger | ||
| from phoenix.otel import register as register_phoenix | ||
| from pipecat.audio.turn.smart_turn.base_smart_turn import SmartTurnParams | ||
| from pipecat.audio.turn.smart_turn.local_smart_turn_v3 import LocalSmartTurnAnalyzerV3 | ||
| from pipecat.audio.vad.silero import SileroVADAnalyzer | ||
| from pipecat.audio.vad.vad_analyzer import VADParams | ||
| from pipecat.frames.frames import LLMRunFrame | ||
| from pipecat.pipeline.pipeline import Pipeline | ||
| from pipecat.pipeline.runner import PipelineRunner | ||
| from pipecat.pipeline.task import PipelineParams, PipelineTask | ||
| from pipecat.processors.aggregators.llm_context import LLMContext | ||
| from pipecat.processors.aggregators.llm_response_universal import ( | ||
| LLMContextAggregatorPair, | ||
| ) | ||
| from pipecat.runner.types import RunnerArguments | ||
| from pipecat.runner.utils import create_transport | ||
| from pipecat.services.openai.llm import OpenAILLMService | ||
| from pipecat.services.openai.stt import OpenAISTTService | ||
| from pipecat.services.openai.tts import OpenAITTSService | ||
| from pipecat.transports.base_transport import BaseTransport, TransportParams | ||
| from pipecat.transports.daily.transport import DailyParams | ||
| from pipecat.transports.websocket.fastapi import FastAPIWebsocketParams | ||
|
|
||
| from openinference.instrumentation.pipecat import PipecatInstrumentor | ||
|
|
||
| load_dotenv(override=True) | ||
|
|
||
| conversation_id = f"test-conversation-001_{datetime.now().strftime('%Y%m%d_%H%M%S')}" | ||
| debug_log_filename = os.path.join(os.getcwd(), f"pipecat_frames_{conversation_id}.log") | ||
| print(f"_____49-oi-instrumentation.py * debug_log_filename: {debug_log_filename}") | ||
|
|
||
| def setup_tracer_provider(): | ||
| """ | ||
| Setup the tracer provider. | ||
| """ | ||
| project_name = os.getenv("ARIZE_PROJECT_NAME", "default") | ||
|
|
||
| ARIZE_SPACE_ID = os.getenv("ARIZE_SPACE_ID") | ||
| ARIZE_API_KEY = os.getenv("ARIZE_API_KEY") | ||
| if ARIZE_SPACE_ID and ARIZE_API_KEY: | ||
| return register_arize( | ||
| space_id=ARIZE_SPACE_ID, | ||
| api_key=ARIZE_API_KEY, | ||
| project_name=project_name, | ||
| ) | ||
| else: | ||
| return register_phoenix(project_name="default") | ||
|
|
||
|
|
||
| tracer_provider = setup_tracer_provider() | ||
| PipecatInstrumentor().instrument( | ||
| tracer_provider=tracer_provider, | ||
| debug_log_filename=debug_log_filename, | ||
| ) | ||
|
|
||
| transport_params = { | ||
| "daily": lambda: DailyParams( | ||
| audio_in_enabled=True, | ||
| audio_out_enabled=True, | ||
| vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.2)), | ||
| turn_analyzer=LocalSmartTurnAnalyzerV3(params=SmartTurnParams()), | ||
| ), | ||
| "twilio": lambda: FastAPIWebsocketParams( | ||
| audio_in_enabled=True, | ||
| audio_out_enabled=True, | ||
| vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.2)), | ||
| turn_analyzer=LocalSmartTurnAnalyzerV3(params=SmartTurnParams()), | ||
| ), | ||
| "webrtc": lambda: TransportParams( | ||
| audio_in_enabled=True, | ||
| audio_out_enabled=True, | ||
| vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.2)), | ||
| turn_analyzer=LocalSmartTurnAnalyzerV3(params=SmartTurnParams()), | ||
| ), | ||
| } | ||
|
|
||
|
|
||
| async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): | ||
| logger.info("Starting bot") | ||
|
|
||
| ### STT ### | ||
| stt = OpenAISTTService( | ||
| api_key=os.getenv("OPENAI_API_KEY"), | ||
| model="gpt-4o-transcribe", | ||
| prompt="Expect normal helpful conversation.", | ||
| ) | ||
| ### alternative stt - cartesia ### | ||
| # stt = CartesiaSTTService(api_key=os.getenv("CARTESIA_API_KEY")) | ||
|
|
||
| ### LLM ### | ||
| llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY")) | ||
|
|
||
| ### TTS ### | ||
| tts = OpenAITTSService( | ||
| api_key=os.getenv("OPENAI_API_KEY"), | ||
| voice="ballad", | ||
| params=OpenAITTSService.InputParams( | ||
| instructions="Please speak clearly and at a moderate pace." | ||
| ), | ||
| ) | ||
|
|
||
| messages = [ | ||
| { | ||
| "role": "system", | ||
| "content": "You are a helpful LLM in a WebRTC call. " | ||
| + "Your goal is to demonstrate your capabilities in a succinct way. " | ||
| + "Your output will be converted to audio so don't " | ||
| + "include special characters in your answers. " | ||
| + "Respond to what the user said in a creative and helpful way.", | ||
| } | ||
| ] | ||
|
|
||
| context = LLMContext(messages) | ||
| context_aggregator = LLMContextAggregatorPair(context) | ||
|
|
||
| ### PIPELINE ### | ||
| pipeline = Pipeline( | ||
| [ | ||
| transport.input(), # Transport user input | ||
| stt, | ||
| context_aggregator.user(), # User responses | ||
| llm, # LLM | ||
| tts, # TTS | ||
| transport.output(), # Transport bot output | ||
| context_aggregator.assistant(), # Assistant spoken responses | ||
| ] | ||
| ) | ||
|
|
||
| ### TASK ### | ||
|
|
||
| task = PipelineTask( | ||
| pipeline, | ||
| params=PipelineParams( | ||
| enable_metrics=True, | ||
| enable_usage_metrics=True, | ||
| ), | ||
| conversation_id=conversation_id, # Use dynamic conversation ID for session tracking | ||
| idle_timeout_secs=runner_args.pipeline_idle_timeout_secs, | ||
| ) | ||
|
|
||
| @transport.event_handler("on_client_connected") | ||
| async def on_client_connected(transport, client): | ||
| logger.info("Client connected") | ||
| # Kick off the conversation. | ||
| messages.append({"role": "system", "content": "Please introduce yourself to the user."}) | ||
| await task.queue_frames([LLMRunFrame()]) | ||
|
|
||
| @transport.event_handler("on_client_disconnected") | ||
| async def on_client_disconnected(transport, client): | ||
| logger.info("Client disconnected") | ||
| await task.cancel() | ||
|
|
||
| runner = PipelineRunner(handle_sigint=runner_args.handle_sigint) | ||
|
|
||
| await runner.run(task) | ||
|
|
||
|
|
||
| async def bot(runner_args: RunnerArguments): | ||
| """Main bot entry point compatible with Pipecat Cloud.""" | ||
| transport = await create_transport(runner_args, transport_params) | ||
| await run_bot(transport, runner_args) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| from pipecat.runner.run import main | ||
|
|
||
| main() |
This file contains hidden or 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.
uv pip install -e '.[oiip]'