Skip to content

Commit 92a3d55

Browse files
committed
pass through the model's response ID
1 parent f5e4d38 commit 92a3d55

6 files changed

Lines changed: 142 additions & 74 deletions

File tree

src/agentscope/agent/_react_agent.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ async def _reasoning(
584584
msg = Msg(name=self.name, content=[], role="assistant")
585585
if self.model.stream:
586586
async for content_chunk in res:
587+
msg.invocation_id = content_chunk.id
587588
msg.content = content_chunk.content
588589

589590
# The speech generated from multimodal (audio) models
@@ -601,6 +602,7 @@ async def _reasoning(
601602
await self.print(msg, False, speech=speech)
602603

603604
else:
605+
msg.invocation_id = res.id
604606
msg.content = list(res.content)
605607

606608
if self.tts_model:
@@ -757,6 +759,7 @@ async def _summarizing(self) -> Msg:
757759
res_msg = Msg(self.name, [], "assistant")
758760
if isinstance(res, AsyncGenerator):
759761
async for chunk in res:
762+
res_msg.invocation_id = chunk.id
760763
res_msg.content = chunk.content
761764

762765
# The speech generated from multimodal (audio) models
@@ -774,6 +777,7 @@ async def _summarizing(self) -> Msg:
774777
await self.print(res_msg, False, speech=speech)
775778

776779
else:
780+
res_msg.invocation_id = res.id
777781
res_msg.content = res.content
778782

779783
if self.tts_model:

src/agentscope/model/_anthropic_model.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,16 @@ async def _parse_anthropic_completion_response(
348348
time=(datetime.now() - start_datetime).total_seconds(),
349349
)
350350

351-
parsed_response = ChatResponse(
352-
content=content_blocks,
353-
usage=usage,
354-
metadata=metadata,
355-
)
351+
resp_kwargs: dict[str, Any] = {
352+
"content": content_blocks,
353+
"usage": usage,
354+
"metadata": metadata,
355+
}
356+
response_id = getattr(response, "id", None)
357+
if response_id:
358+
resp_kwargs["id"] = response_id
356359

357-
return parsed_response
360+
return ChatResponse(**resp_kwargs)
358361

359362
async def _parse_anthropic_stream_completion_response(
360363
self,
@@ -386,6 +389,7 @@ async def _parse_anthropic_stream_completion_response(
386389
"""
387390

388391
usage = None
392+
response_id: str | None = None
389393
text_buffer = ""
390394
thinking_buffer = ""
391395
thinking_signature = ""
@@ -404,6 +408,8 @@ async def _parse_anthropic_stream_completion_response(
404408

405409
if event.type == "message_start":
406410
message = event.message
411+
if response_id is None:
412+
response_id = getattr(message, "id", None)
407413
if message.usage:
408414
usage = ChatUsage(
409415
input_tokens=message.usage.input_tokens,
@@ -504,11 +510,14 @@ async def _parse_anthropic_stream_completion_response(
504510
metadata = repaired_input
505511

506512
if contents:
507-
res = ChatResponse(
508-
content=contents,
509-
usage=usage,
510-
metadata=metadata,
511-
)
513+
_kwargs: dict[str, Any] = {
514+
"content": contents,
515+
"usage": usage,
516+
"metadata": metadata,
517+
}
518+
if response_id:
519+
_kwargs["id"] = response_id
520+
res = ChatResponse(**_kwargs)
512521
yield res
513522
last_content = copy.deepcopy(contents)
514523

@@ -525,11 +534,14 @@ async def _parse_anthropic_stream_completion_response(
525534
if structured_model:
526535
metadata = input_obj
527536

528-
yield ChatResponse(
529-
content=last_content,
530-
usage=usage,
531-
metadata=metadata,
532-
)
537+
_final_kwargs: dict[str, Any] = {
538+
"content": last_content,
539+
"usage": usage,
540+
"metadata": metadata,
541+
}
542+
if response_id:
543+
_final_kwargs["id"] = response_id
544+
yield ChatResponse(**_final_kwargs)
533545

534546
def _format_tools_json_schemas(
535547
self,

src/agentscope/model/_dashscope_model.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,17 @@ async def _parse_dashscope_stream_response(
340340
metadata = None
341341
last_content = None
342342
usage = None
343+
response_id: str | None = None
343344

344345
async for chunk in giter(response):
345346
if chunk.status_code != HTTPStatus.OK:
346347
raise RuntimeError(
347348
f"Failed to get response from _ API: {chunk}",
348349
)
349350

351+
if response_id is None:
352+
response_id = getattr(chunk, "request_id", None)
353+
350354
message = chunk.output.choices[0].message
351355

352356
# Update reasoning content
@@ -451,11 +455,14 @@ async def _parse_dashscope_stream_response(
451455
)
452456

453457
if content_blocks:
454-
parsed_chunk = ChatResponse(
455-
content=content_blocks,
456-
usage=usage,
457-
metadata=metadata,
458-
)
458+
_kwargs: dict[str, Any] = {
459+
"content": content_blocks,
460+
"usage": usage,
461+
"metadata": metadata,
462+
}
463+
if response_id:
464+
_kwargs["id"] = response_id
465+
parsed_chunk = ChatResponse(**_kwargs)
459466
yield parsed_chunk
460467
last_content = copy.deepcopy(content_blocks)
461468

@@ -473,11 +480,14 @@ async def _parse_dashscope_stream_response(
473480
if structured_model:
474481
metadata = input_obj
475482

476-
yield ChatResponse(
477-
content=last_content,
478-
usage=usage,
479-
metadata=metadata,
480-
)
483+
_final_kwargs: dict[str, Any] = {
484+
"content": last_content,
485+
"usage": usage,
486+
"metadata": metadata,
487+
}
488+
if response_id:
489+
_final_kwargs["id"] = response_id
490+
yield ChatResponse(**_final_kwargs)
481491

482492
async def _parse_dashscope_generation_response(
483493
self,
@@ -574,13 +584,16 @@ async def _parse_dashscope_generation_response(
574584
metadata=response.usage,
575585
)
576586

577-
parsed_response = ChatResponse(
578-
content=content_blocks,
579-
usage=usage,
580-
metadata=metadata,
581-
)
587+
resp_kwargs: dict[str, Any] = {
588+
"content": content_blocks,
589+
"usage": usage,
590+
"metadata": metadata,
591+
}
592+
response_id = getattr(response, "request_id", None)
593+
if response_id:
594+
resp_kwargs["id"] = response_id
582595

583-
return parsed_response
596+
return ChatResponse(**resp_kwargs)
584597

585598
def _format_tools_json_schemas(
586599
self,

src/agentscope/model/_gemini_model.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ def _extract_usage(
333333
)
334334
return None
335335

336+
# pylint: disable=too-many-branches
336337
async def _parse_gemini_stream_generation_response(
337338
self,
338339
start_datetime: datetime,
@@ -366,6 +367,7 @@ async def _parse_gemini_stream_generation_response(
366367
thinking = ""
367368
tool_calls: list[ToolUseBlock] = []
368369
metadata: dict | None = None
370+
response_id: str | None = None
369371
async for chunk in response:
370372
if (
371373
chunk.candidates
@@ -434,11 +436,17 @@ async def _parse_gemini_stream_generation_response(
434436
),
435437
)
436438

437-
yield ChatResponse(
438-
content=content_blocks + tool_calls,
439-
usage=usage,
440-
metadata=metadata,
441-
)
439+
if response_id is None:
440+
response_id = getattr(chunk, "response_id", None)
441+
442+
_kwargs: dict[str, Any] = {
443+
"content": content_blocks + tool_calls,
444+
"usage": usage,
445+
"metadata": metadata,
446+
}
447+
if response_id:
448+
_kwargs["id"] = response_id
449+
yield ChatResponse(**_kwargs)
442450

443451
def _parse_gemini_generation_response(
444452
self,
@@ -527,11 +535,16 @@ def _parse_gemini_generation_response(
527535

528536
usage = self._extract_usage(response.usage_metadata, start_datetime)
529537

530-
return ChatResponse(
531-
content=content_blocks + tool_calls,
532-
usage=usage,
533-
metadata=metadata,
534-
)
538+
resp_kwargs: dict[str, Any] = {
539+
"content": content_blocks + tool_calls,
540+
"usage": usage,
541+
"metadata": metadata,
542+
}
543+
response_id = getattr(response, "response_id", None)
544+
if response_id:
545+
resp_kwargs["id"] = response_id
546+
547+
return ChatResponse(**resp_kwargs)
535548

536549
def _format_tools_json_schemas(
537550
self,

src/agentscope/model/_ollama_model.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ async def _parse_ollama_stream_completion_response(
204204
acc_thinking_content = ""
205205
tool_calls = OrderedDict() # Store tool calls
206206
metadata: dict | None = None
207+
response_id: str | None = None
207208

208209
async for chunk in response:
209210
# Handle text content
@@ -262,13 +263,19 @@ async def _parse_ollama_stream_completion_response(
262263
except Exception as e:
263264
print(f"Error parsing tool call input: {e}")
264265

266+
if response_id is None:
267+
response_id = getattr(chunk, "id", None)
268+
265269
# Generate response when there's new content or at final chunk
266270
if chunk.done or contents:
267-
res = ChatResponse(
268-
content=contents,
269-
usage=usage,
270-
metadata=metadata,
271-
)
271+
_kwargs: dict[str, Any] = {
272+
"content": contents,
273+
"usage": usage,
274+
"metadata": metadata,
275+
}
276+
if response_id:
277+
_kwargs["id"] = response_id
278+
res = ChatResponse(**_kwargs)
272279
yield res
273280

274281
async def _parse_ollama_completion_response(
@@ -339,13 +346,16 @@ async def _parse_ollama_completion_response(
339346
time=(datetime.now() - start_datetime).total_seconds(),
340347
)
341348

342-
parsed_response = ChatResponse(
343-
content=content_blocks,
344-
usage=usage,
345-
metadata=metadata,
346-
)
349+
resp_kwargs: dict[str, Any] = {
350+
"content": content_blocks,
351+
"usage": usage,
352+
"metadata": metadata,
353+
}
354+
response_id = getattr(response, "id", None)
355+
if response_id:
356+
resp_kwargs["id"] = response_id
347357

348-
return parsed_response
358+
return ChatResponse(**resp_kwargs)
349359

350360
def _format_tools_json_schemas(
351361
self,

0 commit comments

Comments
 (0)