Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions dashscope/api_entities/websocket_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,27 +335,34 @@ async def _send_finished_task(self, ws):
await ws.send_str(message)

async def _send_continue_task_data(self, ws):
headers = {
"task_id": self.task_headers["task_id"],
"action": "continue-task",
}
headers = {**self.task_headers, ACTION_KEY: ActionType.CONTINUE}
for input_item in self.data.get_websocket_continue_data():
if self.is_binary_input:
if len(input_item) > 0:
if isinstance(input_item, bytes):
await ws.send_bytes(input_item)
if len(input_item) > 0:
if self.is_binary_input and isinstance(
input_item,
(bytes, bytearray, memoryview),
):
await ws.send_bytes(input_item)
logger.debug(
"Send continue task with bytes: %s",
len(input_item),
)
elif self.is_binary_input and isinstance(input_item, dict):
binary_data = next(iter(input_item.values()))
if isinstance(binary_data, (bytes, bytearray, memoryview)):
await ws.send_bytes(binary_data)
logger.debug(
"Send continue task with bytes: %s",
"Send continue task with list[byte]: %s",
len(input_item),
)
else:
await ws.send_bytes(list(input_item.values())[0])
logger.debug(
"Send continue task with list[byte]: %s",
len(input_item),
message = self._build_up_message(
headers=headers,
payload=input_item,
)
else:
if len(input_item) > 0:
logger.debug("Send continue task: %s", message)
await ws.send_str(message)
else:
message = self._build_up_message(
headers=headers,
payload=input_item,
Expand Down
25 changes: 23 additions & 2 deletions dashscope/audio/asr/recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,27 @@ def send_audio_frame(self, buffer: bytes):
logger.debug("send_audio_frame: %s", len(buffer))
self._stream_data.put(buffer)

def update_context(self, payload_input: dict):
"""Update recognition context while the task is running.

The context is sent through a ``continue-task`` event and takes effect
on subsequent audio frames.

Args:
payload_input (dict): Conversation context messages.

Raises:
InvalidParameter: Cannot update an uninitiated recognition, or the
context is None.
"""
if self._running is False:
raise InvalidParameter("Speech recognition has stopped.")
if payload_input is None:
raise InvalidParameter("Context is required.")

logger.debug("update_context: %s", payload_input)
self._stream_data.put({"input": payload_input})

def _tidy_kwargs(self):
for k in self._kwargs.copy():
if self._kwargs[k] is None:
Expand Down Expand Up @@ -656,7 +677,7 @@ def _input_stream_cycle(self):

while not self._stream_data.empty():
frame = self._stream_data.get()
yield bytes(frame)
yield frame if isinstance(frame, dict) else bytes(frame)

if self._recognition_once:
self._running = False
Expand All @@ -665,7 +686,7 @@ def _input_stream_cycle(self):
if self._recognition_once is False:
while not self._stream_data.empty():
frame = self._stream_data.get()
yield bytes(frame)
yield frame if isinstance(frame, dict) else bytes(frame)

def _silence_stop_timer(self):
"""If audio data is not received for a long time, exit worker."""
Expand Down
14 changes: 14 additions & 0 deletions dashscope/audio/qwen_omni/omni_realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ def update_session(
transcription_params: TranscriptionParams = None,
input_audio_config: AudioFormatConfig = None,
output_audio_config: AudioFormatConfig = None,
input_video_representation_compactness: int = None,
**kwargs,
) -> None:
"""
Expand Down Expand Up @@ -459,6 +460,11 @@ def update_session(
type (pcm/wav) and sample rate (8000/16000/24000/48000), as well
as free extension parameters via ``extra_params``. When provided,
the request emits the ``session.audio.output.format`` structure.
input_video_representation_compactness: int
input video representation compactness. When provided, the request
emits ``session.video.input.representation_compactness``. No strict
client-side validation is performed so newly supported values can
be used without upgrading the SDK.

Notes
-----
Expand Down Expand Up @@ -509,6 +515,14 @@ def update_session(
}
if transcription_params is not None:
self._apply_transcription_params(transcription_params)
if input_video_representation_compactness is not None:
self.config["video"] = {
"input": {
"representation_compactness": (
input_video_representation_compactness
),
},
}
self.config.update(kwargs)
self.__send_str(
json.dumps(
Expand Down
Loading