Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Built in tool cache with tool call id #3617

Merged
merged 6 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions backend/onyx/tools/built_in_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def auto_add_search_tool_to_personas(db_session: Session) -> None:
logger.notice("Completed adding SearchTool to relevant Personas.")


_built_in_tools_cache: dict[int, Type[Tool]] | None = None
_built_in_tools_cache: dict[str, Type[Tool]] | None = None


def refresh_built_in_tools_cache(db_session: Session) -> None:
Expand All @@ -173,23 +173,31 @@ def refresh_built_in_tools_cache(db_session: Session) -> None:
),
None,
)
if tool_info:
_built_in_tools_cache[tool.id] = tool_info["cls"]
if tool_info and tool.in_code_tool_id:
_built_in_tools_cache[tool.in_code_tool_id] = tool_info["cls"]


def get_built_in_tool_by_id(
tool_id: int, db_session: Session, force_refresh: bool = False
in_code_tool_id: str, db_session: Session, force_refresh: bool = False
) -> Type[Tool]:
global _built_in_tools_cache
if _built_in_tools_cache is None or force_refresh:

# If the tool is not in the cache, refresh it once
if (
_built_in_tools_cache is None
or force_refresh
or in_code_tool_id not in _built_in_tools_cache
):
refresh_built_in_tools_cache(db_session)

if _built_in_tools_cache is None:
raise RuntimeError(
"Built-in tools cache is None despite being refreshed. Should never happen."
)

if tool_id in _built_in_tools_cache:
return _built_in_tools_cache[tool_id]
else:
raise ValueError(f"No built-in tool found in the cache with ID {tool_id}")
if in_code_tool_id not in _built_in_tools_cache:
raise ValueError(
pablonyx marked this conversation as resolved.
Show resolved Hide resolved
f"No built-in tool found in the cache with ID {in_code_tool_id}"
)

return _built_in_tools_cache[in_code_tool_id]
4 changes: 3 additions & 1 deletion backend/onyx/tools/tool_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ def construct_tools(

for db_tool_model in persona.tools:
if db_tool_model.in_code_tool_id:
tool_cls = get_built_in_tool_by_id(db_tool_model.id, db_session)
tool_cls = get_built_in_tool_by_id(
db_tool_model.in_code_tool_id, db_session
)

# Handle Search Tool
if tool_cls.__name__ == SearchTool.__name__:
Expand Down
6 changes: 5 additions & 1 deletion web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const cspHeader = `
base-uri 'self';
form-action 'self';
frame-ancestors 'none';
${process.env.NEXT_PUBLIC_CLOUD_ENABLED === "true" ? "upgrade-insecure-requests;" : ""}
${
process.env.NEXT_PUBLIC_CLOUD_ENABLED === "true"
? "upgrade-insecure-requests;"
: ""
}
`;

/** @type {import('next').NextConfig} */
Expand Down
Loading