mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-23 23:48:56 +01:00
Prefetch related agent when retrieving the conversation for performance improvements
This commit is contained in:
parent
2ab8fb78b1
commit
7426a4f819
1 changed files with 20 additions and 5 deletions
|
@ -681,18 +681,33 @@ class ConversationAdapters:
|
||||||
user: KhojUser, client_application: ClientApplication = None, conversation_id: int = None, title: str = None
|
user: KhojUser, client_application: ClientApplication = None, conversation_id: int = None, title: str = None
|
||||||
) -> Optional[Conversation]:
|
) -> Optional[Conversation]:
|
||||||
if conversation_id:
|
if conversation_id:
|
||||||
return await Conversation.objects.filter(user=user, client=client_application, id=conversation_id).afirst()
|
return (
|
||||||
|
await Conversation.objects.filter(user=user, client=client_application, id=conversation_id)
|
||||||
|
.prefetch_related("agent")
|
||||||
|
.afirst()
|
||||||
|
)
|
||||||
elif title:
|
elif title:
|
||||||
return await Conversation.objects.filter(user=user, client=client_application, title=title).afirst()
|
return (
|
||||||
|
await Conversation.objects.filter(user=user, client=client_application, title=title)
|
||||||
|
.prefetch_related("agent")
|
||||||
|
.afirst()
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
conversation = Conversation.objects.filter(user=user, client=client_application).order_by("-updated_at")
|
conversation = (
|
||||||
|
Conversation.objects.filter(user=user, client=client_application)
|
||||||
|
.prefetch_related("agent")
|
||||||
|
.order_by("-updated_at")
|
||||||
|
)
|
||||||
|
|
||||||
if await conversation.aexists():
|
if await conversation.aexists():
|
||||||
return await conversation.prefetch_related("agent").afirst()
|
return await conversation.prefetch_related("agent").afirst()
|
||||||
|
|
||||||
return await (
|
return await (
|
||||||
Conversation.objects.filter(user=user, client=client_application).order_by("-updated_at").afirst()
|
Conversation.objects.filter(user=user, client=client_application)
|
||||||
) or await Conversation.objects.acreate(user=user, client=client_application)
|
.prefetch_related("agent")
|
||||||
|
.order_by("-updated_at")
|
||||||
|
.afirst()
|
||||||
|
) or await Conversation.objects.prefetch_related("agent").acreate(user=user, client=client_application)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def adelete_conversation_by_user(
|
async def adelete_conversation_by_user(
|
||||||
|
|
Loading…
Reference in a new issue