Skip to content
Merged
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
37 changes: 21 additions & 16 deletions apps/application/serializers/application_chat_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import Dict

import uuid_utils.compat as uuid
from application.models import Application, ApplicationAccessToken, ChatRecord
from application.models import Application, ApplicationAccessToken, ChatRecord, Chat
from application.serializers.application_chat import ChatCountSerializer
from application.serializers.common import ChatInfo
from common.auth.authentication import get_is_permissions
Expand Down Expand Up @@ -58,7 +58,7 @@ class Meta:
"update_time",
"version",
"question",
"messages"
"messages",
]


Expand All @@ -76,9 +76,12 @@ def is_valid(self, *, debug=False, raise_exception=False):
query_set = query_set.filter(workspace_id=workspace_id)
if not query_set.exists():
raise AppApiException(500, _("Application id does not exist"))
if not ChatRecord.objects.filter(
chat_id=self.data.get("chat_id"), chat__application_id=self.data.get("application_id")
).exists() and not debug:
if (
not ChatRecord.objects.filter(
chat_id=self.data.get("chat_id"), chat__application_id=self.data.get("application_id")
).exists()
and not debug
):
raise AppApiException(500, _("Chat records for the application do not exist"))
application_access_token = (
QuerySet(ApplicationAccessToken).filter(application_id=self.data.get("application_id")).first()
Expand All @@ -96,9 +99,11 @@ def get_chat_record(self):
]
if chat_record_list is not None and len(chat_record_list):
return chat_record_list[-1]
return QuerySet(ChatRecord).filter(
id=chat_record_id, chat_id=chat_id, chat__application_id=self.data.get("application_id")
).first()
return (
QuerySet(ChatRecord)
.filter(id=chat_record_id, chat_id=chat_id, chat__application_id=self.data.get("application_id"))
.first()
)

