Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion llm/default_plugins/default_tools.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import llm
from llm.tools import llm_time, llm_version
from llm.tools import llm_random_choice, llm_time, llm_version


@llm.hookimpl
def register_tools(register):
register(llm_version)
register(llm_time)
register(llm_random_choice)
6 changes: 6 additions & 0 deletions llm/tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
import time
from datetime import datetime, timezone
from importlib.metadata import version
Expand Down Expand Up @@ -35,3 +36,8 @@ def llm_time() -> dict:
"timezone_offset": timezone_offset,
"is_dst": is_dst,
}


def llm_random_choice(choices: list[str]) -> str:
"Return a random choice from a list of strings"
return random.choice(choices)
29 changes: 28 additions & 1 deletion tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import llm
from llm import CancelToolCall, cli
from llm.migrations import migrate
from llm.tools import llm_time
from llm.tools import llm_random_choice, llm_time

API_KEY = os.environ.get("PYTEST_OPENAI_API_KEY", None) or "badkey"

Expand Down Expand Up @@ -380,6 +380,33 @@ def test_default_tool_llm_time():
}


def test_default_tool_llm_random_choice(monkeypatch):
monkeypatch.setattr("llm.tools.random.choice", lambda choices: choices[-1])
runner = CliRunner()
result = runner.invoke(
cli.cli,
[
"-m",
"echo",
"-T",
"llm_random_choice",
json.dumps(
{
"tool_calls": [
{
"name": "llm_random_choice",
"arguments": {"choices": ["red", "green", "blue"]},
}
]
}
),
],
)
assert result.exit_code == 0
assert '"output": "blue"' in result.output
assert llm_random_choice(["one", "two"]) == "two"


def test_incorrect_tool_usage():
model = llm.get_model("echo")

Expand Down