Skip to content

Conversation

@Raven95676
Copy link
Member

@Raven95676 Raven95676 commented May 20, 2025

好的,这是翻译成中文的 pull request 总结:

Sourcery 总结

引入由角色驱动的情感分析支持,通过添加详细的 LLM 提示、定义情感枚举以及实现 EmotionAnalysis 管道模块。

新特性:

  • 添加用于角色驱动的情感分析的 EMOTION_ANALYSIS_PROMPT 模板。
  • 定义 EmotionEmotionTendency 枚举来分类和归类情感状态。
  • 实现 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:

  • Add EMOTION_ANALYSIS_PROMPT template for persona-driven sentiment analysis.
  • Define Emotion and EmotionTendency enums to classify and categorize emotional states.
  • Implement EmotionAnalysis class to invoke LLM-based emotion detection using the new prompt.

@sourcery-ai
Copy link

sourcery-ai bot commented May 20, 2025

## 审查者指南

此 PR 在 prompts 模块中添加了一个由角色驱动的情感分析提示,并实现了一个新的情感分析管道,其中包含枚举和用于基于 LLM 的情感检测的类。

#### 新情感分析组件的类图

```mermaid
classDiagram
    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)"

文件级别更改

Change Details Files
引入用于角色驱动的情感分析的 EMOTION_ANALYSIS_PROMPT
  • 添加包含 JSON 输入模式和输出格式的多行系统提示
  • 定义包含优先级和情感选择及强度指南的分析步骤
core/util/prompts.py
使用枚举和分析类实现情感分析管道
  • 定义包含倾向和提示属性的 EmotionTendency 和 Emotion 枚举
  • 创建 EmotionAnalysis 类,该类格式化提示,调用 LLM,并处理 JSON 解析错误
core/pipeline/emotion.py

提示和命令

与 Sourcery 互动

  • 触发新的审查: 在 pull request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 通过回复审查评论,要求 Sourcery 从审查评论创建一个 issue。您也可以回复审查评论并使用 @sourcery-ai issue 从中创建一个 issue。
  • 生成 pull request 标题: 在 pull request 标题中的任何位置写入 @sourcery-ai 以随时生成标题。您也可以在 pull request 上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文中的任何位置写入 @sourcery-ai summary 以随时在您想要的位置生成 PR 摘要。您也可以在 pull request 上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成审查者指南: 在 pull request 上评论 @sourcery-ai guide 以随时(重新)生成审查者指南。
  • 解决所有 Sourcery 评论: 在 pull request 上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经解决了所有评论并且不想再看到它们,这将非常有用。
  • 驳回所有 Sourcery 审查: 在 pull request 上评论 @sourcery-ai dismiss 以驳回所有现有的 Sourcery 审查。如果您想从新的审查开始,这将特别有用 - 不要忘记评论 @sourcery-ai review 以触发新的审查!

自定义您的体验

访问您的 仪表板 以:

  • 启用或禁用审查功能,例如 Sourcery 生成的 pull request 摘要、审查者指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获得帮助

```
Original review guide in English

Reviewer's Guide

This 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 Components

classDiagram
    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)"
Loading

File-Level Changes

Change Details Files
Introduce EMOTION_ANALYSIS_PROMPT for persona-driven sentiment analysis
  • Add multiline system prompt with JSON input schema and output format
  • Define analysis steps with priorities and guidelines for emotion selection and intensity
core/util/prompts.py
Implement emotion analysis pipeline with enums and analysis class
  • Define EmotionTendency and Emotion enums with properties for tendency and prompts
  • Create EmotionAnalysis class that formats prompt, calls LLM, and handles JSON parsing errors
core/pipeline/emotion.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a 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: 一切看起来都很好

Sourcery 对开源是免费的 - 如果你喜欢我们的评论,请考虑分享它们 ✨
帮助我更有用!请点击每个评论上的 👍 或 👎,我将使用反馈来改进你的评论。
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

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +42 to +43
@property
def tendency(self) -> EmotionTendency:
Copy link

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

positivenegative 移动到类级别的常量(例如 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 = """
Copy link

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()
  1. 生成动态位(例如,情感列表)而不是硬编码:

    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()
  1. 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.

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.

1 participant