Skip to content

Implement time tracking for image caption requests#8711

Open
tjc6666666666666 wants to merge 11 commits into
AstrBotDevs:masterfrom
tjc6666666666666:patch-13
Open

Implement time tracking for image caption requests#8711
tjc6666666666666 wants to merge 11 commits into
AstrBotDevs:masterfrom
tjc6666666666666:patch-13

Conversation

@tjc6666666666666

@tjc6666666666666 tjc6666666666666 commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Added time tracking for image caption provider usage and logging.

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Track and record provider usage and timing for image caption generation in group chats.

New Features:

  • Add time-based usage tracking for image caption provider calls, including token usage and status logging for group chat images.

Enhancements:

  • Propagate the message event into image caption requests to support storing unified message origin metadata with provider statistics.

Added time tracking for image caption provider usage and logging.
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jun 10, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues, and left some high level feedback:

  • For time tracking, consider using time.monotonic() instead of time.time() so elapsed durations are not affected by system clock changes.
  • In the provider stats block you call provider.get_model() twice; storing the result once and reusing it would simplify the logic and avoid redundant calls.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- For time tracking, consider using `time.monotonic()` instead of `time.time()` so elapsed durations are not affected by system clock changes.
- In the provider stats block you call `provider.get_model()` twice; storing the result once and reusing it would simplify the logic and avoid redundant calls.

## Individual Comments

### Comment 1
<location path="astrbot/builtin_stars/astrbot/group_chat_context.py" line_range="86-89" />
<code_context>
         image_caption_prompt: str,
+        event: AstrMessageEvent | None = None,
     ) -> str:
+        provider_id = image_caption_provider_id
         if not image_caption_provider_id:
             provider = self.context.get_using_provider()
+            provider_id = provider.meta().id if hasattr(provider, 'meta') else ""
         else:
             provider = self.context.get_provider_by_id(image_caption_provider_id)
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider avoiding an empty string fallback for `provider_id` when `meta()` is unavailable.

Using `""` here can conflate “unknown” with a real (but empty) ID and make downstream stats harder to interpret. Prefer a sentinel that clearly means “missing”, e.g. `None`, or fall back to another identifier (e.g. `getattr(provider, "id", None)`) so the persistence layer can distinguish missing vs real IDs.

```suggestion
        provider_id = image_caption_provider_id
        if not image_caption_provider_id:
            provider = self.context.get_using_provider()
            meta = getattr(provider, "meta", None)
            if callable(meta):
                provider_meta = meta()
                provider_id = getattr(provider_meta, "id", None)
            else:
                provider_id = getattr(provider, "id", None)
```
</issue_to_address>

### Comment 2
<location path="astrbot/builtin_stars/astrbot/group_chat_context.py" line_range="106-112" />
<code_context>
+        # 记录图片转述模型的调用统计
+        if event is not None:
+            try:
+                provider_model = (
+                    provider.get_model() if provider.get_model() else None
+                )
+                usage_dict: dict = {}
</code_context>
<issue_to_address>
**suggestion:** Avoid calling `provider.get_model()` twice in the same expression.

Since `get_model()` is called twice here, assign it to a local variable first (e.g. `model = provider.get_model(); provider_model = model or None`) to avoid duplicate work and potential side effects, and to keep the code clearer.

```suggestion
        # 记录图片转述模型的调用统计
        if event is not None:
            try:
                model = provider.get_model()
                provider_model = model or None
                usage_dict: dict = {}
```
</issue_to_address>

### Comment 3
<location path="astrbot/builtin_stars/astrbot/group_chat_context.py" line_range="132" />
<code_context>
+                        "token_usage": usage_dict,
+                        "start_time": start_time,
+                        "end_time": end_time,
+                        "time_to_first_token": 0.0,
+                    },
+                    agent_type="internal",
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Hardcoding `time_to_first_token` to 0.0 may be misleading in metrics/analytics.

If this path is always non‑streaming, it’d be better to omit this field or use `None` to mean “not measured” rather than `0.0`, which looks like a real datapoint and will distort latency metrics and dashboards.

Suggested implementation:

```python
                    stats={
                        "token_usage": usage_dict,
                        "start_time": start_time,
                        "end_time": end_time,
                    },

```

If other call sites or downstream consumers expect `time_to_first_token` to always exist, you may want to instead set it explicitly to `None` (or the equivalent for your metrics pipeline) rather than removing it. In that case, replace the removed line with `"time_to_first_token": None,` and ensure the stats recording/analytics layer treats `None` as “not measured” instead of `0.0`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +86 to +89
provider_id = image_caption_provider_id
if not image_caption_provider_id:
provider = self.context.get_using_provider()
provider_id = provider.meta().id if hasattr(provider, 'meta') else ""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider avoiding an empty string fallback for provider_id when meta() is unavailable.

Using "" here can conflate “unknown” with a real (but empty) ID and make downstream stats harder to interpret. Prefer a sentinel that clearly means “missing”, e.g. None, or fall back to another identifier (e.g. getattr(provider, "id", None)) so the persistence layer can distinguish missing vs real IDs.

Suggested change
provider_id = image_caption_provider_id
if not image_caption_provider_id:
provider = self.context.get_using_provider()
provider_id = provider.meta().id if hasattr(provider, 'meta') else ""
provider_id = image_caption_provider_id
if not image_caption_provider_id:
provider = self.context.get_using_provider()
meta = getattr(provider, "meta", None)
if callable(meta):
provider_meta = meta()
provider_id = getattr(provider_meta, "id", None)
else:
provider_id = getattr(provider, "id", None)

