66
77import functools
88import inspect
9+ import os
910import re
1011from collections .abc import Awaitable , Callable
1112from typing import Any
2526)
2627
2728
29+ def are_docs_available () -> bool :
30+ """Checks if documentation search is available."""
31+ return bool (
32+ os .environ .get ("DEEPSET_DOCS_WORKSPACE" , False )
33+ and os .environ .get ("DEEPSET_DOCS_PIPELINE_NAME" , False )
34+ and os .environ .get ("DEEPSET_DOCS_API_KEY" , False )
35+ )
36+
37+
38+ def get_workspace_from_env () -> str :
39+ """Gets the workspace configured from environment variable."""
40+ workspace = os .environ .get ("DEEPSET_WORKSPACE" )
41+ if not workspace :
42+ raise ValueError ("DEEPSET_WORKSPACE environment variable not set" )
43+ return workspace
44+
45+
2846def apply_custom_args (base_func : Callable [..., Any ], config : ToolConfig ) -> Callable [..., Any ]:
2947 """
3048 Applies custom keyword arguments defined in the ToolConfig to a function.
@@ -99,7 +117,8 @@ def apply_workspace(
99117
100118 @functools .wraps (base_func )
101119 async def workspace_wrapper (* args : Any , ** kwargs : Any ) -> Any :
102- return await base_func (* args , workspace = workspace , ** kwargs )
120+ ws = workspace or get_workspace_from_env ()
121+ return await base_func (* args , workspace = ws , ** kwargs )
103122
104123 # Remove workspace from signature
105124 original_sig = inspect .signature (base_func )
@@ -146,11 +165,7 @@ def apply_memory(
146165
147166
148167def apply_client (
149- base_func : Callable [..., Any ],
150- config : ToolConfig ,
151- use_request_context : bool = True ,
152- base_url : str | None = None ,
153- api_key : str | None = None ,
168+ base_func : Callable [..., Any ], config : ToolConfig , use_request_context : bool = True , base_url : str | None = None
154169) -> Callable [..., Any ]:
155170 """
156171 Applies the deepset API client to a function.
@@ -163,7 +178,6 @@ def apply_client(
163178 :param config: The ToolConfig for the function.
164179 :param use_request_context: Whether to collect the API key from the request context.
165180 :param base_url: Base URL for the deepset API.
166- :param api_key: The API key to use.
167181 :returns: Function with client injection applied and updated signature/docstring.
168182 :raises ValueError: If API key cannot be extracted from request context.
169183 """
@@ -199,14 +213,26 @@ async def client_wrapper_with_context(*args: Any, **kwargs: Any) -> Any:
199213 ctx_param = inspect .Parameter (name = "ctx" , kind = inspect .Parameter .KEYWORD_ONLY , annotation = Context )
200214 new_params .append (ctx_param )
201215 client_wrapper_with_context .__signature__ = original_sig .replace (parameters = new_params ) # type: ignore
202- client_wrapper_with_context .__doc__ = remove_params_from_docstring (base_func .__doc__ , {"client" })
216+
217+ # Remove client from docstring
218+ if base_func .__doc__ :
219+ import re
220+
221+ doc = base_func .__doc__
222+ doc = re .sub (
223+ r"^\s*:param\s+client.*?(?=^\s*:|^\s*$|\Z)" ,
224+ "" ,
225+ doc ,
226+ flags = re .MULTILINE | re .DOTALL ,
227+ )
228+ client_wrapper_with_context .__doc__ = "\n " .join ([line .rstrip () for line in doc .strip ().split ("\n " )])
203229
204230 return client_wrapper_with_context
205231 else :
206232
207233 @functools .wraps (base_func )
208234 async def client_wrapper_without_context (* args : Any , ** kwargs : Any ) -> Any :
209- client_kwargs : dict [str , Any ] = {"transport_config" : DEFAULT_CLIENT_HEADER , "api_key" : api_key }
235+ client_kwargs : dict [str , Any ] = {"transport_config" : DEFAULT_CLIENT_HEADER }
210236 if base_url :
211237 client_kwargs ["base_url" ] = base_url
212238 async with AsyncDeepsetClient (** client_kwargs ) as client :
0 commit comments