Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: optimize agent load (#2507)

cthomas3d67f56c5819
diff --git a/letta/orm/agent.py b/letta/orm/agent.py
index fa3e48233..025e01cb9 100644
--- a/letta/orm/agent.py
+++ b/letta/orm/agent.py
@@ -1,3 +1,4 @@
+import asyncio
import uuid
from typing import TYPE_CHECKING, List, Optional, Set
@@ -166,6 +167,8 @@ class Agent(SqlalchemyBase, OrganizationMixin, AsyncAttrs):
"last_updated_by_id": self.last_updated_by_id,
"created_at": self.created_at,
"updated_at": self.updated_at,
+ "enable_sleeptime": self.enable_sleeptime,
+ "response_format": self.response_format,
# optional field defaults
"tags": [],
"tools": [],
@@ -174,8 +177,6 @@ class Agent(SqlalchemyBase, OrganizationMixin, AsyncAttrs):
"identity_ids": [],
"multi_agent_group": None,
"tool_exec_environment_variables": [],
- "enable_sleeptime": None,
- "response_format": self.response_format,
}
# Optional fields: only included if requested
@@ -190,7 +191,6 @@ class Agent(SqlalchemyBase, OrganizationMixin, AsyncAttrs):
"identity_ids": lambda: [i.id for i in self.identities],
"multi_agent_group": lambda: self.multi_agent_group,
"tool_exec_environment_variables": lambda: self.tool_exec_environment_variables,
- "enable_sleeptime": lambda: self.enable_sleeptime,
}
include_relationships = set(optional_fields.keys() if include_relationships is None else include_relationships)
@@ -242,15 +242,7 @@ class Agent(SqlalchemyBase, OrganizationMixin, AsyncAttrs):
"last_updated_by_id": self.last_updated_by_id,
"created_at": self.created_at,
"updated_at": self.updated_at,
- # optional field defaults
- "tags": [],
- "tools": [],
- "sources": [],
- "memory": Memory(blocks=[]),
- "identity_ids": [],
- "multi_agent_group": None,
- "tool_exec_environment_variables": [],
- "enable_sleeptime": None,
+ "enable_sleeptime": self.enable_sleeptime,
"response_format": self.response_format,
}
optional_fields = {
@@ -261,43 +253,57 @@ class Agent(SqlalchemyBase, OrganizationMixin, AsyncAttrs):
"identity_ids": [],
"multi_agent_group": None,
"tool_exec_environment_variables": [],
- "enable_sleeptime": None,
- "response_format": self.response_format,
}
# Initialize include_relationships to an empty set if it's None
include_relationships = set(optional_fields.keys() if include_relationships is None else include_relationships)
- # Only load requested relationships
- if "tags" in include_relationships:
- tags = await self.awaitable_attrs.tags
- state["tags"] = [t.tag for t in tags]
-
- if "tools" in include_relationships:
- state["tools"] = await self.awaitable_attrs.tools
+ async def empty_list_async():
+ return []
- if "sources" in include_relationships:
- sources = await self.awaitable_attrs.sources
- state["sources"] = [s.to_pydantic() for s in sources]
-
- if "memory" in include_relationships:
- memory_blocks = await self.awaitable_attrs.core_memory
- state["memory"] = Memory(
- blocks=[b.to_pydantic() for b in memory_blocks],
- prompt_template=get_prompt_template_for_agent_type(self.agent_type),
- )
+ async def none_async():
+ return None
- if "identity_ids" in include_relationships:
- identities = await self.awaitable_attrs.identities
- state["identity_ids"] = [i.id for i in identities]
-
- if "multi_agent_group" in include_relationships:
- state["multi_agent_group"] = await self.awaitable_attrs.multi_agent_group
-
- if "tool_exec_environment_variables" in include_relationships:
- state["tool_exec_environment_variables"] = await self.awaitable_attrs.tool_exec_environment_variables
-
- if "enable_sleeptime" in include_relationships:
- state["enable_sleeptime"] = await self.awaitable_attrs.enable_sleeptime
+ # Only load requested relationships
+ tags = self.awaitable_attrs.tags if "tags" in include_relationships else empty_list_async()
+ tools = self.awaitable_attrs.tools if "tools" in include_relationships else empty_list_async()
+ sources = self.awaitable_attrs.sources if "sources" in include_relationships else empty_list_async()
+ memory = self.awaitable_attrs.core_memory if "memory" in include_relationships else empty_list_async()
+ identities = self.awaitable_attrs.identities if "identity_ids" in include_relationships else empty_list_async()
+ multi_agent_group = self.awaitable_attrs.multi_agent_group if "multi_agent_group" in include_relationships else none_async()
+ tool_exec_environment_variables = (
+ self.awaitable_attrs.tool_exec_environment_variables
+ if "tool_exec_environment_variables" in include_relationships
+ else empty_list_async()
+ )
+
+ (
+ tags,
+ tools,
+ sources,
+ memory,
+ identities,
+ multi_agent_group,
+ tool_exec_environment_variables,
+ ) = await asyncio.gather(
+ tags,
+ tools,
+ sources,
+ memory,
+ identities,
+ multi_agent_group,
+ tool_exec_environment_variables,
+ )
+
+ state["tags"] = [t.tag for t in tags]
+ state["tools"] = [t.to_pydantic() for t in tools]
+ state["sources"] = [s.to_pydantic() for s in sources]
+ state["memory"] = Memory(
+ blocks=[m.to_pydantic() for m in memory],
+ prompt_template=get_prompt_template_for_agent_type(self.agent_type),
+ )
+ state["identity_ids"] = [i.id for i in identities]
+ state["multi_agent_group"] = multi_agent_group
+ state["tool_exec_environment_variables"] = tool_exec_environment_variables
return self.__pydantic_model__(**state)