From 503d611d51b8c64d29c4e7584dd0d1890be2ebb1 Mon Sep 17 00:00:00 2001 From: aakrem Date: Wed, 16 Oct 2024 20:59:55 +0200 Subject: [PATCH 1/7] move templates to backend --- .../resources/templates/templates.py | 20 +++++++++++++++++++ .../services/templates_manager.py | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 agenta-backend/agenta_backend/resources/templates/templates.py diff --git a/agenta-backend/agenta_backend/resources/templates/templates.py b/agenta-backend/agenta_backend/resources/templates/templates.py new file mode 100644 index 0000000000..28b085a803 --- /dev/null +++ b/agenta-backend/agenta_backend/resources/templates/templates.py @@ -0,0 +1,20 @@ +templates_info = { + "chat_openai": { + "name": "Single Prompt OpenAI", + "description": "Sample single prompt application using different OpenAI models", + }, + "technical_startup_ideas": { + "name": "Chat Application OpenAI", + "description": "Sample Chat application using different OpenAI models", + }, +} + + +def get_oss_templates(): + """ + Returns a list of templates. + + Returns: + List[dict]: A list of template dictionaries. + """ + return templates_info diff --git a/agenta-backend/agenta_backend/services/templates_manager.py b/agenta-backend/agenta_backend/services/templates_manager.py index 3c9f074fd5..52604c1c01 100644 --- a/agenta-backend/agenta_backend/services/templates_manager.py +++ b/agenta-backend/agenta_backend/services/templates_manager.py @@ -9,6 +9,7 @@ from agenta_backend.utils import redis_utils from agenta_backend.services import db_manager from agenta_backend.utils.common import isCloud, isOss +from agenta_backend.resources.templates.templates import get_oss_templates from datetime import datetime, timezone @@ -36,7 +37,7 @@ async def update_and_sync_templates(cache: bool = True) -> None: templates = await retrieve_templates_from_dockerhub_cached(cache) templates_ids_not_to_remove = [] - templates_info = await retrieve_templates_info_from_s3(cache) + templates_info = get_oss_templates() for temp in templates: if temp["name"] in list(templates_info.keys()): templates_ids_not_to_remove.append(int(temp["id"])) From 3656c05d89da38566c66caf897ba911f85c738e4 Mon Sep 17 00:00:00 2001 From: aakrem Date: Wed, 16 Oct 2024 21:51:11 +0200 Subject: [PATCH 2/7] get templates based on environment --- .../agenta_backend/services/templates_manager.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/agenta-backend/agenta_backend/services/templates_manager.py b/agenta-backend/agenta_backend/services/templates_manager.py index 52604c1c01..0c55e386a4 100644 --- a/agenta-backend/agenta_backend/services/templates_manager.py +++ b/agenta-backend/agenta_backend/services/templates_manager.py @@ -18,6 +18,13 @@ if isCloud() or isOss(): from agenta_backend.services import container_manager +if isCloud(): + from agenta_backend.commons.resources.templates.templates import get_templates +elif isOss(): + from agenta_backend.resources.templates.templates import ( + get_oss_templates as get_templates, + ) + templates_base_url = os.getenv("TEMPLATES_BASE_URL") agenta_template_repo = os.getenv("AGENTA_TEMPLATE_REPO") docker_hub_url = os.getenv("DOCKER_HUB_URL") @@ -37,7 +44,7 @@ async def update_and_sync_templates(cache: bool = True) -> None: templates = await retrieve_templates_from_dockerhub_cached(cache) templates_ids_not_to_remove = [] - templates_info = get_oss_templates() + templates_info = get_templates() for temp in templates: if temp["name"] in list(templates_info.keys()): templates_ids_not_to_remove.append(int(temp["id"])) From 598d65b388423fd3c8eb1a660f4d5a2ea51a8d34 Mon Sep 17 00:00:00 2001 From: aakrem Date: Thu, 17 Oct 2024 09:11:46 +0200 Subject: [PATCH 3/7] remove retrieve_templates_info_from_s3 --- .../services/templates_manager.py | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/agenta-backend/agenta_backend/services/templates_manager.py b/agenta-backend/agenta_backend/services/templates_manager.py index 0c55e386a4..d90c7c47d5 100644 --- a/agenta-backend/agenta_backend/services/templates_manager.py +++ b/agenta-backend/agenta_backend/services/templates_manager.py @@ -104,34 +104,6 @@ async def retrieve_templates_from_dockerhub_cached(cache: bool) -> List[dict]: return response_data -async def retrieve_templates_info_from_s3( - cache: bool, -) -> Dict[str, Dict[str, Any]]: - """Retrieves templates information from s3 and caches the data in Redis for future use. - - Args: - cache: A boolean value that indicates whether to use the cached data or not. - - Returns: - Information about organization in s3 (cached or network-call) - """ - - r = redis_utils.redis_connection() - if cache: - cached_data = r.get("temp_data") - if cached_data is not None: - print("Using cache...") - return json.loads(cached_data) - - # If not cached, fetch data from Docker Hub and cache it in Redis - response = await get_templates_info_from_s3(f"{templates_base_url}/llm_info.json") - - # Cache the data in Redis for 60 minutes - r.set("temp_data", json.dumps(response), ex=900) - print("Using network call...") - return response - - @backoff.on_exception(backoff.expo, (ConnectError, CancelledError), max_tries=5) async def retrieve_templates_from_dockerhub( url: str, repo_name: str From 0d8a19eca03238ef2b2d00057faf3f00c6678f8f Mon Sep 17 00:00:00 2001 From: aakrem Date: Thu, 17 Oct 2024 12:14:22 +0200 Subject: [PATCH 4/7] remove seperated get templates as we are using differente templates manager in oss and cloud --- .../agenta_backend/services/templates_manager.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/agenta-backend/agenta_backend/services/templates_manager.py b/agenta-backend/agenta_backend/services/templates_manager.py index d90c7c47d5..29a1970283 100644 --- a/agenta-backend/agenta_backend/services/templates_manager.py +++ b/agenta-backend/agenta_backend/services/templates_manager.py @@ -15,15 +15,13 @@ from agenta_backend.services.helpers import convert_to_utc_datetime +from agenta_backend.resources.templates.templates import ( + get_oss_templates as get_templates, +) + if isCloud() or isOss(): from agenta_backend.services import container_manager -if isCloud(): - from agenta_backend.commons.resources.templates.templates import get_templates -elif isOss(): - from agenta_backend.resources.templates.templates import ( - get_oss_templates as get_templates, - ) templates_base_url = os.getenv("TEMPLATES_BASE_URL") agenta_template_repo = os.getenv("AGENTA_TEMPLATE_REPO") From d622f088f018a4030a242cab875dd9f2ea4312ad Mon Sep 17 00:00:00 2001 From: aakrem Date: Thu, 17 Oct 2024 15:25:27 +0200 Subject: [PATCH 5/7] refactor --- .../resources/templates/templates.py | 12 +----------- .../agenta_backend/services/templates_manager.py | 15 ++++++--------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/agenta-backend/agenta_backend/resources/templates/templates.py b/agenta-backend/agenta_backend/resources/templates/templates.py index 28b085a803..6fd9b19511 100644 --- a/agenta-backend/agenta_backend/resources/templates/templates.py +++ b/agenta-backend/agenta_backend/resources/templates/templates.py @@ -1,4 +1,4 @@ -templates_info = { +templates = { "chat_openai": { "name": "Single Prompt OpenAI", "description": "Sample single prompt application using different OpenAI models", @@ -8,13 +8,3 @@ "description": "Sample Chat application using different OpenAI models", }, } - - -def get_oss_templates(): - """ - Returns a list of templates. - - Returns: - List[dict]: A list of template dictionaries. - """ - return templates_info diff --git a/agenta-backend/agenta_backend/services/templates_manager.py b/agenta-backend/agenta_backend/services/templates_manager.py index 29a1970283..2dbd00d1f4 100644 --- a/agenta-backend/agenta_backend/services/templates_manager.py +++ b/agenta-backend/agenta_backend/services/templates_manager.py @@ -9,15 +9,12 @@ from agenta_backend.utils import redis_utils from agenta_backend.services import db_manager from agenta_backend.utils.common import isCloud, isOss -from agenta_backend.resources.templates.templates import get_oss_templates from datetime import datetime, timezone from agenta_backend.services.helpers import convert_to_utc_datetime -from agenta_backend.resources.templates.templates import ( - get_oss_templates as get_templates, -) +from agenta_backend.resources.templates.templates import templates if isCloud() or isOss(): from agenta_backend.services import container_manager @@ -39,14 +36,14 @@ async def update_and_sync_templates(cache: bool = True) -> None: Returns: None """ - templates = await retrieve_templates_from_dockerhub_cached(cache) + docker_templates = await retrieve_templates_from_dockerhub_cached(cache) templates_ids_not_to_remove = [] - templates_info = get_templates() - for temp in templates: - if temp["name"] in list(templates_info.keys()): + + for temp in docker_templates: + if temp["name"] in list(templates.keys()): templates_ids_not_to_remove.append(int(temp["id"])) - temp_info = templates_info[temp["name"]] + temp_info = templates[temp["name"]] last_pushed = convert_to_utc_datetime(temp.get("last_pushed")) template_id = await db_manager.add_template( From c4122d1c0271384497cd210f806279315be510cc Mon Sep 17 00:00:00 2001 From: aakrem Date: Thu, 17 Oct 2024 15:29:59 +0200 Subject: [PATCH 6/7] remove another method that won't be used anymore --- .../services/templates_manager.py | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/agenta-backend/agenta_backend/services/templates_manager.py b/agenta-backend/agenta_backend/services/templates_manager.py index 2dbd00d1f4..0de15de5c6 100644 --- a/agenta-backend/agenta_backend/services/templates_manager.py +++ b/agenta-backend/agenta_backend/services/templates_manager.py @@ -124,28 +124,3 @@ async def retrieve_templates_from_dockerhub( response_data = response.json() return response_data - - -@backoff.on_exception( - backoff.expo, (ConnectError, TimeoutException, CancelledError), max_tries=5 -) -async def get_templates_info_from_s3(url: str) -> Dict[str, Dict[str, Any]]: - """ - Business logic to retrieve templates information from S3. - - Args: - url (str): The URL endpoint for retrieving templates info. - - Returns: - response_data (Dict[str, Dict[str, Any]]): A dictionary \ - containing dictionaries of templates information. - """ - - async with httpx.AsyncClient() as client: - response = await client.get(url, timeout=90) - if response.status_code == 200: - response_data = response.json() - return response_data - - response_data = response.json() - return response_data From 04e644a4684d6ca1669baf3e5b32f87b6085436b Mon Sep 17 00:00:00 2001 From: aakrem Date: Thu, 17 Oct 2024 16:00:20 +0200 Subject: [PATCH 7/7] remove TEMPLATES_BASE_URL --- .../migrations/mongo_to_postgres/docker-compose.migration.yml | 1 - agenta-backend/agenta_backend/services/templates_manager.py | 1 - docker-compose.gh.yml | 1 - docker-compose.prod.yml | 1 - docker-compose.test.yml | 1 - docker-compose.yml | 1 - 6 files changed, 6 deletions(-) diff --git a/agenta-backend/agenta_backend/migrations/mongo_to_postgres/docker-compose.migration.yml b/agenta-backend/agenta_backend/migrations/mongo_to_postgres/docker-compose.migration.yml index ee40ca9aaa..050013aa6d 100644 --- a/agenta-backend/agenta_backend/migrations/mongo_to_postgres/docker-compose.migration.yml +++ b/agenta-backend/agenta_backend/migrations/mongo_to_postgres/docker-compose.migration.yml @@ -27,7 +27,6 @@ services: - POSTHOG_API_KEY=phc_hmVSxIjTW1REBHXgj2aw4HW9X6CXb6FzerBgP9XenC7 - CELERY_BROKER_URL=amqp://guest@rabbitmq// - CELERY_RESULT_BACKEND=redis://redis:6379/0 - - TEMPLATES_BASE_URL=https://llm-app-json.s3.eu-central-1.amazonaws.com - REGISTRY_REPO_NAME=agentaai - DOCKER_HUB_URL=https://hub.docker.com/v2/repositories volumes: diff --git a/agenta-backend/agenta_backend/services/templates_manager.py b/agenta-backend/agenta_backend/services/templates_manager.py index 0de15de5c6..4ccd83c5bb 100644 --- a/agenta-backend/agenta_backend/services/templates_manager.py +++ b/agenta-backend/agenta_backend/services/templates_manager.py @@ -20,7 +20,6 @@ from agenta_backend.services import container_manager -templates_base_url = os.getenv("TEMPLATES_BASE_URL") agenta_template_repo = os.getenv("AGENTA_TEMPLATE_REPO") docker_hub_url = os.getenv("DOCKER_HUB_URL") diff --git a/docker-compose.gh.yml b/docker-compose.gh.yml index 920af50bc6..b371690b84 100644 --- a/docker-compose.gh.yml +++ b/docker-compose.gh.yml @@ -26,7 +26,6 @@ services: - DOMAIN_NAME=${DOMAIN_NAME:-http://localhost} - CELERY_BROKER_URL=amqp://guest@rabbitmq// - CELERY_RESULT_BACKEND=redis://redis:6379/0 - - TEMPLATES_BASE_URL=https://llm-app-json.s3.eu-central-1.amazonaws.com - REGISTRY_REPO_NAME=agentaai - DOCKER_HUB_URL=https://hub.docker.com/v2/repositories command: diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index d28662c1b0..39f8653af4 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -23,7 +23,6 @@ services: - AGENTA_TEMPLATE_REPO=agentaai/templates_v2 - CELERY_BROKER_URL=amqp://guest@rabbitmq// - CELERY_RESULT_BACKEND=redis://redis:6379/0 - - TEMPLATES_BASE_URL=https://llm-app-json.s3.eu-central-1.amazonaws.com - REGISTRY_REPO_NAME=agentaai - DOCKER_HUB_URL=https://hub.docker.com/v2/repositories volumes: diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 4df2d0679b..a4008ea40a 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -27,7 +27,6 @@ services: - ALEMBIC_CFG_PATH=/app/agenta_backend/migrations/postgres/alembic.oss.ini - AGENTA_TEMPLATE_REPO=agentaai/templates_v2 - POSTHOG_API_KEY=phc_hmVSxIjTW1REBHXgj2aw4HW9X6CXb6FzerBgP9XenC7 - - TEMPLATES_BASE_URL=https://llm-app-json.s3.eu-central-1.amazonaws.com - REGISTRY_REPO_NAME=agentaai - DOCKER_HUB_URL=https://hub.docker.com/v2/repositories volumes: diff --git a/docker-compose.yml b/docker-compose.yml index 99e3df086a..50c8a665f8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,7 +26,6 @@ services: - POSTHOG_API_KEY=phc_hmVSxIjTW1REBHXgj2aw4HW9X6CXb6FzerBgP9XenC7 - CELERY_BROKER_URL=amqp://guest@rabbitmq// - CELERY_RESULT_BACKEND=redis://redis:6379/0 - - TEMPLATES_BASE_URL=https://llm-app-json.s3.eu-central-1.amazonaws.com - REGISTRY_REPO_NAME=agentaai - DOCKER_HUB_URL=https://hub.docker.com/v2/repositories volumes: