tooluniverse 1.0.3__py3-none-any.whl → 1.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of tooluniverse might be problematic. Click here for more details.
- tooluniverse/agentic_tool.py +262 -330
- tooluniverse/compose_scripts/output_summarizer.py +21 -15
- tooluniverse/data/output_summarization_tools.json +2 -2
- tooluniverse/llm_clients.py +369 -0
- tooluniverse/output_hook.py +92 -3
- tooluniverse/smcp_server.py +19 -13
- tooluniverse/test/list_azure_openai_models.py +210 -0
- tooluniverse/test/test_agentic_tool_azure_models.py +91 -0
- tooluniverse/test/test_api_key_validation_min.py +64 -0
- tooluniverse/test/test_global_fallback.py +288 -0
- tooluniverse/test/test_hooks_direct.py +219 -0
- tooluniverse/test/test_stdio_hooks.py +285 -0
- {tooluniverse-1.0.3.dist-info → tooluniverse-1.0.4.dist-info}/METADATA +2 -1
- {tooluniverse-1.0.3.dist-info → tooluniverse-1.0.4.dist-info}/RECORD +18 -11
- {tooluniverse-1.0.3.dist-info → tooluniverse-1.0.4.dist-info}/WHEEL +0 -0
- {tooluniverse-1.0.3.dist-info → tooluniverse-1.0.4.dist-info}/entry_points.txt +0 -0
- {tooluniverse-1.0.3.dist-info → tooluniverse-1.0.4.dist-info}/licenses/LICENSE +0 -0
- {tooluniverse-1.0.3.dist-info → tooluniverse-1.0.4.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
List Azure OpenAI deployments (deployed models) for the current resource.
|
|
4
|
+
|
|
5
|
+
Environment variables used:
|
|
6
|
+
- AZURE_OPENAI_ENDPOINT (required) e.g., https://<your-resource>.openai.azure.com
|
|
7
|
+
- AZURE_OPENAI_API_KEY (required)
|
|
8
|
+
- AZURE_OPENAI_API_VERSION (optional; default: 2024-12-01-preview)
|
|
9
|
+
|
|
10
|
+
This script queries the Azure OpenAI data-plane deployments endpoint:
|
|
11
|
+
GET {endpoint}/openai/deployments?api-version={api_version}
|
|
12
|
+
It also tries alternative paths and versions if the first attempt fails.
|
|
13
|
+
If REST fails, it falls back to listing models via the SDK (client.models.list()).
|
|
14
|
+
|
|
15
|
+
CLI options:
|
|
16
|
+
--rest-only Only use REST
|
|
17
|
+
--sdk-only Only use SDK fallback
|
|
18
|
+
--raw Print raw JSON result for REST (when available)
|
|
19
|
+
--versions v1 v2 Override API versions to try (space-separated)
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import argparse
|
|
23
|
+
import json
|
|
24
|
+
import os
|
|
25
|
+
import sys
|
|
26
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
27
|
+
|
|
28
|
+
import requests
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
DEFAULT_VERSIONS = [
|
|
32
|
+
# Common recent versions
|
|
33
|
+
"2024-12-01-preview",
|
|
34
|
+
"2024-10-21",
|
|
35
|
+
# Add more if needed
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def try_rest_once(
|
|
40
|
+
endpoint: str, api_key: str, api_version: str, path_variant: str
|
|
41
|
+
) -> Tuple[Optional[List[Dict[str, Any]]], Optional[Dict[str, Any]], Optional[str]]:
|
|
42
|
+
url = endpoint.rstrip("/") + f"{path_variant}?api-version={api_version}"
|
|
43
|
+
headers = {"api-key": api_key, "Content-Type": "application/json"}
|
|
44
|
+
try:
|
|
45
|
+
resp = requests.get(url, headers=headers, timeout=15)
|
|
46
|
+
resp.raise_for_status()
|
|
47
|
+
data = resp.json()
|
|
48
|
+
items = data.get("data") or data.get("value") or []
|
|
49
|
+
deployments: List[Dict[str, Any]] = []
|
|
50
|
+
for item in items:
|
|
51
|
+
deployments.append(
|
|
52
|
+
{
|
|
53
|
+
"id": item.get("id") or item.get("name"),
|
|
54
|
+
"name": item.get("name") or item.get("id"),
|
|
55
|
+
"model": (
|
|
56
|
+
(item.get("model") or {}).get("name")
|
|
57
|
+
if isinstance(item.get("model"), dict)
|
|
58
|
+
else item.get("model")
|
|
59
|
+
),
|
|
60
|
+
"model_format": item.get("model_format"),
|
|
61
|
+
"created": item.get("created"),
|
|
62
|
+
"status": item.get("status")
|
|
63
|
+
or item.get("provisioningState")
|
|
64
|
+
or item.get("provisioning_state"),
|
|
65
|
+
"properties": item.get("properties"),
|
|
66
|
+
}
|
|
67
|
+
)
|
|
68
|
+
return deployments, data, None
|
|
69
|
+
except Exception as e:
|
|
70
|
+
return None, None, f"{e} (url: {url})"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def list_deployments_via_rest(
|
|
74
|
+
endpoint: str, api_key: str, versions: List[str]
|
|
75
|
+
) -> Tuple[List[Dict[str, Any]], Optional[Dict[str, Any]], List[str]]:
|
|
76
|
+
errors: List[str] = []
|
|
77
|
+
raw: Optional[Dict[str, Any]] = None
|
|
78
|
+
# Try two common path variants
|
|
79
|
+
path_variants = ["/openai/deployments", "/deployments"]
|
|
80
|
+
for v in versions:
|
|
81
|
+
for pv in path_variants:
|
|
82
|
+
deployments, raw_json, err = try_rest_once(endpoint, api_key, v, pv)
|
|
83
|
+
if deployments is not None:
|
|
84
|
+
raw = raw_json
|
|
85
|
+
return deployments, raw, errors
|
|
86
|
+
if err:
|
|
87
|
+
errors.append(f"{v} {pv}: {err}")
|
|
88
|
+
return [], raw, errors
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def list_models_via_sdk(
|
|
92
|
+
endpoint: str, api_key: str, api_version: str
|
|
93
|
+
) -> List[Dict[str, Any]]:
|
|
94
|
+
try:
|
|
95
|
+
from openai import AzureOpenAI # type: ignore
|
|
96
|
+
except Exception as e: # pragma: no cover
|
|
97
|
+
raise RuntimeError("Failed to import openai AzureOpenAI client: %s" % e)
|
|
98
|
+
|
|
99
|
+
client = AzureOpenAI(
|
|
100
|
+
azure_endpoint=endpoint,
|
|
101
|
+
api_key=api_key,
|
|
102
|
+
api_version=api_version,
|
|
103
|
+
)
|
|
104
|
+
resp = client.models.list()
|
|
105
|
+
data = getattr(resp, "data", None) or []
|
|
106
|
+
models: List[Dict[str, Any]] = []
|
|
107
|
+
for m in data:
|
|
108
|
+
models.append(
|
|
109
|
+
{
|
|
110
|
+
"id": getattr(m, "id", None) or getattr(m, "root", None),
|
|
111
|
+
"owned_by": getattr(m, "owned_by", None),
|
|
112
|
+
"created": getattr(m, "created", None),
|
|
113
|
+
}
|
|
114
|
+
)
|
|
115
|
+
return models
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def main() -> None:
|
|
119
|
+
parser = argparse.ArgumentParser(description="List Azure OpenAI deployments/models")
|
|
120
|
+
parser.add_argument("--rest-only", action="store_true", help="Use REST only")
|
|
121
|
+
parser.add_argument("--sdk-only", action="store_true", help="Use SDK only")
|
|
122
|
+
parser.add_argument(
|
|
123
|
+
"--raw", action="store_true", help="Print raw JSON from REST when available"
|
|
124
|
+
)
|
|
125
|
+
parser.add_argument(
|
|
126
|
+
"--versions", nargs="*", help="API versions to try for REST (override)"
|
|
127
|
+
)
|
|
128
|
+
args = parser.parse_args()
|
|
129
|
+
|
|
130
|
+
endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
|
|
131
|
+
api_key = os.getenv("AZURE_OPENAI_API_KEY")
|
|
132
|
+
env_version = os.getenv("AZURE_OPENAI_API_VERSION")
|
|
133
|
+
|
|
134
|
+
if not endpoint or not api_key:
|
|
135
|
+
print("ERROR: Missing required environment variables.")
|
|
136
|
+
print(" - AZURE_OPENAI_ENDPOINT (current: %s)" % (endpoint or "<unset>"))
|
|
137
|
+
print(
|
|
138
|
+
" - AZURE_OPENAI_API_KEY (current: %s)"
|
|
139
|
+
% ("<set>" if api_key else "<unset>")
|
|
140
|
+
)
|
|
141
|
+
sys.exit(1)
|
|
142
|
+
|
|
143
|
+
versions = (
|
|
144
|
+
args.versions
|
|
145
|
+
if args.versions
|
|
146
|
+
else ([env_version] if env_version else DEFAULT_VERSIONS)
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
print("Listing Azure OpenAI deployments for resource:")
|
|
150
|
+
print(f" - Endpoint : {endpoint}")
|
|
151
|
+
print(f" - Versions : {', '.join([v for v in versions if v])}")
|
|
152
|
+
print()
|
|
153
|
+
|
|
154
|
+
deployments: List[Dict[str, Any]] = []
|
|
155
|
+
rest_errors: List[str] = []
|
|
156
|
+
raw: Optional[Dict[str, Any]] = None
|
|
157
|
+
|
|
158
|
+
if not args.sdk_only:
|
|
159
|
+
deployments, raw, rest_errors = list_deployments_via_rest(
|
|
160
|
+
endpoint, api_key, [v for v in versions if v]
|
|
161
|
+
)
|
|
162
|
+
if deployments:
|
|
163
|
+
print(f"Found {len(deployments)} deployment(s) via REST:")
|
|
164
|
+
for d in deployments:
|
|
165
|
+
print("- Deployment:")
|
|
166
|
+
print(f" name : {d.get('name')}")
|
|
167
|
+
print(f" id : {d.get('id')}")
|
|
168
|
+
print(f" model : {d.get('model')}")
|
|
169
|
+
print(f" status : {d.get('status')}")
|
|
170
|
+
if args.raw and raw is not None:
|
|
171
|
+
print("\nRaw JSON (REST):")
|
|
172
|
+
print(json.dumps(raw, indent=2, ensure_ascii=False))
|
|
173
|
+
print(
|
|
174
|
+
"\nTip: Use the 'name' (deployment name) as model_id in your requests."
|
|
175
|
+
)
|
|
176
|
+
return
|
|
177
|
+
else:
|
|
178
|
+
print("No deployments found via REST or REST not available.")
|
|
179
|
+
if rest_errors:
|
|
180
|
+
print("\nREST attempt details (for debugging):")
|
|
181
|
+
for e in rest_errors[:5]: # limit output
|
|
182
|
+
print(" -", e)
|
|
183
|
+
print()
|
|
184
|
+
|
|
185
|
+
if not args.rest_only:
|
|
186
|
+
api_version_for_sdk = env_version or DEFAULT_VERSIONS[0]
|
|
187
|
+
try:
|
|
188
|
+
models = list_models_via_sdk(endpoint, api_key, api_version_for_sdk)
|
|
189
|
+
if models:
|
|
190
|
+
print(f"Found {len(models)} model(s) via SDK:")
|
|
191
|
+
for m in models[:200]:
|
|
192
|
+
print("- Model (SDK):")
|
|
193
|
+
print(f" id : {m.get('id')}")
|
|
194
|
+
if m.get("owned_by") is not None:
|
|
195
|
+
print(f" owned_by: {m.get('owned_by')}")
|
|
196
|
+
if m.get("created") is not None:
|
|
197
|
+
print(f" created : {m.get('created')}")
|
|
198
|
+
print()
|
|
199
|
+
print(
|
|
200
|
+
"Note: SDK list may show global IDs; real calls require the deployment name."
|
|
201
|
+
)
|
|
202
|
+
else:
|
|
203
|
+
print("No models found via SDK either.")
|
|
204
|
+
except Exception as e:
|
|
205
|
+
print("ERROR: Unable to list models via SDK: %s" % e)
|
|
206
|
+
sys.exit(2)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
if __name__ == "__main__":
|
|
210
|
+
main()
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Batch test: AgenticTool with multiple Azure OpenAI deployments.
|
|
4
|
+
- Validates API key during initialization
|
|
5
|
+
- Performs a tiny run call per model (1 token)
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import sys
|
|
10
|
+
from typing import List
|
|
11
|
+
|
|
12
|
+
# Ensure src/ is importable
|
|
13
|
+
CURRENT_DIR = os.path.dirname(__file__)
|
|
14
|
+
SRC_DIR = os.path.join(CURRENT_DIR, "src")
|
|
15
|
+
if SRC_DIR not in sys.path:
|
|
16
|
+
sys.path.insert(0, SRC_DIR)
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
from tooluniverse.agentic_tool import AgenticTool # type: ignore
|
|
20
|
+
except ImportError:
|
|
21
|
+
# Fallback for when running from different directory
|
|
22
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))
|
|
23
|
+
from tooluniverse.agentic_tool import AgenticTool # type: ignore
|
|
24
|
+
|
|
25
|
+
# Chat-capable deployment IDs to test (skip embeddings)
|
|
26
|
+
MODELS: List[str] = [
|
|
27
|
+
"gpt-4.1",
|
|
28
|
+
"gpt-4.1-mini",
|
|
29
|
+
"gpt-4.1-nano",
|
|
30
|
+
"gpt-4o-1120",
|
|
31
|
+
"gpt-4o-0806",
|
|
32
|
+
"gpt-4o-mini-0718",
|
|
33
|
+
"o4-mini-0416",
|
|
34
|
+
"o3-mini-0131",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_model(model_id: str) -> None:
|
|
39
|
+
print(f"\n=== Testing model: {model_id} ===")
|
|
40
|
+
config = {
|
|
41
|
+
"name": f"agentic_test_{model_id}",
|
|
42
|
+
"description": "AgenticTool model validation",
|
|
43
|
+
"type": "AgenticTool",
|
|
44
|
+
"prompt": "Echo: {q}",
|
|
45
|
+
"input_arguments": ["q"],
|
|
46
|
+
"parameter": {
|
|
47
|
+
"type": "object",
|
|
48
|
+
"properties": {
|
|
49
|
+
"q": {"type": "string", "description": "input", "required": True}
|
|
50
|
+
},
|
|
51
|
+
"required": ["q"],
|
|
52
|
+
},
|
|
53
|
+
"configs": {
|
|
54
|
+
"api_type": "CHATGPT",
|
|
55
|
+
"model_id": model_id,
|
|
56
|
+
"validate_api_key": True,
|
|
57
|
+
"temperature": 0.0,
|
|
58
|
+
"max_new_tokens": 1,
|
|
59
|
+
"return_json": False,
|
|
60
|
+
"return_metadata": False,
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
tool = AgenticTool(config)
|
|
66
|
+
print("- Init: OK (API key validated)")
|
|
67
|
+
except Exception as e:
|
|
68
|
+
print(f"- Init: FAIL -> {e}")
|
|
69
|
+
return
|
|
70
|
+
|
|
71
|
+
try:
|
|
72
|
+
out = tool.run({"q": "ping"})
|
|
73
|
+
ok = isinstance(out, (str, dict))
|
|
74
|
+
print(
|
|
75
|
+
f"- Run : {'OK' if ok else 'WARN'} -> {str(out)[:120].replace('\n', ' ')}"
|
|
76
|
+
)
|
|
77
|
+
except Exception as e:
|
|
78
|
+
print(f"- Run : FAIL -> {e}")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def main() -> None:
|
|
82
|
+
print("Azure endpoint:", os.getenv("AZURE_OPENAI_ENDPOINT"))
|
|
83
|
+
print("Using per-model versions:")
|
|
84
|
+
print(os.getenv("AZURE_OPENAI_API_VERSION_BY_MODEL", "<none>"))
|
|
85
|
+
|
|
86
|
+
for m in MODELS:
|
|
87
|
+
test_model(m)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
if __name__ == "__main__":
|
|
91
|
+
main()
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Minimal test: validate API key during AgenticTool initialization.
|
|
4
|
+
- Success: prints OK and model info
|
|
5
|
+
- Failure: prints the error message (e.g., invalid/missing key or model not deployed)
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import sys
|
|
10
|
+
|
|
11
|
+
# Ensure src/ is importable
|
|
12
|
+
CURRENT_DIR = os.path.dirname(__file__)
|
|
13
|
+
SRC_DIR = os.path.join(CURRENT_DIR, "src")
|
|
14
|
+
if SRC_DIR not in sys.path:
|
|
15
|
+
sys.path.insert(0, SRC_DIR)
|
|
16
|
+
|
|
17
|
+
try:
|
|
18
|
+
from tooluniverse.agentic_tool import AgenticTool # type: ignore
|
|
19
|
+
except ImportError:
|
|
20
|
+
# Fallback for when running from different directory
|
|
21
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))
|
|
22
|
+
from tooluniverse.agentic_tool import AgenticTool # type: ignore
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def main() -> None:
|
|
26
|
+
config = {
|
|
27
|
+
"name": "api_key_validation_test",
|
|
28
|
+
"description": "Minimal API key validation test",
|
|
29
|
+
"type": "AgenticTool",
|
|
30
|
+
"prompt": "Test: {q}",
|
|
31
|
+
"input_arguments": ["q"],
|
|
32
|
+
"parameter": {
|
|
33
|
+
"type": "object",
|
|
34
|
+
"properties": {
|
|
35
|
+
"q": {
|
|
36
|
+
"type": "string",
|
|
37
|
+
"description": "placeholder",
|
|
38
|
+
"required": True,
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"required": ["q"],
|
|
42
|
+
},
|
|
43
|
+
"configs": {
|
|
44
|
+
"api_type": "CHATGPT", # or "GEMINI" if you prefer
|
|
45
|
+
"model_id": "gpt-4o-1120", # requested model to test
|
|
46
|
+
"validate_api_key": True, # this triggers validation during init
|
|
47
|
+
"temperature": 0.0,
|
|
48
|
+
"max_new_tokens": 1,
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
print("Running minimal API key validation test...")
|
|
53
|
+
try:
|
|
54
|
+
tool = AgenticTool(config)
|
|
55
|
+
info = tool.get_model_info()
|
|
56
|
+
print("OK: initialization succeeded and API key validated.")
|
|
57
|
+
print(f"Model info: {info}")
|
|
58
|
+
except Exception as e:
|
|
59
|
+
print("ERROR: initialization failed during API key validation.")
|
|
60
|
+
print(f"Reason: {e}")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
if __name__ == "__main__":
|
|
64
|
+
main()
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Test script to verify global fallback functionality in AgenticTool.
|
|
4
|
+
Tests the system-wide default fallback chain.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import json
|
|
9
|
+
from src.tooluniverse.agentic_tool import AgenticTool
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_default_global_fallback():
|
|
13
|
+
"""Test: Tool uses default global fallback chain when no explicit fallback is configured."""
|
|
14
|
+
|
|
15
|
+
print("=== Test 1: Default Global Fallback Chain ===\n")
|
|
16
|
+
|
|
17
|
+
# Remove Gemini API key to force fallback
|
|
18
|
+
original_gemini_key = os.environ.get("GEMINI_API_KEY")
|
|
19
|
+
if "GEMINI_API_KEY" in os.environ:
|
|
20
|
+
del os.environ["GEMINI_API_KEY"]
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
# Create tool with only primary API (no explicit fallback)
|
|
24
|
+
tool = AgenticTool(
|
|
25
|
+
{
|
|
26
|
+
"name": "gemini_with_global_fallback",
|
|
27
|
+
"api_type": "GEMINI",
|
|
28
|
+
"model_id": "gemini-2.0-flash",
|
|
29
|
+
# No fallback_api_type configured - should use global fallback
|
|
30
|
+
"prompt": "You are a helpful assistant. Answer: {question}",
|
|
31
|
+
"input_arguments": ["question"],
|
|
32
|
+
}
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
print(f"Tool available: {tool.is_available()}")
|
|
36
|
+
print(f"Current API: {tool._current_api_type}")
|
|
37
|
+
print(f"Current model: {tool._current_model_id}")
|
|
38
|
+
print(f"Original config: {tool._api_type} with {tool._model_id}")
|
|
39
|
+
print(f"Global fallback enabled: {tool._use_global_fallback}")
|
|
40
|
+
print(f"Global fallback chain: {tool._global_fallback_chain}")
|
|
41
|
+
|
|
42
|
+
if tool.is_available():
|
|
43
|
+
result = tool.run({"question": "What is 2+2?"})
|
|
44
|
+
if result.get("success"):
|
|
45
|
+
print(f"✅ Tool worked with global fallback: {result['result']}")
|
|
46
|
+
else:
|
|
47
|
+
print(f"❌ Tool failed: {result['error']}")
|
|
48
|
+
else:
|
|
49
|
+
print(f"❌ Tool not available: {tool._initialization_error}")
|
|
50
|
+
|
|
51
|
+
finally:
|
|
52
|
+
# Restore Gemini key
|
|
53
|
+
if original_gemini_key:
|
|
54
|
+
os.environ["GEMINI_API_KEY"] = original_gemini_key
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_custom_global_fallback():
|
|
58
|
+
"""Test: Tool uses custom global fallback chain from environment variable."""
|
|
59
|
+
|
|
60
|
+
print("\n=== Test 2: Custom Global Fallback Chain ===\n")
|
|
61
|
+
|
|
62
|
+
# Set custom global fallback chain
|
|
63
|
+
custom_chain = [
|
|
64
|
+
{"api_type": "GEMINI", "model_id": "gemini-2.0-flash"},
|
|
65
|
+
{"api_type": "CHATGPT", "model_id": "gpt-4o-mini-0718"},
|
|
66
|
+
]
|
|
67
|
+
os.environ["AGENTIC_TOOL_FALLBACK_CHAIN"] = json.dumps(custom_chain)
|
|
68
|
+
|
|
69
|
+
# Remove Azure OpenAI API key to force fallback
|
|
70
|
+
original_azure_key = os.environ.get("AZURE_OPENAI_API_KEY")
|
|
71
|
+
if "AZURE_OPENAI_API_KEY" in os.environ:
|
|
72
|
+
del os.environ["AZURE_OPENAI_API_KEY"]
|
|
73
|
+
|
|
74
|
+
try:
|
|
75
|
+
# Create tool with ChatGPT as primary
|
|
76
|
+
tool = AgenticTool(
|
|
77
|
+
{
|
|
78
|
+
"name": "chatgpt_with_custom_global_fallback",
|
|
79
|
+
"api_type": "CHATGPT",
|
|
80
|
+
"model_id": "gpt-4o-1120",
|
|
81
|
+
"prompt": "You are a helpful assistant. Answer: {question}",
|
|
82
|
+
"input_arguments": ["question"],
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
print(f"Tool available: {tool.is_available()}")
|
|
87
|
+
print(f"Current API: {tool._current_api_type}")
|
|
88
|
+
print(f"Current model: {tool._current_model_id}")
|
|
89
|
+
print(f"Original config: {tool._api_type} with {tool._model_id}")
|
|
90
|
+
print(f"Custom global fallback chain: {tool._global_fallback_chain}")
|
|
91
|
+
|
|
92
|
+
if tool.is_available():
|
|
93
|
+
result = tool.run({"question": "What is 2+2?"})
|
|
94
|
+
if result.get("success"):
|
|
95
|
+
print(f"✅ Tool worked with custom global fallback: {result['result']}")
|
|
96
|
+
else:
|
|
97
|
+
print(f"❌ Tool failed: {result['error']}")
|
|
98
|
+
else:
|
|
99
|
+
print(f"❌ Tool not available: {tool._initialization_error}")
|
|
100
|
+
|
|
101
|
+
finally:
|
|
102
|
+
# Restore Azure key and remove custom chain
|
|
103
|
+
if original_azure_key:
|
|
104
|
+
os.environ["AZURE_OPENAI_API_KEY"] = original_azure_key
|
|
105
|
+
if "AGENTIC_TOOL_FALLBACK_CHAIN" in os.environ:
|
|
106
|
+
del os.environ["AGENTIC_TOOL_FALLBACK_CHAIN"]
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def test_disable_global_fallback():
|
|
110
|
+
"""Test: Tool can disable global fallback."""
|
|
111
|
+
|
|
112
|
+
print("\n=== Test 3: Disable Global Fallback ===\n")
|
|
113
|
+
|
|
114
|
+
# Remove Azure OpenAI API key
|
|
115
|
+
original_azure_key = os.environ.get("AZURE_OPENAI_API_KEY")
|
|
116
|
+
if "AZURE_OPENAI_API_KEY" in os.environ:
|
|
117
|
+
del os.environ["AZURE_OPENAI_API_KEY"]
|
|
118
|
+
|
|
119
|
+
try:
|
|
120
|
+
# Create tool with global fallback disabled
|
|
121
|
+
tool = AgenticTool(
|
|
122
|
+
{
|
|
123
|
+
"name": "no_global_fallback",
|
|
124
|
+
"api_type": "CHATGPT",
|
|
125
|
+
"model_id": "gpt-4o-1120",
|
|
126
|
+
"use_global_fallback": False, # Disable global fallback
|
|
127
|
+
"prompt": "You are a helpful assistant. Answer: {question}",
|
|
128
|
+
"input_arguments": ["question"],
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
print(f"Tool available: {tool.is_available()}")
|
|
133
|
+
print(f"Current API: {tool._current_api_type}")
|
|
134
|
+
print(f"Current model: {tool._current_model_id}")
|
|
135
|
+
print(f"Global fallback enabled: {tool._use_global_fallback}")
|
|
136
|
+
|
|
137
|
+
if tool.is_available():
|
|
138
|
+
result = tool.run({"question": "What is 2+2?"})
|
|
139
|
+
print(f"Unexpected success: {result}")
|
|
140
|
+
else:
|
|
141
|
+
print(
|
|
142
|
+
f"✅ Tool correctly failed without global fallback: {tool._initialization_error}"
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
finally:
|
|
146
|
+
# Restore Azure key
|
|
147
|
+
if original_azure_key:
|
|
148
|
+
os.environ["AZURE_OPENAI_API_KEY"] = original_azure_key
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_explicit_fallback_priority():
|
|
152
|
+
"""Test: Explicit fallback takes priority over global fallback."""
|
|
153
|
+
|
|
154
|
+
print("\n=== Test 4: Explicit Fallback Priority ===\n")
|
|
155
|
+
|
|
156
|
+
# Remove Gemini API key
|
|
157
|
+
original_gemini_key = os.environ.get("GEMINI_API_KEY")
|
|
158
|
+
if "GEMINI_API_KEY" in os.environ:
|
|
159
|
+
del os.environ["GEMINI_API_KEY"]
|
|
160
|
+
|
|
161
|
+
try:
|
|
162
|
+
# Create tool with both explicit and global fallback
|
|
163
|
+
tool = AgenticTool(
|
|
164
|
+
{
|
|
165
|
+
"name": "explicit_vs_global_fallback",
|
|
166
|
+
"api_type": "GEMINI",
|
|
167
|
+
"model_id": "gemini-2.0-flash",
|
|
168
|
+
"fallback_api_type": "CHATGPT", # Explicit fallback
|
|
169
|
+
"fallback_model_id": "gpt-4o-mini-0718", # Different from global default
|
|
170
|
+
"prompt": "You are a helpful assistant. Answer: {question}",
|
|
171
|
+
"input_arguments": ["question"],
|
|
172
|
+
}
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
print(f"Tool available: {tool.is_available()}")
|
|
176
|
+
print(f"Current API: {tool._current_api_type}")
|
|
177
|
+
print(f"Current model: {tool._current_model_id}")
|
|
178
|
+
print(
|
|
179
|
+
f"Explicit fallback: {tool._fallback_api_type} ({tool._fallback_model_id})"
|
|
180
|
+
)
|
|
181
|
+
print(f"Global fallback chain: {tool._global_fallback_chain}")
|
|
182
|
+
|
|
183
|
+
if tool.is_available():
|
|
184
|
+
result = tool.run({"question": "What is 2+2?"})
|
|
185
|
+
if result.get("success"):
|
|
186
|
+
print(f"✅ Tool worked with explicit fallback: {result['result']}")
|
|
187
|
+
# Check if it used explicit fallback (gpt-4o-mini-0718) instead of global default (gpt-4o-1120)
|
|
188
|
+
if tool._current_model_id == "gpt-4o-mini-0718":
|
|
189
|
+
print("✅ Used explicit fallback as expected")
|
|
190
|
+
else:
|
|
191
|
+
print(
|
|
192
|
+
f"⚠️ Used {tool._current_model_id} instead of explicit fallback"
|
|
193
|
+
)
|
|
194
|
+
else:
|
|
195
|
+
print(f"❌ Tool failed: {result['error']}")
|
|
196
|
+
else:
|
|
197
|
+
print(f"❌ Tool not available: {tool._initialization_error}")
|
|
198
|
+
|
|
199
|
+
finally:
|
|
200
|
+
# Restore Gemini key
|
|
201
|
+
if original_gemini_key:
|
|
202
|
+
os.environ["GEMINI_API_KEY"] = original_gemini_key
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_multiple_tools_global_fallback():
|
|
206
|
+
"""Test: Multiple tools using global fallback."""
|
|
207
|
+
|
|
208
|
+
print("\n=== Test 5: Multiple Tools with Global Fallback ===\n")
|
|
209
|
+
|
|
210
|
+
# Remove Gemini API key to force fallback for Gemini tools
|
|
211
|
+
original_gemini_key = os.environ.get("GEMINI_API_KEY")
|
|
212
|
+
if "GEMINI_API_KEY" in os.environ:
|
|
213
|
+
del os.environ["GEMINI_API_KEY"]
|
|
214
|
+
|
|
215
|
+
try:
|
|
216
|
+
tools_config = [
|
|
217
|
+
{
|
|
218
|
+
"name": "gemini_tool_1",
|
|
219
|
+
"api_type": "GEMINI",
|
|
220
|
+
"model_id": "gemini-2.0-flash",
|
|
221
|
+
"prompt": "Gemini tool 1: {question}",
|
|
222
|
+
"input_arguments": ["question"],
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
"name": "gemini_tool_2",
|
|
226
|
+
"api_type": "GEMINI",
|
|
227
|
+
"model_id": "gemini-2.0-flash",
|
|
228
|
+
"prompt": "Gemini tool 2: {question}",
|
|
229
|
+
"input_arguments": ["question"],
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
"name": "chatgpt_tool",
|
|
233
|
+
"api_type": "CHATGPT",
|
|
234
|
+
"model_id": "gpt-4o-1120",
|
|
235
|
+
"prompt": "ChatGPT tool: {question}",
|
|
236
|
+
"input_arguments": ["question"],
|
|
237
|
+
},
|
|
238
|
+
]
|
|
239
|
+
|
|
240
|
+
tools = []
|
|
241
|
+
for config in tools_config:
|
|
242
|
+
tool = AgenticTool(config)
|
|
243
|
+
tools.append(tool)
|
|
244
|
+
|
|
245
|
+
status = "✅ Available" if tool.is_available() else "❌ Unavailable"
|
|
246
|
+
current_api = (
|
|
247
|
+
f"{tool._current_api_type} ({tool._current_model_id})"
|
|
248
|
+
if tool._current_api_type
|
|
249
|
+
else "None"
|
|
250
|
+
)
|
|
251
|
+
print(f"{config['name']}: {status} - Using: {current_api}")
|
|
252
|
+
|
|
253
|
+
print()
|
|
254
|
+
|
|
255
|
+
# Test all available tools
|
|
256
|
+
available_tools = [tool for tool in tools if tool.is_available()]
|
|
257
|
+
if available_tools:
|
|
258
|
+
print("=== Testing Available Tools ===")
|
|
259
|
+
for tool in available_tools:
|
|
260
|
+
result = tool.run({"question": "Hello!"})
|
|
261
|
+
if result.get("success"):
|
|
262
|
+
print(f"✅ {tool.name}: {result['result'][:50]}...")
|
|
263
|
+
else:
|
|
264
|
+
print(f"❌ {tool.name}: {result['error']}")
|
|
265
|
+
|
|
266
|
+
finally:
|
|
267
|
+
# Restore Gemini key
|
|
268
|
+
if original_gemini_key:
|
|
269
|
+
os.environ["GEMINI_API_KEY"] = original_gemini_key
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
if __name__ == "__main__":
|
|
273
|
+
test_default_global_fallback()
|
|
274
|
+
test_custom_global_fallback()
|
|
275
|
+
test_disable_global_fallback()
|
|
276
|
+
test_explicit_fallback_priority()
|
|
277
|
+
test_multiple_tools_global_fallback()
|
|
278
|
+
|
|
279
|
+
print("\n=== Summary ===")
|
|
280
|
+
print("✅ Global fallback chain works as default")
|
|
281
|
+
print("✅ Custom global fallback chain from environment variable")
|
|
282
|
+
print("✅ Global fallback can be disabled per tool")
|
|
283
|
+
print("✅ Explicit fallback takes priority over global fallback")
|
|
284
|
+
print("✅ Multiple tools can use global fallback independently")
|
|
285
|
+
print("\n=== Configuration Options ===")
|
|
286
|
+
print("• use_global_fallback: true/false (default: true)")
|
|
287
|
+
print("• AGENTIC_TOOL_FALLBACK_CHAIN: JSON array of {api_type, model_id}")
|
|
288
|
+
print("• Explicit fallback_api_type/fallback_model_id takes priority")
|