agentscope-ai / agentscope-ai/agentscope
[Bug]: react agent中的usage没有获取
- Langage dominant
- Python
- Étoiles
- 31.5k
- Forks
- 3.5k
- Merge moyen
- 1 j 23 h
- PR mergées (30 j)
- 95
Description
async def _reasoning(
self,
tool_choice: Literal["auto", "none", "required"] | None = None,
) -> Msg:
"""Perform the reasoning process."""
if self.plan_notebook:
# Insert the reasoning hint from the plan notebook
hint_msg = await self.plan_notebook.get_current_hint()
if self.print_hint_msg and hint_msg:
await self.print(hint_msg)
await self.memory.add(hint_msg, marks=_MemoryMark.HINT)
# Convert Msg objects into the required format of the model API
prompt = await self.formatter.format(
msgs=[
Msg("system", self.sys_prompt, "system"),
*await self.memory.get_memory(),
],
)
# Clear the hint messages after use
await self.memory.delete_by_mark(mark=_MemoryMark.HINT)
res = await self.model(
prompt,
tools=self.toolkit.get_json_schemas(),
tool_choice=tool_choice,
)
# handle output from the model
interrupted_by_user = False
msg = None
# TTS model context manager
tts_context = self.tts_model or _AsyncNullContext()
speech: AudioBlock | list[AudioBlock] | None = None
try:
async with tts_context:
msg = Msg(name=self.name, content=[], role="assistant")
if self.model.stream:
async for content_chunk in res:
msg.content = content_chunk.content
# The speech generated from multimodal (audio) models
# e.g. Qwen-Omni and GPT-AUDIO
speech = msg.get_content_blocks("audio") or None
# Push to TTS model if available
if (
self.tts_model
and self.tts_model.supports_streaming_input
):
tts_res = await self.tts_model.push(msg)
speech = tts_res.content
await self.print(msg, False, speech=speech)
else:
msg.content = list(res.content)
if self.tts_model:
# Push to TTS model and block to receive the full speech
# synthesis result
tts_res = await self.tts_model.synthesize(msg)
if self.tts_model.stream:
async for tts_chunk in tts_res:
speech = tts_chunk.content
await self.print(msg, False, speech=speech)
else:
speech = tts_res.content
await self.print(msg, True, speech=speech)
# Add a tiny sleep to yield the last message object in the
# message queue
await asyncio.sleep(0.001)
except asyncio.CancelledError as e:
interrupted_by_user = True
raise e from None
finally:
# None will be ignored by the memory
await self.memory.add(msg)
# Post-process for user interruption
if interrupted_by_user and msg:
# Fake tool results
tool_use_blocks: list = msg.get_content_blocks(
"tool_use",
)
for tool_call in tool_use_blocks:
msg_res = Msg(
"system",
[
ToolResultBlock(
type="tool_result",
id=tool_call["id"],
name=tool_call["name"],
output="The tool call has been interrupted "
"by the user.",
),
],
"system",
)
await self.memory.add(msg_res)
await self.print(msg_res, True)
return msg
Guide de contribution
Ouvrir le guide de contribution
Évaluation
Cette issue n'a pas encore été évaluée.