Skip to content

Commit

Permalink
Merge branch 'main' into bug-fixes/release-cycle-63
Browse files Browse the repository at this point in the history
  • Loading branch information
aybruhm committed Nov 27, 2024
2 parents 7163376 + 3829495 commit 44f95ee
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -909,11 +909,11 @@ def parse_legacy_analytics(
for bucket_dto in bucket_dtos:
data_point = LegacyDataPoint(
timestamp=bucket_dto.timestamp,
success_count=bucket_dto.total.count - bucket_dto.error.count,
failure_count=bucket_dto.error.count,
cost=bucket_dto.total.cost,
latency=bucket_dto.total.duration,
total_tokens=bucket_dto.total.tokens,
success_count=(bucket_dto.total.count or 0) - (bucket_dto.error.count or 0),
failure_count=bucket_dto.error.count or 0,
cost=bucket_dto.total.cost or 0.0,
latency=bucket_dto.total.duration or 0.0,
total_tokens=bucket_dto.total.tokens or 0,
)

data_points.append(data_point)
Expand Down
3 changes: 2 additions & 1 deletion agenta-cli/agenta/sdk/middleware/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ async def dispatch(
self,
request: Request,
call_next: Callable,
project_id: Optional[UUID] = None,
):
if AGENTA_UNAUTHORIZED_EXECUTION_ALLOWED:
return await call_next(request)
Expand All @@ -85,6 +84,8 @@ async def dispatch(
"resource_id": self.resource_id,
}

project_id = request.query_params.get("project_id")

if project_id:
params["project_id"] = project_id

Expand Down
11 changes: 7 additions & 4 deletions agenta-cli/agenta/sdk/tracing/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class LifecycleDTO(BaseModel):
class TimeDTO(BaseModel):
start: datetime
end: datetime
span: int


class StatusCode(Enum):
Expand Down Expand Up @@ -846,12 +845,9 @@ def parse_from_otel_span_dto(
else None
)

duration = (otel_span_dto.end_time - otel_span_dto.start_time).total_seconds()

time = TimeDTO(
start=otel_span_dto.start_time,
end=otel_span_dto.end_time,
span=round(duration * 1_000_000), # microseconds
)

status = StatusDTO(
Expand All @@ -863,6 +859,13 @@ def parse_from_otel_span_dto(

data, metrics, meta, tags, refs = _parse_from_attributes(otel_span_dto)

duration = (otel_span_dto.end_time - otel_span_dto.start_time).total_seconds()

if metrics is None:
metrics = dict()

metrics["acc.duration.total"] = round(duration * 1_000, 3) # milliseconds

root_id = str(tree_id)
if refs is not None:
root_id = refs.get("scenario.id", root_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ const ABTestingEvaluationTable: React.FC<EvaluationTableProps> = ({
variantData[idx].isChatVariant
? testsetRowToChatMessages(evaluation.testset.csvdata[rowIndex], false)
: [],
undefined,
true,
)

let res: BaseResponse | undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ const SingleModelEvaluationTable: React.FC<EvaluationTableProps> = ({
variantData[idx].isChatVariant
? testsetRowToChatMessages(evaluation.testset.csvdata[rowIndex], false)
: [],
undefined,
true,
)

let res: BaseResponse | undefined
Expand Down
9 changes: 7 additions & 2 deletions agenta-web/src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {ThemeProvider} from "react-jss"
import {JSSTheme, StyleProps as MainStyleProps} from "@/lib/Types"
import {Lightning} from "@phosphor-icons/react"
import packageJsonData from "../../../package.json"
import {useProjectData} from "@/contexts/project.context"

const {Content, Footer} = Layout
const {Text} = Typography
Expand Down Expand Up @@ -85,6 +86,7 @@ const App: React.FC<LayoutProps> = ({children}) => {
const {appTheme} = useAppTheme()
const {currentApp} = useAppsData()
const [footerRef, {height: footerHeight}] = useElementSize()
const {isProjectId} = useProjectData()
const classes = useStyles({themeMode: appTheme, footerHeight} as StyleProps)
const router = useRouter()
const appId = router.query.app_id as string
Expand Down Expand Up @@ -161,19 +163,22 @@ const App: React.FC<LayoutProps> = ({children}) => {
// wait unitl we have the app id, if its an app route
if (isAppRoute && (!appId || !currentApp)) return null

const isAuthRoute =
router.pathname.includes("/auth") || router.pathname.includes("/post-signup")

return (
<NoSSRWrapper>
{typeof window === "undefined" ? null : (
<ThemeProvider theme={{...token, isDark: isDarkTheme}}>
{router.pathname.includes("/auth") ||
router.pathname.includes("/post-signup") ? (
{isAuthRoute ? (
<Layout className={classes.layout}>
<ErrorBoundary FallbackComponent={ErrorFallback}>
{children}
{contextHolder}
</ErrorBoundary>
</Layout>
) : (
// !isAuthRoute && isProjectId
<Layout hasSider className={classes.layout}>
<Sidebar />
<Layout className={classes.layout}>
Expand Down
4 changes: 1 addition & 3 deletions agenta-web/src/components/Playground/Views/TestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,7 @@ const App: React.FC<TestViewProps> = ({
const firstTraceNode = tree.nodes[0]
newDataList[index] = {
cost: firstTraceNode?.metrics?.acc?.costs?.total ?? null,
latency: firstTraceNode?.time?.span
? firstTraceNode.time.span / 1_000_000
: null,
latency: firstTraceNode?.metrics?.acc?.duration?.total / 1000 ?? null,
usage: firstTraceNode?.metrics?.acc?.tokens?.total ?? null,
}
}
Expand Down
10 changes: 6 additions & 4 deletions agenta-web/src/components/ProtectedRoute/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import {useSession} from "@/hooks/useSession"
import {useRouter} from "next/router"
import React, {PropsWithChildren, useEffect, useState} from "react"
import {useProjectData} from "@/contexts/project.context"

const ProtectedRoute: React.FC<PropsWithChildren> = ({children}) => {
const router = useRouter()
const {loading, doesSessionExist: isSignedIn} = useSession()
const {pathname} = router
const [shouldRender, setShouldRender] = useState(false)
const {isLoading, isProjectId} = useProjectData()

useEffect(() => {
if (loading) {
if (loading || isLoading) {
setShouldRender(false)
} else {
if (pathname.startsWith("/auth")) {
if (isSignedIn) {
router.push("/apps")
}
setShouldRender(!isSignedIn)
setShouldRender(true)
} else {
if (!isSignedIn) {
router.push(
Expand All @@ -25,10 +27,10 @@ const ProtectedRoute: React.FC<PropsWithChildren> = ({children}) => {
)}`,
)
}
setShouldRender(isSignedIn)
setShouldRender(!!isProjectId)
}
}
}, [pathname, isSignedIn, loading])
}, [pathname, isSignedIn, loading, isProjectId, isLoading])

return <>{shouldRender ? children : null}</>
}
Expand Down
2 changes: 1 addition & 1 deletion agenta-web/src/contexts/project.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const ProjectContextProvider: React.FC<PropsWithChildren> = ({children}) => {

const workspaceId: string = selectedOrg?.default_workspace.id || DEFAULT_UUID

const isProjectId = !isLoading && Boolean(project?.project_id)
const isProjectId = !isLoading && !!project?.project_id
const projectId = (project?.project_id as string) || DEFAULT_UUID

const fetcher = async (onSuccess?: () => void) => {
Expand Down
102 changes: 74 additions & 28 deletions agenta-web/src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ export async function fetchVariants(
return []
}

/**
* Get the JWT from SuperTokens
*/
const getJWT = async () => {
try {
if (await Session.doesSessionExist()) {
let jwt = await Session.getAccessToken()

return jwt
}
} catch (error) {}

return undefined
}

/**
* Calls the variant endpoint with the input parameters and the optional parameters and returns the response.
* @param inputParametersDict A dictionary of the input parameters to be passed to the variant endpoint
Expand Down Expand Up @@ -120,34 +135,41 @@ export async function callVariant(
}

const appContainerURI = await fetchAppContainerURL(appId, undefined, baseId)
const {projectId} = getCurrentProject()
const jwt = await getJWT()

return axios
.post(`${appContainerURI}/generate`, requestBody, {
const base_url = `${appContainerURI}/generate`
const secure_url = `${base_url}?project_id=${projectId}`
const secure_headers = {Authorization: jwt && `Bearer ${jwt}`}

let response = await axios
.post(base_url, requestBody, {
signal,
_ignoreError: ignoreAxiosError,
headers: {
Authorization: jwt && `Bearer ${jwt}`,
},
} as any)
.then((res) => {
return res.data
.then((response) => {
return response
})
}
.catch(async (error) => {
console.log("Unsecure call to LLM App failed:", error?.status)

/**
* Get the JWT from SuperTokens
*/
const getJWT = async () => {
try {
if (await Session.doesSessionExist()) {
let jwt = await Session.getAccessToken()
let response = await axios
.post(secure_url, requestBody, {
signal,
_ignoreError: ignoreAxiosError,
headers: secure_headers,
} as any)
.then((response) => {
return response
})
.catch((error) => {
console.log("Secure call to LLM App failed:", error?.status)
})

return jwt
}
} catch (error) {}
return response
})

return undefined
return response?.data
}

/**
Expand All @@ -163,16 +185,40 @@ export const fetchVariantParametersFromOpenAPI = async (
ignoreAxiosError: boolean = false,
) => {
const appContainerURI = await fetchAppContainerURL(appId, variantId, baseId)
const url = `${appContainerURI}/openapi.json`
const {projectId} = getCurrentProject()
const jwt = await getJWT()
const response = await axios.get(url, {
_ignoreError: ignoreAxiosError,
headers: {
Authorization: jwt && `Bearer ${jwt}`,
},
} as any)
const isChatVariant = detectChatVariantFromOpenAISchema(response.data)
let APIParams = openAISchemaToParameters(response.data)

const base_url = `${appContainerURI}/openapi.json`
const secure_url = `${base_url}?project_id=${projectId}`
const secure_headers = {Authorization: jwt && `Bearer ${jwt}`}

let response = await axios
.get(base_url, {
_ignoreError: ignoreAxiosError,
} as any)
.then((response) => {
return response
})
.catch(async (error) => {
console.log("Unsecure call to LLM App failed:", error?.status)

let response = await axios
.get(secure_url, {
_ignoreError: ignoreAxiosError,
headers: secure_headers,
} as any)
.then((response) => {
return response
})
.catch((error) => {
console.log("Secure call to LLM App failed:", error?.status)
})

return response
})

const isChatVariant = detectChatVariantFromOpenAISchema(response?.data)
let APIParams = openAISchemaToParameters(response?.data)

// we create a new param for DictInput that will contain the name of the inputs
APIParams = APIParams.map((param) => {
Expand Down

0 comments on commit 44f95ee

Please sign in to comment.