-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path_shared.py
More file actions
72 lines (59 loc) · 2.47 KB
/
Copy path_shared.py
File metadata and controls
72 lines (59 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
from typing import Any, Dict, Optional
from qveris import QverisClient, ToolInfo
def require_api_key() -> bool:
if os.getenv("QVERIS_API_KEY"):
return True
print("Set QVERIS_API_KEY to run this example against the QVeris API.")
return False
def should_call() -> bool:
return os.getenv("RUN_QVERIS_CALLS") == "1"
def sample_parameters(tool: ToolInfo, fallback: Dict[str, Any]) -> Dict[str, Any]:
if tool.examples and tool.examples.sample_parameters:
return tool.examples.sample_parameters
return fallback
async def preview_capability(
query: str,
fallback_params: Dict[str, Any],
*,
limit: int = 5,
max_response_size: Optional[int] = 4096,
) -> None:
if not require_api_key():
return
client = QverisClient()
try:
discovered = await client.discover(query, limit=limit)
print(f"search_id: {discovered.search_id}")
print(f"matches: {len(discovered.results)} / total={discovered.total}")
if not discovered.results:
return
tool = discovered.results[0]
inspected = await client.inspect([tool.tool_id], search_id=discovered.search_id)
tool = inspected.results[0] if inspected.results else tool
print(f"selected: {tool.tool_id} - {tool.name or tool.description or 'unnamed'}")
if tool.stats:
print(f"quality: success_rate={tool.stats.success_rate} latency_ms={tool.stats.avg_execution_time_ms}")
if tool.billing_rule:
print(f"billing: {tool.billing_rule.description or tool.billing_rule.metering_mode}")
params = sample_parameters(tool, fallback_params)
print(f"params: {params}")
if not should_call():
print("Set RUN_QVERIS_CALLS=1 to execute the selected capability.")
return
result = await client.call(
tool.tool_id,
params,
search_id=discovered.search_id,
max_response_size=max_response_size,
)
print(f"execution_id: {result.execution_id}")
print(f"success: {result.success}")
print(f"billing: {result.billing.summary if result.billing else None}")
print(f"result: {result.result}")
usage = await client.usage(execution_id=result.execution_id, summary=True, limit=5)
print(f"usage_records: {usage.total}")
ledger = await client.ledger(summary=True, limit=5)
print(f"ledger_records: {ledger.total}")
finally:
await client.close()