Comment on lines +106 to +112
# 记录图片转述模型的调用统计
if event is not None:
try:
provider_model = (
provider.get_model() if provider.get_model() else None
)
usage_dict: dict = {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Avoid calling provider.get_model() twice in the same expression.

Since get_model() is called twice here, assign it to a local variable first (e.g. model = provider.get_model(); provider_model = model or None) to avoid duplicate work and potential side effects, and to keep the code clearer.

Suggested change
# 记录图片转述模型的调用统计
if event is not None:
try:
provider_model = (
provider.get_model() if provider.get_model() else None
)
usage_dict: dict = {}
# 记录图片转述模型的调用统计
if event is not None:
try:
model = provider.get_model()
provider_model = model or None
usage_dict: dict = {}

"token_usage": usage_dict,
"start_time": start_time,
"end_time": end_time,
"time_to_first_token": 0.0,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Hardcoding time_to_first_token to 0.0 may be misleading in metrics/analytics.

If this path is always non‑streaming, it’d be better to omit this field or use None to mean “not measured” rather than 0.0, which looks like a real datapoint and will distort latency metrics and dashboards.

Suggested implementation:

                    stats={
                        "token_usage": usage_dict,
                        "start_time": start_time,
                        "end_time": end_time,
                    },

If other call sites or downstream consumers expect time_to_first_token to always exist, you may want to instead set it explicitly to None (or the equivalent for your metrics pipeline) rather than removing it. In that case, replace the removed line with "time_to_first_token": None, and ensure the stats recording/analytics layer treats None as “not measured” instead of 0.0.

Added event and conversation_id parameters to image caption processing and included timing and usage statistics logging.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces call statistics tracking for the image caption provider in group chats by passing the message event and recording usage metrics, start/end times, and status to the database. The review feedback highlights that API failures are currently not tracked because exceptions propagate before the database insertion block. Additionally, the reviewer suggests using getattr for safer attribute access on the response object and avoiding redundant calls to provider.get_model().

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +97 to 142
start_time = time.time()
response = await provider.text_chat(
prompt=image_caption_prompt,
session_id=uuid.uuid4().hex,
image_urls=[image_url],
persist=False,
)
end_time = time.time()

# 记录图片转述模型的调用统计
if event is not None:
try:
provider_model = (
provider.get_model() if provider.get_model() else None
)
usage_dict: dict = {}
if response.usage:
usage_dict = {
"input_other": response.usage.input_other,
"input_cached": response.usage.input_cached,
"output": response.usage.output,
}

await db_helper.insert_provider_stat(
umo=event.unified_msg_origin,
provider_id=provider_id,
provider_model=provider_model,
conversation_id=None,
status="completed"
if response.role != "err"
else "error",
stats={
"token_usage": usage_dict,
"start_time": start_time,
"end_time": end_time,
"time_to_first_token": 0.0,
},
agent_type="internal",
)
except Exception:
logger.debug(
"Failed to record group chat image caption provider stat",
exc_info=True,
)

return response.completion_text

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Issues Identified:

  1. API Failure Statistics are Not Tracked: If provider.text_chat raises an exception (e.g., due to network timeout, API error, etc.), the exception propagates immediately, and the statistics tracking block is bypassed. This means failed requests are never recorded in the database, making error rate calculations impossible.
  2. Potential AttributeError: Accessing response.usage.input_other and response.role directly can raise an AttributeError if the provider returns a different structure or if the response is malformed. Using getattr is a safer, more defensive approach.
  3. Redundant Method Call: provider.get_model() is called twice. We can simplify this to a single call.

Solution:

Wrap the provider.text_chat call in a try...except block to capture any exceptions, record the "error" status in the database, and then re-raise the exception. Use getattr to safely access response attributes.

        start_time = time.time()
        response = None
        exception = None
        try:
            response = await provider.text_chat(
                prompt=image_caption_prompt,
                session_id=uuid.uuid4().hex,
                image_urls=[image_url],
                persist=False,
            )
        except Exception as e:
            exception = e
        end_time = time.time()

        # 记录图片转述模型的调用统计
        if event is not None:
            try:
                provider_model = provider.get_model() or None
                usage_dict: dict = {}
                if response and getattr(response, "usage", None):
                    usage_dict = {
                        "input_other": getattr(response.usage, "input_other", 0),
                        "input_cached": getattr(response.usage, "input_cached", 0),
                        "output": getattr(response.usage, "output", 0),
                    }

                status = "error"
                if exception is None and response:
                    status = "completed" if getattr(response, "role", "") != "err" else "error"

                await db_helper.insert_provider_stat(
                    umo=event.unified_msg_origin,
                    provider_id=provider_id,
                    provider_model=provider_model,
                    conversation_id=None,
                    status=status,
                    stats={
                        "token_usage": usage_dict,
                        "start_time": start_time,
                        "end_time": end_time,
                        "time_to_first_token": 0.0,
                    },
                    agent_type="internal",
                )
            except Exception:
                logger.debug(
                    "Failed to record group chat image caption provider stat",
                    exc_info=True,
                )

        if exception is not None:
            raise exception

        return response.completion_text

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 10, 2026
Removed unused database helper calls for image caption statistics and streamlined the logging process.
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jun 10, 2026

@Dt8333 Dt8333 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

想法很好,但是实现的似乎不太对劲。
为什么所有的Provider都提供这么个record_image_caption_stat方法?
也许应该找个更合适的地方安放。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants