Skip to content

fix: optimize default AI prompt to reduce thinking process output#175

Merged
AmintaCCCP merged 4 commits into
mainfrom
fix/optimize-default-prompt-174
May 29, 2026
Merged

fix: optimize default AI prompt to reduce thinking process output#175
AmintaCCCP merged 4 commits into
mainfrom
fix/optimize-default-prompt-174

Conversation

@AmintaCCCP
Copy link
Copy Markdown
Owner

@AmintaCCCP AmintaCCCP commented May 29, 2026

Summary

  • 基于 AI分析会返回思考内容 #174 的反馈,优化默认 AI 分析提示词,减少模型返回思考过程/推理内容的现象
  • 在 system prompt 和 user prompt 中明确要求"只输出合法JSON",禁止输出思考过程、Markdown、代码块等额外文本
  • 重构提示词为清晰的 Requirements/输出格式 结构
  • 新增平台检测线索(Dockerfile→docker, CLI→cli 等),提升平台识别准确性
  • 约束 platforms 字段为显式枚举值,无法判断时使用 []
  • 提供具体的 JSON 输出示例替代泛化的占位值

Closes #174

Test plan

  • 使用默认提示词分析若干仓库,验证 AI 仅返回 JSON 而不包含思考过程
  • 验证设置面板中默认提示词显示与新格式一致
  • 验证自定义提示词仍能正常替换 {REPO_INFO}{CATEGORIES_INFO} 占位符
  • 验证平台检测线索对常见仓库类型的识别效果

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Enforced strict JSON-only AI output with a fixed schema (summary, tags, platforms) to remove stray text or formatting.
    • Ensured invalid or unknown platforms are dropped (yielding an empty platforms list when none remain).
  • Enhancements

    • Standardized and validated platform classification (allowed list, normalization, dedupe, max 8).
    • Improved tag selection and category prioritization, including better handling of custom categories and platform hints.

Review Change Stack

Based on #174 feedback, the default prompt was causing AI models to
return thinking/reasoning content instead of pure JSON. Key changes:

- Add explicit "only output valid JSON" instruction in system prompt
  and user prompt to suppress thinking process output
- Restructure prompt into clear Requirements/输出格式 sections
- Add platform detection hints (Dockerfile→docker, CLI→cli, etc.)
- Constrain platforms to explicit enum with [] fallback
- Provide concrete JSON example instead of generic placeholder values

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 29, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 83f0e668-f603-42e3-b2b7-0ee7871b2541

📥 Commits

Reviewing files that changed from the base of the PR and between d1d3cfc and 3bbd1da.

📒 Files selected for processing (1)
  • src/services/aiService.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/services/aiService.ts

📝 Walkthrough

Walkthrough

Service and UI prompts were tightened to force strictly valid JSON output with a fixed schema (summary, tags, platforms), explicit platform-hint mappings, and server-side post-validation that filters returned platforms to a known allowlist.

Changes

AI Prompt Constraint Enforcement

Layer / File(s) Summary
System-level JSON-only requirement
src/services/aiService.ts
The system prompt in analyzeRepository (CN/EN) now explicitly forbids non-JSON artifacts such as thinking process, Markdown, and code fences.
Detailed repository analysis prompt with schema and constraints
src/services/aiService.ts
createAnalysisPrompt now emits expanded CN/EN templates specifying the JSON schema (summary, tags, platforms), summary length limit (≤50 words), tags count and prioritization rules (3–5, optionally prioritized from customCategories), a fixed allowlist for platforms, and platform-hint mappings. A VALID_PLATFORMS constant is introduced.
Platform post-validation in parser
src/services/aiService.ts
parseAIResponse filters/normalizes parsed.platforms against the allowed set (case-insensitive), caps the number returned, and treats unknown/invalid entries as [].
Config panel default prompt with matching constraints
src/components/settings/AIConfigPanel.tsx
The defaultPrompt CN/EN templates were updated to enforce the same strict JSON-only schema, platform allowlist, and platform-hint mappings as the service prompts.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰
Prompts now whisper: "JSON, no more fuss,"
Tags trimmed and platforms mapped for us.
Models speak in tidy, bracketed art—
No stray thoughts, just structured heart. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main change—optimizing the default AI prompt to reduce model-returned thinking process output, which is the primary objective of the PR.
Linked Issues check ✅ Passed The PR fully addresses all coding requirements from issue #174: enforces JSON-only output, constrains platforms to a fixed enum, adds platform-detection cues, validates/normalizes platforms in parseAIResponse, and supports placeholder replacement in settings.
Out of Scope Changes check ✅ Passed All changes align with the scope: AIConfigPanel and aiService updates restructure prompts, add validation logic, and introduce VALID_PLATFORMS constant—all directly supporting the goal of reducing spurious output.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/optimize-default-prompt-174

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

AmintaCCCP and others added 2 commits May 29, 2026 20:45
Previously categories were silently appended to repo info at the end
of the prompt, which some models could overlook. Now categories are
placed as a dedicated line right next to the tags requirement, with
an explicit cross-reference ("请优先从上方的可用分类中选择") so
models clearly see the connection between available categories and
the tags field they need to fill.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…e platforms enum

