aiverify-moonshot 0.4.11__py3-none-any.whl → 0.5.1__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.
- {aiverify_moonshot-0.4.11.dist-info → aiverify_moonshot-0.5.1.dist-info}/METADATA +6 -5
- {aiverify_moonshot-0.4.11.dist-info → aiverify_moonshot-0.5.1.dist-info}/RECORD +39 -39
- {aiverify_moonshot-0.4.11.dist-info → aiverify_moonshot-0.5.1.dist-info}/WHEEL +1 -1
- moonshot/integrations/cli/benchmark/cookbook.py +1 -5
- moonshot/integrations/cli/benchmark/recipe.py +1 -5
- moonshot/integrations/cli/cli_errors.py +3 -0
- moonshot/integrations/cli/common/connectors.py +22 -10
- moonshot/integrations/cli/common/dataset.py +5 -6
- moonshot/integrations/web_api/app.py +1 -1
- moonshot/integrations/web_api/schemas/cookbook_response_model.py +1 -1
- moonshot/integrations/web_api/schemas/endpoint_create_dto.py +1 -0
- moonshot/integrations/web_api/schemas/recipe_response_model.py +1 -1
- moonshot/integrations/web_api/services/cookbook_service.py +52 -21
- moonshot/integrations/web_api/services/endpoint_service.py +1 -0
- moonshot/integrations/web_api/services/recipe_service.py +36 -15
- moonshot/src/api/api_bookmark.py +16 -5
- moonshot/src/api/api_connector.py +3 -3
- moonshot/src/api/api_connector_endpoint.py +6 -3
- moonshot/src/api/api_context_strategy.py +2 -2
- moonshot/src/api/api_cookbook.py +6 -6
- moonshot/src/api/api_dataset.py +5 -5
- moonshot/src/api/api_environment_variables.py +3 -0
- moonshot/src/api/api_metrics.py +1 -1
- moonshot/src/api/api_prompt_template.py +9 -0
- moonshot/src/api/api_recipe.py +3 -3
- moonshot/src/api/api_red_teaming.py +4 -8
- moonshot/src/api/api_result.py +10 -6
- moonshot/src/api/api_run.py +3 -3
- moonshot/src/api/api_runner.py +7 -6
- moonshot/src/api/api_session.py +11 -7
- moonshot/src/connectors/connector.py +121 -58
- moonshot/src/connectors/connector_prompt_arguments.py +7 -4
- moonshot/src/connectors/connector_response.py +5 -10
- moonshot/src/connectors_endpoints/connector_endpoint.py +32 -20
- moonshot/src/connectors_endpoints/connector_endpoint_arguments.py +4 -1
- moonshot/src/messages_constants.py +85 -3
- {aiverify_moonshot-0.4.11.dist-info → aiverify_moonshot-0.5.1.dist-info}/licenses/AUTHORS.md +0 -0
- {aiverify_moonshot-0.4.11.dist-info → aiverify_moonshot-0.5.1.dist-info}/licenses/LICENSE.md +0 -0
- {aiverify_moonshot-0.4.11.dist-info → aiverify_moonshot-0.5.1.dist-info}/licenses/NOTICES.md +0 -0
|
@@ -3,17 +3,32 @@ from __future__ import annotations
|
|
|
3
3
|
import asyncio
|
|
4
4
|
import time
|
|
5
5
|
from abc import abstractmethod
|
|
6
|
-
from
|
|
7
|
-
from functools import wraps
|
|
6
|
+
from functools import partial, wraps
|
|
8
7
|
from pathlib import Path
|
|
9
8
|
from typing import Callable
|
|
10
9
|
|
|
10
|
+
from tenacity import RetryCallState, retry, stop_after_attempt, wait_random_exponential
|
|
11
|
+
|
|
11
12
|
from moonshot.src.configs.env_variables import EnvVariables
|
|
12
13
|
from moonshot.src.connectors.connector_prompt_arguments import ConnectorPromptArguments
|
|
13
14
|
from moonshot.src.connectors.connector_response import ConnectorResponse
|
|
14
15
|
from moonshot.src.connectors_endpoints.connector_endpoint_arguments import (
|
|
15
16
|
ConnectorEndpointArguments,
|
|
16
17
|
)
|
|
18
|
+
from moonshot.src.messages_constants import (
|
|
19
|
+
CONNECTOR_CREATE_CONNECTOR_ENDPOINT_ARGUMENTS_VALIDATION_ERROR,
|
|
20
|
+
CONNECTOR_CREATE_ERROR,
|
|
21
|
+
CONNECTOR_GET_AVAILABLE_ITEMS_ERROR,
|
|
22
|
+
CONNECTOR_GET_PREDICTION_ARGUMENTS_CONNECTOR_VALIDATION_ERROR,
|
|
23
|
+
CONNECTOR_GET_PREDICTION_ARGUMENTS_GENERATED_PROMPT_VALIDATION_ERROR,
|
|
24
|
+
CONNECTOR_GET_PREDICTION_ERROR,
|
|
25
|
+
CONNECTOR_GET_PREDICTION_INFO,
|
|
26
|
+
CONNECTOR_GET_PREDICTION_TIME_TAKEN_INFO,
|
|
27
|
+
CONNECTOR_LOAD_CONNECTOR_ENDPOINT_ARGUMENTS_VALIDATION_ERROR,
|
|
28
|
+
CONNECTOR_LOAD_CONNECTOR_INSTANCE_RUNTIME_ERROR,
|
|
29
|
+
CONNECTOR_PERFORM_RETRY_CALLBACK_ERROR,
|
|
30
|
+
CONNECTOR_SET_SYSTEM_PROMPT_VALIDATION_ERROR,
|
|
31
|
+
)
|
|
17
32
|
from moonshot.src.storage.storage import Storage
|
|
18
33
|
from moonshot.src.utils.import_modules import get_instance
|
|
19
34
|
from moonshot.src.utils.log import configure_logger
|
|
@@ -22,43 +37,56 @@ from moonshot.src.utils.log import configure_logger
|
|
|
22
37
|
logger = configure_logger(__name__)
|
|
23
38
|
|
|
24
39
|
|
|
40
|
+
def perform_retry_callback(connector_id: str, retry_state: RetryCallState) -> None:
|
|
41
|
+
"""
|
|
42
|
+
Callback function to log retry attempts with detailed information.
|
|
43
|
+
|
|
44
|
+
This function is called by the tenacity library before each retry attempt.
|
|
45
|
+
It logs the retry attempt number, the sleep time before the next attempt,
|
|
46
|
+
the error message that caused the retry, and the connector ID.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
connector_id (str): The ID of the connector.
|
|
50
|
+
retry_state (RetryCallState): The state of the retry call, which includes
|
|
51
|
+
information about the current attempt, the exception raised, and the next action.
|
|
52
|
+
"""
|
|
53
|
+
sleep_time = retry_state.idle_for if retry_state else 0
|
|
54
|
+
exception = (
|
|
55
|
+
retry_state.outcome.exception() if retry_state.outcome else "Unknown exception"
|
|
56
|
+
)
|
|
57
|
+
logger.error(
|
|
58
|
+
CONNECTOR_PERFORM_RETRY_CALLBACK_ERROR.format(
|
|
59
|
+
connector_id=connector_id,
|
|
60
|
+
attempt_no=retry_state.attempt_number,
|
|
61
|
+
sleep=f"{sleep_time:.2f}",
|
|
62
|
+
message=str(exception),
|
|
63
|
+
)
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
25
67
|
def perform_retry(func):
|
|
26
68
|
"""
|
|
27
|
-
A decorator to perform retries on a function.
|
|
69
|
+
A decorator to perform retries on a function using tenacity.
|
|
28
70
|
|
|
29
|
-
This decorator wraps
|
|
30
|
-
if it fails. The number of
|
|
31
|
-
are determined by the `
|
|
32
|
-
attributes of the class instance.
|
|
71
|
+
This decorator wraps an asynchronous function to enable retrying the function call
|
|
72
|
+
if it fails. The number of attempts and the delay between attempts
|
|
73
|
+
are determined by the `max_attempts` attribute of the class instance.
|
|
33
74
|
|
|
34
|
-
|
|
35
|
-
|
|
75
|
+
Args:
|
|
76
|
+
func (Callable): The asynchronous function to be wrapped and retried.
|
|
36
77
|
|
|
37
78
|
Returns:
|
|
38
|
-
|
|
79
|
+
Callable: A wrapper function that includes retry logic.
|
|
39
80
|
"""
|
|
40
81
|
|
|
41
82
|
async def wrapper(self, *args, **kwargs):
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
except Exception as exc:
|
|
50
|
-
logger.warning(f"Operation failed. {str(exc)} - Retrying...")
|
|
51
|
-
|
|
52
|
-
# Perform retry
|
|
53
|
-
retry_count += 1
|
|
54
|
-
if retry_count <= self.retries_times:
|
|
55
|
-
delay = base_delay * (2**retry_count)
|
|
56
|
-
logger.warning(
|
|
57
|
-
f"Attempt {retry_count}, Retrying in {delay} seconds..."
|
|
58
|
-
)
|
|
59
|
-
await sleep(delay)
|
|
60
|
-
# Raise an exception
|
|
61
|
-
raise ConnectionError("Failed to get response.")
|
|
83
|
+
retry_decorator = retry(
|
|
84
|
+
wait=wait_random_exponential(min=1, max=60),
|
|
85
|
+
stop=stop_after_attempt(self.max_attempts),
|
|
86
|
+
after=partial(perform_retry_callback, self.id),
|
|
87
|
+
reraise=True,
|
|
88
|
+
)
|
|
89
|
+
return await retry_decorator(func)(self, *args, **kwargs)
|
|
62
90
|
|
|
63
91
|
return wrapper
|
|
64
92
|
|
|
@@ -71,6 +99,7 @@ class Connector:
|
|
|
71
99
|
self.token = ep_args.token
|
|
72
100
|
self.max_concurrency = ep_args.max_concurrency
|
|
73
101
|
self.max_calls_per_second = ep_args.max_calls_per_second
|
|
102
|
+
self.model = ep_args.model
|
|
74
103
|
self.params = ep_args.params
|
|
75
104
|
|
|
76
105
|
# Rate limiting
|
|
@@ -80,21 +109,19 @@ class Connector:
|
|
|
80
109
|
self.updated_at = time.time()
|
|
81
110
|
self.semaphore = asyncio.Semaphore(ep_args.max_concurrency)
|
|
82
111
|
|
|
83
|
-
# Set Prompts if they
|
|
112
|
+
# Set Prompts if they exist
|
|
84
113
|
self.pre_prompt = ep_args.params.get("pre_prompt", "")
|
|
85
114
|
self.post_prompt = ep_args.params.get("post_prompt", "")
|
|
86
115
|
self.system_prompt = ep_args.params.get("system_prompt", "")
|
|
87
116
|
|
|
88
117
|
# Connection timeout
|
|
89
118
|
self.timeout = ep_args.params.get("timeout", 600)
|
|
90
|
-
self.
|
|
91
|
-
self.retries_times = ep_args.params.get("num_of_retries", 3)
|
|
119
|
+
self.max_attempts = ep_args.params.get("max_attempts", 3)
|
|
92
120
|
|
|
93
121
|
# Optional params
|
|
94
122
|
excluded_keys = {
|
|
95
|
-
"allow_retries",
|
|
96
123
|
"timeout",
|
|
97
|
-
"
|
|
124
|
+
"max_attempts",
|
|
98
125
|
"pre_prompt",
|
|
99
126
|
"post_prompt",
|
|
100
127
|
"system_prompt",
|
|
@@ -114,10 +141,6 @@ class Connector:
|
|
|
114
141
|
|
|
115
142
|
The method updates the `updated_at` attribute to the current time after tokens are added, ensuring that
|
|
116
143
|
the next token addition will be calculated based on the correct elapsed time.
|
|
117
|
-
|
|
118
|
-
Usage:
|
|
119
|
-
# Assume `self` is an instance of a class with `rate_limiter`, `tokens`, and `updated_at` attributes.
|
|
120
|
-
await self.add_tokens()
|
|
121
144
|
"""
|
|
122
145
|
now = time.time()
|
|
123
146
|
elapsed = now - self.updated_at
|
|
@@ -144,11 +167,6 @@ class Connector:
|
|
|
144
167
|
|
|
145
168
|
Returns:
|
|
146
169
|
Callable: The decorated function wrapped with rate limiting logic.
|
|
147
|
-
|
|
148
|
-
Usage:
|
|
149
|
-
@rate_limited
|
|
150
|
-
async def some_async_function(*args, **kwargs):
|
|
151
|
-
# Function implementation
|
|
152
170
|
"""
|
|
153
171
|
|
|
154
172
|
@wraps(func)
|
|
@@ -187,7 +205,7 @@ class Connector:
|
|
|
187
205
|
@classmethod
|
|
188
206
|
def load(cls, ep_args: ConnectorEndpointArguments) -> Connector:
|
|
189
207
|
"""
|
|
190
|
-
|
|
208
|
+
Dynamically loads a connector instance based on the provided endpoint arguments.
|
|
191
209
|
|
|
192
210
|
The connector type specified in the `ep_args` is used to dynamically load the corresponding
|
|
193
211
|
connector class. The connector is then instantiated with the provided endpoint arguments. If the
|
|
@@ -203,17 +221,24 @@ class Connector:
|
|
|
203
221
|
Raises:
|
|
204
222
|
RuntimeError: If the specified connector type does not match any available connector classes.
|
|
205
223
|
"""
|
|
224
|
+
if ep_args is None or not isinstance(ep_args, ConnectorEndpointArguments):
|
|
225
|
+
raise ValueError(
|
|
226
|
+
CONNECTOR_LOAD_CONNECTOR_ENDPOINT_ARGUMENTS_VALIDATION_ERROR
|
|
227
|
+
)
|
|
228
|
+
|
|
206
229
|
connector_instance = get_instance(
|
|
207
230
|
ep_args.connector_type,
|
|
208
231
|
Storage.get_filepath(
|
|
209
232
|
EnvVariables.CONNECTORS.name, ep_args.connector_type, "py"
|
|
210
233
|
),
|
|
211
234
|
)
|
|
212
|
-
if connector_instance:
|
|
235
|
+
if connector_instance and isinstance(connector_instance, Callable):
|
|
213
236
|
return connector_instance(ep_args)
|
|
214
237
|
else:
|
|
215
238
|
raise RuntimeError(
|
|
216
|
-
|
|
239
|
+
CONNECTOR_LOAD_CONNECTOR_INSTANCE_RUNTIME_ERROR.format(
|
|
240
|
+
message=ep_args.connector_type
|
|
241
|
+
)
|
|
217
242
|
)
|
|
218
243
|
|
|
219
244
|
@staticmethod
|
|
@@ -223,7 +248,7 @@ class Connector:
|
|
|
223
248
|
|
|
224
249
|
This method takes a ConnectorEndpointArguments object, which contains the necessary information
|
|
225
250
|
to initialize and return a Connector object. The Connector object is created by calling the
|
|
226
|
-
`
|
|
251
|
+
`load` method, which dynamically loads and initializes the connector based on the
|
|
227
252
|
endpoint arguments provided.
|
|
228
253
|
|
|
229
254
|
Args:
|
|
@@ -231,12 +256,20 @@ class Connector:
|
|
|
231
256
|
|
|
232
257
|
Returns:
|
|
233
258
|
Connector: An initialized Connector object based on the provided endpoint arguments.
|
|
259
|
+
|
|
260
|
+
Raises:
|
|
261
|
+
ValueError: If the provided endpoint arguments are invalid.
|
|
262
|
+
Exception: If there is an error during the creation of the connector.
|
|
234
263
|
"""
|
|
235
264
|
try:
|
|
265
|
+
if ep_args is None or not isinstance(ep_args, ConnectorEndpointArguments):
|
|
266
|
+
raise ValueError(
|
|
267
|
+
CONNECTOR_CREATE_CONNECTOR_ENDPOINT_ARGUMENTS_VALIDATION_ERROR
|
|
268
|
+
)
|
|
236
269
|
return Connector.load(ep_args)
|
|
237
270
|
|
|
238
271
|
except Exception as e:
|
|
239
|
-
logger.error(
|
|
272
|
+
logger.error(CONNECTOR_CREATE_ERROR.format(message=str(e)))
|
|
240
273
|
raise e
|
|
241
274
|
|
|
242
275
|
@staticmethod
|
|
@@ -245,7 +278,7 @@ class Connector:
|
|
|
245
278
|
Fetches a list of all available connector types.
|
|
246
279
|
|
|
247
280
|
This method employs the `get_connectors` method to locate all Python files in the directory
|
|
248
|
-
defined by the `
|
|
281
|
+
defined by the `EnvVariables.CONNECTORS` environment variable. It subsequently excludes any files that are
|
|
249
282
|
not intended to be exposed as connectors (those containing "__" in their names). The method yields a list of the
|
|
250
283
|
names of these connector types.
|
|
251
284
|
|
|
@@ -263,7 +296,7 @@ class Connector:
|
|
|
263
296
|
]
|
|
264
297
|
|
|
265
298
|
except Exception as e:
|
|
266
|
-
logger.error(
|
|
299
|
+
logger.error(CONNECTOR_GET_AVAILABLE_ITEMS_ERROR.format(message=str(e)))
|
|
267
300
|
raise e
|
|
268
301
|
|
|
269
302
|
@staticmethod
|
|
@@ -299,9 +332,24 @@ class Connector:
|
|
|
299
332
|
Raises:
|
|
300
333
|
Exception: If there is an error during prediction.
|
|
301
334
|
"""
|
|
335
|
+
if generated_prompt is None or not isinstance(
|
|
336
|
+
generated_prompt, ConnectorPromptArguments
|
|
337
|
+
):
|
|
338
|
+
raise ValueError(
|
|
339
|
+
CONNECTOR_GET_PREDICTION_ARGUMENTS_GENERATED_PROMPT_VALIDATION_ERROR
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
if connector is None or not isinstance(connector, Connector):
|
|
343
|
+
raise ValueError(
|
|
344
|
+
CONNECTOR_GET_PREDICTION_ARGUMENTS_CONNECTOR_VALIDATION_ERROR
|
|
345
|
+
)
|
|
346
|
+
|
|
302
347
|
try:
|
|
303
348
|
logger.info(
|
|
304
|
-
|
|
349
|
+
CONNECTOR_GET_PREDICTION_INFO.format(
|
|
350
|
+
connector_id=connector.id,
|
|
351
|
+
prompt_index=generated_prompt.prompt_index,
|
|
352
|
+
)
|
|
305
353
|
)
|
|
306
354
|
|
|
307
355
|
start_time = time.perf_counter()
|
|
@@ -310,7 +358,11 @@ class Connector:
|
|
|
310
358
|
)
|
|
311
359
|
generated_prompt.duration = time.perf_counter() - start_time
|
|
312
360
|
logger.debug(
|
|
313
|
-
|
|
361
|
+
CONNECTOR_GET_PREDICTION_TIME_TAKEN_INFO.format(
|
|
362
|
+
connector_id=connector.id,
|
|
363
|
+
prompt_index=generated_prompt.prompt_index,
|
|
364
|
+
prompt_duration=f"{generated_prompt.duration:.4f}",
|
|
365
|
+
)
|
|
314
366
|
)
|
|
315
367
|
|
|
316
368
|
# Call prompt callback
|
|
@@ -321,17 +373,28 @@ class Connector:
|
|
|
321
373
|
return generated_prompt
|
|
322
374
|
|
|
323
375
|
except Exception as e:
|
|
324
|
-
logger.error(
|
|
376
|
+
logger.error(
|
|
377
|
+
CONNECTOR_GET_PREDICTION_ERROR.format(
|
|
378
|
+
connector_id=connector.id,
|
|
379
|
+
prompt_index=generated_prompt.prompt_index,
|
|
380
|
+
message=str(e),
|
|
381
|
+
)
|
|
382
|
+
)
|
|
325
383
|
raise e
|
|
326
384
|
|
|
327
385
|
def set_system_prompt(self, system_prompt: str) -> None:
|
|
328
386
|
"""
|
|
329
|
-
|
|
387
|
+
Sets a new system prompt for this connector instance.
|
|
330
388
|
|
|
331
|
-
The system prompt
|
|
332
|
-
|
|
389
|
+
The system prompt is a predefined message or command that the connector can use to start interactions
|
|
390
|
+
or perform specific tasks.
|
|
333
391
|
|
|
334
|
-
|
|
392
|
+
Args:
|
|
335
393
|
system_prompt (str): The new system prompt to set for this connector.
|
|
394
|
+
|
|
395
|
+
Raises:
|
|
396
|
+
ValueError: If the provided system prompt is not a string or is None.
|
|
336
397
|
"""
|
|
398
|
+
if system_prompt is None or not isinstance(system_prompt, str):
|
|
399
|
+
raise ValueError(CONNECTOR_SET_SYSTEM_PROMPT_VALIDATION_ERROR)
|
|
337
400
|
self.system_prompt = system_prompt
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from typing import Any
|
|
3
|
+
from typing import Annotated, Any
|
|
4
4
|
|
|
5
|
-
from pydantic import BaseModel
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
6
|
|
|
7
7
|
from moonshot.src.connectors.connector_response import ConnectorResponse
|
|
8
8
|
|
|
@@ -11,7 +11,9 @@ class ConnectorPromptArguments(BaseModel):
|
|
|
11
11
|
class Config:
|
|
12
12
|
arbitrary_types_allowed = True
|
|
13
13
|
|
|
14
|
-
prompt_index:
|
|
14
|
+
prompt_index: Annotated[
|
|
15
|
+
int, Field(strict=True, ge=0)
|
|
16
|
+
] # The index of the prompt in the dataset
|
|
15
17
|
|
|
16
18
|
prompt: str # The actual prompt text
|
|
17
19
|
|
|
@@ -21,4 +23,5 @@ class ConnectorPromptArguments(BaseModel):
|
|
|
21
23
|
None # The predicted results, default is None
|
|
22
24
|
)
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
# The duration it took to get the results, must be a positive float
|
|
27
|
+
duration: Annotated[float, Field(strict=True, ge=0.0)] = 0.0
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
def __init__(self, response: str, context: list = []):
|
|
3
|
-
"""
|
|
4
|
-
Initializes a ConnectorResponse instance.
|
|
1
|
+
from pydantic import BaseModel
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
self.response = response
|
|
11
|
-
self.context = context
|
|
3
|
+
|
|
4
|
+
class ConnectorResponse(BaseModel):
|
|
5
|
+
response: str = ""
|
|
6
|
+
context: list = []
|
|
12
7
|
|
|
13
8
|
def to_dict(self) -> dict:
|
|
14
9
|
"""
|
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
from pathlib import Path
|
|
2
2
|
|
|
3
|
-
from pydantic import validate_call
|
|
3
|
+
from pydantic import constr, validate_call
|
|
4
4
|
from slugify import slugify
|
|
5
5
|
|
|
6
6
|
from moonshot.src.configs.env_variables import EnvVariables
|
|
7
7
|
from moonshot.src.connectors_endpoints.connector_endpoint_arguments import (
|
|
8
8
|
ConnectorEndpointArguments,
|
|
9
9
|
)
|
|
10
|
+
from moonshot.src.messages_constants import (
|
|
11
|
+
CONNECTOR_ENDPOINT_CREATE_ERROR,
|
|
12
|
+
CONNECTOR_ENDPOINT_DELETE_ERROR,
|
|
13
|
+
CONNECTOR_ENDPOINT_GET_AVAILABLE_ITEMS_ERROR,
|
|
14
|
+
CONNECTOR_ENDPOINT_READ_ERROR,
|
|
15
|
+
CONNECTOR_ENDPOINT_READ_INVALID,
|
|
16
|
+
CONNECTOR_ENDPOINT_UPDATE_ERROR,
|
|
17
|
+
)
|
|
10
18
|
from moonshot.src.storage.storage import Storage
|
|
11
19
|
from moonshot.src.utils.log import configure_logger
|
|
12
20
|
|
|
@@ -16,6 +24,7 @@ logger = configure_logger(__name__)
|
|
|
16
24
|
|
|
17
25
|
class ConnectorEndpoint:
|
|
18
26
|
@staticmethod
|
|
27
|
+
@validate_call
|
|
19
28
|
def create(ep_args: ConnectorEndpointArguments) -> str:
|
|
20
29
|
"""
|
|
21
30
|
Creates a new connector endpoint and stores its details as a JSON object.
|
|
@@ -47,6 +56,7 @@ class ConnectorEndpoint:
|
|
|
47
56
|
"token": ep_args.token,
|
|
48
57
|
"max_calls_per_second": ep_args.max_calls_per_second,
|
|
49
58
|
"max_concurrency": ep_args.max_concurrency,
|
|
59
|
+
"model": ep_args.model,
|
|
50
60
|
"params": ep_args.params,
|
|
51
61
|
}
|
|
52
62
|
|
|
@@ -57,12 +67,12 @@ class ConnectorEndpoint:
|
|
|
57
67
|
return ep_id
|
|
58
68
|
|
|
59
69
|
except Exception as e:
|
|
60
|
-
logger.error(
|
|
70
|
+
logger.error(CONNECTOR_ENDPOINT_CREATE_ERROR.format(message=str(e)))
|
|
61
71
|
raise e
|
|
62
72
|
|
|
63
73
|
@staticmethod
|
|
64
74
|
@validate_call
|
|
65
|
-
def read(ep_id:
|
|
75
|
+
def read(ep_id: constr(min_length=1)) -> ConnectorEndpointArguments:
|
|
66
76
|
"""
|
|
67
77
|
Retrieves the details of a specified endpoint by its ID.
|
|
68
78
|
|
|
@@ -72,27 +82,24 @@ class ConnectorEndpoint:
|
|
|
72
82
|
any other error occurs, an exception is raised with an appropriate error message.
|
|
73
83
|
|
|
74
84
|
Args:
|
|
75
|
-
ep_id (
|
|
85
|
+
ep_id (constr(min_length=1)): The unique identifier of the endpoint whose details are to be retrieved.
|
|
76
86
|
|
|
77
87
|
Returns:
|
|
78
88
|
ConnectorEndpointArguments: An instance filled with the endpoint's details.
|
|
79
89
|
|
|
80
90
|
Raises:
|
|
81
|
-
RuntimeError: If the
|
|
91
|
+
RuntimeError: If the specified endpoint does not exist.
|
|
82
92
|
Exception: For any issues encountered during the file reading or data parsing process.
|
|
83
93
|
"""
|
|
84
94
|
try:
|
|
85
|
-
if not ep_id:
|
|
86
|
-
raise RuntimeError("Connector Endpoint ID is empty.")
|
|
87
|
-
|
|
88
95
|
endpoint_details = ConnectorEndpoint._read_endpoint(ep_id)
|
|
89
96
|
if not endpoint_details:
|
|
90
|
-
raise RuntimeError(
|
|
97
|
+
raise RuntimeError(CONNECTOR_ENDPOINT_READ_INVALID.format(ep_id=ep_id))
|
|
91
98
|
|
|
92
99
|
return ConnectorEndpointArguments(**endpoint_details)
|
|
93
100
|
|
|
94
101
|
except Exception as e:
|
|
95
|
-
logger.error(
|
|
102
|
+
logger.error(CONNECTOR_ENDPOINT_READ_ERROR.format(message=str(e)))
|
|
96
103
|
raise e
|
|
97
104
|
|
|
98
105
|
@staticmethod
|
|
@@ -124,6 +131,7 @@ class ConnectorEndpoint:
|
|
|
124
131
|
return connector_endpoint_info
|
|
125
132
|
|
|
126
133
|
@staticmethod
|
|
134
|
+
@validate_call
|
|
127
135
|
def update(ep_args: ConnectorEndpointArguments) -> bool:
|
|
128
136
|
"""
|
|
129
137
|
Updates the endpoint information in the storage based on the provided ConnectorEndpointArguments object.
|
|
@@ -138,11 +146,10 @@ class ConnectorEndpoint:
|
|
|
138
146
|
ep_args (ConnectorEndpointArguments): The object encapsulating the updated attributes of the endpoint.
|
|
139
147
|
|
|
140
148
|
Returns:
|
|
141
|
-
bool:
|
|
142
|
-
persisted to the storage; otherwise, an exception is raised.
|
|
149
|
+
bool: True if the update was successfully persisted to the storage; otherwise, an exception is raised.
|
|
143
150
|
|
|
144
151
|
Raises:
|
|
145
|
-
Exception:
|
|
152
|
+
Exception: If the update process encounters an error, potentially due to issues with data serialization or
|
|
146
153
|
storage access.
|
|
147
154
|
"""
|
|
148
155
|
try:
|
|
@@ -160,24 +167,24 @@ class ConnectorEndpoint:
|
|
|
160
167
|
return True
|
|
161
168
|
|
|
162
169
|
except Exception as e:
|
|
163
|
-
logger.error(
|
|
170
|
+
logger.error(CONNECTOR_ENDPOINT_UPDATE_ERROR.format(message=str(e)))
|
|
164
171
|
raise e
|
|
165
172
|
|
|
166
173
|
@staticmethod
|
|
167
174
|
@validate_call
|
|
168
|
-
def delete(ep_id:
|
|
175
|
+
def delete(ep_id: constr(min_length=1)) -> bool:
|
|
169
176
|
"""
|
|
170
177
|
Deletes the endpoint with the specified ID.
|
|
171
178
|
|
|
172
179
|
This method attempts to delete the endpoint corresponding to the given ID from the storage.
|
|
173
|
-
If the deletion is successful, it returns True. If an error occurs, it
|
|
180
|
+
If the deletion is successful, it returns True. If an error occurs, it logs an error message
|
|
174
181
|
and re-raises the exception.
|
|
175
182
|
|
|
176
183
|
Args:
|
|
177
|
-
ep_id (
|
|
184
|
+
ep_id (constr(min_length=1)): The unique identifier of the endpoint to be deleted
|
|
178
185
|
|
|
179
186
|
Returns:
|
|
180
|
-
bool: True if the endpoint was successfully deleted.
|
|
187
|
+
bool: True if the endpoint was successfully deleted; otherwise, an exception is raised.
|
|
181
188
|
|
|
182
189
|
Raises:
|
|
183
190
|
Exception: If the deletion process encounters an error.
|
|
@@ -187,7 +194,7 @@ class ConnectorEndpoint:
|
|
|
187
194
|
return True
|
|
188
195
|
|
|
189
196
|
except Exception as e:
|
|
190
|
-
logger.error(
|
|
197
|
+
logger.error(CONNECTOR_ENDPOINT_DELETE_ERROR.format(message=str(e)))
|
|
191
198
|
raise e
|
|
192
199
|
|
|
193
200
|
@staticmethod
|
|
@@ -204,6 +211,9 @@ class ConnectorEndpoint:
|
|
|
204
211
|
Returns:
|
|
205
212
|
tuple[list[str], list[ConnectorEndpointArguments]]: A tuple containing a list of endpoint IDs and a list of
|
|
206
213
|
ConnectorEndpointArguments objects with endpoint details.
|
|
214
|
+
|
|
215
|
+
Raises:
|
|
216
|
+
Exception: If the process of fetching available items encounters an error.
|
|
207
217
|
"""
|
|
208
218
|
try:
|
|
209
219
|
retn_eps = []
|
|
@@ -223,5 +233,7 @@ class ConnectorEndpoint:
|
|
|
223
233
|
return retn_eps_ids, retn_eps
|
|
224
234
|
|
|
225
235
|
except Exception as e:
|
|
226
|
-
logger.error(
|
|
236
|
+
logger.error(
|
|
237
|
+
CONNECTOR_ENDPOINT_GET_AVAILABLE_ITEMS_ERROR.format(message=str(e))
|
|
238
|
+
)
|
|
227
239
|
raise e
|
|
@@ -22,6 +22,8 @@ class ConnectorEndpointArguments(BaseModel):
|
|
|
22
22
|
gt=0
|
|
23
23
|
) # max_concurrency (int): The number of concurrent api calls
|
|
24
24
|
|
|
25
|
+
model: str # model (str): The model identifier for the LLM connector.
|
|
26
|
+
|
|
25
27
|
params: dict # params (dict): A dictionary that contains connection specified parameters
|
|
26
28
|
|
|
27
29
|
# created_date (str): The date and time the endpoint was created in isoformat without 'T'.
|
|
@@ -34,7 +36,7 @@ class ConnectorEndpointArguments(BaseModel):
|
|
|
34
36
|
|
|
35
37
|
This method takes all the attributes of the ConnectorEndpointArguments instance and constructs a dictionary
|
|
36
38
|
with attribute names as keys and their corresponding values. This includes the id, name, connector_type, uri,
|
|
37
|
-
token, max_calls_per_second, max_concurrency, params, and created_date. This dictionary can be used for
|
|
39
|
+
token, max_calls_per_second, max_concurrency, model, params, and created_date. This dictionary can be used for
|
|
38
40
|
serialization purposes, such as storing the endpoint information in a JSON file or sending it over a network.
|
|
39
41
|
|
|
40
42
|
Returns:
|
|
@@ -49,6 +51,7 @@ class ConnectorEndpointArguments(BaseModel):
|
|
|
49
51
|
"token": self.token,
|
|
50
52
|
"max_calls_per_second": self.max_calls_per_second,
|
|
51
53
|
"max_concurrency": self.max_concurrency,
|
|
54
|
+
"model": self.model,
|
|
52
55
|
"params": self.params,
|
|
53
56
|
"created_date": self.created_date,
|
|
54
57
|
}
|
|
@@ -15,7 +15,9 @@ BOOKMARK_GET_BOOKMARK_ERROR_1 = "[Bookmark] Invalid bookmark name: {message}"
|
|
|
15
15
|
# BOOKMARK - delete_bookmark
|
|
16
16
|
# ------------------------------------------------------------------------------
|
|
17
17
|
BOOKMARK_DELETE_BOOKMARK_SUCCESS = "[Bookmark] Bookmark record deleted."
|
|
18
|
-
BOOKMARK_DELETE_BOOKMARK_FAIL =
|
|
18
|
+
BOOKMARK_DELETE_BOOKMARK_FAIL = (
|
|
19
|
+
"[Bookmark] Bookmark record not found. Unable to delete."
|
|
20
|
+
)
|
|
19
21
|
BOOKMARK_DELETE_BOOKMARK_ERROR = (
|
|
20
22
|
"[Bookmark] Failed to delete bookmark record: {message}"
|
|
21
23
|
)
|
|
@@ -33,9 +35,89 @@ BOOKMARK_DELETE_ALL_BOOKMARK_ERROR = (
|
|
|
33
35
|
# BOOKMARK - export_bookmarks
|
|
34
36
|
# ------------------------------------------------------------------------------
|
|
35
37
|
BOOKMARK_EXPORT_BOOKMARK_ERROR = "[Bookmark] Failed to export bookmarks: {message}"
|
|
36
|
-
BOOKMARK_EXPORT_BOOKMARK_VALIDATION_ERROR =
|
|
38
|
+
BOOKMARK_EXPORT_BOOKMARK_VALIDATION_ERROR = (
|
|
39
|
+
"Export filename must be a non-empty string."
|
|
40
|
+
)
|
|
37
41
|
|
|
38
42
|
# ------------------------------------------------------------------------------
|
|
39
43
|
# BOOKMARK ARGUMENTS - from_tuple_to_dict
|
|
40
44
|
# ------------------------------------------------------------------------------
|
|
41
|
-
BOOKMARK_ARGUMENTS_FROM_TUPLE_TO_DICT_VALIDATION_ERROR = "[BookmarkArguments] Failed to convert to dictionary because of the insufficient number of values" # noqa: E501
|
|
45
|
+
BOOKMARK_ARGUMENTS_FROM_TUPLE_TO_DICT_VALIDATION_ERROR = "[BookmarkArguments] Failed to convert to dictionary because of the insufficient number of values." # noqa: E501
|
|
46
|
+
|
|
47
|
+
# ------------------------------------------------------------------------------
|
|
48
|
+
# CONNECTOR - perform_retry_callback
|
|
49
|
+
# ------------------------------------------------------------------------------
|
|
50
|
+
CONNECTOR_PERFORM_RETRY_CALLBACK_ERROR = "[Connector ID: {connector_id}] Attempt {attempt_no} failed due to error: {message}" # noqa: E501
|
|
51
|
+
|
|
52
|
+
# ------------------------------------------------------------------------------
|
|
53
|
+
# CONNECTOR - load
|
|
54
|
+
# ------------------------------------------------------------------------------
|
|
55
|
+
CONNECTOR_LOAD_CONNECTOR_ENDPOINT_ARGUMENTS_VALIDATION_ERROR = "[Connector] The 'ep_args' argument must be an instance of ConnectorEndpointArguments and not None." # noqa: E501
|
|
56
|
+
CONNECTOR_LOAD_CONNECTOR_INSTANCE_RUNTIME_ERROR = (
|
|
57
|
+
"[Connector] Failed to get connector instance: {message}"
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
# ------------------------------------------------------------------------------
|
|
61
|
+
# CONNECTOR - create
|
|
62
|
+
# ------------------------------------------------------------------------------
|
|
63
|
+
CONNECTOR_CREATE_CONNECTOR_ENDPOINT_ARGUMENTS_VALIDATION_ERROR = "[Connector] The 'ep_args' argument must be an instance of ConnectorEndpointArguments and not None." # noqa: E501
|
|
64
|
+
CONNECTOR_CREATE_ERROR = "[Connector] Failed to create connector: {message}"
|
|
65
|
+
|
|
66
|
+
# ------------------------------------------------------------------------------
|
|
67
|
+
# CONNECTOR - get_available_items
|
|
68
|
+
# ------------------------------------------------------------------------------
|
|
69
|
+
CONNECTOR_GET_AVAILABLE_ITEMS_ERROR = (
|
|
70
|
+
"[Connector] Failed to get available connectors: {message}"
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# ------------------------------------------------------------------------------
|
|
74
|
+
# CONNECTOR - get_prediction
|
|
75
|
+
# ------------------------------------------------------------------------------
|
|
76
|
+
CONNECTOR_GET_PREDICTION_ARGUMENTS_GENERATED_PROMPT_VALIDATION_ERROR = "[Connector] The 'generated_prompt' argument must be an instance of ConnectorPromptArguments and not None." # noqa: E501
|
|
77
|
+
CONNECTOR_GET_PREDICTION_ARGUMENTS_CONNECTOR_VALIDATION_ERROR = "[Connector] The 'connector' argument must be an instance of Connector and not None." # noqa: E501
|
|
78
|
+
CONNECTOR_GET_PREDICTION_INFO = (
|
|
79
|
+
"[Connector ID: {connector_id}] Predicting Prompt Index {prompt_index}."
|
|
80
|
+
)
|
|
81
|
+
CONNECTOR_GET_PREDICTION_TIME_TAKEN_INFO = "[Connector ID: {connector_id}] Prompt Index {prompt_index} took {prompt_duration}s." # noqa: E501
|
|
82
|
+
CONNECTOR_GET_PREDICTION_ERROR = "[Connector ID: {connector_id}] Prompt Index {prompt_index} failed to get prediction: {message}" # noqa: E501
|
|
83
|
+
|
|
84
|
+
# ------------------------------------------------------------------------------
|
|
85
|
+
# CONNECTOR - set_system_prompt
|
|
86
|
+
# ------------------------------------------------------------------------------
|
|
87
|
+
CONNECTOR_SET_SYSTEM_PROMPT_VALIDATION_ERROR = "[Connector] The 'system_prompt' argument must be an instance of string and not None." # noqa: E501
|
|
88
|
+
|
|
89
|
+
# ------------------------------------------------------------------------------
|
|
90
|
+
# CONNECTOR ENDPOINT - create
|
|
91
|
+
# ------------------------------------------------------------------------------
|
|
92
|
+
CONNECTOR_ENDPOINT_CREATE_ERROR = (
|
|
93
|
+
"[ConnectorEndpoint] Failed to create connector endpoint: {message}"
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
# ------------------------------------------------------------------------------
|
|
97
|
+
# CONNECTOR ENDPOINT - read
|
|
98
|
+
# ------------------------------------------------------------------------------
|
|
99
|
+
CONNECTOR_ENDPOINT_READ_INVALID = "Invalid connector endpoint id - {ep_id}"
|
|
100
|
+
CONNECTOR_ENDPOINT_READ_ERROR = (
|
|
101
|
+
"[ConnectorEndpoint] Failed to read connector endpoint: {message}"
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
# ------------------------------------------------------------------------------
|
|
105
|
+
# CONNECTOR ENDPOINT - update
|
|
106
|
+
# ------------------------------------------------------------------------------
|
|
107
|
+
CONNECTOR_ENDPOINT_UPDATE_ERROR = (
|
|
108
|
+
"[ConnectorEndpoint] Failed to update connector endpoint: {message}"
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
# ------------------------------------------------------------------------------
|
|
112
|
+
# CONNECTOR ENDPOINT - delete
|
|
113
|
+
# ------------------------------------------------------------------------------
|
|
114
|
+
CONNECTOR_ENDPOINT_DELETE_ERROR = (
|
|
115
|
+
"[ConnectorEndpoint] Failed to delete connector endpoint: {message}"
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# ------------------------------------------------------------------------------
|
|
119
|
+
# CONNECTOR ENDPOINT - get_available_items
|
|
120
|
+
# ------------------------------------------------------------------------------
|
|
121
|
+
CONNECTOR_ENDPOINT_GET_AVAILABLE_ITEMS_ERROR = (
|
|
122
|
+
"[ConnectorEndpoint] Failed to get available connector endpoints: {message}"
|
|
123
|
+
)
|
{aiverify_moonshot-0.4.11.dist-info → aiverify_moonshot-0.5.1.dist-info}/licenses/AUTHORS.md
RENAMED
|
File without changes
|
{aiverify_moonshot-0.4.11.dist-info → aiverify_moonshot-0.5.1.dist-info}/licenses/LICENSE.md
RENAMED
|
File without changes
|