Skip to content

Commit

Permalink
Merge pull request #2125 from Agenta-AI/move-templates-to-backend
Browse files Browse the repository at this point in the history
Move templates to backend
  • Loading branch information
aakrem authored Oct 18, 2024
2 parents 66e1e5b + 04e644a commit c8c61c7
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions agenta-backend/agenta_backend/resources/templates/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
templates = {
"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",
},
}
67 changes: 8 additions & 59 deletions agenta-backend/agenta_backend/services/templates_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

from agenta_backend.services.helpers import convert_to_utc_datetime

from agenta_backend.resources.templates.templates import templates

if isCloud() or isOss():
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")

Expand All @@ -33,14 +35,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 = await retrieve_templates_info_from_s3(cache)
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(
Expand Down Expand Up @@ -96,34 +98,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
Expand All @@ -149,28 +123,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
1 change: 0 additions & 1 deletion docker-compose.gh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c8c61c7

Please sign in to comment.