1. AIConfigPanel default prompt now includes {CATEGORIES_INFO} placeholder
   next to the tags requirement, so users can see and use it in custom
   prompts. Previously the placeholder was undocumented in the UI.

2. parseAIResponse now filters platforms against a VALID_PLATFORMS enum
   (mac/windows/linux/ios/android/docker/web/cli) with case-insensitive
   matching. Invalid values like "mobile" are silently dropped instead of
   passing through to the UI.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/services/aiService.ts (1)

381-404: 💤 Low value

Consider simplifying category reference to avoid redundancy.

When customCategories is provided, the prompt mentions categories twice:

  1. Line 389: "请优先从上方的可用分类中选择" (prioritize from categories above)
  2. Line 389 (via categoriesLine): "\n可用分类(tags 请优先从中选择):..." (available categories list)

While this redundancy may help reinforce the instruction for AI models, you could simplify by removing the inline reference and relying solely on the categoriesLine, or vice versa.

Same pattern exists in the English version (lines 406-408, 414).

♻️ Suggested simplification (if desired)

Option 1: Remove inline reference, keep categoriesLine:

-      return `
-请分析以下GitHub仓库信息,并只输出合法JSON对象。不要输出思考过程、Markdown、代码块标记、解释或任何额外文本。
-
-要求:
-- summary:中文概述,说明仓库的主要功能和用途,不超过50字。
-- tags:3-5个中文应用类型标签${customCategories && customCategories.length > 0 ? ',请优先从上方的可用分类中选择' : ',类似应用商店的分类,如:开发工具、Web应用、移动应用、数据库、AI工具等'}。${categoriesLine}
+      return `
+请分析以下GitHub仓库信息,并只输出合法JSON对象。不要输出思考过程、Markdown、代码块标记、解释或任何额外文本。
+
+要求:
+- summary:中文概述,说明仓库的主要功能和用途,不超过50字。
+- tags:3-5个中文应用类型标签${customCategories && customCategories.length > 0 ? '' : ',类似应用商店的分类,如:开发工具、Web应用、移动应用、数据库、AI工具等'}。${categoriesLine}

Apply similar change to English version.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/services/aiService.ts` around lines 381 - 404, The prompt template
duplicates the available-category guidance when customCategories is present by
both injecting the inline phrase "请优先从上方的可用分类中选择" inside the tags sentence and
also appending categoriesLine; update the prompt construction in
src/services/aiService.ts to remove the redundant inline reference and rely only
on categoriesLine (or conversely remove categoriesLine and keep the inline
phrase) so that the returned template string uses a single category reference;
adjust the same pattern in the English counterpart to ensure consistency; look
for usages of customCategories, categoriesLine and the template-returning
function to make the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/services/aiService.ts`:
- Around line 381-404: The prompt template duplicates the available-category
guidance when customCategories is present by both injecting the inline phrase
"请优先从上方的可用分类中选择" inside the tags sentence and also appending categoriesLine;
update the prompt construction in src/services/aiService.ts to remove the
redundant inline reference and rely only on categoriesLine (or conversely remove
categoriesLine and keep the inline phrase) so that the returned template string
uses a single category reference; adjust the same pattern in the English
counterpart to ensure consistency; look for usages of customCategories,
categoriesLine and the template-returning function to make the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6f0e36c2-2aec-48ec-8394-0ab9035b50cd

📥 Commits

Reviewing files that changed from the base of the PR and between b77ff5d and dd85189.

📒 Files selected for processing (2)
  • src/components/settings/AIConfigPanel.tsx
  • src/services/aiService.ts

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/services/aiService.ts`:
- Around line 450-452: The platforms array currently filters case-insensitively
using AIService.VALID_PLATFORMS but returns the original-cased values from
parsed.platforms; change the mapping so you canonicalize each accepted platform
(e.g., v.toLowerCase() or a canonicalizer that matches
AIService.VALID_PLATFORMS) after the filter and before slice/return—operate on
parsed.platforms, use the same validator
(AIService.VALID_PLATFORMS.includes(v.toLowerCase())) and then map to the
canonical form (v.toLowerCase() or lookup) to ensure returned platform values
conform to the enum.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ba9cba2d-b40e-4c27-9269-0a81cc540ce0

📥 Commits

Reviewing files that changed from the base of the PR and between dd85189 and d1d3cfc.

📒 Files selected for processing (2)
  • src/components/settings/AIConfigPanel.tsx
  • src/services/aiService.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/settings/AIConfigPanel.tsx

Comment thread src/services/aiService.ts
Filter now normalizes each platform to lowercase before validation
and uses Set to deduplicate, ensuring the returned values always
match the VALID_PLATFORMS enum exactly. Previously, values like
"WEB" could pass validation but retain non-canonical casing.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@AmintaCCCP AmintaCCCP merged commit 87df3c2 into main May 29, 2026
5 checks passed
@AmintaCCCP AmintaCCCP deleted the fix/optimize-default-prompt-174 branch May 29, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AI分析会返回思考内容

1 participant