-
Notifications
You must be signed in to change notification settings - Fork 2
添加情感分析prompt/类(暂未集成) #6
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?
Conversation
文件级别更改
提示和命令与 Sourcery 互动
自定义您的体验访问您的 仪表板 以:
获得帮助Original review guide in EnglishReviewer's GuideThis PR adds a persona-driven sentiment analysis prompt in the prompts module and implements a new emotion analysis pipeline with enums and a class for LLM-based emotion detection. Class Diagram for New Emotion Analysis ComponentsclassDiagram
class EmotionTendency {
<<enumeration>>
POSITIVE
NEGATIVE
NEUTRAL
}
class Emotion {
<<enumeration>>
JOY
CONTENTMENT
SURPRISE
NEUTRAL
FEAR
SADNESS
ANGER
DISGUST
PANIC
+tendency: EmotionTendency
+prompt: str
}
class EmotionAnalysis {
-provider: ProviderOpenAI
+__init__(provider: ProviderOpenAI)
+analyze(text: dict) : dict_or_None
}
Emotion ..> EmotionTendency : "property `tendency` returns"
EmotionAnalysis ..> Emotion : "uses (formats prompt with `Emotion` enum)"
EmotionAnalysis o-- ProviderOpenAI : "has a (dependency)"
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
嘿 @Raven95676 - 我已经查看了你的更改 - 这里有一些反馈:
- 将 EMOTION_ANALYSIS_PROMPT 中的内联 Python 推导式(即 {[e.value for e in Emotion]})替换为显式的情感值列表,以避免将原始代码发送到模型。
- 使 analyze() 中的 JSON 解析更加健壮——例如,使用正则表达式或内容标记仅提取 JSON 块——以防止在意外的模型输出上出现静默失败。
- 为 EmotionAnalysis.analyze() 的预期输入结构(文本、个性、上下文)定义一个清晰的类型或数据类,以提高清晰度并强制执行必需的字段。
以下是我在审查期间查看的内容
- 🟡 General issues: 发现 1 个问题
- 🟢 Security: 一切看起来都很好
- 🟢 Testing: 一切看起来都很好
- 🟡 Complexity: 发现 1 个问题
- 🟢 Documentation: 一切看起来都很好
帮助我更有用!请点击每个评论上的 👍 或 👎,我将使用反馈来改进你的评论。
Original comment in English
Hey @Raven95676 - I've reviewed your changes - here's some feedback:
- Replace the inline Python comprehension in EMOTION_ANALYSIS_PROMPT (i.e. {[e.value for e in Emotion]}) with an explicit list of emotion values to avoid sending raw code to the model.
- Make the JSON parsing in analyze() more robust—e.g. extract only the JSON block with a regex or content markers—to prevent silent failures on unexpected model output.
- Define a clear type or dataclass for the expected input structure (text, personality, context) for EmotionAnalysis.analyze() to improve clarity and enforce required fields.
Here's what I looked at during the review
- 🟡 General issues: 1 issue found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| @property | ||
| def tendency(self) -> EmotionTendency: |
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.
suggestion (performance): Recreating emotion sets on each access
将 positive 和 negative 移动到类级别的常量(例如 POSITIVE_SET = frozenset([...])),以避免在每次属性访问时重新分配它们并简化方法。
Original comment in English
suggestion (performance): Recreating emotion sets on each access
Move positive and negative to class-level constants (e.g. POSITIVE_SET = frozenset([...])) to avoid reallocating them on each property access and simplify the method.
| - You should use `USER_ID` as the source or target content for any self-references (e.g., "I", "me", "my" etc.) in user messages.""" | ||
|
|
||
| EMOTION_ANALYSIS_PROMPT = """ |
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.
issue (complexity): Consider moving the large prompt into a separate file and loading it dynamically to keep prompts.py concise and maintainable.
考虑将这个大的提示移动到一个单独的文件中,或者将它分解成更小的常量,这样`prompts.py`就可以保持简洁和可维护性。例如:
1) 将文字提示移动到`prompts/emotion_analysis.txt`中:
├── prompts/
│ ├── __init__.py
│ └── emotion_analysis.txt ← 你的50行模板
2) 在`prompts.py`中,在运行时加载它:
```python
import os
_TEMPLATE_PATH = os.path.join(
os.path.dirname(__file__),
"prompts",
"emotion_analysis.txt",
)
with open(_TEMPLATE_PATH, "r", encoding="utf-8") as _f:
EMOTION_ANALYSIS_PROMPT = _f.read()-
生成动态位(例如,情感列表)而不是硬编码:
from your_module import Emotion EMOTIONS = ", ".join(e.value for e in Emotion) # In emotion_analysis.txt, replace the static line… # 选择这些情绪: [happy, sad, …] # …with a placeholder: # 选择这些情绪: {{EMOTIONS}}
然后渲染它:
EMOTION_ANALYSIS_PROMPT = ( EMOTION_ANALYSIS_PROMPT.replace("{{EMOTIONS}}", EMOTIONS) )
这使得逻辑在您的代码中,而大型模板在它自己的文件中,以便于维护。
Original comment in English
issue (complexity): Consider moving the large prompt into a separate file and loading it dynamically to keep prompts.py concise and maintainable.
Consider extracting this big prompt into a separate file (or breaking it into smaller constants) so `prompts.py` stays concise. For example:
1) Move the literal prompt into `prompts/emotion_analysis.txt`:
├── prompts/
│ ├── __init__.py
│ └── emotion_analysis.txt ← your 50-line template
2) In `prompts.py`, load it at runtime:
```python
import os
_TEMPLATE_PATH = os.path.join(
os.path.dirname(__file__),
"prompts",
"emotion_analysis.txt",
)
with open(_TEMPLATE_PATH, "r", encoding="utf-8") as _f:
EMOTION_ANALYSIS_PROMPT = _f.read()-
Generate dynamic bits (e.g. the emotion list) instead of hard-coding:
from your_module import Emotion EMOTIONS = ", ".join(e.value for e in Emotion) # In emotion_analysis.txt, replace the static line… # Select from these emotions: [happy, sad, …] # …with a placeholder: # Select from these emotions: {{EMOTIONS}}
And then render it:
EMOTION_ANALYSIS_PROMPT = ( EMOTION_ANALYSIS_PROMPT.replace("{{EMOTIONS}}", EMOTIONS) )
This keeps the logic in your code and the large template in its own file for easier maintenance.
好的,这是翻译成中文的 pull request 总结:
Sourcery 总结
引入由角色驱动的情感分析支持,通过添加详细的 LLM 提示、定义情感枚举以及实现 EmotionAnalysis 管道模块。
新特性:
EMOTION_ANALYSIS_PROMPT模板。Emotion和EmotionTendency枚举来分类和归类情感状态。EmotionAnalysis类,以使用新的提示调用基于 LLM 的情感检测。Original summary in English
Summary by Sourcery
Introduce persona-driven emotion analysis support by adding a detailed LLM prompt, defining emotion enums, and implementing an EmotionAnalysis pipeline module.
New Features: