-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add custom tool headers #2773
Add custom tool headers #2773
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,3 +119,19 @@ | |
logger.error( | ||
"Failed to parse LITELLM_PASS_THROUGH_HEADERS, must be a valid JSON object" | ||
) | ||
|
||
|
||
# List of headers to pass through to tool calls (e.g., API requests made by tools) | ||
# This allows for dynamic configuration of tool behavior based on incoming request headers | ||
TOOL_PASS_THROUGH_HEADERS: list[str] | None = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider adding a comment explaining the purpose of TOOL_PASS_THROUGH_HEADERS |
||
_TOOL_PASS_THROUGH_HEADERS_RAW = os.environ.get("TOOL_PASS_THROUGH_HEADERS") | ||
if _TOOL_PASS_THROUGH_HEADERS_RAW: | ||
try: | ||
TOOL_PASS_THROUGH_HEADERS = json.loads(_TOOL_PASS_THROUGH_HEADERS_RAW) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Ensure TOOL_PASS_THROUGH_HEADERS is a list of strings after parsing |
||
except Exception: | ||
from danswer.utils.logger import setup_logger | ||
pablonyx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
logger = setup_logger() | ||
logger.error( | ||
"Failed to parse TOOL_PASS_THROUGH_HEADERS, must be a valid JSON object" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,18 +47,17 @@ def __init__( | |
method_spec: MethodSpec, | ||
base_url: str, | ||
custom_headers: list[dict[str, str]] | None = [], | ||
tool_additional_headers: dict[str, str] | None = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider adding type hints for the |
||
) -> None: | ||
self._base_url = base_url | ||
self._method_spec = method_spec | ||
self._tool_definition = self._method_spec.to_tool_definition() | ||
|
||
self._name = self._method_spec.name | ||
self._description = self._method_spec.summary | ||
self.headers = ( | ||
{header["key"]: header["value"] for header in custom_headers} | ||
if custom_headers | ||
else {} | ||
) | ||
self.headers = { | ||
header["key"]: header["value"] for header in (custom_headers or []) | ||
} | (tool_additional_headers or {}) | ||
|
||
@property | ||
def name(self) -> str: | ||
|
@@ -185,6 +184,7 @@ def final_result(self, *args: ToolResponse) -> JSON_ro: | |
|
||
def build_custom_tools_from_openapi_schema_and_headers( | ||
openapi_schema: dict[str, Any], | ||
tool_additional_headers: dict[str, str] | None = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Add a docstring explaining the purpose of |
||
custom_headers: list[dict[str, str]] | None = [], | ||
dynamic_schema_info: DynamicSchemaInfo | None = None, | ||
) -> list[CustomTool]: | ||
|
@@ -205,7 +205,8 @@ def build_custom_tools_from_openapi_schema_and_headers( | |
url = openapi_to_url(openapi_schema) | ||
method_specs = openapi_to_method_specs(openapi_schema) | ||
return [ | ||
CustomTool(method_spec, url, custom_headers) for method_spec in method_specs | ||
CustomTool(method_spec, url, custom_headers, tool_additional_headers) | ||
for method_spec in method_specs | ||
] | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: New parameter
tool_additional_headers
added but not used in the function body