65 lines
3.9 KiB
Python
65 lines
3.9 KiB
Python
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
SPEC = importlib.util.spec_from_file_location("tool_adapter", Path(__file__).resolve().parents[2] / "scripts/ollama-tool-call-adapter.py")
|
|
ADAPTER = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(ADAPTER)
|
|
PROXY_SPEC = importlib.util.spec_from_file_location("tool_proxy", Path(__file__).resolve().parents[2] / "scripts/ollama-tool-proxy.py")
|
|
PROXY = importlib.util.module_from_spec(PROXY_SPEC)
|
|
PROXY_SPEC.loader.exec_module(PROXY)
|
|
|
|
|
|
class ToolAdapterTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tools = [{"type": "function", "function": {"name": "get_status", "parameters": {
|
|
"type": "object", "properties": {"service": {"type": "string"}},
|
|
"required": ["service"], "additionalProperties": False}}}]
|
|
|
|
def test_valid_call(self):
|
|
result = ADAPTER.convert(json.dumps({"name": "get_status", "arguments": {"service": "demo"}}), self.tools)
|
|
self.assertEqual(result["function"]["arguments"], {"service": "demo"})
|
|
self.assertIsNotNone(ADAPTER.convert('```json\n{"name":"get_status","arguments":{"service":"demo"}}\n```', self.tools))
|
|
|
|
def test_invalid_or_ambiguous_calls_are_not_translated(self):
|
|
for value in ('prefix {"name":"get_status","arguments":{}}',
|
|
'prose\n```json\n{"name":"get_status","arguments":{"service":"demo"}}\n```',
|
|
'{"name":"unknown","arguments":{}}',
|
|
'{"name":"get_status","arguments":{"service":42}}',
|
|
'{"name":"get_status","arguments":{"service":"demo","extra":1}}',
|
|
'{"name":"get_status","name":"other","arguments":{"service":"demo"}}',
|
|
'{"name":"get_status","arguments":{"service":"demo"},"extra":true}'):
|
|
self.assertIsNone(ADAPTER.convert(value, self.tools))
|
|
self.assertIsNone(ADAPTER.convert('{"name":"get_status","arguments":{"service":"demo"}}', self.tools * 2))
|
|
|
|
def test_bash_only_excludes_other_tools(self):
|
|
bash = {"type": "function", "function": {"name": "bash", "parameters": {}}}
|
|
self.assertEqual(PROXY.bash_only(self.tools + [bash]), [bash])
|
|
with self.assertRaises(ValueError):
|
|
PROXY.bash_only(self.tools)
|
|
with self.assertRaises(ValueError):
|
|
PROXY.bash_only([bash, bash])
|
|
|
|
def test_first_prose_answer_retries_and_translates_only_valid_call(self):
|
|
tools = [{"type": "function", "function": {"name": "bash", "parameters": {
|
|
"type": "object", "properties": {"command": {"type": "string"},
|
|
"suggested_prefixes": {"type": "array", "items": {"type": "string"}}},
|
|
"required": ["command", "suggested_prefixes"]}}}]
|
|
request = {"tools": tools, "messages": [{"role": "user", "content": "investigate"}]}
|
|
plan = {"choices": [{"message": {"role": "assistant", "content": "Inspect the pod."}, "finish_reason": "stop"}]}
|
|
call = {"choices": [{"message": {"role": "assistant", "content": json.dumps({"name": "bash", "arguments": {
|
|
"command": "kubectl get pods", "suggested_prefixes": ["kubectl"]}})}, "finish_reason": "stop"}]}
|
|
with patch.object(PROXY, "query_ollama", side_effect=[plan, call]) as query:
|
|
result = PROXY.complete(request, True)
|
|
self.assertEqual(query.call_count, 2)
|
|
self.assertEqual(result["choices"][0]["message"]["tool_calls"][0]["function"]["name"], "bash")
|
|
|
|
def test_does_not_force_another_tool_after_tool_history(self):
|
|
request = {"tools": self.tools, "messages": [{"role": "tool", "content": "ok"}]}
|
|
plan = {"choices": [{"message": {"role": "assistant", "content": "Complete."}, "finish_reason": "stop"}]}
|
|
with patch.object(PROXY, "query_ollama", return_value=plan) as query:
|
|
PROXY.complete(request, True)
|
|
self.assertEqual(query.call_count, 1)
|