doc(langchain-sdk): Improve doc and tests for LangChain SDK. (#154)

This commit is contained in:
Anubhav Dhawan
2024-12-19 18:56:25 +05:30
committed by GitHub
parent 21a9146404
commit 3ac7dcfb9c
3 changed files with 25 additions and 12 deletions

View File

@@ -195,7 +195,9 @@ access tokens for [Google OAuth
### Configuring Tools for Authentication
Refer to [these instructions](https://github.com/googleapis/genai-toolbox/blob/main/docs/tools/README.md#authenticated-parameters) on configuring tools for authenticated parameters.
Refer to [these
instructions](../../docs/tools/README.md#authenticated-parameters) on
configuring tools for authenticated parameters.
### Configure SDK for Authentication
@@ -230,11 +232,9 @@ toolbox.add_auth_header("my_auth_service", get_auth_header)
```
> [!NOTE]
> After adding authentication headers, either through `load_tool`,
> `load_toolset`, or `add_auth_header`, all subsequent tool invocations from
> that point onward will use these authentications, regardless of whether the
> tool was loaded before or after calling these methods. This maintains a
> consistent authentication context.
> Authentication headers added via `load_tool`, `load_toolset`, or
> `add_auth_header` apply to all subsequent tool invocations, regardless of when
> the tool was loaded. This ensures a consistent authentication context.
### Complete Example

View File

@@ -106,11 +106,11 @@ def _get_auth_headers(id_token_getters: dict[str, Callable[[], str]]) -> dict[st
headers to be included in tool invocation.
Args:
id_token_getters: A dict that maps auth source names to the functions
that return its ID token.
id_token_getters: A dict that maps auth source names to the functions
that return its ID token.
Returns:
A dictionary of headers to be included in the tool invocation.
A dictionary of headers to be included in the tool invocation.
"""
auth_headers = {}
for auth_source, get_id_token in id_token_getters.items():

View File

@@ -135,7 +135,17 @@ async def test_generate_tool_success():
assert isinstance(tool, StructuredTool)
assert tool.name == "test_tool"
assert tool.description == "This is test tool."
assert tool.args_schema is not None # Check if args_schema is generated
assert tool.args is not None
assert "param1" in tool.args
assert tool.args["param1"]["title"] == "Param1"
assert tool.args["param1"]["description"] == "Parameter 1"
assert tool.args["param1"]["anyOf"] == [{"type": "string"}, {"type": "null"}]
assert "param2" in tool.args
assert tool.args["param2"]["title"] == "Param2"
assert tool.args["param2"]["description"] == "Parameter 2"
assert tool.args["param2"]["anyOf"] == [{"type": "integer"}, {"type": "null"}]
@pytest.mark.asyncio
@@ -258,7 +268,7 @@ async def test_generate_tool_invoke(mock_invoke_tool):
tool = client._generate_tool("test_tool", ManifestSchema(**manifest_data))
# Call the tool function with some arguments
result = await tool.coroutine(param1="test_value", param2=123)
result = await tool.ainvoke({"param1": "test_value", "param2": 123})
# Assert that _invoke_tool was called with the correct parameters
mock_invoke_tool.assert_called_once_with(
@@ -739,7 +749,10 @@ async def test_generate_tool(
assert isinstance(tool, StructuredTool)
assert tool.name == "tool_name"
assert tool.description == "Test tool description"
assert tool.args_schema.__name__ == "tool_name"
assert "param1" in tool.args
assert tool.args["param1"]["title"] == "Param1"
assert tool.args["param1"]["description"] == "Test param"
assert tool.args["param1"]["anyOf"] == [{"type": "string"}, {"type": "null"}]
# Call the tool function to check if _invoke_tool is called
if expected_invoke_tool_call: