|
| 1 | +"""Tests for default parameter values in tool definitions.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from src.lmstudio.json_api import ToolFunctionDef, ToolFunctionDefDict |
| 7 | +from src.lmstudio.schemas import _to_json_schema |
| 8 | + |
| 9 | + |
| 10 | +def greet(name: str, greeting: str = "Hello", punctuation: str = "!") -> str: |
| 11 | + """Greet someone with a customizable message. |
| 12 | + |
| 13 | + Args: |
| 14 | + name: The name of the person to greet |
| 15 | + greeting: The greeting word to use (default: "Hello") |
| 16 | + punctuation: The punctuation to end with (default: "!") |
| 17 | + |
| 18 | + Returns: |
| 19 | + A greeting message |
| 20 | + """ |
| 21 | + return f"{greeting}, {name}{punctuation}" |
| 22 | + |
| 23 | + |
| 24 | +def calculate(expression: str, precision: int = 2) -> str: |
| 25 | + """Calculate a mathematical expression. |
| 26 | + |
| 27 | + Args: |
| 28 | + expression: The mathematical expression to evaluate |
| 29 | + precision: Number of decimal places (default: 2) |
| 30 | + |
| 31 | + Returns: |
| 32 | + The calculated result |
| 33 | + """ |
| 34 | + return f"Result: {eval(expression):.{precision}f}" |
| 35 | + |
| 36 | + |
| 37 | +class TestDefaultValues: |
| 38 | + """Test default parameter value functionality.""" |
| 39 | + |
| 40 | + def test_extract_defaults_from_callable(self): |
| 41 | + """Test extracting default values from function signature.""" |
| 42 | + tool_def = ToolFunctionDef.from_callable(greet) |
| 43 | + |
| 44 | + assert tool_def.name == "greet" |
| 45 | + assert tool_def.parameter_defaults == { |
| 46 | + "greeting": "Hello", |
| 47 | + "punctuation": "!" |
| 48 | + } |
| 49 | + assert "name" not in tool_def.parameter_defaults |
| 50 | + |
| 51 | + def test_manual_defaults(self): |
| 52 | + """Test manually specifying default values.""" |
| 53 | + tool_def = ToolFunctionDef( |
| 54 | + name="calculate", |
| 55 | + description="Calculate a mathematical expression", |
| 56 | + parameters={"expression": str, "precision": int}, |
| 57 | + implementation=calculate, |
| 58 | + parameter_defaults={"precision": 2} |
| 59 | + ) |
| 60 | + |
| 61 | + assert tool_def.parameter_defaults == {"precision": 2} |
| 62 | + assert "expression" not in tool_def.parameter_defaults |
| 63 | + |
| 64 | + def test_json_schema_with_defaults(self): |
| 65 | + """Test that JSON Schema includes default values.""" |
| 66 | + tool_def = ToolFunctionDef.from_callable(greet) |
| 67 | + params_struct, _ = tool_def._to_llm_tool_def() |
| 68 | + json_schema = _to_json_schema(params_struct) |
| 69 | + |
| 70 | + properties = json_schema["properties"] |
| 71 | + |
| 72 | + # name should not have a default (required parameter) |
| 73 | + assert "name" in properties |
| 74 | + assert "default" not in properties["name"] |
| 75 | + |
| 76 | + # greeting should have default "Hello" |
| 77 | + assert "greeting" in properties |
| 78 | + assert properties["greeting"]["default"] == "Hello" |
| 79 | + |
| 80 | + # punctuation should have default "!" |
| 81 | + assert "punctuation" in properties |
| 82 | + assert properties["punctuation"]["default"] == "!" |
| 83 | + |
| 84 | + # Only name should be required |
| 85 | + assert json_schema["required"] == ["name"] |
| 86 | + |
| 87 | + def test_dict_based_definition(self): |
| 88 | + """Test dictionary-based tool definition with defaults.""" |
| 89 | + dict_tool: ToolFunctionDefDict = { |
| 90 | + "name": "format_text", |
| 91 | + "description": "Format text with specified style", |
| 92 | + "parameters": {"text": str, "style": str, "uppercase": bool}, |
| 93 | + "implementation": lambda text, style="normal", uppercase=False: text.upper() if uppercase else text, |
| 94 | + "parameter_defaults": {"style": "normal", "uppercase": False} |
| 95 | + } |
| 96 | + |
| 97 | + # This should work without errors |
| 98 | + tool_def = ToolFunctionDef(**dict_tool) |
| 99 | + assert tool_def.parameter_defaults == {"style": "normal", "uppercase": False} |
| 100 | + |
| 101 | + def test_no_defaults(self): |
| 102 | + """Test function with no default values.""" |
| 103 | + def no_defaults(a: int, b: str) -> str: |
| 104 | + """Function with no default parameters.""" |
| 105 | + return f"{a}{b}" |
| 106 | + |
| 107 | + tool_def = ToolFunctionDef.from_callable(no_defaults) |
| 108 | + assert tool_def.parameter_defaults == {} |
| 109 | + |
| 110 | + params_struct, _ = tool_def._to_llm_tool_def() |
| 111 | + json_schema = _to_json_schema(params_struct) |
| 112 | + |
| 113 | + # All parameters should be required |
| 114 | + assert json_schema["required"] == ["a", "b"] |
| 115 | + |
| 116 | + # No properties should have defaults |
| 117 | + for prop in json_schema["properties"].values(): |
| 118 | + assert "default" not in prop |
| 119 | + |
| 120 | + def test_mixed_defaults(self): |
| 121 | + """Test function with some parameters having defaults.""" |
| 122 | + def mixed_defaults(required: str, optional1: int = 42, optional2: bool = True) -> str: |
| 123 | + """Function with mixed required and optional parameters.""" |
| 124 | + return f"{required}{optional1}{optional2}" |
| 125 | + |
| 126 | + tool_def = ToolFunctionDef.from_callable(mixed_defaults) |
| 127 | + assert tool_def.parameter_defaults == { |
| 128 | + "optional1": 42, |
| 129 | + "optional2": True |
| 130 | + } |
| 131 | + |
| 132 | + params_struct, _ = tool_def._to_llm_tool_def() |
| 133 | + json_schema = _to_json_schema(params_struct) |
| 134 | + |
| 135 | + # Only required should be in required list |
| 136 | + assert json_schema["required"] == ["required"] |
| 137 | + |
| 138 | + # Check defaults |
| 139 | + assert json_schema["properties"]["optional1"]["default"] == 42 |
| 140 | + assert json_schema["properties"]["optional2"]["default"] is True |
| 141 | + assert "default" not in json_schema["properties"]["required"] |
0 commit comments