Implement time tracking for image caption requests#8711
Implement time tracking for image caption requests#8711tjc6666666666666 wants to merge 11 commits into
Conversation
Added time tracking for image caption provider usage and logging.
There was a problem hiding this comment.
Hey - I've found 3 issues, and left some high level feedback:
- For time tracking, consider using
time.monotonic()instead oftime.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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| 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 "" |
There was a problem hiding this comment.
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.
| 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) |
| # 记录图片转述模型的调用统计 | ||
| if event is not None: | ||
| try: | ||
| provider_model = ( | ||
| provider.get_model() if provider.get_model() else None | ||
| ) | ||
| usage_dict: dict = {} |
There was a problem hiding this comment.
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.
| # 记录图片转述模型的调用统计 | |
| 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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
Issues Identified:
- API Failure Statistics are Not Tracked: If
provider.text_chatraises 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. - Potential
AttributeError: Accessingresponse.usage.input_otherandresponse.roledirectly can raise anAttributeErrorif the provider returns a different structure or if the response is malformed. Usinggetattris a safer, more defensive approach. - 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_textRemoved redundant import of time module.
Removed unused database helper calls and exception handling for image caption statistics.
Adds a method to record statistics for image caption model calls.
Removed unused database helper calls for image caption statistics and streamlined the logging process.
Dt8333
left a comment
There was a problem hiding this comment.
想法很好,但是实现的似乎不太对劲。
为什么所有的Provider都提供这么个record_image_caption_stat方法?
也许应该找个更合适的地方安放。
Added time tracking for image caption provider usage and logging.
Modifications / 改动点
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.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.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:
Enhancements: