Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New playground #2374

Draft
wants to merge 35 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6cf94a1
feat(app): AGE-1430 create new services for old apps
mmabrouk Dec 13, 2024
99e23b3
serverless-sdk
mmabrouk Dec 13, 2024
4f145e2
added live sdk
mmabrouk Dec 13, 2024
3ae4ee5
serverless > stateless + latest stateless SDK
jp-agenta Dec 13, 2024
6656a09
added mocks for litellm
mmabrouk Dec 13, 2024
b709e01
added rest file to communicate with single service
mmabrouk Dec 13, 2024
5de1d54
Merge branch 'feat/AGE-1430-new-playground' of https://github.com/Age…
mmabrouk Dec 13, 2024
72c129e
minor fixes in stateless service SDK
jp-agenta Dec 13, 2024
fc3d238
fix default resquest state values
jp-agenta Dec 13, 2024
5102048
update stateless
mmabrouk Dec 13, 2024
cd5f108
added service tests
mmabrouk Dec 13, 2024
f100e61
removed ingestible files
mmabrouk Dec 13, 2024
3672838
clean up SDK
mmabrouk Dec 13, 2024
23728b0
formatting
mmabrouk Dec 13, 2024
3e75805
Merge pull request #2376 from Agenta-AI/refactor/clean-up-sdk
mmabrouk Dec 13, 2024
d3095d7
fix stateless AGENTA_HOST
jp-agenta Dec 13, 2024
c6d5884
Merge branch 'feat/AGE-1430-new-playground' of github.com:Agenta-AI/a…
jp-agenta Dec 13, 2024
ed40fc2
docker compoes update
mmabrouk Dec 13, 2024
3ad9536
Merge branch 'feat/AGE-1430-new-playground' of github.com:Agenta-AI/a…
jp-agenta Dec 13, 2024
182eaa0
fix config is none
jp-agenta Dec 13, 2024
4fbf947
Checkpoint 1: the configuration is under agenta-config and playground…
mmabrouk Dec 13, 2024
95374fe
fix to template
mmabrouk Dec 13, 2024
d737ef6
updated tempalets
mmabrouk Dec 13, 2024
061a037
minor test update
mmabrouk Dec 13, 2024
0a35746
fixing services rest
mmabrouk Dec 13, 2024
ce9a740
Merge branch 'feat/AGE-1430-new-playground' of https://github.com/Age…
mmabrouk Dec 13, 2024
f3e04b1
reorganize and add new services for propmt
mmabrouk Dec 14, 2024
f69b901
updates to the new prompt
mmabrouk Dec 16, 2024
d64ac80
addded prompt type
mmabrouk Dec 17, 2024
492c3ba
v1 app
mmabrouk Dec 17, 2024
f722b0f
running the app locally
mmabrouk Dec 17, 2024
d63ed11
requests
mmabrouk Dec 17, 2024
4973547
updated the the completion object
mmabrouk Dec 19, 2024
37cc27d
added basereponse type and added version
mmabrouk Dec 19, 2024
ed69cd9
added chat mode
mmabrouk Dec 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion agenta-cli/agenta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
DictInput,
MultipleChoice,
FloatParam,
InFile,
IntParam,
MultipleChoiceParam,
GroupedMultipleChoiceParam,
Expand All @@ -28,6 +27,7 @@
from .sdk.utils.costs import calculate_token_usage
from .sdk.client import Agenta
from .sdk.litellm import litellm as callbacks
from .sdk.managers.vault import VaultManager
from .sdk.managers.config import ConfigManager
from .sdk.managers.variant import VariantManager
from .sdk.managers.deployment import DeploymentManager
Expand Down
2 changes: 1 addition & 1 deletion agenta-cli/agenta/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,5 +559,5 @@ def run_evaluation(app_name: str, host: str, api_key: str = None) -> str:
raise APIRequestError(
f"Request to run evaluations failed with status code {response.status_code} and error message: {error_message}."
)
print(response.json())

