agenta 0.14.1a0__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 -3
- agenta/sdk/__init__.py +2 -4
- 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 +104 -81
- {agenta-0.14.1a0.dist-info → agenta-0.14.1a1.dist-info}/METADATA +1 -2
- {agenta-0.14.1a0.dist-info → agenta-0.14.1a1.dist-info}/RECORD +11 -11
- agenta/sdk/agenta_decorator.py +0 -515
- agenta/sdk/tracing/callbacks.py +0 -125
- agenta/sdk/tracing/decorators.py +0 -41
- {agenta-0.14.1a0.dist-info → agenta-0.14.1a1.dist-info}/WHEEL +0 -0
- {agenta-0.14.1a0.dist-info → agenta-0.14.1a1.dist-info}/entry_points.txt +0 -0
agenta/__init__.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
from .sdk.utils.preinit import PreInitObject
|
|
2
|
-
from .sdk.agenta_decorator import app, entrypoint
|
|
3
2
|
from .sdk.context import get_contexts, save_context
|
|
4
3
|
from .sdk.types import (
|
|
5
4
|
Context,
|
|
@@ -14,9 +13,9 @@ from .sdk.types import (
|
|
|
14
13
|
FileInputURL,
|
|
15
14
|
BinaryParam,
|
|
16
15
|
)
|
|
17
|
-
from .sdk.tracing
|
|
16
|
+
from .sdk.decorators.tracing import span
|
|
17
|
+
from .sdk.decorators.llm_entrypoint import entrypoint
|
|
18
18
|
from .sdk.agenta_init import Config, init, llm_tracing
|
|
19
|
-
from .sdk.tracing.callbacks import agenta_litellm_handler
|
|
20
19
|
from .sdk.utils.helper.openai_cost import calculate_token_usage
|
|
21
20
|
from .sdk.client import Agenta
|
|
22
21
|
|
agenta/sdk/__init__.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
from .utils.preinit import PreInitObject # always the first import!
|
|
2
|
-
from . import agenta_decorator, context, types, utils # noqa: F401
|
|
3
|
-
from .agenta_decorator import app, entrypoint
|
|
4
2
|
from .context import get_contexts, save_context
|
|
5
3
|
from .types import (
|
|
6
4
|
Context,
|
|
@@ -15,9 +13,9 @@ from .types import (
|
|
|
15
13
|
FileInputURL,
|
|
16
14
|
BinaryParam,
|
|
17
15
|
)
|
|
18
|
-
from .tracing
|
|
16
|
+
from .decorators.tracing import span
|
|
17
|
+
from .decorators.llm_entrypoint import entrypoint
|
|
19
18
|
from .agenta_init import Config, init, llm_tracing
|
|
20
|
-
from .tracing.callbacks import agenta_litellm_handler
|
|
21
19
|
from .utils.helper.openai_cost import calculate_token_usage
|
|
22
20
|
|
|
23
21
|
|
agenta/sdk/agenta_init.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import os
|
|
2
2
|
import logging
|
|
3
|
-
from typing import
|
|
4
|
-
|
|
5
|
-
from .utils.globals import set_global
|
|
3
|
+
from typing import Optional
|
|
6
4
|
|
|
5
|
+
from agenta.sdk.utils.globals import set_global
|
|
7
6
|
from agenta.client.backend.client import AgentaApi
|
|
8
7
|
from agenta.sdk.tracing.llm_tracing import Tracing
|
|
9
8
|
from agenta.client.exceptions import APIRequestError
|
|
@@ -13,16 +12,17 @@ logger = logging.getLogger(__name__)
|
|
|
13
12
|
logger.setLevel(logging.DEBUG)
|
|
14
13
|
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
class SDKClient(object):
|
|
16
|
+
"""
|
|
17
|
+
Class to build SDK client instance.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self, api_key: str, host: str):
|
|
21
|
+
self.api_key = api_key
|
|
22
|
+
self.host = host + "/api"
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
client = AgentaApi(
|
|
23
|
-
base_url=backend_url,
|
|
24
|
-
api_key=CLIENT_API_KEY if CLIENT_API_KEY else "",
|
|
25
|
-
)
|
|
24
|
+
def _build_sdk_client(self) -> AgentaApi:
|
|
25
|
+
return AgentaApi(base_url=self.host, api_key=self.api_key)
|
|
26
26
|
|
|
27
27
|
|
|
28
28
|
class AgentaSingleton:
|
|
@@ -37,15 +37,22 @@ class AgentaSingleton:
|
|
|
37
37
|
cls._instance = super(AgentaSingleton, cls).__new__(cls)
|
|
38
38
|
return cls._instance
|
|
39
39
|
|
|
40
|
+
@property
|
|
41
|
+
def client(self):
|
|
42
|
+
"""Builds sdk client instance.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
AgentaAPI: instance of agenta api backend
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
sdk_client = SDKClient(api_key=self.api_key, host=self.host) # type: ignore
|
|
49
|
+
return sdk_client._build_sdk_client()
|
|
50
|
+
|
|
40
51
|
def init(
|
|
41
52
|
self,
|
|
42
|
-
app_name: Optional[str] = None,
|
|
43
|
-
base_name: Optional[str] = None,
|
|
44
|
-
api_key: Optional[str] = None,
|
|
45
|
-
base_id: Optional[str] = None,
|
|
46
53
|
app_id: Optional[str] = None,
|
|
47
54
|
host: Optional[str] = None,
|
|
48
|
-
|
|
55
|
+
api_key: Optional[str] = None,
|
|
49
56
|
) -> None:
|
|
50
57
|
"""Main function to initialize the singleton.
|
|
51
58
|
|
|
@@ -53,57 +60,44 @@ class AgentaSingleton:
|
|
|
53
60
|
the function will look for them in environment variables.
|
|
54
61
|
|
|
55
62
|
Args:
|
|
56
|
-
|
|
57
|
-
base_name (Optional[str]): Base name for the Agenta setup. Defaults to None. If not provided, will look for "AGENTA_BASE_NAME" in environment variables.
|
|
63
|
+
app_id (Optional[str]): ID of the Agenta application. Defaults to None. If not provided, will look for "AGENTA_APP_NAME" in environment variables.
|
|
58
64
|
host (Optional[str]): Host name of the backend server. Defaults to None. If not provided, will look for "AGENTA_HOST" in environment variables.
|
|
65
|
+
api_key (Optional[str]): API Key to use with the host of the backend server.
|
|
59
66
|
kwargs (Any): Additional keyword arguments.
|
|
60
67
|
|
|
61
68
|
Raises:
|
|
62
69
|
ValueError: If `app_name`, `base_name`, or `host` are not specified either as arguments or in the environment variables.
|
|
63
70
|
"""
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
|
|
72
|
+
self.api_key = api_key or os.environ.get("AGENTA_API_KEY")
|
|
73
|
+
self.host = host or os.environ.get("AGENTA_HOST", "http://localhost")
|
|
74
|
+
|
|
75
|
+
app_id = app_id or os.environ.get("AGENTA_APP_ID")
|
|
76
|
+
if not app_id:
|
|
77
|
+
raise ValueError("App ID must be specified.")
|
|
78
|
+
|
|
79
|
+
base_id = os.environ.get("AGENTA_BASE_ID")
|
|
80
|
+
base_name = os.environ.get("AGENTA_BASE_NAME")
|
|
81
|
+
if base_id is None and (app_id is None or base_name is None):
|
|
82
|
+
print(
|
|
83
|
+
f"Warning: Your configuration will not be saved permanently since app_name and base_name are not provided."
|
|
84
|
+
)
|
|
85
|
+
else:
|
|
86
|
+
try:
|
|
87
|
+
base_id = self.get_app_base(app_id, base_name) # type: ignore
|
|
88
|
+
except Exception as ex:
|
|
89
|
+
raise APIRequestError(
|
|
90
|
+
f"Failed to get base id and/or app_id from the server with error: {ex}"
|
|
79
91
|
)
|
|
80
|
-
else:
|
|
81
|
-
try:
|
|
82
|
-
app_id = self.get_app(app_name)
|
|
83
|
-
base_id = self.get_app_base(app_id, base_name)
|
|
84
|
-
except Exception as ex:
|
|
85
|
-
raise APIRequestError(
|
|
86
|
-
f"Failed to get base id and/or app_id from the server with error: {ex}"
|
|
87
|
-
)
|
|
88
92
|
|
|
93
|
+
self.app_id = app_id
|
|
89
94
|
self.base_id = base_id
|
|
90
|
-
self.host = host
|
|
91
|
-
self.app_id = os.environ.get("AGENTA_APP_ID") if app_id is None else app_id
|
|
92
95
|
self.variant_id = os.environ.get("AGENTA_VARIANT_ID")
|
|
93
96
|
self.variant_name = os.environ.get("AGENTA_VARIANT_NAME")
|
|
94
|
-
self.api_key = api_key
|
|
95
97
|
self.config = Config(base_id=base_id, host=host)
|
|
96
98
|
|
|
97
|
-
def get_app(self, app_name: str) -> str:
|
|
98
|
-
apps = client.apps.list_apps(app_name=app_name)
|
|
99
|
-
if len(apps) == 0:
|
|
100
|
-
raise APIRequestError(f"App with name {app_name} not found")
|
|
101
|
-
|
|
102
|
-
app_id = apps[0].app_id
|
|
103
|
-
return app_id
|
|
104
|
-
|
|
105
99
|
def get_app_base(self, app_id: str, base_name: str) -> str:
|
|
106
|
-
bases = client.bases.list_bases(app_id=app_id, base_name=base_name)
|
|
100
|
+
bases = self.client.bases.list_bases(app_id=app_id, base_name=base_name)
|
|
107
101
|
if len(bases) == 0:
|
|
108
102
|
raise APIRequestError(f"No base was found for the app {app_id}")
|
|
109
103
|
return bases[0].base_id
|
|
@@ -122,11 +116,23 @@ class Config:
|
|
|
122
116
|
def __init__(self, base_id, host):
|
|
123
117
|
self.base_id = base_id
|
|
124
118
|
self.host = host
|
|
119
|
+
|
|
125
120
|
if base_id is None or host is None:
|
|
126
121
|
self.persist = False
|
|
127
122
|
else:
|
|
128
123
|
self.persist = True
|
|
129
124
|
|
|
125
|
+
@property
|
|
126
|
+
def client(self):
|
|
127
|
+
"""Builds sdk client instance.
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
AgentaAPI: instance of agenta api backend
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
sdk_client = SDKClient(api_key=self.api_key, host=self.host) # type: ignore
|
|
134
|
+
return sdk_client._build_sdk_client()
|
|
135
|
+
|
|
130
136
|
def register_default(self, overwrite=False, **kwargs):
|
|
131
137
|
"""alias for default"""
|
|
132
138
|
return self.default(overwrite=overwrite, **kwargs)
|
|
@@ -157,7 +163,7 @@ class Config:
|
|
|
157
163
|
if not self.persist:
|
|
158
164
|
return
|
|
159
165
|
try:
|
|
160
|
-
client.configs.save_config(
|
|
166
|
+
self.client.configs.save_config(
|
|
161
167
|
base_id=self.base_id,
|
|
162
168
|
config_name=config_name,
|
|
163
169
|
parameters=kwargs,
|
|
@@ -168,7 +174,9 @@ class Config:
|
|
|
168
174
|
"Failed to push the configuration to the server with error: " + str(ex)
|
|
169
175
|
)
|
|
170
176
|
|
|
171
|
-
def pull(
|
|
177
|
+
def pull(
|
|
178
|
+
self, config_name: str = "default", environment_name: Optional[str] = None
|
|
179
|
+
):
|
|
172
180
|
"""Pulls the parameters for the app variant from the server and sets them to the config"""
|
|
173
181
|
if not self.persist and (
|
|
174
182
|
config_name != "default" or environment_name is not None
|
|
@@ -179,12 +187,12 @@ class Config:
|
|
|
179
187
|
if self.persist:
|
|
180
188
|
try:
|
|
181
189
|
if environment_name:
|
|
182
|
-
config = client.configs.get_config(
|
|
190
|
+
config = self.client.configs.get_config(
|
|
183
191
|
base_id=self.base_id, environment_name=environment_name
|
|
184
192
|
)
|
|
185
193
|
|
|
186
194
|
else:
|
|
187
|
-
config = client.configs.get_config(
|
|
195
|
+
config = self.client.configs.get_config(
|
|
188
196
|
base_id=self.base_id,
|
|
189
197
|
config_name=config_name,
|
|
190
198
|
)
|
|
@@ -218,15 +226,22 @@ class Config:
|
|
|
218
226
|
setattr(self, key, value)
|
|
219
227
|
|
|
220
228
|
|
|
221
|
-
def init(
|
|
229
|
+
def init(
|
|
230
|
+
app_id: Optional[str] = None,
|
|
231
|
+
host: Optional[str] = None,
|
|
232
|
+
api_key: Optional[str] = None,
|
|
233
|
+
):
|
|
222
234
|
"""Main function to be called by the user to initialize the sdk.
|
|
223
235
|
|
|
224
236
|
Args:
|
|
225
|
-
|
|
226
|
-
|
|
237
|
+
app_id (str): The Id of the app.
|
|
238
|
+
host (str): The host of the backend server.
|
|
239
|
+
api_key (str): The API key to use for the backend server.
|
|
227
240
|
"""
|
|
241
|
+
|
|
228
242
|
singleton = AgentaSingleton()
|
|
229
|
-
|
|
243
|
+
|
|
244
|
+
singleton.init(app_id=app_id, host=host, api_key=api_key)
|
|
230
245
|
set_global(setup=singleton.setup, config=singleton.config)
|
|
231
246
|
|
|
232
247
|
|
|
@@ -235,7 +250,7 @@ def llm_tracing(max_workers: Optional[int] = None) -> Tracing:
|
|
|
235
250
|
|
|
236
251
|
singleton = AgentaSingleton()
|
|
237
252
|
return Tracing(
|
|
238
|
-
base_url=singleton.host,
|
|
253
|
+
base_url=singleton.host, # type: ignore
|
|
239
254
|
app_id=singleton.app_id, # type: ignore
|
|
240
255
|
variant_id=singleton.variant_id, # type: ignore
|
|
241
256
|
variant_name=singleton.variant_name,
|