letta-nightly 0.6.4.dev20241216104246__py3-none-any.whl → 0.6.5.dev20241218055539__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of letta-nightly might be problematic. Click here for more details.

Files changed (41) hide show
  1. letta/__init__.py +1 -1
  2. letta/agent.py +95 -101
  3. letta/client/client.py +1 -0
  4. letta/constants.py +6 -1
  5. letta/embeddings.py +3 -9
  6. letta/functions/function_sets/base.py +11 -57
  7. letta/functions/schema_generator.py +2 -6
  8. letta/llm_api/anthropic.py +38 -13
  9. letta/llm_api/llm_api_tools.py +12 -1
  10. letta/local_llm/function_parser.py +2 -2
  11. letta/orm/__init__.py +1 -1
  12. letta/orm/agent.py +19 -1
  13. letta/orm/errors.py +8 -0
  14. letta/orm/file.py +3 -2
  15. letta/orm/mixins.py +3 -14
  16. letta/orm/organization.py +19 -3
  17. letta/orm/passage.py +59 -23
  18. letta/orm/source.py +4 -0
  19. letta/orm/sqlalchemy_base.py +25 -18
  20. letta/prompts/system/memgpt_modified_chat.txt +1 -1
  21. letta/prompts/system/memgpt_modified_o1.txt +1 -1
  22. letta/providers.py +2 -0
  23. letta/schemas/agent.py +35 -0
  24. letta/schemas/embedding_config.py +20 -2
  25. letta/schemas/passage.py +1 -1
  26. letta/schemas/sandbox_config.py +2 -1
  27. letta/server/rest_api/app.py +43 -5
  28. letta/server/rest_api/routers/v1/tools.py +1 -1
  29. letta/server/rest_api/utils.py +24 -5
  30. letta/server/server.py +105 -164
  31. letta/server/ws_api/server.py +1 -1
  32. letta/services/agent_manager.py +344 -9
  33. letta/services/passage_manager.py +76 -100
  34. letta/services/tool_execution_sandbox.py +54 -45
  35. letta/settings.py +10 -5
  36. letta/utils.py +8 -0
  37. {letta_nightly-0.6.4.dev20241216104246.dist-info → letta_nightly-0.6.5.dev20241218055539.dist-info}/METADATA +6 -6
  38. {letta_nightly-0.6.4.dev20241216104246.dist-info → letta_nightly-0.6.5.dev20241218055539.dist-info}/RECORD +41 -41
  39. {letta_nightly-0.6.4.dev20241216104246.dist-info → letta_nightly-0.6.5.dev20241218055539.dist-info}/LICENSE +0 -0
  40. {letta_nightly-0.6.4.dev20241216104246.dist-info → letta_nightly-0.6.5.dev20241218055539.dist-info}/WHEEL +0 -0
  41. {letta_nightly-0.6.4.dev20241216104246.dist-info → letta_nightly-0.6.5.dev20241218055539.dist-info}/entry_points.txt +0 -0
letta/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = "0.6.4"
1
+ __version__ = "0.6.5"
2
2
 
3
3
  # import clients
4
4
  from letta.client.client import LocalClient, RESTClient, create_client
letta/agent.py CHANGED
@@ -18,6 +18,7 @@ from letta.constants import (
18
18
  MESSAGE_SUMMARY_WARNING_FRAC,
19
19
  O1_BASE_TOOLS,
20
20
  REQ_HEARTBEAT_MESSAGE,
21
+ STRUCTURED_OUTPUT_MODELS,
21
22
  )
22
23
  from letta.errors import LLMError
23
24
  from letta.helpers import ToolRulesSolver
@@ -41,7 +42,6 @@ from letta.schemas.openai.chat_completion_response import (
41
42
  Message as ChatCompletionMessage,
42
43
  )
43
44
  from letta.schemas.openai.chat_completion_response import UsageStatistics
44
- from letta.schemas.passage import Passage
45
45
  from letta.schemas.tool import Tool
46
46
  from letta.schemas.tool_rule import TerminalToolRule
47
47
  from letta.schemas.usage import LettaUsageStatistics
@@ -64,6 +64,7 @@ from letta.system import (
64
64
  )
65
65
  from letta.utils import (
66
66
  count_tokens,
67
+ get_friendly_error_msg,
67
68
  get_local_time,
68
69
  get_tool_call_id,
69
70
  get_utc_time,
@@ -82,7 +83,7 @@ def compile_memory_metadata_block(
82
83
  actor: PydanticUser,
83
84
  agent_id: str,
84
85
  memory_edit_timestamp: datetime.datetime,
85
- passage_manager: Optional[PassageManager] = None,
86
+ agent_manager: Optional[AgentManager] = None,
86
87
  message_manager: Optional[MessageManager] = None,
87
88
  ) -> str:
88
89
  # Put the timestamp in the local timezone (mimicking get_local_time())
@@ -93,7 +94,7 @@ def compile_memory_metadata_block(
93
94
  [
94
95
  f"### Memory [last modified: {timestamp_str}]",
95
96
  f"{message_manager.size(actor=actor, agent_id=agent_id) if message_manager else 0} previous messages between you and the user are stored in recall memory (use functions to access them)",
96
- f"{passage_manager.size(actor=actor, agent_id=agent_id) if passage_manager else 0} total memories you created are stored in archival memory (use functions to access them)",
97
+ f"{agent_manager.passage_size(actor=actor, agent_id=agent_id) if agent_manager else 0} total memories you created are stored in archival memory (use functions to access them)",
97
98
  "\nCore memory shown below (limited in size, additional information stored in archival / recall memory):",
98
99
  ]
99
100
  )
@@ -106,7 +107,7 @@ def compile_system_message(
106
107
  in_context_memory: Memory,
107
108
  in_context_memory_last_edit: datetime.datetime, # TODO move this inside of BaseMemory?
108
109
  actor: PydanticUser,
109
- passage_manager: Optional[PassageManager] = None,
110
+ agent_manager: Optional[AgentManager] = None,
110
111
  message_manager: Optional[MessageManager] = None,
111
112
  user_defined_variables: Optional[dict] = None,
112
113
  append_icm_if_missing: bool = True,
@@ -135,7 +136,7 @@ def compile_system_message(
135
136
  actor=actor,
136
137
  agent_id=agent_id,
137
138
  memory_edit_timestamp=in_context_memory_last_edit,
138
- passage_manager=passage_manager,
139
+ agent_manager=agent_manager,
139
140
  message_manager=message_manager,
140
141
  )
141
142
  full_memory_string = memory_metadata_string + "\n" + in_context_memory.compile()
@@ -172,7 +173,7 @@ def initialize_message_sequence(
172
173
  agent_id: str,
173
174
  memory: Memory,
174
175
  actor: PydanticUser,
175
- passage_manager: Optional[PassageManager] = None,
176
+ agent_manager: Optional[AgentManager] = None,
176
177
  message_manager: Optional[MessageManager] = None,
177
178
  memory_edit_timestamp: Optional[datetime.datetime] = None,
178
179
  include_initial_boot_message: bool = True,
@@ -181,7 +182,7 @@ def initialize_message_sequence(
181
182
  memory_edit_timestamp = get_local_time()
182
183
 
183
184
  # full_system_message = construct_system_with_memory(
184
- # system, memory, memory_edit_timestamp, passage_manager=passage_manager, recall_memory=recall_memory
185
+ # system, memory, memory_edit_timestamp, agent_manager=agent_manager, recall_memory=recall_memory
185
186
  # )
186
187
  full_system_message = compile_system_message(
187
188
  agent_id=agent_id,
@@ -189,7 +190,7 @@ def initialize_message_sequence(
189
190
  in_context_memory=memory,
190
191
  in_context_memory_last_edit=memory_edit_timestamp,
191
192
  actor=actor,
192
- passage_manager=passage_manager,
193
+ agent_manager=agent_manager,
193
194
  message_manager=message_manager,
194
195
  user_defined_variables=None,
195
196
  append_icm_if_missing=True,
@@ -259,9 +260,6 @@ class Agent(BaseAgent):
259
260
 
260
261
  self.user = user
261
262
 
262
- # link tools
263
- self.link_tools(agent_state.tools)
264
-
265
263
  # initialize a tool rules solver
266
264
  if agent_state.tool_rules:
267
265
  # if there are tool rules, print out a warning
@@ -277,6 +275,7 @@ class Agent(BaseAgent):
277
275
 
278
276
  # gpt-4, gpt-3.5-turbo, ...
279
277
  self.model = self.agent_state.llm_config.model
278
+ self.check_tool_rules()
280
279
 
281
280
  # state managers
282
281
  self.block_manager = BlockManager()
@@ -291,12 +290,11 @@ class Agent(BaseAgent):
291
290
  self.interface = interface
292
291
 
293
292
  # Create the persistence manager object based on the AgentState info
294
- self.passage_manager = PassageManager()
295
293
  self.message_manager = MessageManager()
294
+ self.passage_manager = PassageManager()
295
+ self.agent_manager = AgentManager()
296
296
 
297
297
  # State needed for heartbeat pausing
298
- self.pause_heartbeats_start = None
299
- self.pause_heartbeats_minutes = 0
300
298
 
301
299
  self.first_message_verify_mono = first_message_verify_mono
302
300
 
@@ -322,7 +320,7 @@ class Agent(BaseAgent):
322
320
  agent_id=self.agent_state.id,
323
321
  memory=self.agent_state.memory,
324
322
  actor=self.user,
325
- passage_manager=None,
323
+ agent_manager=None,
326
324
  message_manager=None,
327
325
  memory_edit_timestamp=get_utc_time(),
328
326
  include_initial_boot_message=True,
@@ -347,7 +345,7 @@ class Agent(BaseAgent):
347
345
  memory=self.agent_state.memory,
348
346
  agent_id=self.agent_state.id,
349
347
  actor=self.user,
350
- passage_manager=None,
348
+ agent_manager=None,
351
349
  message_manager=None,
352
350
  memory_edit_timestamp=get_utc_time(),
353
351
  include_initial_boot_message=True,
@@ -381,6 +379,16 @@ class Agent(BaseAgent):
381
379
  # Create the agent in the DB
382
380
  self.update_state()
383
381
 
382
+ def check_tool_rules(self):
383
+ if self.model not in STRUCTURED_OUTPUT_MODELS:
384
+ if len(self.tool_rules_solver.init_tool_rules) > 1:
385
+ raise ValueError(
386
+ "Multiple initial tools are not supported for non-structured models. Please use only one initial tool rule."
387
+ )
388
+ self.supports_structured_output = False
389
+ else:
390
+ self.supports_structured_output = True
391
+
384
392
  def update_memory_if_change(self, new_memory: Memory) -> bool:
385
393
  """
386
394
  Update internal memory object and system prompt if there have been modifications.
@@ -415,11 +423,21 @@ class Agent(BaseAgent):
415
423
  return True
416
424
  return False
417
425
 
418
- def execute_tool_and_persist_state(self, function_name, function_to_call, function_args):
426
+ def execute_tool_and_persist_state(self, function_name: str, function_args: dict, target_letta_tool: Tool):
419
427
  """
420
428
  Execute tool modifications and persist the state of the agent.
421
429
  Note: only some agent state modifications will be persisted, such as data in the AgentState ORM and block data
422
430
  """
431
+ # TODO: Get rid of this. This whole piece is pretty shady, that we exec the function to just get the type hints for args.
432
+ env = {}
433
+ env.update(globals())
434
+ exec(target_letta_tool.source_code, env)
435
+ callable_func = env[target_letta_tool.json_schema["name"]]
436
+ spec = inspect.getfullargspec(callable_func).annotations
437
+ for name, arg in function_args.items():
438
+ if isinstance(function_args[name], dict):
439
+ function_args[name] = spec[name](**function_args[name])
440
+
423
441
  # TODO: add agent manager here
424
442
  orig_memory_str = self.agent_state.memory.compile()
425
443
 
@@ -432,11 +450,11 @@ class Agent(BaseAgent):
432
450
  if function_name in BASE_TOOLS or function_name in O1_BASE_TOOLS:
433
451
  # base tools are allowed to access the `Agent` object and run on the database
434
452
  function_args["self"] = self # need to attach self to arg since it's dynamically linked
435
- function_response = function_to_call(**function_args)
453
+ function_response = callable_func(**function_args)
436
454
  else:
437
455
  # execute tool in a sandbox
438
456
  # TODO: allow agent_state to specify which sandbox to execute tools in
439
- sandbox_run_result = ToolExecutionSandbox(function_name, function_args, self.agent_state.created_by_id).run(
457
+ sandbox_run_result = ToolExecutionSandbox(function_name, function_args, self.user).run(
440
458
  agent_state=self.agent_state.__deepcopy__()
441
459
  )
442
460
  function_response, updated_agent_state = sandbox_run_result.func_return, sandbox_run_result.agent_state
@@ -446,12 +464,9 @@ class Agent(BaseAgent):
446
464
  except Exception as e:
447
465
  # Need to catch error here, or else trunction wont happen
448
466
  # TODO: modify to function execution error
449
- from letta.constants import MAX_ERROR_MESSAGE_CHAR_LIMIT
450
-
451
- error_msg = f"Error executing tool {function_name}: {e}"
452
- if len(error_msg) > MAX_ERROR_MESSAGE_CHAR_LIMIT:
453
- error_msg = error_msg[:MAX_ERROR_MESSAGE_CHAR_LIMIT]
454
- raise ValueError(error_msg)
467
+ function_response = get_friendly_error_msg(
468
+ function_name=function_name, exception_name=type(e).__name__, exception_message=str(e)
469
+ )
455
470
 
456
471
  return function_response
457
472
 
@@ -464,27 +479,6 @@ class Agent(BaseAgent):
464
479
  def messages(self, value):
465
480
  raise Exception("Modifying message list directly not allowed")
466
481
 
467
- def link_tools(self, tools: List[Tool]):
468
- """Bind a tool object (schema + python function) to the agent object"""
469
-
470
- # Store the functions schemas (this is passed as an argument to ChatCompletion)
471
- self.functions = []
472
- self.functions_python = {}
473
- env = {}
474
- env.update(globals())
475
- for tool in tools:
476
- try:
477
- # WARNING: name may not be consistent?
478
- # if tool.module: # execute the whole module
479
- # exec(tool.module, env)
480
- # else:
481
- exec(tool.source_code, env)
482
- self.functions_python[tool.json_schema["name"]] = env[tool.json_schema["name"]]
483
- self.functions.append(tool.json_schema)
484
- except Exception:
485
- warnings.warn(f"WARNING: tool {tool.name} failed to link")
486
- assert all([callable(f) for k, f in self.functions_python.items()]), self.functions_python
487
-
488
482
  def _load_messages_from_recall(self, message_ids: List[str]) -> List[Message]:
489
483
  """Load a list of messages from recall storage"""
490
484
 
@@ -588,14 +582,32 @@ class Agent(BaseAgent):
588
582
  empty_response_retry_limit: int = 3,
589
583
  backoff_factor: float = 0.5, # delay multiplier for exponential backoff
590
584
  max_delay: float = 10.0, # max delay between retries
585
+ step_count: Optional[int] = None,
591
586
  ) -> ChatCompletionResponse:
592
587
  """Get response from LLM API with robust retry mechanism."""
593
588
 
594
589
  allowed_tool_names = self.tool_rules_solver.get_allowed_tool_names()
590
+ agent_state_tool_jsons = [t.json_schema for t in self.agent_state.tools]
591
+
595
592
  allowed_functions = (
596
- self.functions if not allowed_tool_names else [func for func in self.functions if func["name"] in allowed_tool_names]
593
+ agent_state_tool_jsons
594
+ if not allowed_tool_names
595
+ else [func for func in agent_state_tool_jsons if func["name"] in allowed_tool_names]
597
596
  )
598
597
 
598
+ # For the first message, force the initial tool if one is specified
599
+ force_tool_call = None
600
+ if (
601
+ step_count is not None
602
+ and step_count == 0
603
+ and not self.supports_structured_output
604
+ and len(self.tool_rules_solver.init_tool_rules) > 0
605
+ ):
606
+ force_tool_call = self.tool_rules_solver.init_tool_rules[0].tool_name
607
+ # Force a tool call if exactly one tool is specified
608
+ elif step_count is not None and step_count > 0 and len(allowed_tool_names) == 1:
609
+ force_tool_call = allowed_tool_names[0]
610
+
599
611
  for attempt in range(1, empty_response_retry_limit + 1):
600
612
  try:
601
613
  response = create(
@@ -603,9 +615,10 @@ class Agent(BaseAgent):
603
615
  messages=message_sequence,
604
616
  user_id=self.agent_state.created_by_id,
605
617
  functions=allowed_functions,
606
- functions_python=self.functions_python,
618
+ # functions_python=self.functions_python, do we need this?
607
619
  function_call=function_call,
608
620
  first_message=first_message,
621
+ force_tool_call=force_tool_call,
609
622
  stream=stream,
610
623
  stream_interface=self.interface,
611
624
  )
@@ -711,10 +724,13 @@ class Agent(BaseAgent):
711
724
  function_name = function_call.name
712
725
  printd(f"Request to call function {function_name} with tool_call_id: {tool_call_id}")
713
726
 
714
- # Failure case 1: function name is wrong
715
- try:
716
- function_to_call = self.functions_python[function_name]
717
- except KeyError:
727
+ # Failure case 1: function name is wrong (not in agent_state.tools)
728
+ target_letta_tool = None
729
+ for t in self.agent_state.tools:
730
+ if t.name == function_name:
731
+ target_letta_tool = t
732
+
733
+ if not target_letta_tool:
718
734
  error_msg = f"No function named {function_name}"
719
735
  function_response = package_function_response(False, error_msg)
720
736
  messages.append(
@@ -782,14 +798,8 @@ class Agent(BaseAgent):
782
798
  # this is because the function/tool role message is only created once the function/tool has executed/returned
783
799
  self.interface.function_message(f"Running {function_name}({function_args})", msg_obj=messages[-1])
784
800
  try:
785
- spec = inspect.getfullargspec(function_to_call).annotations
786
-
787
- for name, arg in function_args.items():
788
- if isinstance(function_args[name], dict):
789
- function_args[name] = spec[name](**function_args[name])
790
-
791
801
  # handle tool execution (sandbox) and state updates
792
- function_response = self.execute_tool_and_persist_state(function_name, function_to_call, function_args)
802
+ function_response = self.execute_tool_and_persist_state(function_name, function_args, target_letta_tool)
793
803
 
794
804
  # handle trunction
795
805
  if function_name in ["conversation_search", "conversation_search_date", "archival_memory_search"]:
@@ -801,8 +811,7 @@ class Agent(BaseAgent):
801
811
  truncate = True
802
812
 
803
813
  # get the function response limit
804
- tool_obj = [tool for tool in self.agent_state.tools if tool.name == function_name][0]
805
- return_char_limit = tool_obj.return_char_limit
814
+ return_char_limit = target_letta_tool.return_char_limit
806
815
  function_response_string = validate_function_response(
807
816
  function_response, return_char_limit=return_char_limit, truncate=truncate
808
817
  )
@@ -897,6 +906,7 @@ class Agent(BaseAgent):
897
906
  step_count = 0
898
907
  while True:
899
908
  kwargs["first_message"] = False
909
+ kwargs["step_count"] = step_count
900
910
  step_response = self.inner_step(
901
911
  messages=next_input_message,
902
912
  **kwargs,
@@ -972,6 +982,7 @@ class Agent(BaseAgent):
972
982
  first_message_retry_limit: int = FIRST_MESSAGE_ATTEMPTS,
973
983
  skip_verify: bool = False,
974
984
  stream: bool = False, # TODO move to config?
985
+ step_count: Optional[int] = None,
975
986
  ) -> AgentStepResponse:
976
987
  """Runs a single step in the agent loop (generates at most one LLM call)"""
977
988
 
@@ -1014,7 +1025,9 @@ class Agent(BaseAgent):
1014
1025
  else:
1015
1026
  response = self._get_ai_reply(
1016
1027
  message_sequence=input_message_sequence,
1028
+ first_message=first_message,
1017
1029
  stream=stream,
1030
+ step_count=step_count,
1018
1031
  )
1019
1032
 
1020
1033
  # Step 3: check if LLM wanted to call a function
@@ -1235,17 +1248,6 @@ class Agent(BaseAgent):
1235
1248
 
1236
1249
  printd(f"Ran summarizer, messages length {prior_len} -> {len(self.messages)}")
1237
1250
 
1238
- def heartbeat_is_paused(self):
1239
- """Check if there's a requested pause on timed heartbeats"""
1240
-
1241
- # Check if the pause has been initiated
1242
- if self.pause_heartbeats_start is None:
1243
- return False
1244
-
1245
- # Check if it's been more than pause_heartbeats_minutes since pause_heartbeats_start
1246
- elapsed_time = get_utc_time() - self.pause_heartbeats_start
1247
- return elapsed_time.total_seconds() < self.pause_heartbeats_minutes * 60
1248
-
1249
1251
  def _swap_system_message_in_buffer(self, new_system_message: str):
1250
1252
  """Update the system message (NOT prompt) of the Agent (requires updating the internal buffer)"""
1251
1253
  assert isinstance(new_system_message, str)
@@ -1290,14 +1292,14 @@ class Agent(BaseAgent):
1290
1292
  # NOTE: a bit of a hack - we pull the timestamp from the message created_by
1291
1293
  memory_edit_timestamp = self._messages[0].created_at
1292
1294
 
1293
- # update memory (TODO: potentially update recall/archival stats seperately)
1295
+ # update memory (TODO: potentially update recall/archival stats separately)
1294
1296
  new_system_message_str = compile_system_message(
1295
1297
  agent_id=self.agent_state.id,
1296
1298
  system_prompt=self.agent_state.system,
1297
1299
  in_context_memory=self.agent_state.memory,
1298
1300
  in_context_memory_last_edit=memory_edit_timestamp,
1299
1301
  actor=self.user,
1300
- passage_manager=self.passage_manager,
1302
+ agent_manager=self.agent_manager,
1301
1303
  message_manager=self.message_manager,
1302
1304
  user_defined_variables=None,
1303
1305
  append_icm_if_missing=True,
@@ -1368,33 +1370,24 @@ class Agent(BaseAgent):
1368
1370
  source_id: str,
1369
1371
  source_manager: SourceManager,
1370
1372
  agent_manager: AgentManager,
1371
- page_size: Optional[int] = None,
1372
1373
  ):
1373
- """Attach data with name `source_name` to the agent from source_connector."""
1374
- # TODO: eventually, adding a data source should just give access to the retriever the source table, rather than modifying archival memory
1375
- passages = self.passage_manager.list_passages(actor=user, source_id=source_id, limit=page_size)
1376
-
1377
- for passage in passages:
1378
- assert isinstance(passage, Passage), f"Generate yielded bad non-Passage type: {type(passage)}"
1379
- passage.agent_id = self.agent_state.id
1380
- self.passage_manager.update_passage_by_id(passage_id=passage.id, passage=passage, actor=user)
1381
-
1382
- agents_passages = self.passage_manager.list_passages(actor=user, agent_id=self.agent_state.id, source_id=source_id, limit=page_size)
1383
- passage_size = self.passage_manager.size(actor=user, agent_id=self.agent_state.id, source_id=source_id)
1384
- assert all([p.agent_id == self.agent_state.id for p in agents_passages])
1385
- assert len(agents_passages) == passage_size # sanity check
1386
- assert passage_size == len(passages), f"Expected {len(passages)} passages, got {passage_size}"
1387
-
1388
- # attach to agent
1374
+ """Attach a source to the agent using the SourcesAgents ORM relationship.
1375
+
1376
+ Args:
1377
+ user: User performing the action
1378
+ source_id: ID of the source to attach
1379
+ source_manager: SourceManager instance to verify source exists
1380
+ agent_manager: AgentManager instance to manage agent-source relationship
1381
+ """
1382
+ # Verify source exists and user has permission to access it
1389
1383
  source = source_manager.get_source_by_id(source_id=source_id, actor=user)
1390
- assert source is not None, f"Source {source_id} not found in metadata store"
1384
+ assert source is not None, f"Source {source_id} not found in user's organization ({user.organization_id})"
1391
1385
 
1392
- # NOTE: need this redundant line here because we haven't migrated agent to ORM yet
1393
- # TODO: delete @matt and remove
1386
+ # Use the agent_manager to create the relationship
1394
1387
  agent_manager.attach_source(agent_id=self.agent_state.id, source_id=source_id, actor=user)
1395
1388
 
1396
1389
  printd(
1397
- f"Attached data source {source.name} to agent {self.agent_state.name}, consisting of {len(passages)}. Agent now has {passage_size} embeddings in archival memory.",
1390
+ f"Attached data source {source.name} to agent {self.agent_state.name}.",
1398
1391
  )
1399
1392
 
1400
1393
  def update_message(self, message_id: str, request: MessageUpdate) -> Message:
@@ -1550,21 +1543,22 @@ class Agent(BaseAgent):
1550
1543
  num_tokens_from_messages(messages=messages_openai_format[1:], model=self.model) if len(messages_openai_format) > 1 else 0
1551
1544
  )
1552
1545
 
1553
- passage_manager_size = self.passage_manager.size(actor=self.user, agent_id=self.agent_state.id)
1546
+ agent_manager_passage_size = self.agent_manager.passage_size(actor=self.user, agent_id=self.agent_state.id)
1554
1547
  message_manager_size = self.message_manager.size(actor=self.user, agent_id=self.agent_state.id)
1555
1548
  external_memory_summary = compile_memory_metadata_block(
1556
1549
  actor=self.user,
1557
1550
  agent_id=self.agent_state.id,
1558
1551
  memory_edit_timestamp=get_utc_time(), # dummy timestamp
1559
- passage_manager=self.passage_manager,
1552
+ agent_manager=self.agent_manager,
1560
1553
  message_manager=self.message_manager,
1561
1554
  )
1562
1555
  num_tokens_external_memory_summary = count_tokens(external_memory_summary)
1563
1556
 
1564
1557
  # tokens taken up by function definitions
1565
- if self.functions:
1566
- available_functions_definitions = [ChatCompletionRequestTool(type="function", function=f) for f in self.functions]
1567
- num_tokens_available_functions_definitions = num_tokens_from_functions(functions=self.functions, model=self.model)
1558
+ agent_state_tool_jsons = [t.json_schema for t in self.agent_state.tools]
1559
+ if agent_state_tool_jsons:
1560
+ available_functions_definitions = [ChatCompletionRequestTool(type="function", function=f) for f in agent_state_tool_jsons]
1561
+ num_tokens_available_functions_definitions = num_tokens_from_functions(functions=agent_state_tool_jsons, model=self.model)
1568
1562
  else:
1569
1563
  available_functions_definitions = []
1570
1564
  num_tokens_available_functions_definitions = 0
@@ -1582,7 +1576,7 @@ class Agent(BaseAgent):
1582
1576
  return ContextWindowOverview(
1583
1577
  # context window breakdown (in messages)
1584
1578
  num_messages=len(self._messages),
1585
- num_archival_memory=passage_manager_size,
1579
+ num_archival_memory=agent_manager_passage_size,
1586
1580
  num_recall_memory=message_manager_size,
1587
1581
  num_tokens_external_memory_summary=num_tokens_external_memory_summary,
1588
1582
  # top-level information
letta/client/client.py CHANGED
@@ -2156,6 +2156,7 @@ class LocalClient(AbstractClient):
2156
2156
  "block_ids": [b.id for b in memory.get_blocks()] + block_ids,
2157
2157
  "tool_ids": tool_ids,
2158
2158
  "tool_rules": tool_rules,
2159
+ "include_base_tools": include_base_tools,
2159
2160
  "system": system,
2160
2161
  "agent_type": agent_type,
2161
2162
  "llm_config": llm_config if llm_config else self._default_llm_config,
letta/constants.py CHANGED
@@ -23,6 +23,7 @@ MIN_CONTEXT_WINDOW = 4096
23
23
 
24
24
  # embeddings
25
25
  MAX_EMBEDDING_DIM = 4096 # maximum supported embeding size - do NOT change or else DBs will need to be reset
26
+ DEFAULT_EMBEDDING_CHUNK_SIZE = 300
26
27
 
27
28
  # tokenizers
28
29
  EMBEDDING_TO_TOKENIZER_MAP = {
@@ -37,7 +38,8 @@ DEFAULT_HUMAN = "basic"
37
38
  DEFAULT_PRESET = "memgpt_chat"
38
39
 
39
40
  # Base tools that cannot be edited, as they access agent state directly
40
- BASE_TOOLS = ["send_message", "conversation_search", "conversation_search_date", "archival_memory_insert", "archival_memory_search"]
41
+ # Note that we don't include "conversation_search_date" for now
42
+ BASE_TOOLS = ["send_message", "conversation_search", "archival_memory_insert", "archival_memory_search"]
41
43
  O1_BASE_TOOLS = ["send_thinking_message", "send_final_message"]
42
44
  # Base memory tools CAN be edited, and are added by default by the server
43
45
  BASE_MEMORY_TOOLS = ["core_memory_append", "core_memory_replace"]
@@ -48,6 +50,9 @@ BASE_MEMORY_TOOLS = ["core_memory_append", "core_memory_replace"]
48
50
  DEFAULT_MESSAGE_TOOL = "send_message"
49
51
  DEFAULT_MESSAGE_TOOL_KWARG = "message"
50
52
 
53
+ # Structured output models
54
+ STRUCTURED_OUTPUT_MODELS = {"gpt-4o", "gpt-4o-mini"}
55
+
51
56
  # LOGGER_LOG_LEVEL is use to convert Text to Logging level value for logging mostly for Cli input to setting level
52
57
  LOGGER_LOG_LEVELS = {"CRITICAL": CRITICAL, "ERROR": ERROR, "WARN": WARN, "WARNING": WARNING, "INFO": INFO, "DEBUG": DEBUG, "NOTSET": NOTSET}
53
58
 
letta/embeddings.py CHANGED
@@ -234,16 +234,10 @@ def embedding_model(config: EmbeddingConfig, user_id: Optional[uuid.UUID] = None
234
234
  )
235
235
  elif endpoint_type == "ollama":
236
236
 
237
- from llama_index.embeddings.ollama import OllamaEmbedding
238
-
239
- ollama_additional_kwargs = {}
240
- callback_manager = None
241
-
242
- model = OllamaEmbedding(
243
- model_name=config.embedding_model,
237
+ model = OllamaEmbeddings(
238
+ model=config.embedding_model,
244
239
  base_url=config.embedding_endpoint,
245
- ollama_additional_kwargs=ollama_additional_kwargs or {},
246
- callback_manager=callback_manager or None,
240
+ ollama_additional_kwargs={},
247
241
  )
248
242
  return model
249
243
 
@@ -1,15 +1,6 @@
1
- from datetime import datetime
2
1
  from typing import Optional
3
2
 
4
3
  from letta.agent import Agent
5
- from letta.constants import MAX_PAUSE_HEARTBEATS
6
-
7
- # import math
8
- # from letta.utils import json_dumps
9
-
10
- ### Functions / tools the agent can use
11
- # All functions should return a response string (or None)
12
- # If the function fails, throw an exception
13
4
 
14
5
 
15
6
  def send_message(self: "Agent", message: str) -> Optional[str]:
@@ -27,36 +18,6 @@ def send_message(self: "Agent", message: str) -> Optional[str]:
27
18
  return None
28
19
 
29
20
 
30
- # Construct the docstring dynamically (since it should use the external constants)
31
- pause_heartbeats_docstring = f"""
32
- Temporarily ignore timed heartbeats. You may still receive messages from manual heartbeats and other events.
33
-
34
- Args:
35
- minutes (int): Number of minutes to ignore heartbeats for. Max value of {MAX_PAUSE_HEARTBEATS} minutes ({MAX_PAUSE_HEARTBEATS // 60} hours).
36
-
37
- Returns:
38
- str: Function status response
39
- """
40
-
41
-
42
- def pause_heartbeats(self: "Agent", minutes: int) -> Optional[str]:
43
- import datetime
44
-
45
- from letta.constants import MAX_PAUSE_HEARTBEATS
46
-
47
- minutes = min(MAX_PAUSE_HEARTBEATS, minutes)
48
-
49
- # Record the current time
50
- self.pause_heartbeats_start = datetime.datetime.now(datetime.timezone.utc)
51
- # And record how long the pause should go for
52
- self.pause_heartbeats_minutes = int(minutes)
53
-
54
- return f"Pausing timed heartbeats for {minutes} min"
55
-
56
-
57
- pause_heartbeats.__doc__ = pause_heartbeats_docstring
58
-
59
-
60
21
  def conversation_search(self: "Agent", query: str, page: Optional[int] = 0) -> Optional[str]:
61
22
  """
62
23
  Search prior conversation history using case-insensitive string matching.
@@ -83,19 +44,19 @@ def conversation_search(self: "Agent", query: str, page: Optional[int] = 0) -> O
83
44
  count = RETRIEVAL_QUERY_DEFAULT_PAGE_SIZE
84
45
  # TODO: add paging by page number. currently cursor only works with strings.
85
46
  # original: start=page * count
86
- results = self.message_manager.list_user_messages_for_agent(
47
+ messages = self.message_manager.list_user_messages_for_agent(
87
48
  agent_id=self.agent_state.id,
88
49
  actor=self.user,
89
50
  query_text=query,
90
51
  limit=count,
91
52
  )
92
- total = len(results)
53
+ total = len(messages)
93
54
  num_pages = math.ceil(total / count) - 1 # 0 index
94
- if len(results) == 0:
55
+ if len(messages) == 0:
95
56
  results_str = f"No results found."
96
57
  else:
97
- results_pref = f"Showing {len(results)} of {total} results (page {page}/{num_pages}):"
98
- results_formatted = [f"timestamp: {d['timestamp']}, {d['message']['role']} - {d['message']['content']}" for d in results]
58
+ results_pref = f"Showing {len(messages)} of {total} results (page {page}/{num_pages}):"
59
+ results_formatted = [message.text for message in messages]
99
60
  results_str = f"{results_pref} {json_dumps(results_formatted)}"
100
61
  return results_str
101
62
 
@@ -113,6 +74,7 @@ def conversation_search_date(self: "Agent", start_date: str, end_date: str, page
113
74
  str: Query result string
114
75
  """
115
76
  import math
77
+ from datetime import datetime
116
78
 
117
79
  from letta.constants import RETRIEVAL_QUERY_DEFAULT_PAGE_SIZE
118
80
  from letta.utils import json_dumps
@@ -141,7 +103,6 @@ def conversation_search_date(self: "Agent", start_date: str, end_date: str, page
141
103
  start_date=start_datetime,
142
104
  end_date=end_datetime,
143
105
  limit=count,
144
- # start_date=start_date, end_date=end_date, limit=count, start=page * count
145
106
  )
146
107
  total = len(results)
147
108
  num_pages = math.ceil(total / count) - 1 # 0 index
@@ -185,10 +146,8 @@ def archival_memory_search(self: "Agent", query: str, page: Optional[int] = 0, s
185
146
  Returns:
186
147
  str: Query result string
187
148
  """
188
- import math
189
149
 
190
150
  from letta.constants import RETRIEVAL_QUERY_DEFAULT_PAGE_SIZE
191
- from letta.utils import json_dumps
192
151
 
193
152
  if page is None or (isinstance(page, str) and page.lower().strip() == "none"):
194
153
  page = 0
@@ -197,15 +156,16 @@ def archival_memory_search(self: "Agent", query: str, page: Optional[int] = 0, s
197
156
  except:
198
157
  raise ValueError(f"'page' argument must be an integer")
199
158
  count = RETRIEVAL_QUERY_DEFAULT_PAGE_SIZE
200
-
159
+
201
160
  try:
202
161
  # Get results using passage manager
203
- all_results = self.passage_manager.list_passages(
162
+ all_results = self.agent_manager.list_passages(
204
163
  actor=self.user,
164
+ agent_id=self.agent_state.id,
205
165
  query_text=query,
206
166
  limit=count + start, # Request enough results to handle offset
207
167
  embedding_config=self.agent_state.embedding_config,
208
- embed_query=True
168
+ embed_query=True,
209
169
  )
210
170
 
211
171
  # Apply pagination
@@ -213,13 +173,7 @@ def archival_memory_search(self: "Agent", query: str, page: Optional[int] = 0, s
213
173
  paged_results = all_results[start:end]
214
174
 
215
175
  # Format results to match previous implementation
216
- formatted_results = [
217
- {
218
- "timestamp": str(result.created_at),
219
- "content": result.text
220
- }
221
- for result in paged_results
222
- ]
176
+ formatted_results = [{"timestamp": str(result.created_at), "content": result.text} for result in paged_results]
223
177
 
224
178
  return formatted_results, len(formatted_results)
225
179