feat: Implement tool calling rules for agents (#1954)

This commit is contained in:
Matthew Zhou
2024-10-30 16:53:41 -07:00
committed by GitHub
parent 0784bdc854
commit 7bb0f53c23
19 changed files with 588 additions and 158 deletions

View File

@@ -43,6 +43,7 @@ from letta.schemas.organization import Organization
from letta.schemas.passage import Passage
from letta.schemas.source import Source, SourceCreate, SourceUpdate
from letta.schemas.tool import Tool, ToolCreate, ToolUpdate
from letta.schemas.tool_rule import BaseToolRule
from letta.server.rest_api.interface import QueuingInterface
from letta.server.server import SyncServer
from letta.utils import get_human_text, get_persona_text
@@ -140,6 +141,8 @@ class AbstractClient(object):
agent_id: Optional[str] = None,
name: Optional[str] = None,
stream: Optional[bool] = False,
stream_steps: bool = False,
stream_tokens: bool = False,
include_full_message: Optional[bool] = False,
) -> LettaResponse:
raise NotImplementedError
@@ -196,7 +199,6 @@ class AbstractClient(object):
self,
func,
name: Optional[str] = None,
update: Optional[bool] = True,
tags: Optional[List[str]] = None,
) -> Tool:
raise NotImplementedError
@@ -405,7 +407,7 @@ class RESTClient(AbstractClient):
# add memory tools
memory_functions = get_memory_functions(memory)
for func_name, func in memory_functions.items():
tool = self.create_tool(func, name=func_name, tags=["memory", "letta-base"], update=True)
tool = self.create_tool(func, name=func_name, tags=["memory", "letta-base"])
tool_names.append(tool.name)
# check if default configs are provided
@@ -1268,7 +1270,6 @@ class RESTClient(AbstractClient):
self,
func: Callable,
name: Optional[str] = None,
update: Optional[bool] = True, # TODO: actually use this
tags: Optional[List[str]] = None,
) -> Tool:
"""
@@ -1278,7 +1279,6 @@ class RESTClient(AbstractClient):
func (callable): The function to create a tool for.
name: (str): Name of the tool (must be unique per-user.)
tags (Optional[List[str]], optional): Tags for the tool. Defaults to None.
update (bool, optional): Update the tool if it already exists. Defaults to True.
Returns:
tool (Tool): The created tool.
@@ -1628,6 +1628,7 @@ class LocalClient(AbstractClient):
system: Optional[str] = None,
# tools
tools: Optional[List[str]] = None,
tool_rules: Optional[List[BaseToolRule]] = None,
include_base_tools: Optional[bool] = True,
# metadata
metadata: Optional[Dict] = {"human:": DEFAULT_HUMAN, "persona": DEFAULT_PERSONA},
@@ -1642,6 +1643,7 @@ class LocalClient(AbstractClient):
memory (Memory): Memory configuration
system (str): System configuration
tools (List[str]): List of tools
tool_rules (Optional[List[BaseToolRule]]): List of tool rules
include_base_tools (bool): Include base tools
metadata (Dict): Metadata
description (str): Description
@@ -1663,7 +1665,7 @@ class LocalClient(AbstractClient):
# add memory tools
memory_functions = get_memory_functions(memory)
for func_name, func in memory_functions.items():
tool = self.create_tool(func, name=func_name, tags=["memory", "letta-base"], update=True)
tool = self.create_tool(func, name=func_name, tags=["memory", "letta-base"])
tool_names.append(tool.name)
self.interface.clear()
@@ -1680,6 +1682,7 @@ class LocalClient(AbstractClient):
metadata_=metadata,
memory=memory,
tools=tool_names,
tool_rules=tool_rules,
system=system,
agent_type=agent_type,
llm_config=llm_config if llm_config else self._default_llm_config,
@@ -2255,8 +2258,8 @@ class LocalClient(AbstractClient):
self,
func,
name: Optional[str] = None,
update: Optional[bool] = True, # TODO: actually use this
tags: Optional[List[str]] = None,
description: Optional[str] = None,
) -> Tool:
"""
Create a tool. This stores the source code of function on the server, so that the server can execute the function and generate an OpenAI JSON schemas for it when using with an agent.
@@ -2265,7 +2268,7 @@ class LocalClient(AbstractClient):
func (callable): The function to create a tool for.
name: (str): Name of the tool (must be unique per-user.)
tags (Optional[List[str]], optional): Tags for the tool. Defaults to None.
update (bool, optional): Update the tool if it already exists. Defaults to True.
description (str, optional): The description.
Returns:
tool (Tool): The created tool.
@@ -2285,6 +2288,7 @@ class LocalClient(AbstractClient):
source_code=source_code,
name=name,
tags=tags,
description=description,
),
actor=self.user,
)