fix: When there is an empty string in the context, the conversation reports an error#2947
fix: When there is an empty string in the context, the conversation reports an error#2947shaohuzhang1 merged 1 commit intomainfrom
Conversation
|
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. DetailsInstructions 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. |
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| return AIMessage(content=answer_text) | ||
|
|
||
| def get_node_details_runtime_node_id(self, runtime_node_id): | ||
| return self.details.get(runtime_node_id, None) |
There was a problem hiding this comment.
The code appears to be well-written, but there are a couple of optimizations you might consider:
-
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. -
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.
fix: When there is an empty string in the context, the conversation reports an error