From ffb0c4ffcc65828964eacd230cfd470ed37f99de Mon Sep 17 00:00:00 2001 From: Kaosiso Ezealigo Date: Fri, 3 May 2024 12:29:31 +0100 Subject: [PATCH 01/52] added fallback value for null cost and usage values --- .../src/components/Playground/Views/TestView.tsx | 6 +++--- agenta-web/src/lib/helpers/formatters.ts | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/agenta-web/src/components/Playground/Views/TestView.tsx b/agenta-web/src/components/Playground/Views/TestView.tsx index 8619ddafdd..58dba68ab2 100644 --- a/agenta-web/src/components/Playground/Views/TestView.tsx +++ b/agenta-web/src/components/Playground/Views/TestView.tsx @@ -22,7 +22,7 @@ import dayjs from "dayjs" import relativeTime from "dayjs/plugin/relativeTime" import duration from "dayjs/plugin/duration" import {useQueryParam} from "@/hooks/useQuery" -import {formatLatency} from "@/lib/helpers/formatters" +import {formatCurrency, formatLatency, formatTokenUsage} from "@/lib/helpers/formatters" dayjs.extend(relativeTime) dayjs.extend(duration) @@ -218,13 +218,13 @@ const BoxComponent: React.FC = ({

Tokens:{" "} {additionalData.usage !== null - ? JSON.stringify(additionalData.usage.total_tokens) + ? formatTokenUsage(additionalData.usage.total_tokens) : 0}

Cost:{" "} {additionalData.cost !== null - ? `$${additionalData.cost.toFixed(4)}` + ? formatCurrency(additionalData.cost) : "$0.00"}

diff --git a/agenta-web/src/lib/helpers/formatters.ts b/agenta-web/src/lib/helpers/formatters.ts index f6e2bae571..0388923a5e 100644 --- a/agenta-web/src/lib/helpers/formatters.ts +++ b/agenta-web/src/lib/helpers/formatters.ts @@ -12,10 +12,22 @@ export const formatNumber = (value = 0) => { return intlNumber.format(value) } -export const formatCurrency = (value = 0) => { - return intlCurrency.format(value) +export const formatCurrency = (value: number) => { + if (value === 0) { + return "-" + } else { + return intlCurrency.format(value) + } } export const formatLatency = (value = 0) => { return `${Math.round(value * 1000)}ms` } + +export const formatTokenUsage = (value: number) => { + if (value === 0) { + return "-" + } else { + return value + } +} From 09b7a2aca68f73963175bd3251c1ba905033b2e3 Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 5 May 2024 15:03:48 +0100 Subject: [PATCH 02/52] Feat: created logs manager service --- .../agenta_backend/services/logs_manager.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 agenta-backend/agenta_backend/services/logs_manager.py diff --git a/agenta-backend/agenta_backend/services/logs_manager.py b/agenta-backend/agenta_backend/services/logs_manager.py new file mode 100644 index 0000000000..98d6f6b412 --- /dev/null +++ b/agenta-backend/agenta_backend/services/logs_manager.py @@ -0,0 +1,18 @@ +import aiodocker + + +async def retrieve_logs(container_id: str) -> str: + """ + Retrieves and returns the last 10 lines of logs (both stdout and stderr) + for a specified Docker container. + Args: + container_id (str): The docker container identifier + Returns: + the last 10 lines of logs + """ + + async with aiodocker.Docker() as client: + container = await client.containers.get(container_id) + logs = await container.log(stdout=True, stderr=True) + outputs = logs[::-1][:10] + return "".join(outputs) From 76cbc4a76489b849e94944622c5b8d0df29bb491 Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 5 May 2024 15:04:59 +0100 Subject: [PATCH 03/52] Feat: created endpoint to fetch variant logs --- .../agenta_backend/routers/variants_router.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/agenta-backend/agenta_backend/routers/variants_router.py b/agenta-backend/agenta_backend/routers/variants_router.py index 41f67c623b..08bdaf2ca1 100644 --- a/agenta-backend/agenta_backend/routers/variants_router.py +++ b/agenta-backend/agenta_backend/routers/variants_router.py @@ -1,4 +1,5 @@ import os +import inspect import logging from typing import Any, Optional, Union, List @@ -24,11 +25,13 @@ Image_ as Image, AppVariantResponse_ as AppVariantResponse, ) + from agenta_backend.cloud.services import logs_manager else: from agenta_backend.models.api.api_models import ( Image, AppVariantResponse, ) + from agenta_backend.services import logs_manager from agenta_backend.models.api.api_models import ( URI, @@ -312,6 +315,22 @@ async def start_variant( return url +@router.get("/{variant_id}/logs/", operation_id="retrieve_variant_logs") +async def retrieve_variant_logs(variant_id: str, request: Request): + try: + app_variant = await db_manager.fetch_app_variant_by_id(variant_id) + deployment = await db_manager.get_deployment_by_appid(str(app_variant.app.id)) + is_coroutine_function = inspect.iscoroutinefunction(logs_manager.retrieve_logs) + if is_coroutine_function: + logs_result = await logs_manager.retrieve_logs(deployment.container_id) + else: + logs_result = logs_manager.retrieve_logs(deployment.container_id) + return logs_result + except Exception as exc: + logger.exception(f"An error occurred: {str(exc)}") + raise HTTPException(500, {"message": str(exc)}) + + @router.get( "/{variant_id}/", operation_id="get_variant", From b229f40df1019835e892dacbefc3e5755a91148f Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 5 May 2024 15:09:27 +0100 Subject: [PATCH 04/52] Improved error handling: Added Axios logic to fetch variant logs and integrated display logic on playground for container error --- .../components/Playground/ViewNavigation.tsx | 61 +++++++++++++------ agenta-web/src/lib/services/api.ts | 9 ++- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/agenta-web/src/components/Playground/ViewNavigation.tsx b/agenta-web/src/components/Playground/ViewNavigation.tsx index 851525383a..de5d177ce7 100644 --- a/agenta-web/src/components/Playground/ViewNavigation.tsx +++ b/agenta-web/src/components/Playground/ViewNavigation.tsx @@ -9,6 +9,7 @@ import {useState} from "react" import axios from "axios" import {createUseStyles} from "react-jss" import { + fetchVariantLogs, getAppContainerURL, removeVariant, restartAppVariantContainer, @@ -36,6 +37,10 @@ const useStyles = createUseStyles({ restartBtnMargin: { marginRight: "10px", }, + errorLogs: { + whiteSpace: "pre-wrap", + wordBreak: "break-all", + }, }) const ViewNavigation: React.FC = ({ @@ -69,6 +74,7 @@ const ViewNavigation: React.FC = ({ const [retrying, setRetrying] = useState(false) const [isParamsCollapsed, setIsParamsCollapsed] = useState("1") const [containerURI, setContainerURI] = useState("") + const [variantErrorLogs, setVariantErrorLogs] = useState("") const [restarting, setRestarting] = useState(false) const {currentApp} = useAppsData() const retriedOnce = useRef(false) @@ -101,7 +107,15 @@ const ViewNavigation: React.FC = ({ setRetrying(false) }) } - }, [netWorkError]) + + if (isError) { + const getLogs = async () => { + const logs = await fetchVariantLogs(variant.variantId) + setVariantErrorLogs(logs) + } + getLogs() + } + }, [netWorkError, isError, variant.variantId]) if (retrying || (!retriedOnce.current && netWorkError)) { return ( @@ -115,7 +129,8 @@ const ViewNavigation: React.FC = ({ if (isError) { let variantDesignator = variant.templateVariantName - let imageName = `agentaai/${(currentApp?.app_name || "").toLowerCase()}_` + let appName = currentApp?.app_name || "" + let imageName = `agentaai/${appName.toLowerCase()}_` if (!variantDesignator || variantDesignator === "") { variantDesignator = variant.variantName @@ -165,31 +180,39 @@ const ViewNavigation: React.FC = ({

Error connecting to the variant {variant.variantName}.{" "} {(axios.isAxiosError(error) && error.response?.status === 404 && ( - Container is not running. + + Container is not running. See logs below: + )) || {error.message}}

-

To debug this issue, please follow the steps below:

- {" "} - In case the docker container is not running. Please check the logs from - docker to understand the issue. Most of the time it is a missing - requirements. Also, please attempt restarting it (using cli or docker - desktop) + Verify API accessibility at{" "} + + {apiAddress} +

{" "} - If the issue persists please file an issue in github here: - https://github.com/Agenta-AI/agenta/issues/new?title=Issue%20in%20ViewNavigation.tsx + If the issue persists please file an issue in github + + {" "} + here +