Skip to content

Commit 9e4bf00

Browse files
authored
Merge pull request #12 from rakeshv247/debug-mode
Introducting LLM debug mode
2 parents 197fe3f + 4914991 commit 9e4bf00

File tree

9 files changed

+97
-3
lines changed

9 files changed

+97
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

7+
## [0.1.0] - 2025-02-13
8+
9+
### Added
10+
11+
- Added Debug mode
12+
- Introduced a new debug field in `StartConversation`.
13+
- When LLM debug mode is enabled, the client will receive debug messages with LLM request and response content
14+
715
## [0.0.9] - 2025-01-24
816

917
### Fixed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.9
1+
0.1.0

speechmatics_flow/cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Interaction,
2525
ConnectionSettings,
2626
PlaybackSettings,
27+
DebugMode,
2728
)
2829
from speechmatics_flow.templates import TemplateOptions
2930

@@ -159,6 +160,10 @@ def get_playback_settings(args):
159160
)
160161

161162

163+
def get_debug_mode_settings(args):
164+
return DebugMode(llm=args.get("llm_debug_enabled"))
165+
166+
162167
# pylint: disable=too-many-arguments,too-many-statements
163168
def add_printing_handlers(
164169
api,
@@ -284,6 +289,7 @@ def run(stream):
284289
audio_settings=get_audio_settings(args),
285290
conversation_config=get_conversation_config(args),
286291
playback_settings=get_playback_settings(args),
292+
debug_mode=get_debug_mode_settings(args),
287293
from_cli=True,
288294
)
289295
except KeyboardInterrupt:

speechmatics_flow/cli_parser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ def get_arg_parser():
153153
choices=[k for k in TemplateOptions.keys()],
154154
help="Choose your assistant.",
155155
)
156+
parser.add_argument(
157+
"--llm-debug-enabled",
158+
default=False,
159+
action="store_true",
160+
help="Flag indicating whether to receive conversations between the LLM and the Flow backend in debug messages.",
161+
)
156162

157163
return parser
158164

