Skip to content

Commit cbfeef6

Browse files
committed
add delete all for chat sessions
1 parent ff07819 commit cbfeef6

File tree

3 files changed

+207
-2
lines changed

3 files changed

+207
-2
lines changed

api/apps/restful_apis/chat_api.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,17 @@ async def delete_sessions(chat_id):
719719
return get_json_result(data=False, message="No authorization.", code=RetCode.AUTHENTICATION_ERROR)
720720
try:
721721
req = await get_request_json()
722-
session_ids = (req or {}).get("ids", [])
722+
if not req:
723+
return get_json_result(data={})
724+
725+
session_ids = req.get("ids")
723726
if not session_ids:
724-
return get_json_result(data=True)
727+
if req.get("delete_all") is True:
728+
session_ids = [conv.id for conv in ConversationService.query(dialog_id=chat_id)]
729+
if not session_ids:
730+
return get_json_result(data={})
731+
else:
732+
return get_json_result(data={})
725733
unique_ids, duplicate_messages = check_duplicate_ids(session_ids, "session")
726734
errors = []
727735
success_count = 0

docs/references/http_api_reference.md

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3619,6 +3619,202 @@ Failure:
36193619

36203620
---
36213621

3622+
### Get chat assistant's session
3623+
3624+
**GET** `/api/v1/chats/{chat_id}/sessions/{session_id}`
3625+
3626+
Gets a specific session of a specified chat assistant, including its messages, references, and avatar.
3627+
3628+
#### Request
3629+
3630+
- Method: GET
3631+
- URL: `/api/v1/chats/{chat_id}/sessions/{session_id}`
3632+
- Headers:
3633+
- `'Authorization: Bearer <YOUR_API_KEY>'`
3634+
3635+
##### Request example
3636+
3637+
```bash
3638+
curl --request GET \
3639+
--url http://{address}/api/v1/chats/{chat_id}/sessions/{session_id} \
3640+
--header 'Authorization: Bearer <YOUR_API_KEY>'
3641+
```
3642+
3643+
##### Request Parameters
3644+
3645+
- `chat_id`: (*Path parameter*)
3646+
The ID of the associated chat assistant.
3647+
- `session_id`: (*Path parameter*)
3648+
The ID of the session to retrieve.
3649+
3650+
#### Response
3651+
3652+
Success:
3653+
3654+
```json
3655+
{
3656+
"code": 0,
3657+
"data": {
3658+
"chat_id": "2ca4b22e878011ef88fe0242ac120005",
3659+
"id": "4606b4ec87ad11efbc4f0242ac120006",
3660+
"name": "new session",
3661+
"avatar": "data:image/png;base64,...",
3662+
"messages": [
3663+
{
3664+
"content": "Hi! I am your assistant, can I help you?",
3665+
"role": "assistant"
3666+
}
3667+
],
3668+
"reference": []
3669+
}
3670+
}
3671+
```
3672+
3673+
Failure:
3674+
3675+
```json
3676+
{
3677+
"code": 102,
3678+
"message": "Session not found!"
3679+
}
3680+
```
3681+
3682+
---
3683+
3684+
### Delete a message from a chat assistant's session
3685+
3686+
**DELETE** `/api/v1/chats/{chat_id}/sessions/{session_id}/messages/{msg_id}`
3687+
3688+
Deletes a user message and its paired assistant reply from a specified chat assistant session.
3689+
3690+
#### Request
3691+
3692+
- Method: DELETE
3693+
- URL: `/api/v1/chats/{chat_id}/sessions/{session_id}/messages/{msg_id}`
3694+
- Headers:
3695+
- `'Authorization: Bearer <YOUR_API_KEY>'`
3696+
3697+
##### Request example
3698+
3699+
```bash
3700+
curl --request DELETE \
3701+
--url http://{address}/api/v1/chats/{chat_id}/sessions/{session_id}/messages/{msg_id} \
3702+
--header 'Authorization: Bearer <YOUR_API_KEY>'
3703+
```
3704+
3705+
##### Request Parameters
3706+
3707+
- `chat_id`: (*Path parameter*)
3708+
The ID of the associated chat assistant.
3709+
- `session_id`: (*Path parameter*)
3710+
The ID of the session that owns the message.
3711+
- `msg_id`: (*Path parameter*)
3712+
The ID of the message to delete.
3713+
3714+
#### Response
3715+
3716+
Success: returns the updated session object.
3717+
3718+
```json
3719+
{
3720+
"code": 0,
3721+
"data": {
3722+
"chat_id": "2ca4b22e878011ef88fe0242ac120005",
3723+
"id": "4606b4ec87ad11efbc4f0242ac120006",
3724+
"messages": [],
3725+
"reference": []
3726+
}
3727+
}
3728+
```
3729+
3730+
Failure:
3731+
3732+
```json
3733+
{
3734+
"code": 102,
3735+
"message": "Session not found!"
3736+
}
3737+
```
3738+
3739+
---
3740+
3741+
### Update message feedback in a chat assistant's session
3742+
3743+
**PUT** `/api/v1/chats/{chat_id}/sessions/{session_id}/messages/{msg_id}/feedback`
3744+
3745+
Updates feedback for an assistant message in a specified chat assistant session.
3746+
3747+
#### Request
3748+
3749+
- Method: PUT
3750+
- URL: `/api/v1/chats/{chat_id}/sessions/{session_id}/messages/{msg_id}/feedback`
3751+
- Headers:
3752+
- `'Content-Type: application/json'`
3753+
- `'Authorization: Bearer <YOUR_API_KEY>'`
3754+
- Body:
3755+
- `"thumbup"`: `boolean`
3756+
- `"feedback"`: `string` (optional)
3757+
3758+
##### Request example
3759+
3760+
```bash
3761+
curl --request PUT \
3762+
--url http://{address}/api/v1/chats/{chat_id}/sessions/{session_id}/messages/{msg_id}/feedback \
3763+
--header 'Content-Type: application/json' \
3764+
--header 'Authorization: Bearer <YOUR_API_KEY>' \
3765+
--data '{
3766+
"thumbup": false,
3767+
"feedback": "The answer missed the cited document."
3768+
}'
3769+
```
3770+
3771+
##### Request Parameters
3772+
3773+
- `chat_id`: (*Path parameter*)
3774+
The ID of the associated chat assistant.
3775+
- `session_id`: (*Path parameter*)
3776+
The ID of the session that owns the message.
3777+
- `msg_id`: (*Path parameter*)
3778+
The ID of the assistant message to update.
3779+
- `"thumbup"`: (*Body parameter*), `boolean`
3780+
Whether the assistant message is marked as positive feedback.
3781+
- `"feedback"`: (*Body parameter*), `string`
3782+
Optional feedback text, typically used when `"thumbup"` is `false`.
3783+
3784+
#### Response
3785+
3786+
Success: returns the updated session object.
3787+
3788+
```json
3789+
{
3790+
"code": 0,
3791+
"data": {
3792+
"chat_id": "2ca4b22e878011ef88fe0242ac120005",
3793+
"id": "4606b4ec87ad11efbc4f0242ac120006",
3794+
"messages": [
3795+
{
3796+
"id": "message-id",
3797+
"role": "assistant",
3798+
"content": "Here is the answer.",
3799+
"thumbup": false,
3800+
"feedback": "The answer missed the cited document."
3801+
}
3802+
]
3803+
}
3804+
}
3805+
```
3806+
3807+
Failure:
3808+
3809+
```json
3810+
{
3811+
"code": 102,
3812+
"message": "Session not found!"
3813+
}
3814+
```
3815+
3816+
---
3817+
36223818
### Delete chat assistant's sessions
36233819

36243820
**DELETE** `/api/v1/chats/{chat_id}/sessions`

test/testcases/test_http_api/test_session_management/test_delete_sessions_with_chat_assistant.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def test_delete_1k(self, HttpApiAuth, add_chat_assistants):
146146
pytest.param("not json", 100, """AttributeError("\'str\' object has no attribute \'get\'")""", 5, marks=pytest.mark.skip),
147147
pytest.param(lambda r: {"ids": r[:1]}, 0, "", 4, marks=pytest.mark.p3),
148148
pytest.param(lambda r: {"ids": r}, 0, "", 0, marks=pytest.mark.p1),
149+
pytest.param({"delete_all": True}, 0, "", 0, marks=pytest.mark.p1),
149150
pytest.param({"ids": []}, 0, "", 5, marks=pytest.mark.p3),
150151
],
151152
)

0 commit comments

Comments
 (0)