return response.json()
2 changes: 1 addition & 1 deletion agenta-cli/agenta/sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
DictInput,
MultipleChoice,
FloatParam,
InFile,
IntParam,
MultipleChoiceParam,
GroupedMultipleChoiceParam,
Expand All @@ -27,6 +26,7 @@
from .decorators.routing import entrypoint, app, route
from .agenta_init import Config, AgentaSingleton, init as _init
from .utils.costs import calculate_token_usage
from .managers.vault import VaultManager
from .managers.config import ConfigManager
from .managers.variant import VariantManager
from .managers.deployment import DeploymentManager
Expand Down
165 changes: 47 additions & 118 deletions agenta-cli/agenta/sdk/agenta_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from agenta.sdk.utils.logging import log
from agenta.sdk.utils.globals import set_global
from agenta.client.backend.client import AgentaApi, AsyncAgentaApi

from agenta.sdk.tracing import Tracing
from agenta.client.exceptions import APIRequestError
from agenta.sdk.context.routing import routing_context


class AgentaSingleton:
Expand Down Expand Up @@ -59,9 +60,7 @@ def init(
ValueError: If `app_id` is not specified either as an argument, in the config file, or in the environment variables.
"""

log.info("---------------------------")
log.info("Agenta SDK - using version: %s", version("agenta"))
log.info("---------------------------")
log.info("Agenta - SDK version: %s", version("agenta"))

config = {}
if config_fname:
Expand All @@ -86,6 +85,13 @@ def init(

self.api_key = api_key or getenv("AGENTA_API_KEY") or config.get("api_key")

self.base_id = getenv("AGENTA_BASE_ID")

self.service_id = getenv("AGENTA_SERVICE_ID") or self.base_id

log.info("Agenta - Service ID: %s", self.service_id)
log.info("Agenta - Application ID: %s", self.app_id)

self.tracing = Tracing(
url=f"{self.host}/api/observability/v1/otlp/traces", # type: ignore
redact=redact,
Expand All @@ -94,6 +100,7 @@ def init(

self.tracing.configure(
api_key=self.api_key,
service_id=self.service_id,
# DEPRECATING
app_id=self.app_id,
)
Expand All @@ -108,8 +115,6 @@ def init(
api_key=self.api_key if self.api_key else "",
)

self.base_id = getenv("AGENTA_BASE_ID")

self.config = Config(
host=self.host,
base_id=self.base_id,
Expand All @@ -120,28 +125,43 @@ def init(
class Config:
def __init__(
self,
host: str,
# LEGACY
host: Optional[str] = None,
base_id: Optional[str] = None,
api_key: Optional[str] = "",
api_key: Optional[str] = None,
# LEGACY
**kwargs,
):
self.host = host
self.default_parameters = {**kwargs}

def set_default(self, **kwargs):
self.default_parameters.update(kwargs)

def get_default(self):
return self.default_parameters

def __getattr__(self, key):
context = routing_context.get()

parameters = context.parameters

if not parameters:
return None

if key in parameters:
value = parameters[key]

if isinstance(value, dict):
nested_config = Config()
nested_config.set_default(**value)

self.base_id = base_id
return nested_config

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

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 "",
)
return None

### --- LEGACY --- ###

def register_default(self, overwrite=False, **kwargs):
"""alias for default"""
Expand All @@ -153,104 +173,13 @@ def default(self, overwrite=False, **kwargs):
overwrite: Whether to overwrite the existing configuration or not
**kwargs: A dict containing the parameters
"""
self.set(
**kwargs
) # In case there is no connectivity, we still can use the default values
try:
self.push(config_name="default", overwrite=overwrite, **kwargs)
except Exception as ex:
log.warning(
"Unable to push the default configuration to the server. %s", str(ex)
)

def push(self, config_name: str, overwrite=True, **kwargs):
"""Pushes the parameters for the app variant to the server
Args:
config_name: Name of the configuration to push to
overwrite: Whether to overwrite the existing configuration or not
**kwargs: A dict containing the parameters
"""
if not self.persist:
return
try:
self.client.configs.save_config(
base_id=self.base_id,
config_name=config_name,
parameters=kwargs,
overwrite=overwrite,
)
except Exception as ex:
log.warning(
"Failed to push the configuration to the server with error: %s", ex
)

def pull(
self, config_name: str = "default", environment_name: Optional[str] = None
):
"""Pulls the parameters for the app variant from the server and sets them to the config"""
if not self.persist and (
config_name != "default" or environment_name is not None
):
raise ValueError(
"Cannot pull the configuration from the server since the app_name and base_name are not provided."
)
if self.persist:
try:
if environment_name:
config = self.client.configs.get_config(
base_id=self.base_id, environment_name=environment_name
)

else:
config = self.client.configs.get_config(
base_id=self.base_id,
config_name=config_name,
)
except Exception as ex:
log.warning(
"Failed to pull the configuration from the server with error: %s",
str(ex),
)
try:
self.set(**{"current_version": config.current_version, **config.parameters})
except Exception as ex:
log.warning("Failed to set the configuration with error: %s", str(ex))
self.set(**kwargs)

def all(self):
"""Returns all the parameters for the app variant"""
return {
k: v
for k, v in self.__dict__.items()
if k
not in [
"app_name",
"base_name",
"host",
"base_id",
"api_key",
"persist",
"client",
]
}

# function to set the parameters for the app variant
def set(self, **kwargs):
"""Sets the parameters for the app variant

Args:
**kwargs: A dict containing the parameters
"""
for key, value in kwargs.items():
setattr(self, key, value)

def dump(self):
"""Returns all the information about the current version in the configuration.
self.set_default(**kwargs)

Raises:
NotImplementedError: _description_
"""

raise NotImplementedError()
def all(self):
return self.default_parameters


def init(
Expand Down
25 changes: 25 additions & 0 deletions agenta-cli/agenta/sdk/context/exporting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import Optional

from contextlib import contextmanager
from contextvars import ContextVar

from pydantic import BaseModel


class ExportingContext(BaseModel):
credentials: Optional[str] = None


exporting_context = ContextVar("exporting_context", default=ExportingContext())


@contextmanager
def exporting_context_manager(
*,
context: Optional[ExportingContext] = None,
):
token = exporting_context.set(context)
try:
yield
finally:
exporting_context.reset(token)
24 changes: 12 additions & 12 deletions agenta-cli/agenta/sdk/context/routing.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
from typing import Any, Dict, List, Optional

from contextlib import contextmanager
from contextvars import ContextVar
from typing import Any, Dict, Optional

routing_context = ContextVar("routing_context", default={})
from pydantic import BaseModel


class RoutingContext(BaseModel):
parameters: Optional[Dict[str, Any]] = None
secrets: Optional[List[Any]] = None


routing_context = ContextVar("routing_context", default=RoutingContext())


@contextmanager
def routing_context_manager(
*,
config: Optional[Dict[str, Any]] = None,
application: Optional[Dict[str, Any]] = None,
variant: Optional[Dict[str, Any]] = None,
environment: Optional[Dict[str, Any]] = None,
context: Optional[RoutingContext] = None,
):
context = {
"config": config,
"application": application,
"variant": variant,
"environment": environment,
}
token = routing_context.set(context)
try:
yield
Expand Down
27 changes: 26 additions & 1 deletion agenta-cli/agenta/sdk/context/tracing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
from typing import Any, Dict, Optional

from contextlib import contextmanager
from contextvars import ContextVar

tracing_context = ContextVar("tracing_context", default={})
from pydantic import BaseModel


class TracingContext(BaseModel):
credentials: Optional[str] = None
parameters: Optional[Dict[str, Any]] = None
references: Optional[Dict[str, Any]] = None
link: Optional[Dict[str, Any]] = None


tracing_context = ContextVar("tracing_context", default=TracingContext())


@contextmanager
def tracing_context_manager(
*,
context: Optional[TracingContext] = None,
):
token = tracing_context.set(context)
try:
yield
finally:
tracing_context.reset(token)
Loading
Loading