speechmatics_flow/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
Interaction,
3131
PlaybackSettings,
3232
ServerMessageType,
33+
DebugMode,
3334
)
3435
from speechmatics_flow.tool_function_param import ToolFunctionParam
3536
from speechmatics_flow.utils import read_in_chunks, json_utf8
@@ -67,6 +68,7 @@ def __init__(
6768
self.audio_settings = None
6869
self.playback_settings = None
6970
self.tools = None
71+
self.debug_mode = None
7072

7173
self.event_handlers = {x: [] for x in ServerMessageType}
7274
self.middlewares = {x: [] for x in ClientMessageType}
@@ -148,6 +150,10 @@ def _start_conversation(self):
148150
}
149151
if self.tools is not None:
150152
msg["tools"] = self.tools
153+
154+
if self.debug_mode:
155+
msg["debug"] = self.debug_mode.asdict()
156+
151157
self.session_running = True
152158
self._call_middleware(ClientMessageType.StartConversation, msg, False)
153159
LOGGER.debug(msg)
@@ -564,6 +570,7 @@ async def run(
564570
from_cli: bool = False,
565571
tools: Optional[List[ToolFunctionParam]] = None,
566572
playback_settings: PlaybackSettings = PlaybackSettings(),
573+
debug_mode: DebugMode = None,
567574
):
568575
"""
569576
Begin a new recognition session.
@@ -586,6 +593,9 @@ async def run(
586593
:param playback_settings: Configuration for the playback stream.
587594
:type playback_settings: models.PlaybackSettings
588595
596+
:param debug_mode: Configuration to receive debug messages from Flow
597+
:type debug_mode: models.DebugMode
598+
589599
:raises Exception: Can raise any exception returned by the
590600
consumer/producer tasks.
591601
@@ -596,6 +606,7 @@ async def run(
596606
self.audio_settings = audio_settings
597607
self.playback_settings = playback_settings
598608
self.tools = tools
609+
self.debug_mode = debug_mode
599610

600611
await self._init_synchronization_primitives()
601612

speechmatics_flow/models.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ def asdict(self):
9191
)
9292

9393

94+
@dataclass
95+
class DebugMode:
96+
"""Defines debug flags for monitoring and troubleshooting Flow"""
97+
98+
llm: bool = False
99+
"""Optional Flag indicating whether to receive conversations between the LLM and the Flow backend as
100+
debug messages."""
101+
102+
def asdict(self):
103+
return asdict(self)
104+
105+
94106
class ClientMessageType(str, Enum):
95107
# pylint: disable=invalid-name
96108
"""Defines various messages sent from client to server."""
@@ -179,7 +191,10 @@ class ServerMessageType(str, Enum):
179191
"""Indicates a generic warning message."""
180192

181193
Error = "Error"
182-
"""Indicates n generic error message."""
194+
"""Indicates a generic error message."""
195+
196+
Debug = "Debug"
197+
"""Indicates a debug message"""
183198

184199

185200
@dataclass

tests/test_cli.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,18 @@ def test_get_conversation_config(args, exp_values):
5252
assert config.asdict() == exp_values, "Expecting {} but got {}".format(
5353
exp_values, config.asdict()
5454
)
55+
56+
57+
@mark.parametrize(
58+
"args, exp_values",
59+
[
60+
param([], {"llm": False}),
61+
param(["--llm-debug-enabled"], {"llm": True}),
62+
],
63+
)
64+
def test_get_debug_mode_settings(args, exp_values):
65+
test_args = vars(cli.parse_args(args=args))
66+
config = cli.get_debug_mode_settings(test_args)
67+
assert (
68+
config.asdict() == exp_values
69+
), f"Expecting {exp_values}, got {config.asdict()}"

tests/test_client.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
from pytest import param
66

7+
from speechmatics_flow import DebugMode
78
from speechmatics_flow.client import WebsocketClient
89
from speechmatics_flow.models import (
910
ServerMessageType,
@@ -33,23 +34,26 @@ def ws_client():
3334

3435

3536
@pytest.mark.parametrize(
36-
"audio_format, conversation_config, tools, expected_start_message",
37+
"audio_format, conversation_config, tools, debug_mode, expected_start_message",
3738
[
3839
param(
3940
AudioSettings(),
4041
ConversationConfig(),
4142
None,
43+
DebugMode(),
4244
{
4345
"message": ClientMessageType.StartConversation.value,
4446
"audio_format": AudioSettings().asdict(),
4547
"conversation_config": ConversationConfig().asdict(),
48+
"debug": DebugMode().asdict(),
4649
},
4750
id="with default values",
4851
),
4952
param(
5053
AudioSettings(),
5154
ConversationConfig(),
5255
[TOOL_FUNCTION],
56+
None,
5357
{
5458
"message": ClientMessageType.StartConversation.value,
5559
"audio_format": AudioSettings().asdict(),
@@ -58,13 +62,27 @@ def ws_client():
5862
},
5963
id="with default values and tools",
6064
),
65+
param(
66+
AudioSettings(),
67+
ConversationConfig(),
68+
None,
69+
DebugMode(llm=True),
70+
{
71+
"message": ClientMessageType.StartConversation.value,
72+
"audio_format": AudioSettings().asdict(),
73+
"conversation_config": ConversationConfig().asdict(),
74+
"debug": DebugMode(llm=True).asdict(),
75+
},
76+
id="with default values and llm debug mode enabled",
77+
),
6178
],
6279
)
6380
def test_start_conversation(
6481
ws_client: WebsocketClient,
6582
audio_format: AudioSettings,
6683
conversation_config: ConversationConfig,
6784
tools: Optional[List[ToolFunctionParam]],
85+
debug_mode: Optional[DebugMode],
6886
expected_start_message: Dict,
6987
):
7088
handler_called = False
@@ -77,6 +95,7 @@ def handler(*_):
7795
ws_client.audio_settings = audio_format
7896
ws_client.conversation_config = conversation_config
7997
ws_client.tools = tools
98+
ws_client.debug_mode = debug_mode
8099
start_conversation_msg = ws_client._start_conversation()
81100
assert start_conversation_msg == json.dumps(
82101
expected_start_message

tests/test_models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,17 @@ def test_playback_settings(config, want):
5757
audio_settings = models.PlaybackSettings(**config)
5858
got = asdict(audio_settings)
5959
assert got == want
60+
61+
62+
@pytest.mark.parametrize(
63+
"config, want",
64+
[
65+
({}, {"llm": False}),
66+
({"llm": False}, {"llm": False}),
67+
({"llm": True}, {"llm": True}),
68+
],
69+
)
70+
def test_debug_mode(config, want):
71+
debug_mode_settings = models.DebugMode(**config)
72+
got = debug_mode_settings.asdict()
73+
assert got == want

0 commit comments

Comments
 (0)