agenta 0.15.0a0__py3-none-any.whl → 0.15.0a1__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 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,13 @@ from .sdk.types import (
14
13
  FileInputURL,
15
14
  BinaryParam,
16
15
  )
17
- from .sdk.tracing.decorators import span
18
- from .sdk.agenta_init import Config, init, llm_tracing
16
+ from .sdk.tracing.llm_tracing import Tracing
17
+ from .sdk.decorators.tracing import instrument
18
+ from .sdk.decorators.llm_entrypoint import entrypoint, app
19
+ from .sdk.agenta_init import Config, AgentaSingleton, init
19
20
  from .sdk.utils.helper.openai_cost import calculate_token_usage
20
21
  from .sdk.client import Agenta
21
22
 
22
23
  config = PreInitObject("agenta.config", Config)
24
+ DEFAULT_AGENTA_SINGLETON_INSTANCE = AgentaSingleton()
25
+ tracing = DEFAULT_AGENTA_SINGLETON_INSTANCE.tracing # type: ignore
@@ -56,9 +56,9 @@ class AgentaApi:
56
56
  self._client_wrapper = SyncClientWrapper(
57
57
  base_url=base_url,
58
58
  api_key=api_key,
59
- httpx_client=httpx.Client(timeout=timeout)
60
- if httpx_client is None
61
- else httpx_client,
59
+ httpx_client=(
60
+ httpx.Client(timeout=timeout) if httpx_client is None else httpx_client
61
+ ),
62
62
  )
63
63
  self.observability = ObservabilityClient(client_wrapper=self._client_wrapper)
64
64
  self.apps = AppsClient(client_wrapper=self._client_wrapper)
@@ -1037,9 +1037,11 @@ class AsyncAgentaApi:
1037
1037
  self._client_wrapper = AsyncClientWrapper(
1038
1038
  base_url=base_url,
1039
1039
  api_key=api_key,
1040
- httpx_client=httpx.AsyncClient(timeout=timeout)
1041
- if httpx_client is None
1042
- else httpx_client,
1040
+ httpx_client=(
1041
+ httpx.AsyncClient(timeout=timeout)
1042
+ if httpx_client is None
1043
+ else httpx_client
1044
+ ),
1043
1045
  )
