Create API endpoint to clear user's chat history

This commit is contained in:
Debanjum Singh Solanky 2023-11-22 03:13:53 -08:00
parent 5d9d50157e
commit 3096544cf2
2 changed files with 25 additions and 0 deletions

View file

@ -232,6 +232,10 @@ class ConversationAdapters:
return await conversation.afirst()
return await Conversation.objects.acreate(user=user)
@staticmethod
async def adelete_conversation_by_user(user: KhojUser):
return await Conversation.objects.filter(user=user).adelete()
@staticmethod
def has_any_conversation_config(user: KhojUser):
return ChatModelOptions.objects.filter(user=user).exists()

View file

@ -534,6 +534,27 @@ def chat_history(
return {"status": "ok", "response": meta_log.get("chat", [])}
@api.delete("/chat/history")
@requires(["authenticated"])
async def clear_chat_history(
request: Request,
common: CommonQueryParams,
):
user = request.user.object
# Clear Conversation History
await ConversationAdapters.adelete_conversation_by_user(user)
update_telemetry_state(
request=request,
telemetry_type="api",
api="clear_chat_history",
**common.__dict__,
)
return {"status": "ok", "message": "Conversation history cleared"}
@api.get("/chat/options", response_class=Response)
@requires(["authenticated"])
async def chat_options(