-
Notifications
You must be signed in to change notification settings - Fork 530
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
docs: add SIP transfer via LLM tool call example #1072
Draft
jamsea
wants to merge
3
commits into
main
Choose a base branch
from
hush/callTransfer
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.
Draft
Changes from all commits
Commits
Show all changes
3 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 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
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 |
---|---|---|
@@ -1,17 +1,25 @@ | ||
# Copyright (c) 2024–2025, Daily | ||
# | ||
# SPDX-License-Identifier: BSD 2-Clause License | ||
# | ||
|
||
import argparse | ||
import asyncio | ||
import os | ||
import sys | ||
|
||
from dotenv import load_dotenv | ||
from loguru import logger | ||
from openai.types.chat import ChatCompletionToolParam | ||
|
||
from pipecat.audio.vad.silero import SileroVADAnalyzer | ||
from pipecat.frames.frames import EndFrame | ||
from pipecat.frames.frames import EndFrame, TextFrame | ||
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.services.ai_services import LLMService | ||
from pipecat.services.deepgram import DeepgramSTTService | ||
from pipecat.services.elevenlabs import ElevenLabsTTSService | ||
from pipecat.services.openai import OpenAILLMService | ||
from pipecat.transports.services.daily import DailyDialinSettings, DailyParams, DailyTransport | ||
|
@@ -55,16 +63,62 @@ async def main(room_url: str, token: str, callId: str, callDomain: str): | |
|
||
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o") | ||
|
||
content = f""" | ||
You are a delivery service customer support specialist supporting customers with their orders. | ||
Begin with: "Hello, this is Hailey from customer support. What can I help you with today?" | ||
""" | ||
|
||
messages = [ | ||
{ | ||
"role": "system", | ||
"content": "You are Chatbot, a friendly, helpful robot. 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, but keep your responses brief. Start by saying 'Oh, hello! Who dares dial me at this hour?!'.", | ||
"content": content, | ||
}, | ||
] | ||
|
||
context = OpenAILLMContext(messages) | ||
tools = [ | ||
ChatCompletionToolParam( | ||
type="function", | ||
function={ | ||
"name": "transfer_call", | ||
"description": "Transfer the call to a person. This function is used to connect the call to a real person. Examples of real people are: managers, supervisors, or other customer support specialists. Any person is okay as long as they are not a bot.", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"call_id": { | ||
"type": "string", | ||
"description": "This is always {callId}.", | ||
}, | ||
"summary": { | ||
"type": "string", | ||
"description": """ | ||
Provide a concise summary in 3-5 sentences. Highlight any important details or unusual aspects of the conversation. | ||
""", | ||
}, | ||
}, | ||
}, | ||
}, | ||
) | ||
] | ||
|
||
context = OpenAILLMContext(messages, tools) | ||
context_aggregator = llm.create_context_aggregator(context) | ||
|
||
async def default_transfer_call( | ||
function_name, tool_call_id, args, llm: LLMService, context, result_callback | ||
): | ||
logger.debug(f"default_transfer_call: {function_name} {tool_call_id} {args}") | ||
await result_callback( | ||
{ | ||
"transfer_call": False, | ||
"reason": "To transfer call calls, please dial in to the room using a phone or a SIP client.", | ||
} | ||
) | ||
|
||
llm.register_function( | ||
function_name="transfer_call", | ||
callback=default_transfer_call, | ||
) | ||
|
||
pipeline = Pipeline( | ||
[ | ||
transport.input(), | ||
|
@@ -87,6 +141,44 @@ async def on_first_participant_joined(transport, participant): | |
async def on_participant_left(transport, participant, reason): | ||
await task.queue_frame(EndFrame()) | ||
|
||
@transport.event_handler("on_dialin_ready") | ||
async def on_dialin_ready(_, sip_endpoint): | ||
logger.info(f"on_dialin_ready: {sip_endpoint}") | ||
|
||
@transport.event_handler("on_dialin_connected") | ||
async def on_dialin_connected(transport, event): | ||
logger.info(f"on_dialin_connected: {event}") | ||
sip_session_id = event["sessionId"] | ||
|
||
async def transfer_call( | ||
function_name, tool_call_id, args, llm: LLMService, context, result_callback | ||
): | ||
logger.debug(f"transfer_call: {function_name} {tool_call_id} {args}") | ||
|
||
# sip_url = "sip:[email protected]" | ||
|
||
sip_url = ( | ||
f"sip:[email protected]?x-daily_id={room_url.split('/')[-1]}" | ||
) | ||
|
||
try: | ||
await transport.sip_refer( | ||
settings={ | ||
"sessionId": sip_session_id, | ||
"toEndPoint": sip_url, | ||
} | ||
) | ||
except Exception as e: | ||
logger.error(f"An error occurred during SIP refer: {e}") | ||
await result_callback({"transfer_call": False}) | ||
|
||
await result_callback({"transfer_call": True}) | ||
|
||
llm.register_function( | ||
function_name="transfer_call", | ||
callback=transfer_call, | ||
) | ||
|
||
runner = PipelineRunner() | ||
|
||
await runner.run(task) | ||
|
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.
this sip uri needs to come from the developer. It will be different.