1044
1046
  self.observability = AsyncObservabilityClient(
1045
1047
  client_wrapper=self._client_wrapper
@@ -53,6 +53,6 @@ class CreateSpan(pydantic.BaseModel):
53
53
  return super().dict(**kwargs_with_defaults)
54
54
 
55
55
  class Config:
56
- frozen = True
56
+ frozen = False
57
57
  smart_union = True
58
58
  json_encoders = {dt.datetime: serialize_datetime}
@@ -8,7 +8,7 @@ from pathlib import Path
8
8
  logger = logging.getLogger(__name__)
9
9
  logger.setLevel(logging.DEBUG)
10
10
 
11
- DEBUG = False
11
+ DEBUG = True
12
12
 
13
13
 
14
14
  def create_dockerfile(out_folder: Path) -> Path:
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,13 @@ from .types import (
15
13
  FileInputURL,
16
14
  BinaryParam,
17
15
  )
18
- from .tracing.decorators import span
19
- from .agenta_init import Config, init, llm_tracing
16
+ from .tracing.llm_tracing import Tracing
17
+ from .decorators.tracing import instrument
18
+ from .decorators.llm_entrypoint import entrypoint, app
19
+ from .agenta_init import Config, AgentaSingleton, init
20
20
  from .utils.helper.openai_cost import calculate_token_usage
21
21
 
22
22
 
23
23
  config = PreInitObject("agenta.config", Config)
24
+ DEFAULT_AGENTA_SINGLETON_INSTANCE = AgentaSingleton()
25
+ tracing = DEFAULT_AGENTA_SINGLETON_INSTANCE.tracing # type: ignore
agenta/sdk/agenta_init.py CHANGED
@@ -1,9 +1,8 @@
1
1
  import os
2
2
  import logging
3
- from typing import Any, Optional
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,39 +12,34 @@ logger = logging.getLogger(__name__)
13
12
  logger.setLevel(logging.DEBUG)
14
13
 
15
14
 
16
- BACKEND_URL_SUFFIX = os.environ.get("BACKEND_URL_SUFFIX", "api")
17
- CLIENT_API_KEY = os.environ.get("AGENTA_API_KEY")
18
- CLIENT_HOST = os.environ.get("AGENTA_HOST", "http://localhost")
19
-
20
- # initialize the client with the backend url and api key
21
- backend_url = f"{CLIENT_HOST}/{BACKEND_URL_SUFFIX}"
22
- client = AgentaApi(
23
- base_url=backend_url,
24
- api_key=CLIENT_API_KEY if CLIENT_API_KEY else "",
25
- )
26
-
27
-
28
15
  class AgentaSingleton:
29
16
  """Singleton class to save all the "global variables" for the sdk."""
30
17
 
31
18
  _instance = None
32
19
  setup = None
33
20
  config = None
21
+ tracing: Optional[Tracing] = None
34
22
 
35
23
  def __new__(cls):
36
24
  if not cls._instance:
37
25
  cls._instance = super(AgentaSingleton, cls).__new__(cls)
38
26
  return cls._instance
39
27
 
28
+ @property
29
+ def client(self):
30
+ """API Backend client.
31
+
32
+ Returns:
33
+ AgentaAPI: instance of agenta api backend
34
+ """
35
+
36
+ return AgentaApi(base_url=self.host + "/api", api_key=self.api_key)
37
+
40
38
  def init(
41
39
  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
40
  app_id: Optional[str] = None,
47
41
  host: Optional[str] = None,
48
- **kwargs: Any,
42
+ api_key: Optional[str] = None,
49
43
  ) -> None:
50
44
  """Main function to initialize the singleton.
51
45
 
@@ -53,57 +47,44 @@ class AgentaSingleton:
53
47
  the function will look for them in environment variables.
54
48
 
55
49
  Args:
56
- app_name (Optional[str]): Name of the Agenta application. Defaults to None. If not provided, will look for "AGENTA_APP_NAME" in environment variables.
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.
50
+ 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
51
  host (Optional[str]): Host name of the backend server. Defaults to None. If not provided, will look for "AGENTA_HOST" in environment variables.
52
+ api_key (Optional[str]): API Key to use with the host of the backend server.
59
53
  kwargs (Any): Additional keyword arguments.
60
54
 
61
55
  Raises:
62
56
  ValueError: If `app_name`, `base_name`, or `host` are not specified either as arguments or in the environment variables.
63
57
  """
64
- if app_name is None:
65
- app_name = os.environ.get("AGENTA_APP_NAME")
66
- if base_name is None:
67
- base_name = os.environ.get("AGENTA_BASE_NAME")
68
- if api_key is None:
69
- api_key = os.environ.get("AGENTA_API_KEY")
70
- if base_id is None:
71
- base_id = os.environ.get("AGENTA_BASE_ID")
72
- if host is None:
73
- host = os.environ.get("AGENTA_HOST", "http://localhost")
74
-
75
- if base_id is None:
76
- if app_name is None or base_name is None:
77
- print(
78
- f"Warning: Your configuration will not be saved permanently since app_name and base_name are not provided."
58
+
59
+ self.api_key = api_key or os.environ.get("AGENTA_API_KEY")
60
+ self.host = host or os.environ.get("AGENTA_HOST", "http://localhost")
61
+
62
+ app_id = app_id or os.environ.get("AGENTA_APP_ID")
63
+ if not app_id:
64
+ raise ValueError("App ID must be specified.")
65
+
66
+ base_id = os.environ.get("AGENTA_BASE_ID")
67
+ base_name = os.environ.get("AGENTA_BASE_NAME")
68
+ if base_id is None and (app_id is None or base_name is None):
69
+ print(
70
+ f"Warning: Your configuration will not be saved permanently since app_name and base_name are not provided."
71
+ )
72
+ else:
73
+ try:
74
+ base_id = self.get_app_base(app_id, base_name) # type: ignore
75
+ except Exception as ex:
76
+ raise APIRequestError(
77
+ f"Failed to get base id and/or app_id from the server with error: {ex}"
79
78
  )
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
79
 
80
+ self.app_id = app_id
89
81
  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
82
  self.variant_id = os.environ.get("AGENTA_VARIANT_ID")
93
83
  self.variant_name = os.environ.get("AGENTA_VARIANT_NAME")
94
- self.api_key = api_key
95
- self.config = Config(base_id=base_id, host=host)
96
-
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
84
+ self.config = Config(base_id=self.base_id, host=self.host, api_key=self.api_key) # type: ignore
104
85
 
105
86
  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)
87
+ bases = self.client.bases.list_bases(app_id=app_id, base_name=base_name)
107
88
  if len(bases) == 0:
108
89
  raise APIRequestError(f"No base was found for the app {app_id}")
109
90
  return bases[0].base_id
@@ -119,14 +100,26 @@ class AgentaSingleton:
119
100
 
120
101
 
121
102
  class Config:
122
- def __init__(self, base_id, host):
103
+ def __init__(self, base_id: str, host: str, api_key: str):
123
104
  self.base_id = base_id
124
105
  self.host = host
106
+ self.api_key = api_key
107
+
125
108
  if base_id is None or host is None:
126
109
  self.persist = False
127
110
  else:
128
111
  self.persist = True
129
112
 
113
+ @property
114
+ def client(self):
115
+ """API Backend client.
116
+
117
+ Returns:
118
+ AgentaAPI: instance of agenta api backend
119
+ """
120
+
121
+ return AgentaApi(base_url=self.host + "/api", api_key=self.api_key)
122
+
130
123
  def register_default(self, overwrite=False, **kwargs):
131
124
  """alias for default"""
132
125
  return self.default(overwrite=overwrite, **kwargs)
@@ -157,7 +150,7 @@ class Config:
157
150
  if not self.persist:
158
151
  return
159
152
  try:
160
- client.configs.save_config(
153
+ self.client.configs.save_config(
161
154
  base_id=self.base_id,
162
155
  config_name=config_name,
163
156
  parameters=kwargs,
@@ -168,7 +161,9 @@ class Config:
168
161
  "Failed to push the configuration to the server with error: " + str(ex)
169
162
  )
170
163
 
171
- def pull(self, config_name: str = "default", environment_name: str = None):
164
+ def pull(
165
+ self, config_name: str = "default", environment_name: Optional[str] = None
166
+ ):
172
167
  """Pulls the parameters for the app variant from the server and sets them to the config"""
173
168
  if not self.persist and (
174
169
  config_name != "default" or environment_name is not None
@@ -179,12 +174,12 @@ class Config:
179
174
  if self.persist:
180
175
  try:
181
176
  if environment_name:
182
- config = client.configs.get_config(
177
+ config = self.client.configs.get_config(
183
178
  base_id=self.base_id, environment_name=environment_name
184
179
  )
185
180
 
186
181
  else:
187
- config = client.configs.get_config(
182
+ config = self.client.configs.get_config(
188
183
  base_id=self.base_id,
189
184
  config_name=config_name,
190
185
  )
@@ -217,28 +212,39 @@ class Config:
217
212
  for key, value in kwargs.items():
218
213
  setattr(self, key, value)
219
214
 
215
+ def dump(self):
216
+ """Returns all the information about the current version in the configuration.
220
217
 
221
- def init(app_name=None, base_name=None, **kwargs):
218
+ Raises:
219
+ NotImplementedError: _description_
220
+ """
221
+
222
+ raise NotImplementedError()
223
+
224
+
225
+ def init(
226
+ app_id: Optional[str] = None,
227
+ host: Optional[str] = None,
228
+ api_key: Optional[str] = None,
229
+ max_workers: Optional[int] = None,
230
+ ):
222
231
  """Main function to be called by the user to initialize the sdk.
223
232
 
224
233
  Args:
225
- app_name: _description_. Defaults to None.
226
- base_name: _description_. Defaults to None.
234
+ app_id (str): The Id of the app.
235
+ host (str): The host of the backend server.
236
+ api_key (str): The API key to use for the backend server.
227
237
  """
228
- singleton = AgentaSingleton()
229
- singleton.init(app_name=app_name, base_name=base_name, **kwargs)
230
- set_global(setup=singleton.setup, config=singleton.config)
231
-
232
-
233
- def llm_tracing(max_workers: Optional[int] = None) -> Tracing:
234
- """Function to start llm tracing."""
235
238
 
236
239
  singleton = AgentaSingleton()
237
- return Tracing(
238
- base_url=singleton.host,
240
+
241
+ singleton.init(app_id=app_id, host=host, api_key=api_key)
242
+ tracing = Tracing(
243
+ host=singleton.host, # type: ignore
239
244
  app_id=singleton.app_id, # type: ignore
240
245
  variant_id=singleton.variant_id, # type: ignore
241
246
  variant_name=singleton.variant_name,
242
247
  api_key=singleton.api_key,
243
248
  max_workers=max_workers,
244
249
  )
250
+ set_global(setup=singleton.setup, config=singleton.config, tracing=tracing)
@@ -0,0 +1,10 @@
1
+ # Stdlib Imports
2
+ from typing import Any, Callable
3
+
4
+
5
+ class BaseDecorator:
6
+ def __init__(self):
7
+ pass
8
+
9
+ def __call__(self, func: Callable[..., Any]) -> Callable[..., Any]:
10
+ raise NotImplementedError