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
8 changes: 8 additions & 0 deletions src/agentscope/model/_dashscope_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ async def _parse_dashscope_stream_response(
input_tokens=chunk.usage.input_tokens,
output_tokens=chunk.usage.output_tokens,
time=(datetime.now() - start_datetime).total_seconds(),
prompt_tokens_details=chunk.usage.get(
"prompt_tokens_details",
None,
),
)

parsed_chunk = ChatResponse(
Expand Down Expand Up @@ -469,6 +473,10 @@ async def _parse_dashscope_generation_response(
input_tokens=response.usage.input_tokens,
output_tokens=response.usage.output_tokens,
time=(datetime.now() - start_datetime).total_seconds(),
prompt_tokens_details=response.usage.get(
"prompt_tokens_details",
None,
),
)

parsed_response = ChatResponse(
Expand Down
3 changes: 3 additions & 0 deletions src/agentscope/model/_model_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ class ChatUsage(DictMixin):
time: float
"""The time used in seconds."""

prompt_tokens_details: dict = field(default_factory=dict)
"""The details of cached tokens. """

type: Literal["chat"] = field(default_factory=lambda: "chat")
"""The type of the usage, must be `chat`."""
3 changes: 3 additions & 0 deletions src/agentscope/tracing/_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class SpanAttributes:
GEN_AI_USAGE_OUTPUT_TOKENS = GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS
"""The gen ai usage output tokens."""

GEN_AI_USAGE_CACHED_TOKENS = "gen_ai.dash.usage.cached_tokens"
"""The gen ai usage cached tokens."""

# GenAI Message Attributes
GEN_AI_INPUT_MESSAGES = GenAIAttributes.GEN_AI_INPUT_MESSAGES
"""The gen ai input messages."""
Expand Down
7 changes: 7 additions & 0 deletions src/agentscope/tracing/_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ def _get_llm_response_attributes(
attributes[
SpanAttributes.GEN_AI_USAGE_OUTPUT_TOKENS
] = chat_response.usage.output_tokens
if (
hasattr(chat_response.usage, "prompt_tokens_details")
and chat_response.usage.prompt_tokens_details
):
attributes[
SpanAttributes.GEN_AI_USAGE_CACHED_TOKENS
] = _serialize_to_str(chat_response.usage.prompt_tokens_details)

output_messages = _get_llm_output_messages(chat_response)
if output_messages:
Expand Down
Loading