|
| 1 | +import pytest |
| 2 | +from ldclient import Config, Context, LDClient |
| 3 | +from ldclient.integrations.test_data import TestData |
| 4 | + |
| 5 | +from ldai import LDTool, LDAIClient |
| 6 | +from ldai.models import AIAgentConfigDefault, AICompletionConfigDefault |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def td() -> TestData: |
| 11 | + td = TestData.data_source() |
| 12 | + td.update( |
| 13 | + td.flag('completion-with-tools') |
| 14 | + .variations( |
| 15 | + { |
| 16 | + 'model': {'name': 'gpt-5', 'parameters': {'temperature': 0.7}}, |
| 17 | + 'messages': [{'role': 'user', 'content': 'Hello'}], |
| 18 | + 'tools': { |
| 19 | + 'web-search-tool': { |
| 20 | + 'name': 'web-search-tool', |
| 21 | + 'type': 'function', |
| 22 | + 'parameters': {'type': 'object', 'properties': {}, 'required': []}, |
| 23 | + 'customParameters': {'some-custom-parameter': 'some-custom-value'}, |
| 24 | + } |
| 25 | + }, |
| 26 | + '_ldMeta': {'enabled': True, 'variationKey': 'v1', 'version': 1}, |
| 27 | + }, |
| 28 | + ) |
| 29 | + .variation_for_all(0) |
| 30 | + ) |
| 31 | + |
| 32 | + td.update( |
| 33 | + td.flag('completion-no-tools') |
| 34 | + .variations( |
| 35 | + { |
| 36 | + 'model': {'name': 'gpt-5'}, |
| 37 | + 'messages': [{'role': 'user', 'content': 'Hello'}], |
| 38 | + '_ldMeta': {'enabled': True, 'variationKey': 'v1', 'version': 1}, |
| 39 | + }, |
| 40 | + ) |
| 41 | + .variation_for_all(0) |
| 42 | + ) |
| 43 | + |
| 44 | + td.update( |
| 45 | + td.flag('agent-with-tools') |
| 46 | + .variations( |
| 47 | + { |
| 48 | + 'model': {'name': 'gpt-5'}, |
| 49 | + 'instructions': 'You are a helpful agent.', |
| 50 | + 'tools': { |
| 51 | + 'search-tool': { |
| 52 | + 'name': 'search-tool', |
| 53 | + 'type': 'function', |
| 54 | + 'customParameters': {'maxResults': 10}, |
| 55 | + } |
| 56 | + }, |
| 57 | + '_ldMeta': {'enabled': True, 'variationKey': 'v1', 'version': 1, 'mode': 'agent'}, |
| 58 | + }, |
| 59 | + ) |
| 60 | + .variation_for_all(0) |
| 61 | + ) |
| 62 | + |
| 63 | + return td |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def client(td) -> LDAIClient: |
| 68 | + config = Config('fake-sdk-key', update_processor_class=td, send_events=False) |
| 69 | + ld_client = LDClient(config=config) |
| 70 | + return LDAIClient(ld_client) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture |
| 74 | +def context() -> Context: |
| 75 | + return Context.builder('test-user').name('Test User').build() |
| 76 | + |
| 77 | + |
| 78 | +def test_completion_config_includes_tools_from_variation(client, context): |
| 79 | + result = client.completion_config('completion-with-tools', context, AICompletionConfigDefault()) |
| 80 | + |
| 81 | + assert result.tools is not None |
| 82 | + assert 'web-search-tool' in result.tools |
| 83 | + tool = result.tools['web-search-tool'] |
| 84 | + assert tool.name == 'web-search-tool' |
| 85 | + assert tool.type == 'function' |
| 86 | + assert tool.custom_parameters == {'some-custom-parameter': 'some-custom-value'} |
| 87 | + |
| 88 | + |
| 89 | +def test_completion_config_tools_none_when_not_in_variation(client, context): |
| 90 | + result = client.completion_config('completion-no-tools', context, AICompletionConfigDefault()) |
| 91 | + |
| 92 | + assert result.tools is None |
| 93 | + |
| 94 | + |
| 95 | +def test_completion_config_tools_none_when_variation_has_no_tools(client, context): |
| 96 | + default_tool = LDTool(name='default-tool', type='function', custom_parameters={'priority': 'high'}) |
| 97 | + default = AICompletionConfigDefault(tools={'default-tool': default_tool}) |
| 98 | + |
| 99 | + result = client.completion_config('completion-no-tools', context, default) |
| 100 | + |
| 101 | + assert result.tools is None |
| 102 | + |
| 103 | + |
| 104 | +def test_agent_config_includes_tools_from_variation(client, context): |
| 105 | + result = client.agent_config('agent-with-tools', context, AIAgentConfigDefault()) |
| 106 | + |
| 107 | + assert result.tools is not None |
| 108 | + assert 'search-tool' in result.tools |
| 109 | + tool = result.tools['search-tool'] |
| 110 | + assert tool.name == 'search-tool' |
| 111 | + assert tool.custom_parameters == {'maxResults': 10} |
| 112 | + |
| 113 | + |
| 114 | +def test_aitool_to_dict_serializes_custom_parameters_as_camel_case(): |
| 115 | + tool = LDTool( |
| 116 | + name='my-tool', |
| 117 | + type='function', |
| 118 | + parameters={'type': 'object'}, |
| 119 | + custom_parameters={'someKey': 'someValue'}, |
| 120 | + ) |
| 121 | + d = tool.to_dict() |
| 122 | + |
| 123 | + assert d['name'] == 'my-tool' |
| 124 | + assert d['type'] == 'function' |
| 125 | + assert d['parameters'] == {'type': 'object'} |
| 126 | + assert 'customParameters' in d |
| 127 | + assert d['customParameters'] == {'someKey': 'someValue'} |
| 128 | + assert 'custom_parameters' not in d |
| 129 | + |
| 130 | + |
| 131 | +def test_aitool_to_dict_omits_none_fields(): |
| 132 | + tool = LDTool(name='bare-tool') |
| 133 | + d = tool.to_dict() |
| 134 | + |
| 135 | + assert d == {'name': 'bare-tool'} |
0 commit comments