From e55e8b63ca18a896f9ac2f1eb8756cbf11a3189c Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Sun, 1 Dec 2024 21:04:19 +0100 Subject: [PATCH 1/7] moving ee components to ee folder --- agenta-web/src/lib/helpers/dynamic.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agenta-web/src/lib/helpers/dynamic.ts b/agenta-web/src/lib/helpers/dynamic.ts index 19f5a003ce..3f5b87658e 100644 --- a/agenta-web/src/lib/helpers/dynamic.ts +++ b/agenta-web/src/lib/helpers/dynamic.ts @@ -1,7 +1,7 @@ import dynamic from "next/dynamic" export function dynamicComponent(path: string, fallback: any = () => null) { - return dynamic(() => import(`@/components/${path}`), { + return dynamic(() => import(`@/ee/components/${path}`), { loading: fallback, ssr: false, }) From 3497e1998b90555c89c4c6159b8e89f17e6ca742 Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Sun, 1 Dec 2024 21:55:05 +0100 Subject: [PATCH 2/7] changed dynamics to use ee --- agenta-web/src/lib/helpers/dynamic.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/agenta-web/src/lib/helpers/dynamic.ts b/agenta-web/src/lib/helpers/dynamic.ts index 3f5b87658e..abd0fb558a 100644 --- a/agenta-web/src/lib/helpers/dynamic.ts +++ b/agenta-web/src/lib/helpers/dynamic.ts @@ -9,7 +9,7 @@ export function dynamicComponent(path: string, fallback: any = () => null) { export async function dynamicContext(path: string, fallback?: any) { try { - return await import(`@/contexts/${path}`) + return await import(`@/ee/contexts/${path}`) } catch (error) { return fallback } @@ -17,7 +17,7 @@ export async function dynamicContext(path: string, fallback?: any) { export async function dynamicHook(path: string, fallback: any = () => null) { try { - return await import(`@/hooks/${path}`) + return await import(`@/ee/hooks/${path}`) } catch (error) { return fallback } @@ -25,7 +25,7 @@ export async function dynamicHook(path: string, fallback: any = () => null) { export async function dynamicService(path: string, fallback?: any) { try { - return await import(`@/services/${path}`) + return await import(`@/ee/services/${path}`) } catch (error) { return fallback } From 7418f463b2383bea0455f8ff22f8107ec90d0601 Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Sun, 1 Dec 2024 22:20:52 +0100 Subject: [PATCH 3/7] refactor(app): moved observability api to oss --- agenta-web/src/lib/Types.ts | 80 +++++++ agenta-web/src/lib/helpers/dynamic.ts | 8 - .../src/services/observability/api/index.ts | 221 ++++++++++++++++++ .../src/services/observability/api/mock.ts | 142 +++++++++++ 4 files changed, 443 insertions(+), 8 deletions(-) create mode 100644 agenta-web/src/services/observability/api/mock.ts diff --git a/agenta-web/src/lib/Types.ts b/agenta-web/src/lib/Types.ts index 6504bb9db0..d21950efb8 100644 --- a/agenta-web/src/lib/Types.ts +++ b/agenta-web/src/lib/Types.ts @@ -705,3 +705,83 @@ export type FilterConditions = | "is_not" | "btwn" | "" + + +export enum GenerationStatus { + UNSET = "UNSET", + OK = "OK", + ERROR = "ERROR", +} + +export enum GenerationKind { + TOOL = "TOOL", + CHAIN = "CHAIN", + LLM = "LLM", + WORKFLOW = "WORKFLOW", + RETRIEVER = "RETRIEVER", + EMBEDDING = "EMBEDDING", + AGENT = "AGENT", + UNKNOWN = "UNKNOWN", +} + +export interface Generation { + id: string + created_at: string + variant: { + variant_id: string + variant_name: string + revision: number + } + environment: string | null + status: GenerationStatus + error?: string + spankind: GenerationKind + metadata?: RequestMetadata + user_id?: string + children?: [] + parent_span_id?: string + name?: string + content: { + inputs: Record + internals: Record + outputs: string[] | Record + } +} + +export interface GenerationTreeNode { + title: React.ReactElement + key: string + children?: GenerationTreeNode[] +} + +export interface GenerationDetails extends Generation { + config: GenericObject +} + +export interface GenerationDashboardData { + data: { + timestamp: number | string + success_count: number + failure_count: number + cost: number + latency: number + total_tokens: number + prompt_tokens: number + completion_tokens: number + enviornment: string + variant: string + }[] + total_count: number + failure_rate: number + total_cost: number + avg_cost: number + avg_latency: number + total_tokens: number + avg_tokens: number +} + +export interface Trace extends Generation {} + +export interface TraceDetails extends GenerationDetails { + spans: Generation[] +} \ No newline at end of file diff --git a/agenta-web/src/lib/helpers/dynamic.ts b/agenta-web/src/lib/helpers/dynamic.ts index abd0fb558a..a0122d89ed 100644 --- a/agenta-web/src/lib/helpers/dynamic.ts +++ b/agenta-web/src/lib/helpers/dynamic.ts @@ -15,14 +15,6 @@ export async function dynamicContext(path: string, fallback?: any) { } } -export async function dynamicHook(path: string, fallback: any = () => null) { - try { - return await import(`@/ee/hooks/${path}`) - } catch (error) { - return fallback - } -} - export async function dynamicService(path: string, fallback?: any) { try { return await import(`@/ee/services/${path}`) diff --git a/agenta-web/src/services/observability/api/index.ts b/agenta-web/src/services/observability/api/index.ts index 30a1282c03..678554a9a6 100644 --- a/agenta-web/src/services/observability/api/index.ts +++ b/agenta-web/src/services/observability/api/index.ts @@ -1,6 +1,227 @@ +import axios from "@/lib/helpers/axiosConfig" +import { + Generation, + GenerationDashboardData, + GenerationDetails, + Trace, + TraceDetails, +} from "@/lib/types_ee" +import {GenericObject, WithPagination} from "@/lib/Types" +import dayjs from "dayjs" +import {TableParams} from "@/components/ServerTable/components" +import {delay, pickRandom} from "@/lib/helpers/utils" +import {ObservabilityMock} from "./mock" +import {meanBy, random, round, sumBy} from "lodash" +import Router from "next/router" +import {getCurrentProject} from "@/contexts/project.context" + //Prefix convention: // - fetch: GET single entity from server // - fetchAll: GET all entities from server // - create: POST data to server // - update: PUT data to server // - delete: DELETE data from server + +const mock = false + +function tableParamsToApiParams(options?: Partial) { + const {page = 1, pageSize = 20} = options?.pagination || {} + const res: GenericObject = {page, pageSize} + if (options?.sorters) { + Object.entries(options.sorters).forEach( + ([key, val]) => (res[key] = val === "ascend" ? "asc" : "desc"), + ) + } + if (options?.filters) { + Object.entries(options.filters).forEach(([key, val]) => (res[key] = val)) + } + return res +} + +const generations = pickRandom(ObservabilityMock.generations, 100).map((item, ix) => ({ + ...item, + id: ix + 1 + "", +})) + +export const fetchAllGenerations = async (appId: string, options?: Partial) => { + const {projectId} = getCurrentProject() + + const params = tableParamsToApiParams(options) + if (mock) { + const {page, pageSize} = params + await delay(800) + return { + data: generations.slice((page - 1) * pageSize, page * pageSize), + total: generations.length, + page, + pageSize, + } as WithPagination + } + + const response = await axios.get(`/api/observability/spans?project_id=${projectId}`, { + params: {app_id: appId, type: "generation", ...params}, + }) + return response.data as WithPagination +} + +export const fetchGeneration = async (generationId: string) => { + const {projectId} = getCurrentProject() + + if (mock) { + await delay(800) + const generation = generations.find((item) => item.id === generationId) + if (!generation) throw new Error("not found!") + + return { + ...generation, + ...ObservabilityMock.generationDetail, + } as GenerationDetails + } + + const response = await axios.get( + `/api/observability/spans/${generationId}?project_id=${projectId}`, + { + params: {type: "generation"}, + }, + ) + return response.data as GenerationDetails +} + +export const fetchGenerationsDashboardData = async ( + appId: string, + options: { + range: string + environment?: string + variant?: string + }, +) => { + const {projectId} = getCurrentProject() + + const {range, environment, variant} = options + if (mock) { + await delay(1200) + const startTs = Date.now() + const endTs = dayjs() + .subtract(1, range === "24_hours" ? "day" : range === "7_days" ? "week" : "month") + .valueOf() + + const data = ObservabilityMock.dashboardData().filter( + (item) => + (item as any).timestamp >= endTs && + (item as any).timestamp <= startTs && + (!environment || item.enviornment === environment), + ) + + const successCount = sumBy(data, "success_count") + const failureCount = sumBy(data, "failure_count") + return { + data: data.map((item) => ({ + ...item, + timestamp: dayjs(item.timestamp).format( + range === "24_hours" ? "h:mm a" : range === "7_days" ? "ddd" : "D MMM", + ), + })) as any, + total_count: successCount + failureCount, + failure_rate: round(failureCount / (successCount + failureCount), 2), + total_cost: sumBy(data, "cost"), + avg_cost: meanBy(data, "cost"), + avg_latency: meanBy(data, "latency"), + total_tokens: sumBy(data, "total_tokens"), + avg_tokens: meanBy(data, "total_tokens"), + } as GenerationDashboardData + } + const response = await axios.get(`/api/observability/v1/analytics?project_id=${projectId}`, { + params: { + app_id: appId, + timeRange: range, + environment, + variant, + format: "legacy", + }, + }) + const val = response.data as GenerationDashboardData + val.data = val.data.map((item) => ({ + ...item, + timestamp: dayjs(item.timestamp).format( + range === "24_hours" ? "h:mm a" : range === "7_days" ? "ddd" : "D MMM", + ), + })) + return val +} + +export const deleteGeneration = async ( + generationIds: string[], + type: string = "generation", + ignoreAxiosError = true, +) => { + const {projectId} = getCurrentProject() + + await axios.delete(`/api/observability/spans?project_id=${projectId}`, { + data: generationIds, + _ignoreError: ignoreAxiosError, + } as any) + return true +} + +export const fetchAllTraces = async (appId: string, options?: Partial) => { + const {projectId} = getCurrentProject() + + const params = tableParamsToApiParams(options) + if (mock) { + const {page, pageSize} = params + await delay(800) + return { + data: generations.slice((page - 1) * pageSize, page * pageSize), + total: generations.length, + page, + pageSize, + } as WithPagination + } + const response = await axios.get(`/api/observability/traces?project_id=${projectId}`, { + params: {app_id: appId, type: "generation", ...params}, + }) + return response.data as WithPagination +} + +export const fetchTrace = async (traceId: string) => { + const {projectId} = getCurrentProject() + + if (mock) { + await delay(800) + const generation = generations.find((item) => item.id === traceId) + if (!generation) throw new Error("not found!") + + return { + ...generation, + ...ObservabilityMock.generationDetail, + spans: await fetchSpansOfTrace(traceId), + } as TraceDetails + } + const [trace, spans] = await Promise.all([ + axios.get(`/api/observability/traces/${traceId}?project_id=${projectId}`), + fetchSpansOfTrace(traceId), + ]) + const response = trace.data + response.spans = spans + return response as TraceDetails +} + +export const fetchSpansOfTrace = async (traceId: string) => { + const {projectId} = getCurrentProject() + + if (mock) { + await delay(200) + const lower = random(0, 100) + const upper = random(lower, 100) + + return generations.slice(lower, upper).map((item) => ({ + ...item, + ...ObservabilityMock.generationDetail, + })) + } + + const response = await axios.get(`/api/observability/spans?project_id=${projectId}`, { + params: {trace_id: traceId, app_id: Router.query.app_id}, + }) + return response.data as Generation[] +} diff --git a/agenta-web/src/services/observability/api/mock.ts b/agenta-web/src/services/observability/api/mock.ts new file mode 100644 index 0000000000..2b9cb3050a --- /dev/null +++ b/agenta-web/src/services/observability/api/mock.ts @@ -0,0 +1,142 @@ +import {randNum} from "@/lib/helpers/utils" +import {Generation, GenerationKind, GenerationDashboardData, GenerationStatus} from "@/ee/lib/types_ee" +import dayjs from "dayjs" + +const generations: Generation[] = [ + { + id: "1", + created_at: "2021-10-01T00:00:00Z", + variant: { + variant_id: "1", + variant_name: "default", + revision: 1, + }, + environment: "production", + status: GenerationStatus.OK, + spankind: GenerationKind.LLM, + metadata: { + cost: 0.0001, + latency: 0.32, + usage: { + total_tokens: 72, + prompt_tokens: 25, + completion_tokens: 47, + }, + }, + user_id: "u-8k3j4", + content: { + inputs: [ + {input_name: "country", input_value: "Pakistan"}, + {input_name: "criteria", input_value: "Most population"}, + ], + outputs: ["The most populous city in Pakistan is Karachi"], + internals: [], + }, + }, + { + id: "2", + created_at: "2023-10-01T00:00:00Z", + variant: { + variant_id: "2", + variant_name: "test", + revision: 1, + }, + environment: "staging", + status: GenerationStatus.ERROR, + spankind: GenerationKind.LLM, + metadata: { + cost: 0.0004, + latency: 0.845, + usage: { + total_tokens: 143, + prompt_tokens: 25, + completion_tokens: 118, + }, + }, + user_id: "u-8k3j4", + content: { + inputs: [], + outputs: [], + internals: [], + }, + }, + { + id: "3", + created_at: "2024-10-01T00:00:00Z", + variant: { + variant_id: "1", + variant_name: "default", + revision: 2, + }, + environment: "development", + status: GenerationStatus.OK, + spankind: GenerationKind.LLM, + metadata: { + cost: 0.0013, + latency: 0.205, + usage: { + total_tokens: 61, + prompt_tokens: 25, + completion_tokens: 36, + }, + }, + user_id: "u-7tij2", + content: { + inputs: [], + outputs: [], + internals: [], + }, + }, +] + +const generationDetail = { + content: { + inputs: [ + {input_name: "country", input_value: "Pakistan"}, + {input_name: "criteria", input_value: "Most population"}, + ], + outputs: ["The most populous city in Pakistan is Karachi"], + internals: [], + }, + config: { + system: "You are an expert in geography.", + user: "What is the city of {country} with the criteria {criteria}?", + variables: [ + {name: "country", type: "string"}, + {name: "criteria", type: "string"}, + ], + temperature: 0.7, + model: "gpt-3.5-turbo", + max_tokens: 100, + top_p: 0.9, + frequency_penalty: 0.5, + presence_penalty: 0, + }, +} + +const dashboardData = (count = 300): GenerationDashboardData["data"] => { + return Array(count) + .fill(true) + .map(() => { + const totalTokens = randNum(0, 600) + const promptTokens = randNum(0, 150) + return { + timestamp: randNum(dayjs().subtract(30, "days").valueOf(), dayjs().valueOf()), // b/w last 30 days + success_count: randNum(0, 20), + failure_count: randNum(0, 5), + latency: Math.random() * 1.5, + cost: Math.random() * 0.01, + total_tokens: totalTokens, + prompt_tokens: promptTokens, + completion_tokens: totalTokens - promptTokens, + enviornment: ["production", "staging", "development"][randNum(0, 2)], + variant: "default", + } + }) +} + +export const ObservabilityMock = { + generations, + generationDetail, + dashboardData, +} From ff92670219d4f0639f766c8dcfe7339a0a989161 Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Tue, 3 Dec 2024 14:34:05 +0100 Subject: [PATCH 4/7] format --- agenta-web/src/components/Playground/Views/TestView.tsx | 2 +- agenta-web/src/lib/Types.ts | 3 +-- agenta-web/src/services/observability/api/mock.ts | 7 ++++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/agenta-web/src/components/Playground/Views/TestView.tsx b/agenta-web/src/components/Playground/Views/TestView.tsx index 2f0cc86450..95a0a72690 100644 --- a/agenta-web/src/components/Playground/Views/TestView.tsx +++ b/agenta-web/src/components/Playground/Views/TestView.tsx @@ -219,7 +219,7 @@ const BoxComponent: React.FC = ({ } }, [traceSpans]) - const activeTrace = useMemo(() => (traces ? traces[0] ?? null : null), [traces]) + const activeTrace = useMemo(() => (traces ? (traces[0] ?? null) : null), [traces]) const [selected, setSelected] = useState("") useEffect(() => { diff --git a/agenta-web/src/lib/Types.ts b/agenta-web/src/lib/Types.ts index d21950efb8..80df7bdb6b 100644 --- a/agenta-web/src/lib/Types.ts +++ b/agenta-web/src/lib/Types.ts @@ -706,7 +706,6 @@ export type FilterConditions = | "btwn" | "" - export enum GenerationStatus { UNSET = "UNSET", OK = "OK", @@ -784,4 +783,4 @@ export interface Trace extends Generation {} export interface TraceDetails extends GenerationDetails { spans: Generation[] -} \ No newline at end of file +} diff --git a/agenta-web/src/services/observability/api/mock.ts b/agenta-web/src/services/observability/api/mock.ts index 2b9cb3050a..aa65643115 100644 --- a/agenta-web/src/services/observability/api/mock.ts +++ b/agenta-web/src/services/observability/api/mock.ts @@ -1,5 +1,10 @@ import {randNum} from "@/lib/helpers/utils" -import {Generation, GenerationKind, GenerationDashboardData, GenerationStatus} from "@/ee/lib/types_ee" +import { + Generation, + GenerationKind, + GenerationDashboardData, + GenerationStatus, +} from "@/ee/lib/types_ee" import dayjs from "dayjs" const generations: Generation[] = [ From e424222201beb6b515fe2498c53ce56c74914d64 Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Tue, 3 Dec 2024 15:28:51 +0100 Subject: [PATCH 5/7] fix --- agenta-web/src/services/observability/api/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agenta-web/src/services/observability/api/index.ts b/agenta-web/src/services/observability/api/index.ts index 678554a9a6..5286f74243 100644 --- a/agenta-web/src/services/observability/api/index.ts +++ b/agenta-web/src/services/observability/api/index.ts @@ -5,7 +5,7 @@ import { GenerationDetails, Trace, TraceDetails, -} from "@/lib/types_ee" +} from "@/ee/lib/types_ee" import {GenericObject, WithPagination} from "@/lib/Types" import dayjs from "dayjs" import {TableParams} from "@/components/ServerTable/components" From ee0cb00d86abc172c94371ccc774f8f4dbe906a4 Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Tue, 3 Dec 2024 15:52:32 +0100 Subject: [PATCH 6/7] REVERT --- .../src/services/observability/api/index.ts | 223 +----------------- .../src/services/observability/api/mock.ts | 147 ------------ 2 files changed, 1 insertion(+), 369 deletions(-) delete mode 100644 agenta-web/src/services/observability/api/mock.ts diff --git a/agenta-web/src/services/observability/api/index.ts b/agenta-web/src/services/observability/api/index.ts index 5286f74243..6a8d0d0d58 100644 --- a/agenta-web/src/services/observability/api/index.ts +++ b/agenta-web/src/services/observability/api/index.ts @@ -1,227 +1,6 @@ -import axios from "@/lib/helpers/axiosConfig" -import { - Generation, - GenerationDashboardData, - GenerationDetails, - Trace, - TraceDetails, -} from "@/ee/lib/types_ee" -import {GenericObject, WithPagination} from "@/lib/Types" -import dayjs from "dayjs" -import {TableParams} from "@/components/ServerTable/components" -import {delay, pickRandom} from "@/lib/helpers/utils" -import {ObservabilityMock} from "./mock" -import {meanBy, random, round, sumBy} from "lodash" -import Router from "next/router" -import {getCurrentProject} from "@/contexts/project.context" - //Prefix convention: // - fetch: GET single entity from server // - fetchAll: GET all entities from server // - create: POST data to server // - update: PUT data to server -// - delete: DELETE data from server - -const mock = false - -function tableParamsToApiParams(options?: Partial) { - const {page = 1, pageSize = 20} = options?.pagination || {} - const res: GenericObject = {page, pageSize} - if (options?.sorters) { - Object.entries(options.sorters).forEach( - ([key, val]) => (res[key] = val === "ascend" ? "asc" : "desc"), - ) - } - if (options?.filters) { - Object.entries(options.filters).forEach(([key, val]) => (res[key] = val)) - } - return res -} - -const generations = pickRandom(ObservabilityMock.generations, 100).map((item, ix) => ({ - ...item, - id: ix + 1 + "", -})) - -export const fetchAllGenerations = async (appId: string, options?: Partial) => { - const {projectId} = getCurrentProject() - - const params = tableParamsToApiParams(options) - if (mock) { - const {page, pageSize} = params - await delay(800) - return { - data: generations.slice((page - 1) * pageSize, page * pageSize), - total: generations.length, - page, - pageSize, - } as WithPagination - } - - const response = await axios.get(`/api/observability/spans?project_id=${projectId}`, { - params: {app_id: appId, type: "generation", ...params}, - }) - return response.data as WithPagination -} - -export const fetchGeneration = async (generationId: string) => { - const {projectId} = getCurrentProject() - - if (mock) { - await delay(800) - const generation = generations.find((item) => item.id === generationId) - if (!generation) throw new Error("not found!") - - return { - ...generation, - ...ObservabilityMock.generationDetail, - } as GenerationDetails - } - - const response = await axios.get( - `/api/observability/spans/${generationId}?project_id=${projectId}`, - { - params: {type: "generation"}, - }, - ) - return response.data as GenerationDetails -} - -export const fetchGenerationsDashboardData = async ( - appId: string, - options: { - range: string - environment?: string - variant?: string - }, -) => { - const {projectId} = getCurrentProject() - - const {range, environment, variant} = options - if (mock) { - await delay(1200) - const startTs = Date.now() - const endTs = dayjs() - .subtract(1, range === "24_hours" ? "day" : range === "7_days" ? "week" : "month") - .valueOf() - - const data = ObservabilityMock.dashboardData().filter( - (item) => - (item as any).timestamp >= endTs && - (item as any).timestamp <= startTs && - (!environment || item.enviornment === environment), - ) - - const successCount = sumBy(data, "success_count") - const failureCount = sumBy(data, "failure_count") - return { - data: data.map((item) => ({ - ...item, - timestamp: dayjs(item.timestamp).format( - range === "24_hours" ? "h:mm a" : range === "7_days" ? "ddd" : "D MMM", - ), - })) as any, - total_count: successCount + failureCount, - failure_rate: round(failureCount / (successCount + failureCount), 2), - total_cost: sumBy(data, "cost"), - avg_cost: meanBy(data, "cost"), - avg_latency: meanBy(data, "latency"), - total_tokens: sumBy(data, "total_tokens"), - avg_tokens: meanBy(data, "total_tokens"), - } as GenerationDashboardData - } - const response = await axios.get(`/api/observability/v1/analytics?project_id=${projectId}`, { - params: { - app_id: appId, - timeRange: range, - environment, - variant, - format: "legacy", - }, - }) - const val = response.data as GenerationDashboardData - val.data = val.data.map((item) => ({ - ...item, - timestamp: dayjs(item.timestamp).format( - range === "24_hours" ? "h:mm a" : range === "7_days" ? "ddd" : "D MMM", - ), - })) - return val -} - -export const deleteGeneration = async ( - generationIds: string[], - type: string = "generation", - ignoreAxiosError = true, -) => { - const {projectId} = getCurrentProject() - - await axios.delete(`/api/observability/spans?project_id=${projectId}`, { - data: generationIds, - _ignoreError: ignoreAxiosError, - } as any) - return true -} - -export const fetchAllTraces = async (appId: string, options?: Partial) => { - const {projectId} = getCurrentProject() - - const params = tableParamsToApiParams(options) - if (mock) { - const {page, pageSize} = params - await delay(800) - return { - data: generations.slice((page - 1) * pageSize, page * pageSize), - total: generations.length, - page, - pageSize, - } as WithPagination - } - const response = await axios.get(`/api/observability/traces?project_id=${projectId}`, { - params: {app_id: appId, type: "generation", ...params}, - }) - return response.data as WithPagination -} - -export const fetchTrace = async (traceId: string) => { - const {projectId} = getCurrentProject() - - if (mock) { - await delay(800) - const generation = generations.find((item) => item.id === traceId) - if (!generation) throw new Error("not found!") - - return { - ...generation, - ...ObservabilityMock.generationDetail, - spans: await fetchSpansOfTrace(traceId), - } as TraceDetails - } - const [trace, spans] = await Promise.all([ - axios.get(`/api/observability/traces/${traceId}?project_id=${projectId}`), - fetchSpansOfTrace(traceId), - ]) - const response = trace.data - response.spans = spans - return response as TraceDetails -} - -export const fetchSpansOfTrace = async (traceId: string) => { - const {projectId} = getCurrentProject() - - if (mock) { - await delay(200) - const lower = random(0, 100) - const upper = random(lower, 100) - - return generations.slice(lower, upper).map((item) => ({ - ...item, - ...ObservabilityMock.generationDetail, - })) - } - - const response = await axios.get(`/api/observability/spans?project_id=${projectId}`, { - params: {trace_id: traceId, app_id: Router.query.app_id}, - }) - return response.data as Generation[] -} +// - delete: DELETE data from server \ No newline at end of file diff --git a/agenta-web/src/services/observability/api/mock.ts b/agenta-web/src/services/observability/api/mock.ts deleted file mode 100644 index aa65643115..0000000000 --- a/agenta-web/src/services/observability/api/mock.ts +++ /dev/null @@ -1,147 +0,0 @@ -import {randNum} from "@/lib/helpers/utils" -import { - Generation, - GenerationKind, - GenerationDashboardData, - GenerationStatus, -} from "@/ee/lib/types_ee" -import dayjs from "dayjs" - -const generations: Generation[] = [ - { - id: "1", - created_at: "2021-10-01T00:00:00Z", - variant: { - variant_id: "1", - variant_name: "default", - revision: 1, - }, - environment: "production", - status: GenerationStatus.OK, - spankind: GenerationKind.LLM, - metadata: { - cost: 0.0001, - latency: 0.32, - usage: { - total_tokens: 72, - prompt_tokens: 25, - completion_tokens: 47, - }, - }, - user_id: "u-8k3j4", - content: { - inputs: [ - {input_name: "country", input_value: "Pakistan"}, - {input_name: "criteria", input_value: "Most population"}, - ], - outputs: ["The most populous city in Pakistan is Karachi"], - internals: [], - }, - }, - { - id: "2", - created_at: "2023-10-01T00:00:00Z", - variant: { - variant_id: "2", - variant_name: "test", - revision: 1, - }, - environment: "staging", - status: GenerationStatus.ERROR, - spankind: GenerationKind.LLM, - metadata: { - cost: 0.0004, - latency: 0.845, - usage: { - total_tokens: 143, - prompt_tokens: 25, - completion_tokens: 118, - }, - }, - user_id: "u-8k3j4", - content: { - inputs: [], - outputs: [], - internals: [], - }, - }, - { - id: "3", - created_at: "2024-10-01T00:00:00Z", - variant: { - variant_id: "1", - variant_name: "default", - revision: 2, - }, - environment: "development", - status: GenerationStatus.OK, - spankind: GenerationKind.LLM, - metadata: { - cost: 0.0013, - latency: 0.205, - usage: { - total_tokens: 61, - prompt_tokens: 25, - completion_tokens: 36, - }, - }, - user_id: "u-7tij2", - content: { - inputs: [], - outputs: [], - internals: [], - }, - }, -] - -const generationDetail = { - content: { - inputs: [ - {input_name: "country", input_value: "Pakistan"}, - {input_name: "criteria", input_value: "Most population"}, - ], - outputs: ["The most populous city in Pakistan is Karachi"], - internals: [], - }, - config: { - system: "You are an expert in geography.", - user: "What is the city of {country} with the criteria {criteria}?", - variables: [ - {name: "country", type: "string"}, - {name: "criteria", type: "string"}, - ], - temperature: 0.7, - model: "gpt-3.5-turbo", - max_tokens: 100, - top_p: 0.9, - frequency_penalty: 0.5, - presence_penalty: 0, - }, -} - -const dashboardData = (count = 300): GenerationDashboardData["data"] => { - return Array(count) - .fill(true) - .map(() => { - const totalTokens = randNum(0, 600) - const promptTokens = randNum(0, 150) - return { - timestamp: randNum(dayjs().subtract(30, "days").valueOf(), dayjs().valueOf()), // b/w last 30 days - success_count: randNum(0, 20), - failure_count: randNum(0, 5), - latency: Math.random() * 1.5, - cost: Math.random() * 0.01, - total_tokens: totalTokens, - prompt_tokens: promptTokens, - completion_tokens: totalTokens - promptTokens, - enviornment: ["production", "staging", "development"][randNum(0, 2)], - variant: "default", - } - }) -} - -export const ObservabilityMock = { - generations, - generationDetail, - dashboardData, -} From 943bce0c4029c3460c47069d2e1cd8a573ac4fdb Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Tue, 3 Dec 2024 15:53:56 +0100 Subject: [PATCH 7/7] reevrt --- agenta-web/src/lib/Types.ts | 79 ------------------- .../src/services/observability/api/index.ts | 2 +- 2 files changed, 1 insertion(+), 80 deletions(-) diff --git a/agenta-web/src/lib/Types.ts b/agenta-web/src/lib/Types.ts index 80df7bdb6b..6504bb9db0 100644 --- a/agenta-web/src/lib/Types.ts +++ b/agenta-web/src/lib/Types.ts @@ -705,82 +705,3 @@ export type FilterConditions = | "is_not" | "btwn" | "" - -export enum GenerationStatus { - UNSET = "UNSET", - OK = "OK", - ERROR = "ERROR", -} - -export enum GenerationKind { - TOOL = "TOOL", - CHAIN = "CHAIN", - LLM = "LLM", - WORKFLOW = "WORKFLOW", - RETRIEVER = "RETRIEVER", - EMBEDDING = "EMBEDDING", - AGENT = "AGENT", - UNKNOWN = "UNKNOWN", -} - -export interface Generation { - id: string - created_at: string - variant: { - variant_id: string - variant_name: string - revision: number - } - environment: string | null - status: GenerationStatus - error?: string - spankind: GenerationKind - metadata?: RequestMetadata - user_id?: string - children?: [] - parent_span_id?: string - name?: string - content: { - inputs: Record - internals: Record - outputs: string[] | Record - } -} - -export interface GenerationTreeNode { - title: React.ReactElement - key: string - children?: GenerationTreeNode[] -} - -export interface GenerationDetails extends Generation { - config: GenericObject -} - -export interface GenerationDashboardData { - data: { - timestamp: number | string - success_count: number - failure_count: number - cost: number - latency: number - total_tokens: number - prompt_tokens: number - completion_tokens: number - enviornment: string - variant: string - }[] - total_count: number - failure_rate: number - total_cost: number - avg_cost: number - avg_latency: number - total_tokens: number - avg_tokens: number -} - -export interface Trace extends Generation {} - -export interface TraceDetails extends GenerationDetails { - spans: Generation[] -} diff --git a/agenta-web/src/services/observability/api/index.ts b/agenta-web/src/services/observability/api/index.ts index 6a8d0d0d58..30a1282c03 100644 --- a/agenta-web/src/services/observability/api/index.ts +++ b/agenta-web/src/services/observability/api/index.ts @@ -3,4 +3,4 @@ // - fetchAll: GET all entities from server // - create: POST data to server // - update: PUT data to server -// - delete: DELETE data from server \ No newline at end of file +// - delete: DELETE data from server