agenta 0.27.0__py3-none-any.whl → 0.27.0a0__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 agenta might be problematic. Click here for more details.
- agenta/__init__.py +3 -23
- agenta/cli/helper.py +1 -5
- agenta/client/backend/__init__.py +0 -14
- agenta/client/backend/apps/client.py +20 -28
- agenta/client/backend/client.py +2 -25
- agenta/client/backend/containers/client.py +1 -5
- agenta/client/backend/core/__init__.py +1 -2
- agenta/client/backend/core/client_wrapper.py +6 -6
- agenta/client/backend/core/file.py +11 -33
- agenta/client/backend/core/http_client.py +18 -24
- agenta/client/backend/core/pydantic_utilities.py +29 -144
- agenta/client/backend/core/request_options.py +0 -3
- agenta/client/backend/core/serialization.py +42 -139
- agenta/client/backend/evaluations/client.py +2 -7
- agenta/client/backend/evaluators/client.py +1 -349
- agenta/client/backend/observability/client.py +2 -11
- agenta/client/backend/testsets/client.py +10 -10
- agenta/client/backend/types/__init__.py +0 -14
- agenta/client/backend/types/app.py +0 -1
- agenta/client/backend/types/app_variant_response.py +1 -3
- agenta/client/backend/types/create_span.py +2 -3
- agenta/client/backend/types/environment_output.py +0 -1
- agenta/client/backend/types/environment_output_extended.py +0 -1
- agenta/client/backend/types/evaluation.py +2 -1
- agenta/client/backend/types/evaluator.py +0 -2
- agenta/client/backend/types/evaluator_config.py +0 -1
- agenta/client/backend/types/human_evaluation.py +2 -1
- agenta/client/backend/types/llm_tokens.py +2 -2
- agenta/client/backend/types/span.py +0 -1
- agenta/client/backend/types/span_detail.py +1 -7
- agenta/client/backend/types/test_set_output_response.py +2 -5
- agenta/client/backend/types/trace_detail.py +1 -7
- agenta/client/backend/types/with_pagination.py +2 -4
- agenta/client/backend/variants/client.py +273 -1566
- agenta/docker/docker-assets/Dockerfile.cloud.template +1 -1
- agenta/sdk/__init__.py +5 -21
- agenta/sdk/agenta_init.py +29 -34
- agenta/sdk/config_manager.py +205 -0
- agenta/sdk/context/routing.py +5 -6
- agenta/sdk/decorators/routing.py +146 -158
- agenta/sdk/decorators/tracing.py +239 -206
- agenta/sdk/litellm/litellm.py +36 -47
- agenta/sdk/tracing/attributes.py +47 -7
- agenta/sdk/tracing/context.py +2 -5
- agenta/sdk/tracing/conventions.py +19 -25
- agenta/sdk/tracing/exporters.py +5 -17
- agenta/sdk/tracing/inline.py +146 -92
- agenta/sdk/tracing/processors.py +13 -65
- agenta/sdk/tracing/spans.py +4 -16
- agenta/sdk/tracing/tracing.py +65 -124
- agenta/sdk/types.py +2 -61
- agenta/sdk/utils/exceptions.py +5 -38
- {agenta-0.27.0.dist-info → agenta-0.27.0a0.dist-info}/METADATA +1 -1
- {agenta-0.27.0.dist-info → agenta-0.27.0a0.dist-info}/RECORD +56 -67
- agenta/client/backend/types/config_dto.py +0 -32
- agenta/client/backend/types/config_response_model.py +0 -32
- agenta/client/backend/types/evaluator_mapping_output_interface.py +0 -21
- agenta/client/backend/types/evaluator_output_interface.py +0 -21
- agenta/client/backend/types/lifecycle_dto.py +0 -24
- agenta/client/backend/types/reference_dto.py +0 -23
- agenta/client/backend/types/reference_request_model.py +0 -23
- agenta/sdk/managers/__init__.py +0 -6
- agenta/sdk/managers/config.py +0 -318
- agenta/sdk/managers/deployment.py +0 -45
- agenta/sdk/managers/shared.py +0 -639
- agenta/sdk/managers/variant.py +0 -182
- {agenta-0.27.0.dist-info → agenta-0.27.0a0.dist-info}/WHEEL +0 -0
- {agenta-0.27.0.dist-info → agenta-0.27.0a0.dist-info}/entry_points.txt +0 -0
agenta/sdk/decorators/routing.py
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
"""The code for the Agenta SDK"""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
import time
|
|
6
|
+
import json
|
|
7
|
+
import inspect
|
|
8
|
+
import argparse
|
|
9
|
+
import asyncio
|
|
10
|
+
import traceback
|
|
11
|
+
import functools
|
|
10
12
|
from pathlib import Path
|
|
11
13
|
from tempfile import NamedTemporaryFile
|
|
12
|
-
from
|
|
13
|
-
|
|
14
|
+
from typing import Any, Callable, Dict, Optional, Tuple, List
|
|
15
|
+
from importlib.metadata import version
|
|
14
16
|
from fastapi.middleware.cors import CORSMiddleware
|
|
15
17
|
from fastapi import Body, FastAPI, UploadFile, HTTPException
|
|
16
18
|
|
|
19
|
+
import agenta as ag
|
|
20
|
+
|
|
17
21
|
from agenta.sdk.context.routing import routing_context_manager, routing_context
|
|
18
22
|
from agenta.sdk.context.tracing import tracing_context
|
|
19
23
|
from agenta.sdk.router import router as router
|
|
@@ -33,8 +37,16 @@ from agenta.sdk.types import (
|
|
|
33
37
|
BaseResponse,
|
|
34
38
|
BinaryParam,
|
|
35
39
|
)
|
|
40
|
+
import pydantic
|
|
41
|
+
|
|
42
|
+
from pydantic import BaseModel
|
|
43
|
+
from typing import Type
|
|
44
|
+
from annotated_types import Ge, Le, Gt, Lt
|
|
45
|
+
|
|
46
|
+
from pydantic import BaseModel, HttpUrl
|
|
47
|
+
import contextvars
|
|
48
|
+
from contextlib import contextmanager
|
|
36
49
|
|
|
37
|
-
import agenta as ag
|
|
38
50
|
|
|
39
51
|
app = FastAPI()
|
|
40
52
|
|
|
@@ -124,12 +136,12 @@ class entrypoint:
|
|
|
124
136
|
DEFAULT_PATH = "generate"
|
|
125
137
|
PLAYGROUND_PATH = "/playground"
|
|
126
138
|
RUN_PATH = "/run"
|
|
127
|
-
func_signature = signature(func)
|
|
139
|
+
func_signature = inspect.signature(func)
|
|
128
140
|
try:
|
|
129
141
|
config = (
|
|
130
142
|
config_schema() if config_schema else None
|
|
131
143
|
) # we initialize the config object to be able to use it
|
|
132
|
-
except ValidationError as e:
|
|
144
|
+
except pydantic.ValidationError as e:
|
|
133
145
|
raise ValueError(
|
|
134
146
|
f"Error initializing config_schema. Please ensure all required fields have default values: {str(e)}"
|
|
135
147
|
) from e
|
|
@@ -144,7 +156,7 @@ class entrypoint:
|
|
|
144
156
|
self.route_path = route_path
|
|
145
157
|
|
|
146
158
|
### --- Playground --- #
|
|
147
|
-
@wraps(func)
|
|
159
|
+
@functools.wraps(func)
|
|
148
160
|
async def wrapper(*args, **kwargs) -> Any:
|
|
149
161
|
func_params, api_config_params = self.split_kwargs(kwargs, config_params)
|
|
150
162
|
self.ingest_files(func_params, ingestible_files)
|
|
@@ -153,6 +165,7 @@ class entrypoint:
|
|
|
153
165
|
|
|
154
166
|
with routing_context_manager(
|
|
155
167
|
config=api_config_params,
|
|
168
|
+
environment="playground",
|
|
156
169
|
):
|
|
157
170
|
entrypoint_result = await self.execute_function(
|
|
158
171
|
func,
|
|
@@ -205,13 +218,11 @@ class entrypoint:
|
|
|
205
218
|
)
|
|
206
219
|
### ---------------------------- #
|
|
207
220
|
|
|
208
|
-
### --- Deployed --- #
|
|
209
|
-
@wraps(func)
|
|
221
|
+
### --- Deployed / Published --- #
|
|
222
|
+
@functools.wraps(func)
|
|
210
223
|
async def wrapper_deployed(*args, **kwargs) -> Any:
|
|
211
224
|
func_params = {
|
|
212
|
-
k: v
|
|
213
|
-
for k, v in kwargs.items()
|
|
214
|
-
if k not in ["config", "environment", "app"]
|
|
225
|
+
k: v for k, v in kwargs.items() if k not in ["config", "environment"]
|
|
215
226
|
}
|
|
216
227
|
if not config_schema:
|
|
217
228
|
if "environment" in kwargs and kwargs["environment"] is not None:
|
|
@@ -221,19 +232,10 @@ class entrypoint:
|
|
|
221
232
|
else:
|
|
222
233
|
ag.config.pull(config_name="default")
|
|
223
234
|
|
|
224
|
-
app_id = environ.get("AGENTA_APP_ID")
|
|
225
|
-
|
|
226
235
|
with routing_context_manager(
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
},
|
|
231
|
-
variant={
|
|
232
|
-
"slug": kwargs.get("config"),
|
|
233
|
-
},
|
|
234
|
-
environment={
|
|
235
|
-
"slug": kwargs.get("environment"),
|
|
236
|
-
},
|
|
236
|
+
config=config_params,
|
|
237
|
+
variant=kwargs["config"],
|
|
238
|
+
environment=kwargs["environment"],
|
|
237
239
|
):
|
|
238
240
|
entrypoint_result = await self.execute_function(
|
|
239
241
|
func,
|
|
@@ -256,7 +258,7 @@ class entrypoint:
|
|
|
256
258
|
|
|
257
259
|
route_deployed = f"{RUN_PATH}{route_path}"
|
|
258
260
|
app.post(route_deployed, response_model=BaseResponse)(wrapper_deployed)
|
|
259
|
-
###
|
|
261
|
+
### ---------------------------- #
|
|
260
262
|
|
|
261
263
|
### --- Update OpenAPI --- #
|
|
262
264
|
app.openapi_schema = None # Forces FastAPI to re-generate the schema
|
|
@@ -287,8 +289,8 @@ class entrypoint:
|
|
|
287
289
|
|
|
288
290
|
def extract_ingestible_files(
|
|
289
291
|
self,
|
|
290
|
-
func_signature: Signature,
|
|
291
|
-
) -> Dict[str, Parameter]:
|
|
292
|
+
func_signature: inspect.Signature,
|
|
293
|
+
) -> Dict[str, inspect.Parameter]:
|
|
292
294
|
"""Extract parameters annotated as InFile from function signature."""
|
|
293
295
|
|
|
294
296
|
return {
|
|
@@ -315,7 +317,7 @@ class entrypoint:
|
|
|
315
317
|
def ingest_files(
|
|
316
318
|
self,
|
|
317
319
|
func_params: Dict[str, Any],
|
|
318
|
-
ingestible_files: Dict[str, Parameter],
|
|
320
|
+
ingestible_files: Dict[str, inspect.Parameter],
|
|
319
321
|
) -> None:
|
|
320
322
|
"""Ingest files specified in function parameters."""
|
|
321
323
|
|
|
@@ -323,65 +325,6 @@ class entrypoint:
|
|
|
323
325
|
if name in func_params and func_params[name] is not None:
|
|
324
326
|
func_params[name] = self.ingest_file(func_params[name])
|
|
325
327
|
|
|
326
|
-
async def execute_function(
|
|
327
|
-
self,
|
|
328
|
-
func: Callable[..., Any],
|
|
329
|
-
inline_trace,
|
|
330
|
-
*args,
|
|
331
|
-
**func_params,
|
|
332
|
-
):
|
|
333
|
-
log.info(f"---------------------------")
|
|
334
|
-
log.info(f"Agenta SDK - running route: {repr(self.route_path or '/')}")
|
|
335
|
-
log.info(f"---------------------------")
|
|
336
|
-
|
|
337
|
-
tracing_context.set(routing_context.get())
|
|
338
|
-
|
|
339
|
-
try:
|
|
340
|
-
result = (
|
|
341
|
-
await func(*args, **func_params["params"])
|
|
342
|
-
if iscoroutinefunction(func)
|
|
343
|
-
else func(*args, **func_params["params"])
|
|
344
|
-
)
|
|
345
|
-
|
|
346
|
-
return await self.handle_success(result, inline_trace)
|
|
347
|
-
|
|
348
|
-
except Exception as error:
|
|
349
|
-
self.handle_failure(error)
|
|
350
|
-
|
|
351
|
-
async def handle_success(self, result: Any, inline_trace: bool):
|
|
352
|
-
data = None
|
|
353
|
-
trace = dict()
|
|
354
|
-
|
|
355
|
-
with suppress():
|
|
356
|
-
data = self.patch_result(result)
|
|
357
|
-
|
|
358
|
-
if inline_trace:
|
|
359
|
-
trace = await self.fetch_inline_trace(inline_trace)
|
|
360
|
-
|
|
361
|
-
log.info(f"----------------------------------")
|
|
362
|
-
log.info(f"Agenta SDK - exiting with success: 200")
|
|
363
|
-
log.info(f"----------------------------------")
|
|
364
|
-
|
|
365
|
-
return BaseResponse(data=data, trace=trace)
|
|
366
|
-
|
|
367
|
-
def handle_failure(self, error: Exception):
|
|
368
|
-
log.error("--------------------------------------------------")
|
|
369
|
-
log.error("Agenta SDK - handling application exception below:")
|
|
370
|
-
log.error("--------------------------------------------------")
|
|
371
|
-
log.error(format_exc().strip("\n"))
|
|
372
|
-
log.error("--------------------------------------------------")
|
|
373
|
-
|
|
374
|
-
status_code = error.status_code if hasattr(error, "status_code") else 500
|
|
375
|
-
message = str(error)
|
|
376
|
-
stacktrace = format_exception(error, value=error, tb=error.__traceback__) # type: ignore
|
|
377
|
-
detail = {"message": message, "stacktrace": stacktrace}
|
|
378
|
-
|
|
379
|
-
log.error(f"----------------------------------")
|
|
380
|
-
log.error(f"Agenta SDK - exiting with failure: {status_code}")
|
|
381
|
-
log.error(f"----------------------------------")
|
|
382
|
-
|
|
383
|
-
raise HTTPException(status_code=status_code, detail=detail)
|
|
384
|
-
|
|
385
328
|
def patch_result(self, result: Any):
|
|
386
329
|
"""
|
|
387
330
|
Patch the result to only include the message if the result is a FuncResponse-style dictionary with message, cost, and usage keys.
|
|
@@ -419,39 +362,82 @@ class entrypoint:
|
|
|
419
362
|
|
|
420
363
|
return data
|
|
421
364
|
|
|
422
|
-
async def
|
|
365
|
+
async def execute_function(
|
|
366
|
+
self,
|
|
367
|
+
func: Callable[..., Any],
|
|
368
|
+
inline_trace,
|
|
369
|
+
*args,
|
|
370
|
+
**func_params,
|
|
371
|
+
):
|
|
372
|
+
log.info(f"\n--------------------------")
|
|
373
|
+
log.info(
|
|
374
|
+
f"Running application route: {repr(self.route_path if self.route_path != '' else '/')}"
|
|
375
|
+
)
|
|
376
|
+
log.info(f"--------------------------\n")
|
|
377
|
+
|
|
378
|
+
tracing_context.set(routing_context.get())
|
|
379
|
+
|
|
423
380
|
WAIT_FOR_SPANS = True
|
|
424
381
|
TIMEOUT = 1
|
|
425
382
|
TIMESTEP = 0.1
|
|
426
383
|
FINALSTEP = 0.001
|
|
427
384
|
NOFSTEPS = TIMEOUT / TIMESTEP
|
|
428
385
|
|
|
429
|
-
|
|
386
|
+
data = None
|
|
387
|
+
trace = {}
|
|
388
|
+
|
|
389
|
+
try:
|
|
390
|
+
result = (
|
|
391
|
+
await func(*args, **func_params["params"])
|
|
392
|
+
if inspect.iscoroutinefunction(func)
|
|
393
|
+
else func(*args, **func_params["params"])
|
|
394
|
+
)
|
|
395
|
+
data = self.patch_result(result)
|
|
396
|
+
except Exception as e:
|
|
397
|
+
log.error(f"Agenta SDK - Routing Exception")
|
|
398
|
+
|
|
399
|
+
traceback.print_exc()
|
|
430
400
|
|
|
431
|
-
|
|
401
|
+
self.handle_exception(e)
|
|
432
402
|
|
|
433
|
-
|
|
403
|
+
with suppress():
|
|
404
|
+
root_context: Dict[str, Any] = tracing_context.get().get("root")
|
|
434
405
|
|
|
435
|
-
|
|
436
|
-
if inline_trace:
|
|
437
|
-
if WAIT_FOR_SPANS:
|
|
438
|
-
remaining_steps = NOFSTEPS
|
|
406
|
+
trace_id = root_context.get("trace_id") if root_context else None
|
|
439
407
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
await sleep(TIMESTEP)
|
|
408
|
+
if trace_id is not None:
|
|
409
|
+
if inline_trace:
|
|
410
|
+
if WAIT_FOR_SPANS:
|
|
411
|
+
remaining_steps = NOFSTEPS
|
|
445
412
|
|
|
446
|
-
|
|
413
|
+
while (
|
|
414
|
+
not ag.tracing.is_inline_trace_ready(trace_id)
|
|
415
|
+
and remaining_steps > 0
|
|
416
|
+
):
|
|
417
|
+
await asyncio.sleep(TIMESTEP)
|
|
447
418
|
|
|
448
|
-
|
|
419
|
+
remaining_steps -= 1
|
|
449
420
|
|
|
450
|
-
|
|
451
|
-
else:
|
|
452
|
-
trace = {"trace_id": trace_id}
|
|
421
|
+
await asyncio.sleep(FINALSTEP)
|
|
453
422
|
|
|
454
|
-
|
|
423
|
+
trace = ag.tracing.get_inline_trace(trace_id)
|
|
424
|
+
else:
|
|
425
|
+
trace = {"trace_id": trace_id}
|
|
426
|
+
|
|
427
|
+
response = BaseResponse(data=data, trace=trace)
|
|
428
|
+
|
|
429
|
+
return response
|
|
430
|
+
|
|
431
|
+
def handle_exception(self, e: Exception):
|
|
432
|
+
status_code = e.status_code if hasattr(e, "status_code") else 500
|
|
433
|
+
message = str(e)
|
|
434
|
+
stacktrace = traceback.format_exception(e, value=e, tb=e.__traceback__) # type: ignore
|
|
435
|
+
detail = {"message": message, "stacktrace": stacktrace}
|
|
436
|
+
|
|
437
|
+
raise HTTPException(
|
|
438
|
+
status_code=status_code,
|
|
439
|
+
detail=detail,
|
|
440
|
+
)
|
|
455
441
|
|
|
456
442
|
def update_wrapper_signature(
|
|
457
443
|
self, wrapper: Callable[..., Any], updated_params: List
|
|
@@ -461,25 +447,25 @@ class entrypoint:
|
|
|
461
447
|
|
|
462
448
|
Args:
|
|
463
449
|
wrapper (callable): A callable object, such as a function or a method, that requires a signature update.
|
|
464
|
-
updated_params (List[Parameter]): A list of `Parameter` objects representing the updated parameters
|
|
450
|
+
updated_params (List[inspect.Parameter]): A list of `inspect.Parameter` objects representing the updated parameters
|
|
465
451
|
for the wrapper function.
|
|
466
452
|
"""
|
|
467
453
|
|
|
468
|
-
wrapper_signature = signature(wrapper)
|
|
454
|
+
wrapper_signature = inspect.signature(wrapper)
|
|
469
455
|
wrapper_signature = wrapper_signature.replace(parameters=updated_params)
|
|
470
456
|
wrapper.__signature__ = wrapper_signature # type: ignore
|
|
471
457
|
|
|
472
458
|
def update_function_signature(
|
|
473
459
|
self,
|
|
474
460
|
wrapper: Callable[..., Any],
|
|
475
|
-
func_signature: Signature,
|
|
461
|
+
func_signature: inspect.Signature,
|
|
476
462
|
config_class: Type[BaseModel], # TODO: change to our type
|
|
477
463
|
config_dict: Dict[str, Any],
|
|
478
|
-
ingestible_files: Dict[str, Parameter],
|
|
464
|
+
ingestible_files: Dict[str, inspect.Parameter],
|
|
479
465
|
) -> None:
|
|
480
466
|
"""Update the function signature to include new parameters."""
|
|
481
467
|
|
|
482
|
-
updated_params: List[Parameter] = []
|
|
468
|
+
updated_params: List[inspect.Parameter] = []
|
|
483
469
|
if config_class:
|
|
484
470
|
self.add_config_params_to_parser(updated_params, config_class)
|
|
485
471
|
else:
|
|
@@ -490,21 +476,21 @@ class entrypoint:
|
|
|
490
476
|
def update_deployed_function_signature(
|
|
491
477
|
self,
|
|
492
478
|
wrapper: Callable[..., Any],
|
|
493
|
-
func_signature: Signature,
|
|
494
|
-
ingestible_files: Dict[str, Parameter],
|
|
479
|
+
func_signature: inspect.Signature,
|
|
480
|
+
ingestible_files: Dict[str, inspect.Parameter],
|
|
495
481
|
) -> None:
|
|
496
482
|
"""Update the function signature to include new parameters."""
|
|
497
483
|
|
|
498
|
-
updated_params: List[Parameter] = []
|
|
484
|
+
updated_params: List[inspect.Parameter] = []
|
|
499
485
|
self.add_func_params_to_parser(updated_params, func_signature, ingestible_files)
|
|
500
486
|
for param in [
|
|
501
487
|
"config",
|
|
502
488
|
"environment",
|
|
503
489
|
]: # we add the config and environment parameters
|
|
504
490
|
updated_params.append(
|
|
505
|
-
Parameter(
|
|
491
|
+
inspect.Parameter(
|
|
506
492
|
name=param,
|
|
507
|
-
kind=Parameter.KEYWORD_ONLY,
|
|
493
|
+
kind=inspect.Parameter.KEYWORD_ONLY,
|
|
508
494
|
default=Body(None),
|
|
509
495
|
annotation=str,
|
|
510
496
|
)
|
|
@@ -518,9 +504,9 @@ class entrypoint:
|
|
|
518
504
|
for name, field in config_class.__fields__.items():
|
|
519
505
|
assert field.default is not None, f"Field {name} has no default value"
|
|
520
506
|
updated_params.append(
|
|
521
|
-
Parameter(
|
|
507
|
+
inspect.Parameter(
|
|
522
508
|
name=name,
|
|
523
|
-
kind=Parameter.KEYWORD_ONLY,
|
|
509
|
+
kind=inspect.Parameter.KEYWORD_ONLY,
|
|
524
510
|
annotation=field.annotation.__name__,
|
|
525
511
|
default=Body(field.default),
|
|
526
512
|
)
|
|
@@ -535,9 +521,9 @@ class entrypoint:
|
|
|
535
521
|
len(param.__class__.__bases__) == 1
|
|
536
522
|
), f"Inherited standard type of {param.__class__} needs to be one."
|
|
537
523
|
updated_params.append(
|
|
538
|
-
Parameter(
|
|
524
|
+
inspect.Parameter(
|
|
539
525
|
name=name,
|
|
540
|
-
kind=Parameter.KEYWORD_ONLY,
|
|
526
|
+
kind=inspect.Parameter.KEYWORD_ONLY,
|
|
541
527
|
default=Body(param),
|
|
542
528
|
annotation=param.__class__.__bases__[
|
|
543
529
|
0
|
|
@@ -550,23 +536,23 @@ class entrypoint:
|
|
|
550
536
|
def add_func_params_to_parser(
|
|
551
537
|
self,
|
|
552
538
|
updated_params: list,
|
|
553
|
-
func_signature: Signature,
|
|
554
|
-
ingestible_files: Dict[str, Parameter],
|
|
539
|
+
func_signature: inspect.Signature,
|
|
540
|
+
ingestible_files: Dict[str, inspect.Parameter],
|
|
555
541
|
) -> None:
|
|
556
542
|
"""Add function parameters to function signature."""
|
|
557
543
|
for name, param in func_signature.parameters.items():
|
|
558
544
|
if name in ingestible_files:
|
|
559
545
|
updated_params.append(
|
|
560
|
-
Parameter(name, param.kind, annotation=UploadFile)
|
|
546
|
+
inspect.Parameter(name, param.kind, annotation=UploadFile)
|
|
561
547
|
)
|
|
562
548
|
else:
|
|
563
549
|
assert (
|
|
564
550
|
len(param.default.__class__.__bases__) == 1
|
|
565
551
|
), f"Inherited standard type of {param.default.__class__} needs to be one."
|
|
566
552
|
updated_params.append(
|
|
567
|
-
Parameter(
|
|
553
|
+
inspect.Parameter(
|
|
568
554
|
name,
|
|
569
|
-
Parameter.KEYWORD_ONLY,
|
|
555
|
+
inspect.Parameter.KEYWORD_ONLY,
|
|
570
556
|
default=Body(..., embed=True),
|
|
571
557
|
annotation=param.default.__class__.__bases__[
|
|
572
558
|
0
|
|
@@ -595,7 +581,7 @@ class entrypoint:
|
|
|
595
581
|
def handle_terminal_run(
|
|
596
582
|
self,
|
|
597
583
|
func: Callable,
|
|
598
|
-
func_params: Dict[str, Parameter],
|
|
584
|
+
func_params: Dict[str, inspect.Parameter],
|
|
599
585
|
config_params: Dict[str, Any],
|
|
600
586
|
ingestible_files: Dict,
|
|
601
587
|
):
|
|
@@ -609,7 +595,7 @@ class entrypoint:
|
|
|
609
595
|
"""
|
|
610
596
|
|
|
611
597
|
# For required parameters, we add them as arguments
|
|
612
|
-
parser = ArgumentParser()
|
|
598
|
+
parser = argparse.ArgumentParser()
|
|
613
599
|
for name, param in func_params.items():
|
|
614
600
|
if name in ingestible_files:
|
|
615
601
|
parser.add_argument(name, type=str)
|
|
@@ -654,7 +640,7 @@ class entrypoint:
|
|
|
654
640
|
}
|
|
655
641
|
)
|
|
656
642
|
|
|
657
|
-
loop = get_event_loop()
|
|
643
|
+
loop = asyncio.get_event_loop()
|
|
658
644
|
|
|
659
645
|
with routing_context_manager(
|
|
660
646
|
config=args_config_params,
|
|
@@ -672,28 +658,27 @@ class entrypoint:
|
|
|
672
658
|
SHOW_DATA = False
|
|
673
659
|
SHOW_TRACE = False
|
|
674
660
|
|
|
675
|
-
|
|
676
|
-
log.info("\n========= Result =========\n")
|
|
661
|
+
log.info("\n========= Result =========\n")
|
|
677
662
|
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
663
|
+
log.info(f"trace_id: {result.trace['trace_id']}")
|
|
664
|
+
if SHOW_DETAILS:
|
|
665
|
+
log.info(f"latency: {result.trace.get('latency')}")
|
|
666
|
+
log.info(f"cost: {result.trace.get('cost')}")
|
|
667
|
+
log.info(f"usage: {list(result.trace.get('usage', {}).values())}")
|
|
683
668
|
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
669
|
+
if SHOW_DATA:
|
|
670
|
+
log.info(" ")
|
|
671
|
+
log.info(f"data:")
|
|
672
|
+
log.info(json.dumps(result.data, indent=2))
|
|
688
673
|
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
674
|
+
if SHOW_TRACE:
|
|
675
|
+
log.info(" ")
|
|
676
|
+
log.info(f"trace:")
|
|
677
|
+
log.info(f"----------------")
|
|
678
|
+
log.info(json.dumps(result.trace.get("spans", []), indent=2))
|
|
679
|
+
log.info(f"----------------")
|
|
695
680
|
|
|
696
|
-
|
|
681
|
+
log.info("\n==========================\n")
|
|
697
682
|
|
|
698
683
|
def override_config_in_schema(
|
|
699
684
|
self,
|
|
@@ -900,7 +885,10 @@ class entrypoint:
|
|
|
900
885
|
subschema["maximum"] = param_val.maxval # type: ignore
|
|
901
886
|
subschema["default"] = param_val
|
|
902
887
|
|
|
903
|
-
elif
|
|
888
|
+
elif (
|
|
889
|
+
isinstance(param_val, inspect.Parameter)
|
|
890
|
+
and param_val.annotation is DictInput
|
|
891
|
+
):
|
|
904
892
|
subschema = find_in_schema(
|
|
905
893
|
param_val.annotation.__schema_type_properties__(),
|
|
906
894
|
schema_to_override,
|
|
@@ -919,7 +907,7 @@ class entrypoint:
|
|
|
919
907
|
subschema["default"] = param_val
|
|
920
908
|
|
|
921
909
|
elif (
|
|
922
|
-
isinstance(param_val, Parameter)
|
|
910
|
+
isinstance(param_val, inspect.Parameter)
|
|
923
911
|
and param_val.annotation is MessagesInput
|
|
924
912
|
):
|
|
925
913
|
subschema = find_in_schema(
|
|
@@ -931,7 +919,7 @@ class entrypoint:
|
|
|
931
919
|
subschema["default"] = param_val.default
|
|
932
920
|
|
|
933
921
|
elif (
|
|
934
|
-
isinstance(param_val, Parameter)
|
|
922
|
+
isinstance(param_val, inspect.Parameter)
|
|
935
923
|
and param_val.annotation is FileInputURL
|
|
936
924
|
):
|
|
937
925
|
subschema = find_in_schema(
|
|
@@ -955,6 +943,6 @@ class entrypoint:
|
|
|
955
943
|
"title": str(param_name).capitalize(),
|
|
956
944
|
"type": get_type_from_param(param_val),
|
|
957
945
|
}
|
|
958
|
-
if param_val.default != _empty:
|
|
946
|
+
if param_val.default != inspect._empty:
|
|
959
947
|
subschema["default"] = param_val.default # type: ignore
|
|
960
948
|
schema_to_override[param_name] = subschema
|