agenta 0.14.1__py3-none-any.whl → 0.14.1a1__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 +2 -2
- agenta/sdk/__init__.py +2 -3
- agenta/sdk/agenta_init.py +78 -63
- agenta/sdk/decorators/base.py +10 -0
- agenta/sdk/decorators/llm_entrypoint.py +538 -0
- agenta/sdk/decorators/tracing.py +56 -0
- agenta/sdk/tracing/llm_tracing.py +138 -88
- {agenta-0.14.1.dist-info → agenta-0.14.1a1.dist-info}/METADATA +1 -1
- {agenta-0.14.1.dist-info → agenta-0.14.1a1.dist-info}/RECORD +11 -10
- agenta/sdk/agenta_decorator.py +0 -501
- agenta/sdk/tracing/decorators.py +0 -41
- {agenta-0.14.1.dist-info → agenta-0.14.1a1.dist-info}/WHEEL +0 -0
- {agenta-0.14.1.dist-info → agenta-0.14.1a1.dist-info}/entry_points.txt +0 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# Stdlib Imports
|
|
2
|
+
from threading import Lock
|
|
2
3
|
from datetime import datetime, timezone
|
|
3
4
|
from typing import Optional, Dict, Any, List, Union
|
|
4
5
|
|
|
@@ -13,10 +14,43 @@ from agenta.client.backend.types.create_span import CreateSpan, SpanKind, SpanSt
|
|
|
13
14
|
from bson.objectid import ObjectId
|
|
14
15
|
|
|
15
16
|
|
|
16
|
-
class
|
|
17
|
-
"""
|
|
17
|
+
class SingletonMeta(type):
|
|
18
|
+
"""
|
|
19
|
+
Thread-safe implementation of Singleton.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
_instances = {} # type: ignore
|
|
23
|
+
|
|
24
|
+
# We need the lock mechanism to synchronize threads \
|
|
25
|
+
# during the initial access to the Singleton object.
|
|
26
|
+
_lock: Lock = Lock()
|
|
27
|
+
|
|
28
|
+
def __call__(cls, *args, **kwargs):
|
|
29
|
+
"""
|
|
30
|
+
Possible changes to the value of the `__init__` argument do not affect
|
|
31
|
+
the returned instance.
|
|
32
|
+
"""
|
|
33
|
+
# Now, imagine that the program has just been launched. Since there's no
|
|
34
|
+
# Singleton instance yet, multiple threads can simultaneously pass the
|
|
35
|
+
# previous conditional and reach this point almost at the same time. The
|
|
36
|
+
# first of them will acquire lock and will proceed further, while the
|
|
37
|
+
# rest will wait here.
|
|
38
|
+
with cls._lock:
|
|
39
|
+
# The first thread to acquire the lock, reaches this conditional,
|
|
40
|
+
# goes inside and creates the Singleton instance. Once it leaves the
|
|
41
|
+
# lock block, a thread that might have been waiting for the lock
|
|
42
|
+
# release may then enter this section. But since the Singleton field
|
|
43
|
+
# is already initialized, the thread won't create a new object.
|
|
44
|
+
if cls not in cls._instances:
|
|
45
|
+
instance = super().__call__(*args, **kwargs)
|
|
46
|
+
cls._instances[cls] = instance
|
|
47
|
+
return cls._instances[cls]
|
|
48
|
+
|
|
18
49
|
|
|
19
|
-
|
|
50
|
+
class Tracing(metaclass=SingletonMeta):
|
|
51
|
+
"""The `Tracing` class is an agent for LLM tracing with specific initialization arguments.
|
|
52
|
+
|
|
53
|
+
__init__ args:
|
|
20
54
|
base_url (str): The URL of the backend host
|
|
21
55
|
api_key (str): The API Key of the backend host
|
|
22
56
|
tasks_manager (TaskQueue): The tasks manager dedicated to handling asynchronous tasks
|
|
@@ -24,13 +58,6 @@ class Tracing(object):
|
|
|
24
58
|
max_workers (int): The maximum number of workers to run tracing
|
|
25
59
|
"""
|
|
26
60
|
|
|
27
|
-
_instance = None
|
|
28
|
-
|
|
29
|
-
def __new__(cls, *args, **kwargs):
|
|
30
|
-
if not cls._instance:
|
|
31
|
-
cls._instance = super().__new__(cls)
|
|
32
|
-
return cls._instance
|
|
33
|
-
|
|
34
61
|
def __init__(
|
|
35
62
|
self,
|
|
36
63
|
base_url: str,
|
|
@@ -46,6 +73,7 @@ class Tracing(object):
|
|
|
46
73
|
self.app_id = app_id
|
|
47
74
|
self.variant_id = variant_id
|
|
48
75
|
self.variant_name = variant_name
|
|
76
|
+
self.tracing_enabled = True
|
|
49
77
|
self.tasks_manager = TaskQueue(
|
|
50
78
|
max_workers if max_workers else 4, logger=llm_logger
|
|
51
79
|
)
|
|
@@ -68,12 +96,24 @@ class Tracing(object):
|
|
|
68
96
|
base_url=self.base_url, api_key=self.api_key, timeout=120 # type: ignore
|
|
69
97
|
).observability
|
|
70
98
|
|
|
99
|
+
@property
|
|
100
|
+
def _is_tracing_enabled(self) -> bool:
|
|
101
|
+
"""Checks if tracing is enabled.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
bool: If tracing is enabled
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
return self.tracing_enabled
|
|
108
|
+
|
|
71
109
|
def set_span_attribute(
|
|
72
110
|
self, parent_key: Optional[str] = None, attributes: Dict[str, Any] = {}
|
|
73
111
|
):
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
112
|
+
if self._is_tracing_enabled:
|
|
113
|
+
span = self.span_dict[self.active_span.id] # type: ignore
|
|
114
|
+
for key, value in attributes.items():
|
|
115
|
+
self.set_attribute(span.attributes, key, value, parent_key) # type: ignore
|
|
116
|
+
return
|
|
77
117
|
|
|
78
118
|
def set_attribute(
|
|
79
119
|
self,
|
|
@@ -96,28 +136,30 @@ class Tracing(object):
|
|
|
96
136
|
def start_parent_span(
|
|
97
137
|
self, name: str, inputs: Dict[str, Any], config: Dict[str, Any], **kwargs
|
|
98
138
|
):
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
139
|
+
if self._is_tracing_enabled:
|
|
140
|
+
trace_id = self._create_trace_id()
|
|
141
|
+
span_id = self._create_span_id()
|
|
142
|
+
self.llm_logger.info("Recording parent span...")
|
|
143
|
+
span = CreateSpan(
|
|
144
|
+
id=span_id,
|
|
145
|
+
app_id=self.app_id,
|
|
146
|
+
variant_id=self.variant_id,
|
|
147
|
+
variant_name=self.variant_name,
|
|
148
|
+
inputs=inputs,
|
|
149
|
+
name=name,
|
|
150
|
+
config=config,
|
|
151
|
+
environment=kwargs.get("environment"),
|
|
152
|
+
spankind=SpanKind.WORKFLOW.value,
|
|
153
|
+
status=SpanStatusCode.UNSET.value,
|
|
154
|
+
start_time=datetime.now(timezone.utc),
|
|
155
|
+
)
|
|
156
|
+
self.active_trace = span
|
|
157
|
+
self.recording_trace_id = trace_id
|
|
158
|
+
self.parent_span_id = span.id
|
|
159
|
+
self.llm_logger.info(
|
|
160
|
+
f"Recorded active_trace and setting parent_span_id: {span.id}"
|
|
161
|
+
)
|
|
162
|
+
return
|
|
121
163
|
|
|
122
164
|
def start_span(
|
|
123
165
|
self,
|
|
@@ -125,71 +167,79 @@ class Tracing(object):
|
|
|
125
167
|
spankind: str,
|
|
126
168
|
input: Dict[str, Any],
|
|
127
169
|
config: Dict[str, Any] = {},
|
|
128
|
-
) -> CreateSpan:
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
170
|
+
) -> Optional[CreateSpan]:
|
|
171
|
+
if self._is_tracing_enabled:
|
|
172
|
+
span_id = self._create_span_id()
|
|
173
|
+
self.llm_logger.info(f"Recording {spankind} span...")
|
|
174
|
+
span = CreateSpan(
|
|
175
|
+
id=span_id,
|
|
176
|
+
inputs=input,
|
|
177
|
+
name=name,
|
|
178
|
+
app_id=self.app_id,
|
|
179
|
+
variant_id=self.variant_id,
|
|
180
|
+
variant_name=self.variant_name,
|
|
181
|
+
config=config,
|
|
182
|
+
environment=self.active_trace.environment,
|
|
183
|
+
parent_span_id=self.parent_span_id,
|
|
184
|
+
spankind=spankind.upper(),
|
|
185
|
+
attributes={},
|
|
186
|
+
status=SpanStatusCode.UNSET.value,
|
|
187
|
+
start_time=datetime.now(timezone.utc),
|
|
188
|
+
)
|
|
146
189
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
190
|
+
self.active_span = span
|
|
191
|
+
self.span_dict[span.id] = span
|
|
192
|
+
self.parent_span_id = span.id
|
|
193
|
+
self.llm_logger.info(
|
|
194
|
+
f"Recorded active_span and setting parent_span_id: {span.id}"
|
|
195
|
+
)
|
|
196
|
+
return span
|
|
197
|
+
return
|
|
154
198
|
|
|
155
199
|
def update_span_status(self, span: CreateSpan, value: str):
|
|
156
|
-
|
|
157
|
-
|
|
200
|
+
if self._is_tracing_enabled:
|
|
201
|
+
updated_span = CreateSpan(**{**span.dict(), "status": value})
|
|
202
|
+
self.active_span = updated_span
|
|
203
|
+
return
|
|
158
204
|
|
|
159
205
|
def end_span(self, outputs: Dict[str, Any], span: CreateSpan, **kwargs):
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
206
|
+
if self._is_tracing_enabled:
|
|
207
|
+
updated_span = CreateSpan(
|
|
208
|
+
**span.dict(),
|
|
209
|
+
end_time=datetime.now(timezone.utc),
|
|
210
|
+
outputs=[outputs["message"]],
|
|
211
|
+
cost=outputs.get("cost", None),
|
|
212
|
+
tokens=outputs.get("usage"),
|
|
213
|
+
)
|
|
167
214
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
215
|
+
# Push span to list of recorded spans
|
|
216
|
+
self.recorded_spans.append(updated_span)
|
|
217
|
+
self.llm_logger.info(
|
|
218
|
+
f"Pushed {updated_span.spankind} span {updated_span.id} to recorded spans."
|
|
219
|
+
)
|
|
220
|
+
return
|
|
173
221
|
|
|
174
222
|
def end_recording(self, outputs: Dict[str, Any], span: CreateSpan, **kwargs):
|
|
175
223
|
self.end_span(outputs=outputs, span=span, **kwargs)
|
|
176
224
|
if self.api_key == "":
|
|
177
225
|
return
|
|
178
226
|
|
|
179
|
-
self.
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
self.
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
227
|
+
if self._is_tracing_enabled:
|
|
228
|
+
self.llm_logger.info(f"Preparing to send recorded spans for processing.")
|
|
229
|
+
self.llm_logger.info(f"Recorded spans => {len(self.recorded_spans)}")
|
|
230
|
+
self.tasks_manager.add_task(
|
|
231
|
+
self.active_trace.id,
|
|
232
|
+
"trace",
|
|
233
|
+
self.client.create_traces(
|
|
234
|
+
trace=self.recording_trace_id, spans=self.recorded_spans # type: ignore
|
|
235
|
+
),
|
|
236
|
+
self.client,
|
|
237
|
+
)
|
|
238
|
+
self.llm_logger.info(
|
|
239
|
+
f"Tracing for {span.id} recorded successfully and sent for processing."
|
|
240
|
+
)
|
|
241
|
+
self._clear_recorded_spans()
|
|
242
|
+
return
|
|
193
243
|
|
|
194
244
|
def _create_trace_id(self) -> str:
|
|
195
245
|
"""Creates a unique mongo id for the trace object.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
agenta/__init__.py,sha256=
|
|
1
|
+
agenta/__init__.py,sha256=Hffk2VlICZOmfWwUh6f6cemQxEB7aD9obCOdnXQZy1s,614
|
|
2
2
|
agenta/cli/evaluation_commands.py,sha256=fs6492tprPId9p8eGO02Xy-NCBm2RZNJLZWcUxugwd8,474
|
|
3
3
|
agenta/cli/helper.py,sha256=vRxHyeNaltzNIGrfU2vO0H28_rXDzx9QqIZ_S-W6zL4,6212
|
|
4
4
|
agenta/cli/main.py,sha256=GgYu6UsrnHbqPV7zPlO14b61IyaDiTIjGMYQS9DlqC4,9551
|
|
@@ -126,15 +126,16 @@ agenta/docker/docker-assets/entrypoint.sh,sha256=29XK8VQjQsx4hN2j-4JDy-6kQb5y4LC
|
|
|
126
126
|
agenta/docker/docker-assets/lambda_function.py,sha256=h4UZSSfqwpfsCgERv6frqwm_4JrYu9rLz3I-LxCfeEg,83
|
|
127
127
|
agenta/docker/docker-assets/main.py,sha256=7MI-21n81U7N7A0GxebNi0cmGWtJKcR2sPB6FcH2QfA,251
|
|
128
128
|
agenta/docker/docker_utils.py,sha256=5uHMCzXkCvIsDdEiwbnnn97KkzsFbBvyMwogCsv_Z5U,3509
|
|
129
|
-
agenta/sdk/__init__.py,sha256=
|
|
130
|
-
agenta/sdk/
|
|
131
|
-
agenta/sdk/agenta_init.py,sha256=wDfStpe8_3ZXRLtikarwDKI_VpA1YW4eIz_3fXq39is,9044
|
|
129
|
+
agenta/sdk/__init__.py,sha256=MNYk8dgbieeL2rTLHTX7lDgKn_CzOCqsPD5cSfTTX6Q,584
|
|
130
|
+
agenta/sdk/agenta_init.py,sha256=APcEklQrRkFw0tssX1IXf3D-aCay5k4utekYLf-wSbk,9005
|
|
132
131
|
agenta/sdk/client.py,sha256=trKyBOYFZRk0v5Eptxvh87yPf50Y9CqY6Qgv4Fy-VH4,2142
|
|
133
132
|
agenta/sdk/context.py,sha256=q-PxL05-I84puunUAs9LGsffEXcYhDxhQxjuOz2vK90,901
|
|
133
|
+
agenta/sdk/decorators/base.py,sha256=9aNdX5h8a2mFweuhdO-BQPwXGKY9ONPIdLRhSGAGMfY,217
|
|
134
|
+
agenta/sdk/decorators/llm_entrypoint.py,sha256=cmfdte6NgDR0aej07Yi9NrYAqyvQg5SyQxyKR1YrlSw,20305
|
|
135
|
+
agenta/sdk/decorators/tracing.py,sha256=7nXt_6oyB2FdUcXITFnaeMI25nn64qmisoGFqbHF6Iw,1636
|
|
134
136
|
agenta/sdk/router.py,sha256=0sbajvn5C7t18anH6yNo7-oYxldHnYfwcbmQnIXBePw,269
|
|
135
137
|
agenta/sdk/tracing/context_manager.py,sha256=HskDaiORoOhjeN375gm05wYnieQzh5UnoIsnSAHkAyc,252
|
|
136
|
-
agenta/sdk/tracing/
|
|
137
|
-
agenta/sdk/tracing/llm_tracing.py,sha256=UiotJ56EFA3VPt7LREkcK2w51D9-0T1QNvBy4zNWEdY,7348
|
|
138
|
+
agenta/sdk/tracing/llm_tracing.py,sha256=mgOGy_ux_bWxVrcnA8col31nBtPWtljfHP-QYDtZZ0I,9559
|
|
138
139
|
agenta/sdk/tracing/logger.py,sha256=4zG9c51p8xPdKA5SL8MOgBfkpCnBSuV6JfWiXO0A7oc,473
|
|
139
140
|
agenta/sdk/tracing/tasks_manager.py,sha256=XVGBEOwmHa6KcCC0PApk0_bZ0Ilk2ESuduNObB1rw2s,3792
|
|
140
141
|
agenta/sdk/types.py,sha256=Mn0yBlHh_Yr_5oQXUfsYI3V7sJAVWkJgkxEOBDOOMS0,5852
|
|
@@ -156,7 +157,7 @@ agenta/templates/simple_prompt/app.py,sha256=kODgF6lhzsaJPdgL5b21bUki6jkvqjWZzWR
|
|
|
156
157
|
agenta/templates/simple_prompt/env.example,sha256=g9AE5bYcGPpxawXMJ96gh8oenEPCHTabsiOnfQo3c5k,70
|
|
157
158
|
agenta/templates/simple_prompt/requirements.txt,sha256=ywRglRy7pPkw8bljmMEJJ4aOOQKrt9FGKULZ-DGkoBU,23
|
|
158
159
|
agenta/templates/simple_prompt/template.toml,sha256=DQBtRrF4GU8LBEXOZ-GGuINXMQDKGTEG5y37tnvIUIE,60
|
|
159
|
-
agenta-0.14.
|
|
160
|
-
agenta-0.14.
|
|
161
|
-
agenta-0.14.
|
|
162
|
-
agenta-0.14.
|
|
160
|
+
agenta-0.14.1a1.dist-info/METADATA,sha256=bSWVu4XuadyQvb99k6sw7hcMGV6hxEkaXrLXC2K7n_s,26476
|
|
161
|
+
agenta-0.14.1a1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
162
|
+
agenta-0.14.1a1.dist-info/entry_points.txt,sha256=PDiu8_8AsL7ibU9v4iNoOKR1S7F2rdxjlEprjM9QOgo,46
|
|
163
|
+
agenta-0.14.1a1.dist-info/RECORD,,
|