Skip to content

fix: When there is an empty string in the context, the conversation reports an error#2947

Merged
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_chat
Apr 22, 2025
Merged

fix: When there is an empty string in the context, the conversation reports an error#2947
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_chat

Conversation

@shaohuzhang1
Copy link
Copy Markdown
Contributor

fix: When there is an empty string in the context, the conversation reports an error

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Apr 22, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Apr 22, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

return AIMessage(content=answer_text)

def get_node_details_runtime_node_id(self, runtime_node_id):
return self.details.get(runtime_node_id, None)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The code appears to be well-written, but there are a couple of optimizations you might consider:

  1. String Formatting: In get_ai_message, using an inline string concatenation directly within _.format() can be less efficient than constructing the entire string first. Consider extracting the result into a variable.

  2. Variable Naming Consistency: Ensure that all variables (like answer_text) use consistent naming conventions throughout the method.

Here's revised version with some of these suggestions implemented:

from django.contrib.postgres.fields import ArrayField
from django.db import models
from langchain.schema import HumanMessage, AIMessage
from django.utils.translation import gettext as _

from common.encoder.encoder import SystemEncoder
from common.mixins.app_model_mixin import AppModelMixin
from dataset.models.data_set import DataSet


def construct_answer_message(text):
    if not text or str(text).strip() == "":
        return _(
            'Sorry, no relevant content was found. Please re-describe your problem or provide more information.'
        )
    else:
        return text

class YourClassName(AppModelMixin):  # Replace with actual class name
    problem_text = models.TextField()
    answer_text = models.TextField()

    def get_human_message(self):
        return HumanMessage(content=self.problem_text)

    def get_ai_message(self):
        return AIMessage(content=construct_answer_message(self.answer_text))

    def get_node_details_runtime_node_id(self, runtime_node_id):
        return self.details.get(runtime_node_id, None)

This change refactors the construction of the AI message into a separate function (construct_answer_message), making it easier to test and reuse if needed. The rest of the logic remains mostly unchanged.

@shaohuzhang1 shaohuzhang1 merged commit c39a6e8 into main Apr 22, 2025
4 of 5 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@fix_chat branch April 22, 2025 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant