Skip to content

Commit

Permalink
feat(sdk) migrate to opentelemetry (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-agenta committed Sep 3, 2024
1 parent 170b68d commit 15db2d3
Show file tree
Hide file tree
Showing 21 changed files with 985 additions and 1,276 deletions.
12 changes: 5 additions & 7 deletions agenta-cli/agenta/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from .sdk.utils.preinit import PreInitObject
from .sdk.context import get_contexts, save_context
from .sdk.types import (
Context,
DictInput,
MultipleChoice,
FloatParam,
Expand All @@ -15,14 +13,14 @@
BinaryParam,
)

from .sdk.tracing.logger import llm_logger as logging
from .sdk.tracing.llm_tracing import Tracing
from .sdk.utils.logging import log as logging
from .sdk.tracing.opentelemetry import Tracing
from .sdk.decorators.tracing import instrument
from .sdk.decorators.llm_entrypoint import entrypoint, app, route
from .sdk.decorators.routing import entrypoint, app, route
from .sdk.agenta_init import Config, AgentaSingleton, init
from .sdk.utils.helper.openai_cost import calculate_token_usage
from .sdk.utils.costs import calculate_token_usage
from .sdk.client import Agenta
from .sdk.tracing import callbacks
from .sdk.tracing import litellm as callbacks
from .sdk.config_manager import ConfigManager
from .sdk import assets as assets

Expand Down
8 changes: 3 additions & 5 deletions agenta-cli/agenta/sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from .utils.preinit import PreInitObject # always the first import!
from .context import get_contexts, save_context
from .types import (
Context,
DictInput,
MultipleChoice,
FloatParam,
Expand All @@ -15,11 +13,11 @@
BinaryParam,
)

from .tracing.llm_tracing import Tracing
from .tracing.opentelemetry import Tracing
from .decorators.tracing import instrument
from .decorators.llm_entrypoint import entrypoint, app, route
from .decorators.routing import entrypoint, app, route
from .agenta_init import Config, AgentaSingleton, init
from .utils.helper.openai_cost import calculate_token_usage
from .utils.costs import calculate_token_usage
from .config_manager import ConfigManager

config = PreInitObject("agenta.config", Config)
Expand Down
107 changes: 70 additions & 37 deletions agenta-cli/agenta/sdk/agenta_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import logging
import toml
from typing import Optional
from importlib.metadata import version

from agenta.sdk.utils.logging import log
from agenta.sdk.utils.globals import set_global
from agenta.client.backend.client import AgentaApi
from agenta.sdk.tracing.llm_tracing import Tracing
from agenta.sdk.tracing.opentelemetry import Tracing
from agenta.client.exceptions import APIRequestError


Expand All @@ -17,9 +19,8 @@ class AgentaSingleton:
"""Singleton class to save all the "global variables" for the sdk."""

_instance = None
setup = None
config = None
tracing: Optional[Tracing] = None
tracing = None

def __new__(cls):
if not cls._instance:
Expand All @@ -28,73 +29,104 @@ def __new__(cls):

def init(
self,
app_id: Optional[str] = None,
project_id: Optional[str] = None,
host: Optional[str] = None,
api_key: Optional[str] = None,
config_fname: Optional[str] = None,
) -> None:
log.info(f"\n--------------------------------")
log.info(f"Using Agenta Python SDK version: {version('agenta')}")
log.info(f"--------------------------------\n")

"""Main function to initialize the singleton.
Initializes the singleton with the given `app_id`, `host`, and `api_key`. The order of precedence for these variables is:
Initializes the singleton with the given `project_id`, `host`, and `api_key`. The order of precedence for these variables is:
1. Explicit argument provided in the function call.
2. Value from the configuration file specified by `config_fname`.
3. Environment variables.
Examples:
ag.init(app_id="xxxx", api_key="xxx")
ag.init(project_id="xxxx", api_key="xxx")
ag.init(config_fname="config.toml")
ag.init() #assuming env vars are set
Args:
app_id (Optional[str]): ID of the Agenta application. Defaults to None. If not provided, will look for "app_id" in the config file, then "AGENTA_APP_ID" in environment variables.
project_id (Optional[str]): ID of the Agenta application. Defaults to None. If not provided, will look for "project_id" in the config file, then "AGENTA_PROJECT_ID" in environment variables.
host (Optional[str]): Host name of the backend server. Defaults to None. If not provided, will look for "backend_host" in the config file, then "AGENTA_HOST" in environment variables.
api_key (Optional[str]): API Key to use with the host of the backend server. Defaults to None. If not provided, will look for "api_key" in the config file, then "AGENTA_API_KEY" in environment variables.
config_fname (Optional[str]): Path to the configuration file (relative or absolute). Defaults to None.
Raises:
ValueError: If `app_id` is not specified either as an argument, in the config file, or in the environment variables.
ValueError: If `project_id` is not specified either as an argument, in the config file, or in the environment variables.
"""

config = {}
if config_fname:
config = toml.load(config_fname)

self.app_id = app_id or config.get("app_id") or os.environ.get("AGENTA_APP_ID")
self.host = (
host
or config.get("backend_host")
or config.get("host")
or os.environ.get("AGENTA_HOST", "https://cloud.agenta.ai")
)
self.api_key = (
api_key or config.get("api_key") or os.environ.get("AGENTA_API_KEY")
)

if not self.app_id:
self.project_id = (
project_id
or config.get("project_id")
or os.environ.get("AGENTA_PROJECT_ID")
)
if not self.project_id:
raise ValueError(
"App ID must be specified. You can provide it in one of the following ways:\n"
"1. As an argument when calling ag.init(app_id='your_app_id').\n"
"Project ID must be specified. You can provide it in one of the following ways:\n"
"1. As an argument when calling ag.init(project_id='your_project_id').\n"
"2. In the configuration file specified by config_fname.\n"
"3. As an environment variable 'AGENTA_APP_ID'."
"3. As an environment variable 'AGENTA_PROJECT_ID'."
)

self.api_key = (
api_key or config.get("api_key") or os.environ.get("AGENTA_API_KEY")
)

self.tracing = Tracing(
url=f"{self.host}/api/observability/v2/traces", # type: ignore
project_id=self.project_id,
api_key=self.api_key,
)

self.base_id = os.environ.get("AGENTA_BASE_ID")
if self.base_id is None:
print(
"Warning: Your configuration will not be saved permanently since base_id is not provided."
)

self.config = Config(base_id=self.base_id, host=self.host, api_key=self.api_key) # type: ignore
self.config = Config(
host=self.host,
base_id=self.base_id,
api_key=self.api_key,
)


class Config:
def __init__(self, base_id: str, host: str, api_key: Optional[str] = ""):
self.base_id = base_id

def __init__(
self,
host: str,
base_id: Optional[str] = None,
api_key: Optional[str] = "",
):
self.host = host

self.base_id = base_id

if self.base_id is None:
print(
"Warning: Your configuration will not be saved permanently since base_id is not provided.\n"
)

if base_id is None or host is None:
self.persist = False
else:
self.persist = True
self.client = AgentaApi(
base_url=self.host + "/api", api_key=api_key if api_key else ""
base_url=self.host + "/api",
api_key=api_key if api_key else "",
)

def register_default(self, overwrite=False, **kwargs):
Expand Down Expand Up @@ -208,42 +240,43 @@ def dump(self):


def init(
app_id: Optional[str] = None,
host: Optional[str] = None,
project_id: Optional[str] = None,
api_key: Optional[str] = None,
config_fname: Optional[str] = None,
max_workers: Optional[int] = None,
):
"""Main function to initialize the agenta sdk.
Initializes agenta with the given `app_id`, `host`, and `api_key`. The order of precedence for these variables is:
Initializes agenta with the given `project_id`, `host`, and `api_key`. The order of precedence for these variables is:
1. Explicit argument provided in the function call.
2. Value from the configuration file specified by `config_fname`.
3. Environment variables.
- `app_id` is a required parameter (to be specified in one of the above ways)
- `project_id` is a required parameter (to be specified in one of the above ways)
- `host` is optional and defaults to "https://cloud.agenta.ai"
- `api_key` is optional and defaults to "". It is required only when using cloud or enterprise version of agenta.
Args:
app_id (Optional[str]): ID of the Agenta application. Defaults to None. If not provided, will look for "app_id" in the config file, then "AGENTA_APP_ID" in environment variables.
project_id (Optional[str]): ID of the Agenta application. Defaults to None. If not provided, will look for "project_id" in the config file, then "AGENTA_PROJECT_ID" in environment variables.
host (Optional[str]): Host name of the backend server. Defaults to None. If not provided, will look for "backend_host" in the config file, then "AGENTA_HOST" in environment variables.
api_key (Optional[str]): API Key to use with the host of the backend server. Defaults to None. If not provided, will look for "api_key" in the config file, then "AGENTA_API_KEY" in environment variables.
config_fname (Optional[str]): Path to the configuration file. Defaults to None.
Raises:
ValueError: If `app_id` is not specified either as an argument, in the config file, or in the environment variables.
ValueError: If `project_id` is not specified either as an argument, in the config file, or in the environment variables.
"""

singleton = AgentaSingleton()

singleton.init(app_id=app_id, host=host, api_key=api_key, config_fname=config_fname)
singleton.init(
host=host,
project_id=project_id,
api_key=api_key,
config_fname=config_fname,
)

tracing = Tracing(
host=singleton.host, # type: ignore
app_id=singleton.app_id, # type: ignore
api_key=singleton.api_key,
max_workers=max_workers,
set_global(
config=singleton.config,
tracing=singleton.tracing,
)
set_global(setup=singleton.setup, config=singleton.config, tracing=tracing)
2 changes: 1 addition & 1 deletion agenta-cli/agenta/sdk/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pydantic import BaseModel, ValidationError

from agenta.client.backend.client import AgentaApi
from agenta.sdk.decorators.llm_entrypoint import route_context
from agenta.sdk.decorators.routing import route_context

from . import AgentaSingleton

Expand Down
41 changes: 0 additions & 41 deletions agenta-cli/agenta/sdk/context.py

This file was deleted.

Empty file.
10 changes: 0 additions & 10 deletions agenta-cli/agenta/sdk/decorators/base.py

This file was deleted.

Loading

0 comments on commit 15db2d3

Please sign in to comment.