@@ -63,7 +63,6 @@ def create(tenant_id,chat_id):
63
63
@manager .route ('/agents/<agent_id>/sessions' , methods = ['POST' ])
64
64
@token_required
65
65
def create_agent_session (tenant_id , agent_id ):
66
- req = request .json
67
66
e , cvs = UserCanvasService .get_by_id (agent_id )
68
67
if not e :
69
68
return get_error_data_result ("Agent not found." )
@@ -77,7 +76,7 @@ def create_agent_session(tenant_id, agent_id):
77
76
conv = {
78
77
"id" : get_uuid (),
79
78
"dialog_id" : cvs .id ,
80
- "user_id" : req . get ( "usr_id" , "" ) if isinstance ( req , dict ) else "" ,
79
+ "user_id" : tenant_id ,
81
80
"message" : [{"role" : "assistant" , "content" : canvas .get_prologue ()}],
82
81
"source" : "agent" ,
83
82
"dsl" :json .loads (cvs .dsl )
@@ -112,13 +111,16 @@ def update(tenant_id,chat_id,session_id):
112
111
@manager .route ('/chats/<chat_id>/completions' , methods = ['POST' ])
113
112
@token_required
114
113
def completion (tenant_id , chat_id ):
114
+ dia = DialogService .query (id = chat_id , tenant_id = tenant_id , status = StatusEnum .VALID .value )
115
+ if not dia :
116
+ return get_error_data_result (message = "You do not own the chat" )
115
117
req = request .json
116
118
if not req .get ("session_id" ):
117
119
conv = {
118
120
"id" : get_uuid (),
119
121
"dialog_id" : chat_id ,
120
122
"name" : req .get ("name" , "New session" ),
121
- "message" : [{"role" : "assistant" , "content" : "Hi! I am your assistant,can I help you?" }]
123
+ "message" : [{"role" : "assistant" , "content" : dia [ 0 ]. prompt_config . get ( "prologue" ) }]
122
124
}
123
125
if not conv .get ("name" ):
124
126
return get_error_data_result (message = "`name` can not be empty." )
@@ -133,8 +135,6 @@ def completion(tenant_id, chat_id):
133
135
if not conv :
134
136
return get_error_data_result (message = "Session does not exist" )
135
137
conv = conv [0 ]
136
- if not DialogService .query (id = chat_id , tenant_id = tenant_id , status = StatusEnum .VALID .value ):
137
- return get_error_data_result (message = "You do not own the chat" )
138
138
msg = []
139
139
question = {
140
140
"content" : req .get ("question" ),
@@ -374,6 +374,8 @@ def sse():
374
374
rename_field (result )
375
375
return get_result (data = result )
376
376
377
+
378
+
377
379
@manager .route ('/chats/<chat_id>/sessions' , methods = ['GET' ])
378
380
@token_required
379
381
def list_session (chat_id ,tenant_id ):
@@ -427,6 +429,60 @@ def list_session(chat_id,tenant_id):
427
429
del conv ["reference" ]
428
430
return get_result (data = convs )
429
431
432
+ @manager .route ('/agents/<agent_id>/sessions' , methods = ['GET' ])
433
+ @token_required
434
+ def list_agent_session (agent_id ,tenant_id ):
435
+ if not UserCanvasService .query (user_id = tenant_id , id = agent_id ):
436
+ return get_error_data_result (message = f"You don't own the agent { agent_id } ." )
437
+ id = request .args .get ("id" )
438
+ if not API4ConversationService .query (id = id ,user_id = tenant_id ):
439
+ return get_error_data_result (f"You don't own the session { id } " )
440
+ page_number = int (request .args .get ("page" , 1 ))
441
+ items_per_page = int (request .args .get ("page_size" , 30 ))
442
+ orderby = request .args .get ("orderby" , "update_time" )
443
+ if request .args .get ("desc" ) == "False" or request .args .get ("desc" ) == "false" :
444
+ desc = False
445
+ else :
446
+ desc = True
447
+ convs = API4ConversationService .get_list (agent_id ,tenant_id ,page_number ,items_per_page ,orderby ,desc ,id )
448
+ if not convs :
449
+ return get_result (data = [])
450
+ for conv in convs :
451
+ conv ['messages' ] = conv .pop ("message" )
452
+ infos = conv ["messages" ]
453
+ for info in infos :
454
+ if "prompt" in info :
455
+ info .pop ("prompt" )
456
+ conv ["agent_id" ] = conv .pop ("dialog_id" )
457
+ if conv ["reference" ]:
458
+ messages = conv ["messages" ]
459
+ message_num = 0
460
+ chunk_num = 0
461
+ while message_num < len (messages ):
462
+ if message_num != 0 and messages [message_num ]["role" ] != "user" :
463
+ chunk_list = []
464
+ if "chunks" in conv ["reference" ][chunk_num ]:
465
+ chunks = conv ["reference" ][chunk_num ]["chunks" ]
466
+ for chunk in chunks :
467
+ new_chunk = {
468
+ "id" : chunk ["chunk_id" ],
469
+ "content" : chunk ["content" ],
470
+ "document_id" : chunk ["doc_id" ],
471
+ "document_name" : chunk ["docnm_kwd" ],
472
+ "dataset_id" : chunk ["kb_id" ],
473
+ "image_id" : chunk .get ("image_id" , "" ),
474
+ "similarity" : chunk ["similarity" ],
475
+ "vector_similarity" : chunk ["vector_similarity" ],
476
+ "term_similarity" : chunk ["term_similarity" ],
477
+ "positions" : chunk ["positions" ],
478
+ }
479
+ chunk_list .append (new_chunk )
480
+ chunk_num += 1
481
+ messages [message_num ]["reference" ] = chunk_list
482
+ message_num += 1
483
+ del conv ["reference" ]
484
+ return get_result (data = convs )
485
+
430
486
431
487
@manager .route ('/chats/<chat_id>/sessions' , methods = ["DELETE" ])
432
488
@token_required
0 commit comments