|
7 | 7 |
|
8 | 8 | import os |
9 | 9 |
|
| 10 | +import aiohttp |
10 | 11 | from dotenv import load_dotenv |
11 | 12 | from loguru import logger |
12 | 13 |
|
|
24 | 25 | ) |
25 | 26 | from pipecat.runner.types import RunnerArguments |
26 | 27 | from pipecat.runner.utils import create_transport |
27 | | -from pipecat.services.cartesia.tts import CartesiaTTSService |
28 | 28 | from pipecat.services.deepgram.stt import DeepgramSTTService |
29 | 29 | from pipecat.services.grok.llm import GrokLLMService |
30 | 30 | from pipecat.services.llm_service import FunctionCallParams |
| 31 | +from pipecat.services.xai.tts import XAIHttpTTSService |
31 | 32 | from pipecat.transports.base_transport import BaseTransport, TransportParams |
32 | 33 | from pipecat.transports.daily.transport import DailyParams |
33 | 34 | from pipecat.transports.websocket.fastapi import FastAPIWebsocketParams |
@@ -60,83 +61,88 @@ async def fetch_weather_from_api(params: FunctionCallParams): |
60 | 61 | async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): |
61 | 62 | logger.info(f"Starting bot") |
62 | 63 |
|
63 | | - stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY")) |
64 | | - |
65 | | - tts = CartesiaTTSService( |
66 | | - api_key=os.getenv("CARTESIA_API_KEY"), |
67 | | - settings=CartesiaTTSService.Settings( |
68 | | - voice="71a7ad14-091c-4e8e-a314-022ece01c121", # British Reading Lady |
69 | | - ), |
70 | | - ) |
71 | | - |
72 | | - llm = GrokLLMService( |
73 | | - api_key=os.getenv("GROK_API_KEY"), |
74 | | - settings=GrokLLMService.Settings( |
75 | | - system_instruction="You are a helpful assistant in a voice conversation. Your responses will be spoken aloud, so avoid emojis, bullet points, or other formatting that can't be spoken. Respond to what the user said in a creative, helpful, and brief way.", |
76 | | - ), |
77 | | - ) |
78 | | - # You can also register a function_name of None to get all functions |
79 | | - # sent to the same callback with an additional function_name parameter. |
80 | | - llm.register_function("get_current_weather", fetch_weather_from_api) |
81 | | - |
82 | | - weather_function = FunctionSchema( |
83 | | - name="get_current_weather", |
84 | | - description="Get the current weather", |
85 | | - properties={ |
86 | | - "location": { |
87 | | - "type": "string", |
88 | | - "description": "The city and state, e.g. San Francisco, CA", |
| 64 | + async with aiohttp.ClientSession() as session: |
| 65 | + stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY")) |
| 66 | + |
| 67 | + tts = XAIHttpTTSService( |
| 68 | + api_key=os.getenv("GROK_API_KEY"), |
| 69 | + aiohttp_session=session, |
| 70 | + settings=XAIHttpTTSService.Settings( |
| 71 | + voice="eve", |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + llm = GrokLLMService( |
| 76 | + api_key=os.getenv("GROK_API_KEY"), |
| 77 | + settings=GrokLLMService.Settings( |
| 78 | + system_instruction="You are a helpful assistant in a voice conversation. Your responses will be spoken aloud, so avoid emojis, bullet points, or other formatting that can't be spoken. Respond to what the user said in a creative, helpful, and brief way.", |
| 79 | + ), |
| 80 | + ) |
| 81 | + # You can also register a function_name of None to get all functions |
| 82 | + # sent to the same callback with an additional function_name parameter. |
| 83 | + llm.register_function("get_current_weather", fetch_weather_from_api) |
| 84 | + |
| 85 | + weather_function = FunctionSchema( |
| 86 | + name="get_current_weather", |
| 87 | + description="Get the current weather", |
| 88 | + properties={ |
| 89 | + "location": { |
| 90 | + "type": "string", |
| 91 | + "description": "The city and state, e.g. San Francisco, CA", |
| 92 | + }, |
| 93 | + "format": { |
| 94 | + "type": "string", |
| 95 | + "enum": ["celsius", "fahrenheit"], |
| 96 | + "description": "The temperature unit to use. Infer this from the user's location.", |
| 97 | + }, |
89 | 98 | }, |
90 | | - "format": { |
91 | | - "type": "string", |
92 | | - "enum": ["celsius", "fahrenheit"], |
93 | | - "description": "The temperature unit to use. Infer this from the user's location.", |
94 | | - }, |
95 | | - }, |
96 | | - required=["location", "format"], |
97 | | - ) |
98 | | - tools = ToolsSchema(standard_tools=[weather_function]) |
99 | | - context = LLMContext(tools=tools) |
100 | | - user_aggregator, assistant_aggregator = LLMContextAggregatorPair( |
101 | | - context, |
102 | | - user_params=LLMUserAggregatorParams(vad_analyzer=SileroVADAnalyzer()), |
103 | | - ) |
104 | | - |
105 | | - pipeline = Pipeline( |
106 | | - [ |
107 | | - transport.input(), |
108 | | - stt, |
109 | | - user_aggregator, |
110 | | - llm, |
111 | | - tts, |
112 | | - transport.output(), |
113 | | - assistant_aggregator, |
114 | | - ] |
115 | | - ) |
116 | | - |
117 | | - task = PipelineTask( |
118 | | - pipeline, |
119 | | - params=PipelineParams( |
120 | | - enable_metrics=True, |
121 | | - enable_usage_metrics=True, |
122 | | - ), |
123 | | - idle_timeout_secs=runner_args.pipeline_idle_timeout_secs, |
124 | | - ) |
125 | | - |
126 | | - @transport.event_handler("on_client_connected") |
127 | | - async def on_client_connected(transport, client): |
128 | | - logger.info(f"Client connected") |
129 | | - # Kick off the conversation. |
130 | | - await task.queue_frames([LLMRunFrame()]) |
131 | | - |
132 | | - @transport.event_handler("on_client_disconnected") |
133 | | - async def on_client_disconnected(transport, client): |
134 | | - logger.info(f"Client disconnected") |
135 | | - await task.cancel() |
136 | | - |
137 | | - runner = PipelineRunner(handle_sigint=runner_args.handle_sigint) |
138 | | - |
139 | | - await runner.run(task) |
| 99 | + required=["location", "format"], |
| 100 | + ) |
| 101 | + tools = ToolsSchema(standard_tools=[weather_function]) |
| 102 | + context = LLMContext(tools=tools) |
| 103 | + user_aggregator, assistant_aggregator = LLMContextAggregatorPair( |
| 104 | + context, |
| 105 | + user_params=LLMUserAggregatorParams(vad_analyzer=SileroVADAnalyzer()), |
| 106 | + ) |
| 107 | + |
| 108 | + pipeline = Pipeline( |
| 109 | + [ |
| 110 | + transport.input(), |
| 111 | + stt, |
| 112 | + user_aggregator, |
| 113 | + llm, |
| 114 | + tts, |
| 115 | + transport.output(), |
| 116 | + assistant_aggregator, |
| 117 | + ] |
| 118 | + ) |
| 119 | + |
| 120 | + task = PipelineTask( |
| 121 | + pipeline, |
| 122 | + params=PipelineParams( |
| 123 | + enable_metrics=True, |
| 124 | + enable_usage_metrics=True, |
| 125 | + ), |
| 126 | + idle_timeout_secs=runner_args.pipeline_idle_timeout_secs, |
| 127 | + ) |
| 128 | + |
| 129 | + @transport.event_handler("on_client_connected") |
| 130 | + async def on_client_connected(transport, client): |
| 131 | + logger.info(f"Client connected") |
| 132 | + # Kick off the conversation. |
| 133 | + context.add_message( |
| 134 | + {"role": "user", "content": "Please introduce yourself to the user."} |
| 135 | + ) |
| 136 | + await task.queue_frames([LLMRunFrame()]) |
| 137 | + |
| 138 | + @transport.event_handler("on_client_disconnected") |
| 139 | + async def on_client_disconnected(transport, client): |
| 140 | + logger.info(f"Client disconnected") |
| 141 | + await task.cancel() |
| 142 | + |
| 143 | + runner = PipelineRunner(handle_sigint=runner_args.handle_sigint) |
| 144 | + |
| 145 | + await runner.run(task) |
140 | 146 |
|
141 | 147 |
|
142 | 148 | async def bot(runner_args: RunnerArguments): |
|
0 commit comments