Skip to content

Commit 5a49edd

Browse files
committed
feat: User information in the application conversation log.
1 parent 3709d34 commit 5a49edd

File tree

11 files changed

+93
-38
lines changed

11 files changed

+93
-38
lines changed

apps/application/chat_pipeline/step/chat_step/impl/base_chat_step.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ def event_content(response,
124124
request_token = 0
125125
response_token = 0
126126
write_context(step, manage, request_token, response_token, all_text)
127+
asker = manage.context.get('form_data', {}).get('asker', None)
127128
post_response_handler.handler(chat_id, chat_record_id, paragraph_list, problem_text,
128129
all_text, manage, step, padding_problem_text, client_id,
129-
reasoning_content=reasoning_content if reasoning_content_enable else '')
130+
reasoning_content=reasoning_content if reasoning_content_enable else ''
131+
, asker=asker)
130132
yield manage.get_base_to_response().to_stream_chunk_response(chat_id, str(chat_record_id), 'ai-chat-node',
131133
[], '', True,
132134
request_token, response_token,
@@ -137,8 +139,10 @@ def event_content(response,
137139
logging.getLogger("max_kb_error").error(f'{str(e)}:{traceback.format_exc()}')
138140
all_text = 'Exception:' + str(e)
139141
write_context(step, manage, 0, 0, all_text)
142+
asker = manage.context.get('asker', None)
140143
post_response_handler.handler(chat_id, chat_record_id, paragraph_list, problem_text,
141-
all_text, manage, step, padding_problem_text, client_id)
144+
all_text, manage, step, padding_problem_text, client_id, reasoning_content='',
145+
asker=asker)
142146
add_access_num(client_id, client_type, manage.context.get('application_id'))
143147
yield manage.get_base_to_response().to_stream_chunk_response(chat_id, str(chat_record_id), 'ai-chat-node',
144148
[], all_text,
@@ -150,7 +154,6 @@ def event_content(response,
150154
'reasoning_content': ''})
151155

152156

153-
154157
class BaseChatStep(IChatStep):
155158
def execute(self, message_list: List[BaseMessage],
156159
chat_id,
@@ -304,9 +307,11 @@ def execute_block(self, message_list: List[BaseMessage],
304307
else:
305308
reasoning_content = reasoning_result.get('reasoning_content') + reasoning_result_end.get(
306309
'reasoning_content')
310+
asker = manage.context.get('asker', None)
307311
post_response_handler.handler(chat_id, chat_record_id, paragraph_list, problem_text,
308312
content, manage, self, padding_problem_text, client_id,
309-
reasoning_content=reasoning_content if reasoning_content_enable else '')
313+
reasoning_content=reasoning_content if reasoning_content_enable else '',
314+
asker=asker)
310315
add_access_num(client_id, client_type, manage.context.get('application_id'))
311316
return manage.get_base_to_response().to_block_response(str(chat_id), str(chat_record_id),
312317
content, True,
@@ -320,8 +325,10 @@ def execute_block(self, message_list: List[BaseMessage],
320325
except Exception as e:
321326
all_text = 'Exception:' + str(e)
322327
write_context(self, manage, 0, 0, all_text)
328+
asker = manage.context.get('asker', None)
323329
post_response_handler.handler(chat_id, chat_record_id, paragraph_list, problem_text,
324-
all_text, manage, self, padding_problem_text, client_id)
330+
all_text, manage, self, padding_problem_text, client_id, reasoning_content='',
331+
asker=asker)
325332
add_access_num(client_id, client_type, manage.context.get('application_id'))
326333
return manage.get_base_to_response().to_block_response(str(chat_id), str(chat_record_id), all_text, True, 0,
327334
0, _status=status.HTTP_500_INTERNAL_SERVER_ERROR)

apps/application/flow/i_step_node.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def handler(self, chat_id,
8484
answer_text_list=answer_text_list,
8585
run_time=time.time() - workflow.context['start_time'],
8686
index=0)
87-
self.chat_info.append_chat_record(chat_record, self.client_id)
87+
asker = workflow.context.get('asker', None)
88+
self.chat_info.append_chat_record(chat_record, self.client_id, asker)
8889
# 重新设置缓存
8990
chat_cache.set(chat_id,
9091
self.chat_info, timeout=60 * 30)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by Django 4.2.18 on 2025-03-18 06:05
2+
3+
import application.models.application
4+
import common.encoder.encoder
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('application', '0025_alter_application_prologue'),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name='chat',
17+
name='asker',
18+
field=models.JSONField(default=application.models.application.default_asker, encoder=common.encoder.encoder.SystemEncoder, verbose_name='访问者'),
19+
),
20+
]

apps/application/models/application.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,15 @@ class Meta:
115115
db_table = "application_dataset_mapping"
116116

117117

118+
def default_asker():
119+
return {'user_name': '游客'}
120+
121+
118122
class Chat(AppModelMixin):
119123
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid1, editable=False, verbose_name="主键id")
120124
application = models.ForeignKey(Application, on_delete=models.CASCADE)
121125
abstract = models.CharField(max_length=1024, verbose_name="摘要")
126+
asker = models.JSONField(verbose_name="访问者", default=default_asker, encoder=SystemEncoder)
122127
client_id = models.UUIDField(verbose_name="客户端id", default=None, null=True)
123128
is_deleted = models.BooleanField(verbose_name="", default=False)
124129

