veris-ai 0.1.0__py3-none-any.whl → 0.2.0__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 veris-ai might be problematic. Click here for more details.
- veris_ai/tool_mock.py +67 -51
- {veris_ai-0.1.0.dist-info → veris_ai-0.2.0.dist-info}/METADATA +1 -3
- veris_ai-0.2.0.dist-info/RECORD +6 -0
- veris_ai-0.1.0.dist-info/RECORD +0 -6
- {veris_ai-0.1.0.dist-info → veris_ai-0.2.0.dist-info}/WHEEL +0 -0
- {veris_ai-0.1.0.dist-info → veris_ai-0.2.0.dist-info}/licenses/LICENSE +0 -0
veris_ai/tool_mock.py
CHANGED
|
@@ -3,62 +3,81 @@ import json
|
|
|
3
3
|
import logging
|
|
4
4
|
import os
|
|
5
5
|
from collections.abc import Callable
|
|
6
|
+
from contextlib import suppress
|
|
6
7
|
from functools import wraps
|
|
7
|
-
from typing import Any, Union, get_type_hints
|
|
8
|
+
from typing import Any, TypeVar, Union, get_args, get_origin, get_type_hints
|
|
8
9
|
|
|
9
10
|
import httpx
|
|
10
11
|
|
|
11
12
|
logger = logging.getLogger(__name__)
|
|
12
13
|
|
|
14
|
+
T = TypeVar("T") # Generic type for return value
|
|
15
|
+
|
|
16
|
+
|
|
13
17
|
class ToolMock:
|
|
14
18
|
"""Class for mocking tool calls."""
|
|
15
|
-
def __init__(self):
|
|
16
|
-
self.endpoint = os.getenv("VERIS_MOCK_ENDPOINT_URL")
|
|
17
|
-
if not self.endpoint:
|
|
18
|
-
raise ValueError("VERIS_MOCK_ENDPOINT_URL environment variable is not set")
|
|
19
|
-
# Default timeout of 30 seconds
|
|
20
|
-
self.timeout = float(os.getenv("VERIS_MOCK_TIMEOUT", "30.0"))
|
|
21
19
|
|
|
22
|
-
def
|
|
20
|
+
def __init__(self) -> None:
|
|
21
|
+
"""Initialize the ToolMock class."""
|
|
22
|
+
|
|
23
|
+
def _convert_to_type(self, value: object, target_type: type) -> object:
|
|
23
24
|
"""Convert a value to the specified type."""
|
|
24
|
-
if target_type
|
|
25
|
+
if target_type is Any:
|
|
25
26
|
return value
|
|
26
|
-
|
|
27
|
+
|
|
27
28
|
# Handle basic types
|
|
28
29
|
if target_type in (str, int, float, bool):
|
|
29
30
|
return target_type(value)
|
|
30
|
-
|
|
31
|
+
|
|
31
32
|
# Handle List types
|
|
32
|
-
|
|
33
|
+
origin = get_origin(target_type)
|
|
34
|
+
if origin is list:
|
|
33
35
|
if not isinstance(value, list):
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
error_msg = f"Expected list but got {type(value)}"
|
|
37
|
+
raise ValueError(error_msg)
|
|
38
|
+
item_type = get_args(target_type)[0]
|
|
36
39
|
return [self._convert_to_type(item, item_type) for item in value]
|
|
37
|
-
|
|
40
|
+
|
|
38
41
|
# Handle Dict types
|
|
39
|
-
if
|
|
42
|
+
if origin is dict:
|
|
40
43
|
if not isinstance(value, dict):
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
error_msg = f"Expected dict but got {type(value)}"
|
|
45
|
+
raise ValueError(error_msg)
|
|
46
|
+
key_type, value_type = get_args(target_type)
|
|
47
|
+
return {
|
|
48
|
+
self._convert_to_type(k, key_type): self._convert_to_type(v, value_type)
|
|
49
|
+
for k, v in value.items()
|
|
50
|
+
}
|
|
51
|
+
|
|
46
52
|
# Handle Union types
|
|
47
|
-
if
|
|
48
|
-
|
|
53
|
+
if origin is Union:
|
|
54
|
+
error_msg = (
|
|
55
|
+
f"Could not convert {value} to any of the union types {get_args(target_type)}"
|
|
56
|
+
)
|
|
57
|
+
for possible_type in get_args(target_type):
|
|
49
58
|
try:
|
|
50
59
|
return self._convert_to_type(value, possible_type)
|
|
51
60
|
except (ValueError, TypeError):
|
|
52
61
|
continue
|
|
53
|
-
raise ValueError(
|
|
54
|
-
|
|
62
|
+
raise ValueError(error_msg)
|
|
63
|
+
|
|
55
64
|
# For other types, try direct conversion
|
|
56
65
|
return target_type(value)
|
|
57
66
|
|
|
58
67
|
def mock(self, func: Callable) -> Callable:
|
|
59
68
|
"""Decorator for mocking tool calls."""
|
|
69
|
+
endpoint = os.getenv("VERIS_MOCK_ENDPOINT_URL")
|
|
70
|
+
if not endpoint:
|
|
71
|
+
error_msg = "VERIS_MOCK_ENDPOINT_URL environment variable is not set"
|
|
72
|
+
raise ValueError(error_msg)
|
|
73
|
+
# Default timeout of 30 seconds
|
|
74
|
+
timeout = float(os.getenv("VERIS_MOCK_TIMEOUT", "30.0"))
|
|
75
|
+
|
|
60
76
|
@wraps(func)
|
|
61
|
-
async def wrapper(
|
|
77
|
+
async def wrapper(
|
|
78
|
+
*args: tuple[object, ...],
|
|
79
|
+
**kwargs: dict[str, object],
|
|
80
|
+
) -> object:
|
|
62
81
|
# Check if we're in simulation mode
|
|
63
82
|
env_mode = os.getenv("ENV", "").lower()
|
|
64
83
|
if env_mode != "simulation":
|
|
@@ -68,25 +87,16 @@ class ToolMock:
|
|
|
68
87
|
logger.info(f"Simulating function: {func.__name__}")
|
|
69
88
|
sig = inspect.signature(func)
|
|
70
89
|
type_hints = get_type_hints(func)
|
|
71
|
-
|
|
90
|
+
|
|
72
91
|
# Extract return type object (not just the name)
|
|
73
92
|
return_type_obj = type_hints.pop("return", Any)
|
|
74
|
-
|
|
93
|
+
|
|
75
94
|
# Create parameter info
|
|
76
95
|
params_info = {}
|
|
77
96
|
bound_args = sig.bind(*args, **kwargs)
|
|
78
97
|
bound_args.apply_defaults()
|
|
79
|
-
|
|
80
|
-
for param_name, param_value in bound_args.arguments.items():
|
|
81
|
-
params_info[param_name] = {
|
|
82
|
-
"value": param_value,
|
|
83
|
-
"type": type_hints.get(param_name, Any).__name__,
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
# Get function docstring
|
|
87
|
-
docstring = inspect.getdoc(func) or ""
|
|
88
98
|
|
|
89
|
-
ctx = bound_args.arguments.pop(
|
|
99
|
+
ctx = bound_args.arguments.pop("ctx", None)
|
|
90
100
|
session_id = None
|
|
91
101
|
if ctx:
|
|
92
102
|
try:
|
|
@@ -94,35 +104,41 @@ class ToolMock:
|
|
|
94
104
|
except AttributeError:
|
|
95
105
|
logger.warning("Cannot get session_id from context.")
|
|
96
106
|
|
|
107
|
+
for param_name, param_value in bound_args.arguments.items():
|
|
108
|
+
params_info[param_name] = {
|
|
109
|
+
"value": param_value,
|
|
110
|
+
"type": type_hints.get(param_name, Any).__name__,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
# Get function docstring
|
|
114
|
+
docstring = inspect.getdoc(func) or ""
|
|
97
115
|
# Prepare payload
|
|
98
116
|
payload = {
|
|
99
117
|
"session_id": session_id,
|
|
100
118
|
"tool_call": {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
119
|
+
"function_name": func.__name__,
|
|
120
|
+
"parameters": params_info,
|
|
121
|
+
"return_type": return_type_obj.__name__,
|
|
122
|
+
"docstring": docstring,
|
|
123
|
+
},
|
|
106
124
|
}
|
|
107
125
|
|
|
108
126
|
# Send request to endpoint with timeout
|
|
109
|
-
async with httpx.AsyncClient(timeout=
|
|
110
|
-
response = await client.post(
|
|
127
|
+
async with httpx.AsyncClient(timeout=timeout) as client:
|
|
128
|
+
response = await client.post(endpoint, json=payload)
|
|
111
129
|
response.raise_for_status()
|
|
112
130
|
mock_result = response.json()["result"]
|
|
113
131
|
logger.info(f"Mock response: {mock_result}")
|
|
114
|
-
|
|
132
|
+
|
|
115
133
|
# Parse the mock result if it's a string
|
|
116
134
|
if isinstance(mock_result, str):
|
|
117
|
-
|
|
135
|
+
with suppress(json.JSONDecodeError):
|
|
118
136
|
mock_result = json.loads(mock_result)
|
|
119
|
-
|
|
120
|
-
# If it's not valid JSON, treat it as a raw string
|
|
121
|
-
pass
|
|
122
|
-
|
|
137
|
+
|
|
123
138
|
# Convert the mock result to the expected return type
|
|
124
139
|
return self._convert_to_type(mock_result, return_type_obj)
|
|
125
140
|
|
|
126
141
|
return wrapper
|
|
127
142
|
|
|
143
|
+
|
|
128
144
|
veris = ToolMock()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: veris-ai
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: A Python package for Veris AI tools
|
|
5
5
|
Project-URL: Homepage, https://github.com/veris-ai/veris-python-sdk
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/veris-ai/veris-python-sdk/issues
|
|
@@ -9,8 +9,6 @@ License-Expression: MIT
|
|
|
9
9
|
License-File: LICENSE
|
|
10
10
|
Requires-Python: >=3.11
|
|
11
11
|
Requires-Dist: httpx>=0.24.0
|
|
12
|
-
Requires-Dist: python-dotenv>=1.0.0
|
|
13
|
-
Requires-Dist: typing-extensions>=4.0.0
|
|
14
12
|
Provides-Extra: dev
|
|
15
13
|
Requires-Dist: black>=23.7.0; extra == 'dev'
|
|
16
14
|
Requires-Dist: mypy>=1.5.1; extra == 'dev'
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
veris_ai/__init__.py,sha256=7mEfVmqxQCn5FZN_1ua3iykCB1NUeJ4RfVXaAzdp4Hw,101
|
|
2
|
+
veris_ai/tool_mock.py,sha256=7qFM35lz10k5i2X-XQVYcuFNF0bczEnHTr0vtineQrM,5172
|
|
3
|
+
veris_ai-0.2.0.dist-info/METADATA,sha256=rD-TyXPbDZfJcQCtNuUUA4KGYD6PAFXQIhB5SL1lyNg,3419
|
|
4
|
+
veris_ai-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
5
|
+
veris_ai-0.2.0.dist-info/licenses/LICENSE,sha256=2g4i20atAgtD5einaKzhQrIB-JrPhyQgD3bC0wkHcCI,1065
|
|
6
|
+
veris_ai-0.2.0.dist-info/RECORD,,
|
veris_ai-0.1.0.dist-info/RECORD
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
veris_ai/__init__.py,sha256=7mEfVmqxQCn5FZN_1ua3iykCB1NUeJ4RfVXaAzdp4Hw,101
|
|
2
|
-
veris_ai/tool_mock.py,sha256=NTaBr8xkAp3tiB3naWCcm27KWO5JSzcr9BcurfhlCfs,5125
|
|
3
|
-
veris_ai-0.1.0.dist-info/METADATA,sha256=DzCmToKvOycKfE3ilpcEEl7mlz0a0w5yITXemoc4DWU,3495
|
|
4
|
-
veris_ai-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
5
|
-
veris_ai-0.1.0.dist-info/licenses/LICENSE,sha256=2g4i20atAgtD5einaKzhQrIB-JrPhyQgD3bC0wkHcCI,1065
|
|
6
|
-
veris_ai-0.1.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|