Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/application/models/application_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ class ChatRecord(AppModelMixin):
source = models.JSONField(verbose_name="来源", default=dict)
ip_address = models.CharField(max_length=128, verbose_name="ip地址", default='')

def save(self, *args, **kwargs):
self.problem_text = self.problem_text.replace("\x00", "")
self.answer_text = self.answer_text.replace("\x00", "")
return super().save(*args, **kwargs)

def get_human_message(self):
if 'problem_padding' in self.details:
return HumanMessage(content=self.details.get('problem_padding').get('padding_problem_text'))
Expand Down
18 changes: 16 additions & 2 deletions apps/application/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
from django.test import TestCase
from unittest import mock

# Create your tests here.
from django.test import SimpleTestCase

from application.models import ChatRecord


class ChatRecordTestCase(SimpleTestCase):
def test_save_removes_nul_characters(self):
chat_record = ChatRecord(problem_text="question\x00text", answer_text="answer\x00text")

with mock.patch("django.db.models.Model.save") as model_save:
chat_record.save()

self.assertEqual(chat_record.problem_text, "questiontext")
self.assertEqual(chat_record.answer_text, "answertext")
model_save.assert_called_once_with()