apps/application/serializers/chat_message_serializers.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ def to_base_pipeline_manage_params(self):
116116
}
117117

118118
def to_pipeline_manage_params(self, problem_text: str, post_response_handler: PostResponseHandler,
119-
exclude_paragraph_id_list, client_id: str, client_type, stream=True):
119+
exclude_paragraph_id_list, client_id: str, client_type, stream=True, form_data=None):
120+
if form_data is None:
121+
form_data = {}
120122
params = self.to_base_pipeline_manage_params()
121123
return {**params, 'problem_text': problem_text, 'post_response_handler': post_response_handler,
122124
'exclude_paragraph_id_list': exclude_paragraph_id_list, 'stream': stream, 'client_id': client_id,
123-
'client_type': client_type}
125+
'client_type': client_type, 'form_data': form_data}
124126

125-
def append_chat_record(self, chat_record: ChatRecord, client_id=None):
127+
def append_chat_record(self, chat_record: ChatRecord, client_id=None, asker=None):
126128
chat_record.problem_text = chat_record.problem_text[0:10240] if chat_record.problem_text is not None else ""
127129
chat_record.answer_text = chat_record.answer_text[0:40960] if chat_record.problem_text is not None else ""
128130
is_save = True
@@ -137,8 +139,17 @@ def append_chat_record(self, chat_record: ChatRecord, client_id=None):
137139
if self.application.id is not None:
138140
# 插入数据库
139141
if not QuerySet(Chat).filter(id=self.chat_id).exists():
142+
asker_dict = {'user_name': '游客'}
143+
if asker is not None:
144+
if isinstance(asker, str):
145+
asker_dict = {
146+
'user_name': asker
147+
}
148+
elif isinstance(asker, dict):
149+
asker_dict = asker
150+
140151
Chat(id=self.chat_id, application_id=self.application.id, abstract=chat_record.problem_text[0:1024],
141-
client_id=client_id, update_time=datetime.now()).save()
152+
client_id=client_id, asker=asker_dict, update_time=datetime.now()).save()
142153
else:
143154
Chat.objects.filter(id=self.chat_id).update(update_time=datetime.now())
144155
# 插入会话记录
@@ -171,7 +182,8 @@ def handler(self,
171182
answer_text_list=answer_list,
172183
run_time=manage.context['run_time'],
173184
index=len(chat_info.chat_record_list) + 1)
174-
chat_info.append_chat_record(chat_record, client_id)
185+
asker = kwargs.get("asker", None)
186+
chat_info.append_chat_record(chat_record, client_id, asker=asker)
175187
# 重新设置缓存
176188
chat_cache.set(chat_id,
177189
chat_info, timeout=60 * 30)
@@ -310,6 +322,7 @@ def chat_simple(self, chat_info: ChatInfo, base_to_response):
310322
stream = self.data.get('stream')
311323
client_id = self.data.get('client_id')
312324
client_type = self.data.get('client_type')
325+
form_data = self.data.get("form_data")
313326
pipeline_manage_builder = PipelineManage.builder()
314327
# 如果开启了问题优化,则添加上问题优化步骤
315328
if chat_info.application.problem_optimization:
@@ -331,7 +344,7 @@ def chat_simple(self, chat_info: ChatInfo, base_to_response):
331344
exclude_paragraph_id_list = list(set(paragraph_id_list))
332345
# 构建运行参数
333346
params = chat_info.to_pipeline_manage_params(message, get_post_handler(chat_info), exclude_paragraph_id_list,
334-
client_id, client_type, stream)
347+
client_id, client_type, stream, form_data)
335348
# 运行流水线作业
336349
pipeline_message.run(params)
337350
return pipeline_message.context['chat_result']