def one(self, debug):
self.is_valid(debug=debug, raise_exception=True)
Expand Down Expand Up @@ -132,8 +137,8 @@ def is_valid(self, *, raise_exception=False):
query_set = query_set.filter(workspace_id=workspace_id)
if not query_set.exists():
raise AppApiException(500, _("Application id does not exist"))
if not ChatRecord.objects.filter(
chat_id=self.data.get("chat_id"), chat__application_id=self.data.get("application_id")
if not Chat.objects.filter(
id=self.data.get("chat_id"), application_id=self.data.get("application_id")
).exists():
raise AppApiException(500, _("Chat records for the application do not exist"))

Expand All @@ -143,9 +148,9 @@ def list(self, with_valid=True):
order_by = "create_time" if self.data.get("order_asc") is None or self.data.get("order_asc") else "-create_time"
return [
ChatRecordSerializerModel(chat_record).data
for chat_record in QuerySet(ChatRecord).filter(
chat_id=self.data.get("chat_id"), chat__application_id=self.data.get("application_id")
).order_by(order_by)
for chat_record in QuerySet(ChatRecord)
.filter(chat_id=self.data.get("chat_id"), chat__application_id=self.data.get("application_id"))
.order_by(order_by)
]

@staticmethod
Expand Down Expand Up @@ -240,9 +245,9 @@ def page(self, current_page: int, page_size: int, with_valid=True, show_source=N
page = page_search(
current_page,
page_size,
QuerySet(ChatRecord).filter(
chat_id=self.data.get("chat_id"), chat__application_id=self.data.get("application_id")
).order_by(order_by),
QuerySet(ChatRecord)
.filter(chat_id=self.data.get("chat_id"), chat__application_id=self.data.get("application_id"))
.order_by(order_by),
post_records_handler=lambda chat_record: self.reset_chat_record(chat_record, show_source, show_exec),
)
return page
Expand Down
109 changes: 54 additions & 55 deletions apps/application/serializers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,61 +400,60 @@ def append_chat_record(self, chat_record: ChatRecord):
break
if is_save:
self.chat_record_list.append(chat_record)
if not self.debug:
if not QuerySet(Chat).filter(id=self.chat_id).exists():
Chat(
id=self.chat_id,
application_id=self.application_id,
abstract=chat_record.problem_text[0:1024],
chat_user_id=self.chat_user_id,
chat_user_type=self.chat_user_type,
ip_address=self.ip_address,
source=self.source,
asker=self.get_chat_user(),
).save()
else:
QuerySet(Chat).filter(id=self.chat_id).update(update_time=timezone.now())
# 记录Token消耗
total_tokens = (chat_record.message_tokens or 0) + (chat_record.answer_tokens or 0)
if total_tokens > 0:
ChatUserTokenQuota.consume(self.chat_user_id, total_tokens)
# 插入会话记录
QuerySet(ChatRecord).update_or_create(
id=chat_record.id,
create_defaults={
"id": chat_record.id,
"chat_id": chat_record.chat_id,
"vote_status": chat_record.vote_status,
"problem_text": chat_record.problem_text,
"answer_text": chat_record.answer_text,
"answer_text_list": chat_record.answer_text_list,
"message_tokens": chat_record.message_tokens,
"answer_tokens": chat_record.answer_tokens,
"const": chat_record.const,
"details": chat_record.details,
"improve_paragraph_id_list": chat_record.improve_paragraph_id_list,
"run_time": chat_record.run_time,
"source": chat_record.source,
"ip_address": chat_record.ip_address or "",
"index": chat_record.index,
},
defaults={
"vote_status": chat_record.vote_status,
"problem_text": chat_record.problem_text,
"answer_text": chat_record.answer_text,
"answer_text_list": chat_record.answer_text_list,
"message_tokens": chat_record.message_tokens,
"answer_tokens": chat_record.answer_tokens,
"const": chat_record.const,
"details": chat_record.details,
"improve_paragraph_id_list": chat_record.improve_paragraph_id_list,
"run_time": chat_record.run_time,
"index": chat_record.index,
"source": chat_record.source,
"ip_address": chat_record.ip_address or "",
},
)
ChatCountSerializer(data={"chat_id": self.chat_id}).update_chat()
if not QuerySet(Chat).filter(id=self.chat_id).exists():
Chat(
id=self.chat_id,
application_id=self.application_id,
abstract=chat_record.problem_text[0:1024],
chat_user_id=self.chat_user_id,
chat_user_type=self.chat_user_type,
ip_address=self.ip_address,
source=self.source,
asker=self.get_chat_user(),
).save()
else:
QuerySet(Chat).filter(id=self.chat_id).update(update_time=timezone.now())
# 记录Token消耗
total_tokens = (chat_record.message_tokens or 0) + (chat_record.answer_tokens or 0)
if total_tokens > 0:
ChatUserTokenQuota.consume(self.chat_user_id, total_tokens)
# 插入会话记录
QuerySet(ChatRecord).update_or_create(
id=chat_record.id,
create_defaults={
"id": chat_record.id,
"chat_id": chat_record.chat_id,
"vote_status": chat_record.vote_status,
"problem_text": chat_record.problem_text,
"answer_text": chat_record.answer_text,
"answer_text_list": chat_record.answer_text_list,
"message_tokens": chat_record.message_tokens,
"answer_tokens": chat_record.answer_tokens,
"const": chat_record.const,
"details": chat_record.details,
"improve_paragraph_id_list": chat_record.improve_paragraph_id_list,
"run_time": chat_record.run_time,
"source": chat_record.source,
"ip_address": chat_record.ip_address or "",
"index": chat_record.index,
},
defaults={
"vote_status": chat_record.vote_status,
"problem_text": chat_record.problem_text,
"answer_text": chat_record.answer_text,
"answer_text_list": chat_record.answer_text_list,
"message_tokens": chat_record.message_tokens,
"answer_tokens": chat_record.answer_tokens,
"const": chat_record.const,
"details": chat_record.details,
"improve_paragraph_id_list": chat_record.improve_paragraph_id_list,
"run_time": chat_record.run_time,
"index": chat_record.index,
"source": chat_record.source,
"ip_address": chat_record.ip_address or "",
},
)
ChatCountSerializer(data={"chat_id": self.chat_id}).update_chat()

def to_dict(self):

Expand Down
Loading
Loading