-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: 修复AI服务的JSON解析问题 #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: 修复AI服务的JSON解析问题 #127
Conversation
Added intelligent JSON extraction to handle AI responses that include explanatory text before or after the JSON. This fixes issues where JSON parsing would fail due to markdown code blocks or AI-generated prefixes like "Here is the JSON:". Changes: - Added `_extract_json_from_text()` method to intelligently extract JSON from mixed text - Improved bracket matching to find valid JSON boundaries - Handles common AI response patterns (explanatory text, markdown blocks) - Maintains backward compatibility with clean JSON responses 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Summary of ChangesHello @XiaohanA2, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在增强AI服务处理JSON响应的鲁棒性。它通过引入一个智能JSON提取机制,解决了AI模型在返回JSON时常附带解释性文字或Markdown代码块标记导致解析失败的问题。这一改进确保了系统能够更可靠地从AI响应中获取结构化数据,从而提升了服务的稳定性和用户体验。 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
本次拉取请求通过引入智能JSON提取功能,有效解决了AI服务返回包含解释性文字的JSON解析失败问题。新增的 _extract_json_from_text 方法能够识别并提取文本中的JSON部分,支持markdown代码块格式,并移除了常见的AI生成前缀,同时保持了向后兼容性。这是一个非常有价值的改进,提升了系统的健壮性。
backend/services/ai_service.py
Outdated
| Returns: | ||
| 提取的JSON字符串 | ||
| """ | ||
| import re |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
backend/services/ai_service.py
Outdated
| if text.startswith('['): | ||
| start_idx = 0 | ||
| elif text.startswith('{'): | ||
| start_idx = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改进建议: - 移除函数内重复的 `import re`(文件顶部已导入) - 移除冗余的 `startswith` 检查,由循环自然处理 这些优化: 1. 遵循 Python PEP 8 规范(所有 import 放在文件顶部) 2. 提高代码可读性和简洁性 3. 避免不必要的性能开销 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
问题说明
当前代码在AI返回包含解释性文字的JSON时会解析失败。AI模型经常会在JSON前后添加markdown代码块标记或说明文字(如"Here is the JSON:")。
解决方案
添加了智能JSON提取功能:
修改内容
_extract_json_from_text()方法