[PR #16910] [CLOSED] feat: Implement prompt usage counter and stats API #95316

Closed
opened 2026-05-15 21:29:08 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/16910
Author: @BoDy-PrISon
Created: 8/25/2025
Status: Closed

Base: mainHead: feature/prompt-counter


📝 Commits (2)

  • bb7af8d feat: Implement prompt usage counter and stats API
  • dea5120 Неуверенные изменения

📊 Changes

12 files changed (+444 additions, -14 deletions)

View changed files

.webui_secret_key (+1 -0)
📝 backend/open_webui/main.py (+7 -0)
backend/open_webui/migrations/versions/14f6763df9ca_merge_heads.py (+27 -0)
backend/open_webui/migrations/versions/e1b2c3d4_add_prompt_usage_table.py (+37 -0)
backend/open_webui/models/prompt_usage.py (+37 -0)
📝 backend/open_webui/routers/chats.py (+5 -1)
📝 backend/open_webui/routers/ollama.py (+7 -0)
📝 backend/open_webui/routers/openai.py (+6 -1)
📝 backend/open_webui/routers/prompts.py (+130 -0)
backend/open_webui/utils/prompt_counter.py (+121 -0)
📝 src/lib/apis/prompts/index.ts (+29 -0)
📝 src/lib/components/chat/Suggestions.svelte (+37 -12)

📄 Description

Changelog Entry

Description

This pull request introduces a comprehensive feature to track the usage of predefined "suggested prompts". It includes a backend counting mechanism and a new suite of API endpoints to retrieve detailed statistics. This fulfills and exceeds the requirements of the provided test task.

Added

  • A new in-memory counter mechanism stored in app.state to track prompt usage by total count, prompt type, user, and date.
  • A new utility module backend/open_webui/utils/prompt_counter.py containing all the logic for prompt detection and counting.
  • A new router backend/open_webui/routers/prompts.py with a suite of new API endpoints:
    • GET /api/v1/prompts/usage/stats: Returns overall usage statistics.
    • GET /api/v1/prompts/usage/prompt/{prompt_command}: Returns statistics for a specific prompt type.
    • GET /api/v1/prompts/usage/user/{user_id}: Returns statistics for a specific user, protected by an authorization check (admins or the user themselves).

Changed

  • Modified the chat completion logic in /routers/openai.py and /routers/ollama.py to call the new prompt counter on every user message.
  • Implemented a check within the counter logic to ignore system-generated "follow-up" prompts, preventing double-counting.
  • Updated main.py to initialize the counter and include the new prompts API router.

Removed

  • N/A

Fixed

  • N/A

Additional Information

  • As this is a test task, the counter is currently stored in-memory and will reset on server restart. For a production environment, I would recommend persisting this data in the database by creating a PromptUsageLog model and logging each event.
  • The prompt detection is currently based on keywords. A potential improvement would be to integrate it with the internal TASK_MODEL for more intelligent, semantic classification of user intent.

Contributor License Agreement

By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/16910 **Author:** [@BoDy-PrISon](https://github.com/BoDy-PrISon) **Created:** 8/25/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feature/prompt-counter` --- ### 📝 Commits (2) - [`bb7af8d`](https://github.com/open-webui/open-webui/commit/bb7af8dc9b9670a1a445e60f50b0794222f8816e) feat: Implement prompt usage counter and stats API - [`dea5120`](https://github.com/open-webui/open-webui/commit/dea5120cfc32346b57b818da05a709999d5ba87a) Неуверенные изменения ### 📊 Changes **12 files changed** (+444 additions, -14 deletions) <details> <summary>View changed files</summary> ➕ `.webui_secret_key` (+1 -0) 📝 `backend/open_webui/main.py` (+7 -0) ➕ `backend/open_webui/migrations/versions/14f6763df9ca_merge_heads.py` (+27 -0) ➕ `backend/open_webui/migrations/versions/e1b2c3d4_add_prompt_usage_table.py` (+37 -0) ➕ `backend/open_webui/models/prompt_usage.py` (+37 -0) 📝 `backend/open_webui/routers/chats.py` (+5 -1) 📝 `backend/open_webui/routers/ollama.py` (+7 -0) 📝 `backend/open_webui/routers/openai.py` (+6 -1) 📝 `backend/open_webui/routers/prompts.py` (+130 -0) ➕ `backend/open_webui/utils/prompt_counter.py` (+121 -0) 📝 `src/lib/apis/prompts/index.ts` (+29 -0) 📝 `src/lib/components/chat/Suggestions.svelte` (+37 -12) </details> ### 📄 Description # Changelog Entry ### Description This pull request introduces a comprehensive feature to track the usage of predefined "suggested prompts". It includes a backend counting mechanism and a new suite of API endpoints to retrieve detailed statistics. This fulfills and exceeds the requirements of the provided test task. ### Added - A new in-memory counter mechanism stored in `app.state` to track prompt usage by total count, prompt type, user, and date. - A new utility module `backend/open_webui/utils/prompt_counter.py` containing all the logic for prompt detection and counting. - A new router `backend/open_webui/routers/prompts.py` with a suite of new API endpoints: - `GET /api/v1/prompts/usage/stats`: Returns overall usage statistics. - `GET /api/v1/prompts/usage/prompt/{prompt_command}`: Returns statistics for a specific prompt type. - `GET /api/v1/prompts/usage/user/{user_id}`: Returns statistics for a specific user, protected by an authorization check (admins or the user themselves). ### Changed - Modified the chat completion logic in `/routers/openai.py` and `/routers/ollama.py` to call the new prompt counter on every user message. - Implemented a check within the counter logic to ignore system-generated "follow-up" prompts, preventing double-counting. - Updated `main.py` to initialize the counter and include the new prompts API router. ### Removed - N/A ### Fixed - N/A --- ### Additional Information - As this is a test task, the counter is currently stored in-memory and will reset on server restart. For a production environment, I would recommend persisting this data in the database by creating a `PromptUsageLog` model and logging each event. - The prompt detection is currently based on keywords. A potential improvement would be to integrate it with the internal `TASK_MODEL` for more intelligent, semantic classification of user intent. --- ### Contributor License Agreement By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-05-15 21:29:08 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#95316