Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Update SDK to return BaseResponse version 3.0 and new trace tree format #2251

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d0a55a6
refactor (cli:sdk): remove legacy spans for `inline_trace` and update…
aybruhm Nov 13, 2024
6a1f627
refactor (web): new trace tree format from observability
aybruhm Nov 13, 2024
c2e7a54
fix (style): format TestView component
aybruhm Nov 13, 2024
544cd40
cleanup (web/cli): remove redundant `_parse_to_legacy_span` function,…
aybruhm Nov 13, 2024
e6b8692
fix (build): resolve lint_and_build error on web
aybruhm Nov 13, 2024
5935899
refactor (backend) `extract_result_from_response` to support version …
aybruhm Nov 13, 2024
554fcef
fix(frontend): added span key to NodeTimeDTO
bekossy Nov 13, 2024
66f6341
fix(frontend): updated types and fixed bugs
bekossy Nov 14, 2024
4895127
refactor (cli:sdk): regenerate SDK client for updated observability r…
aybruhm Nov 14, 2024
dd7eb6c
refactor (web): add backward compatibility for v2 and v3 base respons…
aybruhm Nov 15, 2024
e316157
refactor (cli:sdk): update `parse_inline_trace` function in sdk to ma…
aybruhm Nov 15, 2024
622b18a
refactor (web): update logic for setting additional data list for bas…
aybruhm Nov 15, 2024
d5d494b
refactor (cli): bump version to 0.27.2a1
aybruhm Nov 15, 2024
a484239
chore (style): format sdk client backend
aybruhm Nov 15, 2024
81cadd7
Merge branch 'feature/age-1285-inline-traces-update-sdk-to-return-bas…
aybruhm Nov 16, 2024
bd4dcc8
Merge branch 'feature/age-1285-inline-traces-update-sdk-to-return-bas…
aybruhm Nov 16, 2024
8b2e03b
fix(frontend): updated BaseResponse type and modified code to match type
bekossy Nov 17, 2024
c056702
fix(frontend): removed unused helper function
bekossy Nov 17, 2024
f5b2df6
feat(frontend): added trace helper to check trace version
bekossy Nov 17, 2024
f280490
Merge branch 'feature/age-1285-inline-traces-update-sdk-to-return-bas…
bekossy Nov 17, 2024
6095ea8
feat (backend): add helper functions to process distributed traces an…
aybruhm Nov 17, 2024
772b9f3
refactor (backend): ensure keys can be retrieved optionally to avoid …
aybruhm Nov 17, 2024
c024460
refactor (backend_: make use of version from mapping_inputs
aybruhm Nov 17, 2024
f9421df
feat (tests): add unit tests for rag evaluators for baseresponses wit…
aybruhm Nov 17, 2024
291323d
chore (backend): add doc string back to `_make_spans_tree` function
aybruhm Nov 17, 2024
e7ead58
refactor (backend): add error management on extract_result from response
aybruhm Nov 18, 2024
572119b
fix(frontend): restored baseresponse helper function
bekossy Nov 18, 2024
7dfad2f
Merge pull request #2271 from Agenta-AI/AGE-1286/-update-web-ui-to-us…
bekossy Nov 19, 2024
de19c05
refactor (cli): rename `trace` attribute in BaseResponse to `tree`
aybruhm Nov 19, 2024
66fbc2d
refactor (backend): update backend to make use of new naming from `tr…
aybruhm Nov 19, 2024
8d75400
minor refactor (backend): simplify if-elif-else condition for mapping…
aybruhm Nov 19, 2024
438b87f
Merge pull request #2253 from Agenta-AI/feature/age-1288-inline-trace…
aybruhm Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 5 additions & 134 deletions agenta-cli/agenta/sdk/tracing/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,50 +992,13 @@ def parse_inline_trace(
### services.observability.service.query() ###
##############################################

LEGACY = True
inline_trace = None

if LEGACY:
legacy_spans = [
_parse_to_legacy_span(span_dto) for span_dto in span_idx.values()
]

root_span = agenta_span_dtos[0]

trace_id = root_span.root.id.hex
latency = root_span.time.span / 1_000_000
cost = root_span.metrics.get("acc", {}).get("costs", {}).get("total", 0.0)
tokens = {
"prompt_tokens": root_span.metrics.get("acc", {})
.get("tokens", {})
.get("prompt", 0),
"completion_tokens": root_span.metrics.get("acc", {})
.get("tokens", {})
.get("completion", 0),
"total_tokens": root_span.metrics.get("acc", {})
.get("tokens", {})
.get("total", 0),
}

spans = [
loads(span.model_dump_json(exclude_none=True)) for span in legacy_spans
]

inline_trace = {
"trace_id": trace_id,
"latency": latency,
"cost": cost,
"usage": tokens,
"spans": spans,
}

else:
spans = [
loads(span_dto.model_dump_json(exclude_none=True))
for span_dto in agenta_span_dtos
]

inline_trace = spans # turn into Agenta Model ?
spans = [
loads(span_dto.model_dump_json(exclude_none=True))
for span_dto in agenta_span_dtos
]
inline_trace = spans # NOTE (author: @jp-agenta): turn into Agenta Model ?

return inline_trace

Expand Down Expand Up @@ -1120,98 +1083,6 @@ class LlmTokens(BaseModel):
total_tokens: Optional[int] = 0


class CreateSpan(BaseModel):
id: str
app_id: str
variant_id: Optional[str] = None
variant_name: Optional[str] = None
inputs: Optional[Dict[str, Optional[Any]]] = None
internals: Optional[Dict[str, Optional[Any]]] = None
outputs: Optional[Union[str, Dict[str, Optional[Any]], List[Any]]] = None
config: Optional[Dict[str, Optional[Any]]] = None
environment: Optional[str] = None
tags: Optional[List[str]] = None
token_consumption: Optional[int] = None
name: str
parent_span_id: Optional[str] = None
attributes: Optional[Dict[str, Optional[Any]]] = None
spankind: str
status: str
user: Optional[str] = None
start_time: datetime
end_time: datetime
tokens: Optional[LlmTokens] = None
cost: Optional[float] = None


def _parse_to_legacy_span(span: SpanDTO) -> CreateSpan:
attributes = None
if span.otel:
attributes = span.otel.attributes or {}

if span.otel.events:
for event in span.otel.events:
if event.name == "exception":
attributes.update(**event.attributes)

legacy_span = CreateSpan(
id=span.node.id.hex[:24],
spankind=span.node.type,
name=span.node.name,
#
status=span.status.code.name,
#
start_time=span.time.start,
end_time=span.time.end,
#
parent_span_id=span.parent.id.hex[:24] if span.parent else None,
#
inputs=span.data.get("inputs") if span.data else {},
internals=span.data.get("internals") if span.data else {},
outputs=span.data.get("outputs") if span.data else {},
#
environment=span.meta.get("environment") if span.meta else None,
config=span.meta.get("configuration") if span.meta else None,
#
tokens=(
LlmTokens(
prompt_tokens=span.metrics.get("acc", {})
.get("tokens", {})
.get("prompt", 0.0),
completion_tokens=span.metrics.get("acc", {})
.get("tokens", {})
.get("completion", 0.0),
total_tokens=span.metrics.get("acc", {})
.get("tokens", {})
.get("total", 0.0),
)
if span.metrics
else None
),
cost=(
span.metrics.get("acc", {}).get("costs", {}).get("total", 0.0)
if span.metrics
else None
),
#
app_id=(
span.refs.get("application", {}).get("id", "missing-app-id")
if span.refs
else "missing-app-id"
),
#
attributes=attributes,
#
variant_id=None,
variant_name=None,
tags=None,
token_consumption=None,
user=None,
)

return legacy_span


TYPES_WITH_COSTS = [
"embedding",
"query",
Expand Down
4 changes: 2 additions & 2 deletions agenta-cli/agenta/sdk/types.py
aybruhm marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class LLMTokenUsage(BaseModel):


class BaseResponse(BaseModel):
version: Optional[str] = "2.0"
version: Optional[str] = "3.0"
data: Optional[Union[str, Dict[str, Any]]]
trace: Optional[Dict[str, Any]]
trace: Optional[List[Dict[str, Any]]]


class DictInput(dict):
Expand Down
27 changes: 13 additions & 14 deletions agenta-web/src/components/Playground/Views/TestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ interface BoxComponentProps {
additionalData: {
cost: number | null
latency: number | null
usage: {completion_tokens: number; prompt_tokens: number; total_tokens: number} | null
usage: {completion: number; prompt: number; total: number} | null
}
onInputParamChange: (paramName: string, newValue: any) => void
onRun: () => void
Expand Down Expand Up @@ -336,7 +336,7 @@ const BoxComponent: React.FC<BoxComponentProps> = ({
)}
{additionalData?.cost || additionalData?.latency ? (
<Space className="flex items-center gap-3">
<span>Tokens: {formatTokenUsage(additionalData?.usage?.total_tokens)}</span>
<span>Tokens: {formatTokenUsage(additionalData?.usage?.total)}</span>
<span>Cost: {formatCurrency(additionalData?.cost)}</span>
<span>Latency: {formatLatency(additionalData?.latency)}</span>
{traceSpans?.spans?.length && isDemo() && (
Expand Down Expand Up @@ -397,19 +397,18 @@ const App: React.FC<TestViewProps> = ({
Array<{
cost: number | null
latency: number | null
usage: {completion_tokens: number; prompt_tokens: number; total_tokens: number} | null
usage:
| {completion?: number; prompt?: number; total: number}
| {completion_tokens?: number; prompt_tokens?: number; total_tokens: number}
| null
}>
>(testList.map(() => ({cost: null, latency: null, usage: null})))
const [traceSpans, setTraceSpans] = useState<
| {
trace_id: string
cost?: number
latency?: number
usage?: {
completion_tokens: number
prompt_tokens: number
total_tokens: number
}
usage?: {completion: number; prompt: number; total: number}
spans?: BaseResponseSpans[]
}
| undefined
Expand Down Expand Up @@ -567,10 +566,10 @@ const App: React.FC<TestViewProps> = ({
// Check result type
// String, FuncResponse or BaseResponse
if (typeof result === "string") {
res = {version: "2.0", data: result} as BaseResponse
res = {version: "3.0", data: result} as BaseResponse
setResultForIndex(getStringOrJson(res.data), index)
} else if (isFuncResponse(result)) {
res = {version: "2.0", data: result.message} as BaseResponse
res = {version: "3.0", data: result.message} as BaseResponse
setResultForIndex(getStringOrJson(res.data), index)

const {message, cost, latency, usage} = result
Expand All @@ -583,13 +582,13 @@ const App: React.FC<TestViewProps> = ({
res = result as BaseResponse
setResultForIndex(getStringOrJson(res.data), index)

const {data, trace} = result
const trace = result.trace
setAdditionalDataList((prev) => {
const newDataList = [...prev]
newDataList[index] = {
cost: trace?.cost || null,
latency: trace?.latency || null,
usage: trace?.usage || null,
cost: trace[0]?.metrics?.acc?.costs?.total || null,
latency: trace[0]?.time?.span / 1_000_000 || null,
usage: trace[0]?.metrics?.acc?.tokens || null,
}
return newDataList
})
Expand Down
10 changes: 2 additions & 8 deletions agenta-web/src/lib/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ export type ComparisonResultRow = {
export type RequestMetadata = {
cost: number
latency: number
usage: {completion_tokens?: number; prompt_tokens?: number; total_tokens: number}
usage: {completion?: number; prompt?: number; total: number}
}

export type WithPagination<T> = {
Expand Down Expand Up @@ -547,13 +547,7 @@ export type FuncResponse = {
export type BaseResponse = {
version: string
data: string | Record<string, any>
trace?: {
trace_id: string
cost?: number
latency?: number
usage?: {completion_tokens: number; prompt_tokens: number; total_tokens: number}
spans?: BaseResponseSpans[]
}
trace?: Record<string, any>[]
}

export type BaseResponseSpans = {
Expand Down
2 changes: 1 addition & 1 deletion agenta-web/src/lib/helpers/playgroundResp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export function isFuncResponse(res: any): res is FuncResponse {
}

export function isBaseResponse(res: any): res is BaseResponse {
return res && res?.version === "2.0"
return res && res?.version
}
Loading