|
| 1 | + |
| 2 | +import pytest |
| 3 | +from mellea.backends.tools import add_tools_from_context_actions, add_tools_from_model_options |
| 4 | +from mellea.backends.types import ModelOption |
| 5 | +from mellea.stdlib.base import CBlock, Component, TemplateRepresentation |
| 6 | + |
| 7 | +class FakeToolComponent(Component): |
| 8 | + def __init__(self) -> None: |
| 9 | + super().__init__() |
| 10 | + |
| 11 | + def tool1(self): |
| 12 | + return |
| 13 | + |
| 14 | + def parts(self): |
| 15 | + return [] |
| 16 | + |
| 17 | + def format_for_llm(self) -> TemplateRepresentation: |
| 18 | + return TemplateRepresentation( |
| 19 | + obj=self, |
| 20 | + args={"arg": None}, |
| 21 | + tools={ |
| 22 | + self.tool1.__name__: self.tool1 |
| 23 | + } |
| 24 | + ) |
| 25 | + |
| 26 | +class FakeToolComponentWithExtraTool(FakeToolComponent): |
| 27 | + def __init__(self) -> None: |
| 28 | + super().__init__() |
| 29 | + |
| 30 | + def tool2(self): |
| 31 | + return |
| 32 | + |
| 33 | + def format_for_llm(self) -> TemplateRepresentation: |
| 34 | + tr = super().format_for_llm() |
| 35 | + assert tr.tools is not None |
| 36 | + tr.tools[self.tool2.__name__] = self.tool2 |
| 37 | + return tr |
| 38 | + |
| 39 | + |
| 40 | +def test_add_tools_from_model_options_list(): |
| 41 | + def get_weather(location: str) -> int: |
| 42 | + """Returns the weather in Celsius.""" |
| 43 | + return 21 |
| 44 | + |
| 45 | + ftc = FakeToolComponent() |
| 46 | + model_options = { |
| 47 | + ModelOption.TOOLS: [get_weather, ftc.tool1] |
| 48 | + } |
| 49 | + |
| 50 | + tools = {} |
| 51 | + add_tools_from_model_options(tools, model_options) |
| 52 | + |
| 53 | + assert tools["get_weather"] == get_weather |
| 54 | + |
| 55 | + # Must use `==` for bound methods. |
| 56 | + assert tools["tool1"] == ftc.tool1, f"{tools["tool1"]} should == {ftc.tool1}" |
| 57 | + |
| 58 | + |
| 59 | +def test_add_tools_from_model_options_map(): |
| 60 | + def get_weather(location: str) -> int: |
| 61 | + """Returns the weather in Celsius.""" |
| 62 | + return 21 |
| 63 | + |
| 64 | + ftc = FakeToolComponent() |
| 65 | + model_options = { |
| 66 | + ModelOption.TOOLS: { |
| 67 | + get_weather.__name__: get_weather, |
| 68 | + ftc.tool1.__name__: ftc.tool1 |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + tools = {} |
| 73 | + add_tools_from_model_options(tools, model_options) |
| 74 | + |
| 75 | + assert tools["get_weather"] == get_weather |
| 76 | + |
| 77 | + # Must use `==` for bound methods. |
| 78 | + assert tools["tool1"] == ftc.tool1, f"{tools["tool1"]} should == {ftc.tool1}" |
| 79 | + |
| 80 | + |
| 81 | +def test_add_tools_from_context_actions(): |
| 82 | + |
| 83 | + ftc1 = FakeToolComponentWithExtraTool() |
| 84 | + ftc2 = FakeToolComponent() |
| 85 | + |
| 86 | + ctx_actions = [CBlock("Hello"), ftc1, ftc2] |
| 87 | + tools = {} |
| 88 | + add_tools_from_context_actions(tools, ctx_actions) |
| 89 | + |
| 90 | + # Check that tools with the same name get properly overwritten in order of ctx. |
| 91 | + assert tools["tool1"] == ftc2.tool1, f"{tools["tool1"]} should == {ftc2.tool1}" |
| 92 | + |
| 93 | + # Check that tools that aren't overwritten are still there. |
| 94 | + assert tools["tool2"] == ftc1.tool2, f"{tools["tool2"]} should == {ftc1.tool2}" |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + pytest.main([__file__]) |
0 commit comments