agenta 0.57.0__py3-none-any.whl → 0.65.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.
- agenta/__init__.py +12 -3
- agenta/client/__init__.py +4 -4
- agenta/client/backend/__init__.py +4 -4
- agenta/client/backend/api_keys/client.py +2 -2
- agenta/client/backend/billing/client.py +2 -2
- agenta/client/backend/billing/raw_client.py +2 -2
- agenta/client/backend/client.py +56 -48
- agenta/client/backend/core/client_wrapper.py +2 -2
- agenta/client/backend/core/file.py +3 -1
- agenta/client/backend/core/http_client.py +3 -3
- agenta/client/backend/core/pydantic_utilities.py +13 -3
- agenta/client/backend/human_evaluations/client.py +2 -2
- agenta/client/backend/human_evaluations/raw_client.py +2 -2
- agenta/client/backend/organization/client.py +46 -34
- agenta/client/backend/organization/raw_client.py +32 -26
- agenta/client/backend/raw_client.py +26 -26
- agenta/client/backend/testsets/client.py +18 -18
- agenta/client/backend/testsets/raw_client.py +30 -30
- agenta/client/backend/types/__init__.py +4 -4
- agenta/client/backend/types/account_request.py +3 -1
- agenta/client/backend/types/account_response.py +3 -1
- agenta/client/backend/types/agenta_node_dto.py +3 -1
- agenta/client/backend/types/agenta_nodes_response.py +3 -1
- agenta/client/backend/types/agenta_root_dto.py +3 -1
- agenta/client/backend/types/agenta_roots_response.py +3 -1
- agenta/client/backend/types/agenta_tree_dto.py +3 -1
- agenta/client/backend/types/agenta_trees_response.py +3 -1
- agenta/client/backend/types/aggregated_result.py +3 -1
- agenta/client/backend/types/analytics_response.py +3 -1
- agenta/client/backend/types/annotation.py +6 -4
- agenta/client/backend/types/annotation_create.py +3 -1
- agenta/client/backend/types/annotation_edit.py +3 -1
- agenta/client/backend/types/annotation_link.py +3 -1
- agenta/client/backend/types/annotation_link_response.py +3 -1
- agenta/client/backend/types/annotation_query.py +3 -1
- agenta/client/backend/types/annotation_query_request.py +3 -1
- agenta/client/backend/types/annotation_reference.py +3 -1
- agenta/client/backend/types/annotation_references.py +3 -1
- agenta/client/backend/types/annotation_response.py +3 -1
- agenta/client/backend/types/annotations_response.py +3 -1
- agenta/client/backend/types/app.py +3 -1
- agenta/client/backend/types/app_variant_response.py +3 -1
- agenta/client/backend/types/app_variant_revision.py +3 -1
- agenta/client/backend/types/artifact.py +6 -4
- agenta/client/backend/types/base_output.py +3 -1
- agenta/client/backend/types/body_fetch_workflow_revision.py +3 -1
- agenta/client/backend/types/body_import_testset.py +3 -1
- agenta/client/backend/types/bucket_dto.py +3 -1
- agenta/client/backend/types/collect_status_response.py +3 -1
- agenta/client/backend/types/config_db.py +3 -1
- agenta/client/backend/types/config_dto.py +3 -1
- agenta/client/backend/types/config_response_model.py +3 -1
- agenta/client/backend/types/correct_answer.py +3 -1
- agenta/client/backend/types/create_app_output.py +3 -1
- agenta/client/backend/types/custom_model_settings_dto.py +3 -1
- agenta/client/backend/types/custom_provider_dto.py +3 -1
- agenta/client/backend/types/custom_provider_kind.py +1 -1
- agenta/client/backend/types/custom_provider_settings_dto.py +3 -1
- agenta/client/backend/types/delete_evaluation.py +3 -1
- agenta/client/backend/types/environment_output.py +3 -1
- agenta/client/backend/types/environment_output_extended.py +3 -1
- agenta/client/backend/types/environment_revision.py +3 -1
- agenta/client/backend/types/error.py +3 -1
- agenta/client/backend/types/evaluation.py +3 -1
- agenta/client/backend/types/evaluation_scenario.py +3 -1
- agenta/client/backend/types/evaluation_scenario_input.py +3 -1
- agenta/client/backend/types/evaluation_scenario_output.py +3 -1
- agenta/client/backend/types/evaluation_scenario_result.py +3 -1
- agenta/client/backend/types/evaluator.py +6 -4
- agenta/client/backend/types/evaluator_config.py +6 -4
- agenta/client/backend/types/evaluator_flags.py +3 -1
- agenta/client/backend/types/evaluator_mapping_output_interface.py +3 -1
- agenta/client/backend/types/evaluator_output_interface.py +3 -1
- agenta/client/backend/types/evaluator_query.py +3 -1
- agenta/client/backend/types/evaluator_query_request.py +3 -1
- agenta/client/backend/types/evaluator_request.py +3 -1
- agenta/client/backend/types/evaluator_response.py +3 -1
- agenta/client/backend/types/evaluators_response.py +3 -1
- agenta/client/backend/types/exception_dto.py +3 -1
- agenta/client/backend/types/extended_o_tel_tracing_response.py +3 -1
- agenta/client/backend/types/get_config_response.py +3 -1
- agenta/client/backend/types/header.py +3 -1
- agenta/client/backend/types/http_validation_error.py +3 -1
- agenta/client/backend/types/human_evaluation.py +3 -1
- agenta/client/backend/types/human_evaluation_scenario.py +3 -1
- agenta/client/backend/types/human_evaluation_scenario_input.py +3 -1
- agenta/client/backend/types/human_evaluation_scenario_output.py +3 -1
- agenta/client/backend/types/invite_request.py +3 -1
- agenta/client/backend/types/legacy_analytics_response.py +3 -1
- agenta/client/backend/types/legacy_data_point.py +3 -1
- agenta/client/backend/types/legacy_evaluator.py +3 -1
- agenta/client/backend/types/legacy_scope_request.py +3 -1
- agenta/client/backend/types/legacy_scopes_response.py +3 -1
- agenta/client/backend/types/legacy_subscription_request.py +3 -1
- agenta/client/backend/types/legacy_user_request.py +3 -1
- agenta/client/backend/types/legacy_user_response.py +3 -1
- agenta/client/backend/types/lifecycle_dto.py +3 -1
- agenta/client/backend/types/link_dto.py +3 -1
- agenta/client/backend/types/list_api_keys_response.py +3 -1
- agenta/client/backend/types/llm_run_rate_limit.py +3 -1
- agenta/client/backend/types/meta_request.py +3 -1
- agenta/client/backend/types/metrics_dto.py +3 -1
- agenta/client/backend/types/new_testset.py +3 -1
- agenta/client/backend/types/node_dto.py +3 -1
- agenta/client/backend/types/o_tel_context_dto.py +3 -1
- agenta/client/backend/types/o_tel_event.py +6 -4
- agenta/client/backend/types/o_tel_event_dto.py +3 -1
- agenta/client/backend/types/o_tel_extra_dto.py +3 -1
- agenta/client/backend/types/o_tel_flat_span.py +6 -4
- agenta/client/backend/types/o_tel_link.py +6 -4
- agenta/client/backend/types/o_tel_link_dto.py +3 -1
- agenta/client/backend/types/o_tel_links_response.py +3 -1
- agenta/client/backend/types/o_tel_span.py +1 -1
- agenta/client/backend/types/o_tel_span_dto.py +3 -1
- agenta/client/backend/types/o_tel_spans_tree.py +3 -1
- agenta/client/backend/types/o_tel_tracing_data_response.py +3 -1
- agenta/client/backend/types/o_tel_tracing_request.py +3 -1
- agenta/client/backend/types/o_tel_tracing_response.py +3 -1
- agenta/client/backend/types/organization.py +3 -1
- agenta/client/backend/types/organization_details.py +3 -1
- agenta/client/backend/types/organization_membership_request.py +3 -1
- agenta/client/backend/types/organization_output.py +3 -1
- agenta/client/backend/types/organization_request.py +3 -1
- agenta/client/backend/types/parent_dto.py +3 -1
- agenta/client/backend/types/project_membership_request.py +3 -1
- agenta/client/backend/types/project_request.py +3 -1
- agenta/client/backend/types/project_scope.py +3 -1
- agenta/client/backend/types/projects_response.py +4 -1
- agenta/client/backend/types/reference.py +6 -4
- agenta/client/backend/types/reference_dto.py +3 -1
- agenta/client/backend/types/reference_request_model.py +3 -1
- agenta/client/backend/types/result.py +3 -1
- agenta/client/backend/types/root_dto.py +3 -1
- agenta/client/backend/types/scopes_response_model.py +3 -1
- agenta/client/backend/types/secret_dto.py +3 -1
- agenta/client/backend/types/secret_response_dto.py +3 -1
- agenta/client/backend/types/simple_evaluation_output.py +3 -1
- agenta/client/backend/types/span_dto.py +6 -4
- agenta/client/backend/types/standard_provider_dto.py +3 -1
- agenta/client/backend/types/standard_provider_settings_dto.py +3 -1
- agenta/client/backend/types/status_dto.py +3 -1
- agenta/client/backend/types/tags_request.py +3 -1
- agenta/client/backend/types/testcase_response.py +6 -4
- agenta/client/backend/types/testset.py +6 -4
- agenta/client/backend/types/{test_set_output_response.py → testset_output_response.py} +4 -2
- agenta/client/backend/types/testset_request.py +3 -1
- agenta/client/backend/types/testset_response.py +3 -1
- agenta/client/backend/types/{test_set_simple_response.py → testset_simple_response.py} +4 -2
- agenta/client/backend/types/testsets_response.py +3 -1
- agenta/client/backend/types/time_dto.py +3 -1
- agenta/client/backend/types/tree_dto.py +3 -1
- agenta/client/backend/types/update_app_output.py +3 -1
- agenta/client/backend/types/user_request.py +3 -1
- agenta/client/backend/types/validation_error.py +3 -1
- agenta/client/backend/types/workflow_artifact.py +6 -4
- agenta/client/backend/types/workflow_data.py +3 -1
- agenta/client/backend/types/workflow_flags.py +3 -1
- agenta/client/backend/types/workflow_request.py +3 -1
- agenta/client/backend/types/workflow_response.py +3 -1
- agenta/client/backend/types/workflow_revision.py +6 -4
- agenta/client/backend/types/workflow_revision_request.py +3 -1
- agenta/client/backend/types/workflow_revision_response.py +3 -1
- agenta/client/backend/types/workflow_revisions_response.py +3 -1
- agenta/client/backend/types/workflow_variant.py +6 -4
- agenta/client/backend/types/workflow_variant_request.py +3 -1
- agenta/client/backend/types/workflow_variant_response.py +3 -1
- agenta/client/backend/types/workflow_variants_response.py +3 -1
- agenta/client/backend/types/workflows_response.py +3 -1
- agenta/client/backend/types/workspace.py +3 -1
- agenta/client/backend/types/workspace_member_response.py +3 -1
- agenta/client/backend/types/workspace_membership_request.py +3 -1
- agenta/client/backend/types/workspace_permission.py +3 -1
- agenta/client/backend/types/workspace_request.py +3 -1
- agenta/client/backend/types/workspace_response.py +3 -1
- agenta/client/backend/workspace/client.py +2 -2
- agenta/client/client.py +102 -88
- agenta/sdk/__init__.py +52 -3
- agenta/sdk/agenta_init.py +43 -16
- agenta/sdk/assets.py +22 -15
- agenta/sdk/context/serving.py +20 -8
- agenta/sdk/context/tracing.py +40 -22
- agenta/sdk/contexts/__init__.py +0 -0
- agenta/sdk/contexts/routing.py +38 -0
- agenta/sdk/contexts/running.py +57 -0
- agenta/sdk/contexts/tracing.py +86 -0
- agenta/sdk/decorators/__init__.py +1 -0
- agenta/sdk/decorators/routing.py +284 -0
- agenta/sdk/decorators/running.py +692 -98
- agenta/sdk/decorators/serving.py +20 -21
- agenta/sdk/decorators/tracing.py +176 -131
- agenta/sdk/engines/__init__.py +0 -0
- agenta/sdk/engines/running/__init__.py +0 -0
- agenta/sdk/engines/running/utils.py +17 -0
- agenta/sdk/engines/tracing/__init__.py +1 -0
- agenta/sdk/engines/tracing/attributes.py +185 -0
- agenta/sdk/engines/tracing/conventions.py +49 -0
- agenta/sdk/engines/tracing/exporters.py +130 -0
- agenta/sdk/engines/tracing/inline.py +1154 -0
- agenta/sdk/engines/tracing/processors.py +190 -0
- agenta/sdk/engines/tracing/propagation.py +102 -0
- agenta/sdk/engines/tracing/spans.py +136 -0
- agenta/sdk/engines/tracing/tracing.py +324 -0
- agenta/sdk/evaluations/__init__.py +2 -0
- agenta/sdk/evaluations/metrics.py +37 -0
- agenta/sdk/evaluations/preview/__init__.py +0 -0
- agenta/sdk/evaluations/preview/evaluate.py +765 -0
- agenta/sdk/evaluations/preview/utils.py +861 -0
- agenta/sdk/evaluations/results.py +66 -0
- agenta/sdk/evaluations/runs.py +152 -0
- agenta/sdk/evaluations/scenarios.py +48 -0
- agenta/sdk/litellm/litellm.py +12 -0
- agenta/sdk/litellm/mockllm.py +6 -8
- agenta/sdk/litellm/mocks/__init__.py +5 -5
- agenta/sdk/managers/applications.py +304 -0
- agenta/sdk/managers/config.py +2 -2
- agenta/sdk/managers/evaluations.py +0 -0
- agenta/sdk/managers/evaluators.py +303 -0
- agenta/sdk/managers/secrets.py +161 -24
- agenta/sdk/managers/shared.py +3 -1
- agenta/sdk/managers/testsets.py +441 -0
- agenta/sdk/managers/vault.py +3 -3
- agenta/sdk/middleware/auth.py +0 -176
- agenta/sdk/middleware/vault.py +203 -8
- agenta/sdk/middlewares/__init__.py +0 -0
- agenta/sdk/middlewares/routing/__init__.py +0 -0
- agenta/sdk/middlewares/routing/auth.py +263 -0
- agenta/sdk/middlewares/routing/cors.py +30 -0
- agenta/sdk/middlewares/routing/otel.py +29 -0
- agenta/sdk/middlewares/running/__init__.py +0 -0
- agenta/sdk/middlewares/running/normalizer.py +321 -0
- agenta/sdk/middlewares/running/resolver.py +161 -0
- agenta/sdk/middlewares/running/vault.py +140 -0
- agenta/sdk/models/__init__.py +0 -0
- agenta/sdk/models/blobs.py +33 -0
- agenta/sdk/models/evaluations.py +119 -0
- agenta/sdk/models/git.py +126 -0
- agenta/sdk/models/shared.py +167 -0
- agenta/sdk/models/testsets.py +163 -0
- agenta/sdk/models/tracing.py +202 -0
- agenta/sdk/models/workflows.py +753 -0
- agenta/sdk/tracing/exporters.py +67 -17
- agenta/sdk/tracing/processors.py +97 -0
- agenta/sdk/tracing/propagation.py +3 -1
- agenta/sdk/tracing/spans.py +4 -0
- agenta/sdk/tracing/tracing.py +13 -13
- agenta/sdk/types.py +211 -17
- agenta/sdk/utils/cache.py +1 -1
- agenta/sdk/utils/client.py +38 -0
- agenta/sdk/utils/helpers.py +13 -12
- agenta/sdk/utils/logging.py +18 -78
- agenta/sdk/utils/references.py +23 -0
- agenta/sdk/workflows/builtin.py +600 -0
- agenta/sdk/workflows/configurations.py +22 -0
- agenta/sdk/workflows/errors.py +292 -0
- agenta/sdk/workflows/handlers.py +1791 -0
- agenta/sdk/workflows/interfaces.py +948 -0
- agenta/sdk/workflows/sandbox.py +118 -0
- agenta/sdk/workflows/utils.py +303 -6
- {agenta-0.57.0.dist-info → agenta-0.65.0.dist-info}/METADATA +44 -47
- agenta-0.65.0.dist-info/RECORD +421 -0
- agenta/sdk/middleware/adapt.py +0 -253
- agenta/sdk/middleware/base.py +0 -40
- agenta/sdk/middleware/flags.py +0 -40
- agenta/sdk/workflows/types.py +0 -472
- agenta-0.57.0.dist-info/RECORD +0 -371
- /agenta/sdk/{workflows → engines/running}/registry.py +0 -0
- {agenta-0.57.0.dist-info → agenta-0.65.0.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
from typing import Optional, Any, Dict, Callable
|
|
2
|
+
from enum import Enum
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
from opentelemetry.trace import (
|
|
8
|
+
get_current_span,
|
|
9
|
+
set_tracer_provider,
|
|
10
|
+
get_tracer_provider,
|
|
11
|
+
Status,
|
|
12
|
+
StatusCode,
|
|
13
|
+
)
|
|
14
|
+
from opentelemetry.sdk import trace
|
|
15
|
+
from opentelemetry.sdk.trace import Span, Tracer, TracerProvider
|
|
16
|
+
from opentelemetry.sdk.resources import Resource
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
from agenta.sdk.utils.singleton import Singleton
|
|
20
|
+
from agenta.sdk.utils.exceptions import suppress
|
|
21
|
+
from agenta.sdk.utils.logging import get_module_logger
|
|
22
|
+
from agenta.sdk.engines.tracing.processors import (
|
|
23
|
+
TraceProcessor,
|
|
24
|
+
EndedSpanRecorder,
|
|
25
|
+
_get_last_ended,
|
|
26
|
+
)
|
|
27
|
+
from agenta.sdk.engines.tracing.exporters import InlineExporter, OTLPExporter
|
|
28
|
+
from agenta.sdk.engines.tracing.spans import CustomSpan
|
|
29
|
+
from agenta.sdk.engines.tracing.inline import parse_inline_trace
|
|
30
|
+
from agenta.sdk.engines.tracing.conventions import Reference, is_valid_attribute_key
|
|
31
|
+
from agenta.sdk.engines.tracing.propagation import extract, inject
|
|
32
|
+
from agenta.sdk.utils.cache import TTLLRUCache
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
log = get_module_logger(__name__)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
_original_init = trace.TracerProvider.__init__
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def patched_init(self, *args, **kwargs):
|
|
42
|
+
_original_init(self, *args, **kwargs)
|
|
43
|
+
self.add_span_processor(EndedSpanRecorder())
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
trace.TracerProvider.__init__ = patched_init
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Link(BaseModel):
|
|
50
|
+
trace_id: str
|
|
51
|
+
span_id: str
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class Tracing(metaclass=Singleton):
|
|
55
|
+
Status = Status
|
|
56
|
+
StatusCode = StatusCode
|
|
57
|
+
|
|
58
|
+
def __init__(
|
|
59
|
+
self,
|
|
60
|
+
url: str,
|
|
61
|
+
redact: Optional[Callable[..., Any]] = None,
|
|
62
|
+
redact_on_error: Optional[bool] = True,
|
|
63
|
+
) -> None:
|
|
64
|
+
# ENDPOINT (OTLP)
|
|
65
|
+
self.otlp_url = url
|
|
66
|
+
# HEADERS (OTLP)
|
|
67
|
+
self.headers: Dict[str, str] = dict()
|
|
68
|
+
# REFERENCES
|
|
69
|
+
self.references: Dict[str, str] = dict()
|
|
70
|
+
# CREDENTIALS
|
|
71
|
+
self.credentials: TTLLRUCache = TTLLRUCache(ttl=(60 * 60)) # 1 hour x 512 keys
|
|
72
|
+
|
|
73
|
+
# TRACER PROVIDER
|
|
74
|
+
self.tracer_provider: Optional[TracerProvider] = None
|
|
75
|
+
# TRACE PROCESSORS -- INLINE
|
|
76
|
+
self.inline: Optional[TraceProcessor] = None
|
|
77
|
+
# TRACER
|
|
78
|
+
self.tracer: Optional[Tracer] = None
|
|
79
|
+
# INLINE SPANS for INLINE TRACES (INLINE PROCESSOR)
|
|
80
|
+
self.inline_spans: Dict[str, Any] = dict()
|
|
81
|
+
|
|
82
|
+
# REDACT
|
|
83
|
+
self.redact = redact
|
|
84
|
+
self.redact_on_error = redact_on_error
|
|
85
|
+
|
|
86
|
+
# PUBLIC
|
|
87
|
+
|
|
88
|
+
def configure(
|
|
89
|
+
self,
|
|
90
|
+
api_key: Optional[str] = None,
|
|
91
|
+
inline: Optional[bool] = True,
|
|
92
|
+
):
|
|
93
|
+
# HEADERS (OTLP)
|
|
94
|
+
if api_key:
|
|
95
|
+
self.headers["Authorization"] = f"ApiKey {api_key}"
|
|
96
|
+
|
|
97
|
+
# TRACER PROVIDER
|
|
98
|
+
self.tracer_provider = TracerProvider(
|
|
99
|
+
resource=Resource(attributes={"service.name": "agenta-sdk"})
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
# --- INLINE
|
|
103
|
+
if inline:
|
|
104
|
+
# TRACE PROCESSORS -- INLINE
|
|
105
|
+
self.inline = TraceProcessor(
|
|
106
|
+
InlineExporter(
|
|
107
|
+
registry=self.inline_spans,
|
|
108
|
+
),
|
|
109
|
+
references=self.references,
|
|
110
|
+
inline=inline,
|
|
111
|
+
)
|
|
112
|
+
self.tracer_provider.add_span_processor(self.inline)
|
|
113
|
+
# --- INLINE
|
|
114
|
+
|
|
115
|
+
# TRACE PROCESSORS -- OTLP
|
|
116
|
+
try:
|
|
117
|
+
log.info("Agenta - OLTP URL: %s", self.otlp_url)
|
|
118
|
+
|
|
119
|
+
_otlp = TraceProcessor(
|
|
120
|
+
OTLPExporter(
|
|
121
|
+
endpoint=self.otlp_url,
|
|
122
|
+
headers=self.headers,
|
|
123
|
+
credentials=self.credentials,
|
|
124
|
+
),
|
|
125
|
+
references=self.references,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
self.tracer_provider.add_span_processor(_otlp)
|
|
129
|
+
except: # pylint: disable=bare-except
|
|
130
|
+
log.warning("Agenta - OLTP unreachable, skipping exports.")
|
|
131
|
+
|
|
132
|
+
# GLOBAL TRACER PROVIDER -- INSTRUMENTATION LIBRARIES
|
|
133
|
+
set_tracer_provider(self.tracer_provider)
|
|
134
|
+
# TRACER
|
|
135
|
+
self.tracer: Tracer = self.tracer_provider.get_tracer("agenta.tracer")
|
|
136
|
+
|
|
137
|
+
def get_current_span(self):
|
|
138
|
+
_span = None
|
|
139
|
+
|
|
140
|
+
with suppress():
|
|
141
|
+
_span = get_current_span()
|
|
142
|
+
|
|
143
|
+
if _span.is_recording():
|
|
144
|
+
return CustomSpan(_span)
|
|
145
|
+
|
|
146
|
+
return _span
|
|
147
|
+
|
|
148
|
+
def store_internals(
|
|
149
|
+
self,
|
|
150
|
+
attributes: Dict[str, Any],
|
|
151
|
+
span: Optional[Span] = None,
|
|
152
|
+
):
|
|
153
|
+
with suppress():
|
|
154
|
+
if span is None:
|
|
155
|
+
span = self.get_current_span()
|
|
156
|
+
|
|
157
|
+
span.set_attributes(
|
|
158
|
+
attributes={"internals": attributes},
|
|
159
|
+
namespace="data",
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
def store_refs(
|
|
163
|
+
self,
|
|
164
|
+
refs: Dict[str, str],
|
|
165
|
+
span: Optional[Span] = None,
|
|
166
|
+
):
|
|
167
|
+
with suppress():
|
|
168
|
+
if span is None:
|
|
169
|
+
span = self.get_current_span()
|
|
170
|
+
|
|
171
|
+
for key in refs.keys():
|
|
172
|
+
if key in [_.value for _ in Reference.__members__.values()]:
|
|
173
|
+
# ADD REFERENCE TO THIS SPAN
|
|
174
|
+
span.set_attribute(
|
|
175
|
+
key.value if isinstance(key, Enum) else key,
|
|
176
|
+
refs[key],
|
|
177
|
+
namespace="refs",
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
# AND TO ALL SPANS CREATED AFTER THIS ONE
|
|
181
|
+
self.references[key] = refs[key]
|
|
182
|
+
# TODO: THIS SHOULD BE REPLACED BY A TRACE CONTEXT !!!
|
|
183
|
+
|
|
184
|
+
def store_meta(
|
|
185
|
+
self,
|
|
186
|
+
meta: Dict[str, Any],
|
|
187
|
+
span: Optional[Span] = None,
|
|
188
|
+
):
|
|
189
|
+
with suppress():
|
|
190
|
+
if span is None:
|
|
191
|
+
span = self.get_current_span()
|
|
192
|
+
|
|
193
|
+
for key in meta.keys():
|
|
194
|
+
if is_valid_attribute_key(key):
|
|
195
|
+
span.set_attribute(
|
|
196
|
+
key,
|
|
197
|
+
meta[key],
|
|
198
|
+
namespace="meta",
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
def store_metrics(
|
|
202
|
+
self,
|
|
203
|
+
metrics: Dict[str, Any],
|
|
204
|
+
span: Optional[Span] = None,
|
|
205
|
+
):
|
|
206
|
+
with suppress():
|
|
207
|
+
if span is None:
|
|
208
|
+
span = self.get_current_span()
|
|
209
|
+
|
|
210
|
+
for key in metrics.keys():
|
|
211
|
+
if is_valid_attribute_key(key):
|
|
212
|
+
span.set_attribute(
|
|
213
|
+
key,
|
|
214
|
+
metrics[key],
|
|
215
|
+
namespace="metrics",
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
def is_inline_trace_ready(
|
|
219
|
+
self,
|
|
220
|
+
trace_id: Optional[int] = None,
|
|
221
|
+
) -> bool:
|
|
222
|
+
is_ready = True
|
|
223
|
+
|
|
224
|
+
with suppress():
|
|
225
|
+
if self.inline and trace_id:
|
|
226
|
+
is_ready = self.inline.is_ready(trace_id)
|
|
227
|
+
|
|
228
|
+
return is_ready
|
|
229
|
+
|
|
230
|
+
def get_inline_trace(
|
|
231
|
+
self,
|
|
232
|
+
trace_id: Optional[int] = None,
|
|
233
|
+
) -> Dict[str, Any]:
|
|
234
|
+
_inline_trace = {}
|
|
235
|
+
|
|
236
|
+
with suppress():
|
|
237
|
+
if self.inline and trace_id:
|
|
238
|
+
is_ready = self.inline.is_ready(trace_id)
|
|
239
|
+
|
|
240
|
+
if is_ready is True:
|
|
241
|
+
otel_spans = self.inline.fetch(trace_id)
|
|
242
|
+
|
|
243
|
+
if otel_spans:
|
|
244
|
+
_inline_trace = parse_inline_trace(otel_spans)
|
|
245
|
+
|
|
246
|
+
return _inline_trace
|
|
247
|
+
|
|
248
|
+
def extract(
|
|
249
|
+
self,
|
|
250
|
+
*args,
|
|
251
|
+
**kwargs,
|
|
252
|
+
):
|
|
253
|
+
return extract(*args, **kwargs)
|
|
254
|
+
|
|
255
|
+
def inject(
|
|
256
|
+
self,
|
|
257
|
+
*args,
|
|
258
|
+
**kwargs,
|
|
259
|
+
):
|
|
260
|
+
return inject(*args, **kwargs)
|
|
261
|
+
|
|
262
|
+
def get_current_span_context(self):
|
|
263
|
+
"""Get the current active span context if available.
|
|
264
|
+
|
|
265
|
+
Returns:
|
|
266
|
+
SpanContext or None if no active span
|
|
267
|
+
"""
|
|
268
|
+
span = get_current_span()
|
|
269
|
+
ctx = span.get_span_context()
|
|
270
|
+
return ctx if ctx and ctx.is_valid else None
|
|
271
|
+
|
|
272
|
+
def get_last_span_context(self):
|
|
273
|
+
"""Get the last closed span context if available.
|
|
274
|
+
|
|
275
|
+
This is useful for accessing span information after a span has closed,
|
|
276
|
+
particularly with auto-instrumentation libraries.
|
|
277
|
+
|
|
278
|
+
Returns:
|
|
279
|
+
SpanContext or None if no spans have been closed
|
|
280
|
+
"""
|
|
281
|
+
return _get_last_ended()
|
|
282
|
+
|
|
283
|
+
def get_span_context(self):
|
|
284
|
+
"""Get the most relevant span context.
|
|
285
|
+
|
|
286
|
+
First tries to get the current active span context.
|
|
287
|
+
If no active span exists, falls back to the last closed span.
|
|
288
|
+
|
|
289
|
+
Returns:
|
|
290
|
+
SpanContext or None if no relevant span context is available
|
|
291
|
+
"""
|
|
292
|
+
return self.get_current_span_context() or self.get_last_span_context()
|
|
293
|
+
|
|
294
|
+
def build_invocation_link(self, span_ctx=None) -> Optional[Link]:
|
|
295
|
+
"""
|
|
296
|
+
Builds a Link object containing the hex-formatted trace_id and span_id
|
|
297
|
+
from the current (or fallback last ended) span context.
|
|
298
|
+
Useful to link annotations to spans.
|
|
299
|
+
|
|
300
|
+
Args:
|
|
301
|
+
span_ctx: Optional SpanContext to convert to a Link
|
|
302
|
+
|
|
303
|
+
Returns:
|
|
304
|
+
Link object with trace_id and span_id or None if no valid context
|
|
305
|
+
"""
|
|
306
|
+
if span_ctx is None:
|
|
307
|
+
span_ctx = self.get_span_context()
|
|
308
|
+
|
|
309
|
+
if span_ctx and span_ctx.is_valid:
|
|
310
|
+
return Link(
|
|
311
|
+
trace_id=f"{span_ctx.trace_id:032x}",
|
|
312
|
+
span_id=f"{span_ctx.span_id:016x}",
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
return None
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def get_tracer(
|
|
319
|
+
tracing: Tracing,
|
|
320
|
+
) -> Tracer:
|
|
321
|
+
if tracing is None or tracing.tracer is None or tracing.tracer_provider is None:
|
|
322
|
+
return get_tracer_provider().get_tracer("default.tracer")
|
|
323
|
+
|
|
324
|
+
return tracing.tracer
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from uuid import UUID
|
|
3
|
+
|
|
4
|
+
from agenta.sdk.utils.client import authed_api
|
|
5
|
+
from agenta.sdk.models.evaluations import EvaluationMetrics
|
|
6
|
+
|
|
7
|
+
# TODO: ADD TYPES
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def arefresh(
|
|
11
|
+
run_id: UUID,
|
|
12
|
+
scenario_id: Optional[UUID] = None,
|
|
13
|
+
# timestamp: Optional[str] = None,
|
|
14
|
+
# interval: Optional[float] = None,
|
|
15
|
+
) -> EvaluationMetrics:
|
|
16
|
+
payload = dict(
|
|
17
|
+
run_id=str(run_id),
|
|
18
|
+
scenario_id=str(scenario_id) if scenario_id else None,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
response = authed_api()(
|
|
22
|
+
method="POST",
|
|
23
|
+
endpoint=f"/preview/evaluations/metrics/refresh",
|
|
24
|
+
params=payload,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
try:
|
|
28
|
+
response.raise_for_status()
|
|
29
|
+
except:
|
|
30
|
+
print(response.text)
|
|
31
|
+
raise
|
|
32
|
+
|
|
33
|
+
response = response.json()
|
|
34
|
+
|
|
35
|
+
metrics = EvaluationMetrics(**response["metrics"][0])
|
|
36
|
+
|
|
37
|
+
return metrics
|
|
File without changes
|