apps/application/sql/list_application_chat.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SELECT
2-
*
2+
*,to_json(asker) as asker
33
FROM
44
application_chat application_chat
55
LEFT JOIN (

ui/src/components/ai-chat/component/user-form/index.vue

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,25 @@ function handleInputFieldList() {
276276
: { title: t('chat.userInput') }
277277
})
278278
}
279+
const getRouteQueryValue = (field: string) => {
280+
let _value = route.query[field]
281+
if (_value != null) {
282+
if (_value instanceof Array) {
283+
_value = _value
284+
.map((item) => {
285+
if (item != null) {
286+
return decodeQuery(item)
287+
}
288+
return null
289+
})
290+
.filter((item) => item != null)
291+
} else {
292+
_value = decodeQuery(_value)
293+
}
294+
return _value
295+
}
296+
return null
297+
}
279298
/**
280299
* 校验参数
281300
*/
@@ -294,27 +313,19 @@ const checkInputParam = () => {
294313
let msg = []
295314
for (let f of apiInputFieldList.value) {
296315
if (!api_form_data_context.value[f.field]) {
297-
let _value = route.query[f.field]
316+
let _value = getRouteQueryValue(f.field)
298317
if (_value != null) {
299-
if (_value instanceof Array) {
300-
_value = _value
301-
.map((item) => {
302-
if (item != null) {
303-
return decodeQuery(item)
304-
}
305-
return null
306-
})
307-
.filter((item) => item != null)
308-
} else {
309-
_value = decodeQuery(_value)
310-
}
311318
api_form_data_context.value[f.field] = _value
312319
}
313320
}
314321
if (f.required && !api_form_data_context.value[f.field]) {
315322
msg.push(f.field)
316323
}
317324
}
325+
if (!api_form_data_context.value['asker']) {
326+
api_form_data_context.value['asker'] = getRouteQueryValue('asker')
327+
}
328+
318329
if (msg.length > 0) {
319330
MsgWarning(
320331
`${t('chat.tip.inputParamMessage1')} ${msg.join('')}${t('chat.tip.inputParamMessage2')}`

ui/src/locales/lang/en-US/views/log.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default {
1313
table: {
1414
abstract: 'Title',
1515
chat_record_count: 'Total Messages',
16+
user: 'User',
1617
feedback: {
1718
label: 'User Feedback',
1819
star: 'Agree',

ui/src/locales/lang/zh-CN/views/log.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default {
1313
table: {
1414
abstract: '摘要',
1515
chat_record_count: '对话提问数',
16+
user: '用户',
1617
feedback: {
1718
label: '用户反馈',
1819
star: '赞同',
@@ -35,6 +36,6 @@ export default {
3536
},
3637
title: {
3738
placeholder: '请给当前内容设置一个标题,以便管理查看'
38-
},
39+
}
3940
}
4041
}

ui/src/locales/lang/zh-Hant/views/log.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default {
1313
table: {
1414
abstract: '摘要',
1515
chat_record_count: '對話提問數',
16+
user: '用戶',
1617
feedback: {
1718
label: '用戶反饋',
1819
star: '贊同',
@@ -35,6 +36,6 @@ export default {
3536
},
3637
title: {
3738
placeholder: '請給當前內容設定一個標題,以便管理查看'
38-
},
39+
}
3940
}
4041
}

0 commit comments

Comments
 (0)