Skip to content

Commit ee188f2

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
fix: resolve SessionInput state serialization to cloud evaluation service
PiperOrigin-RevId: 924891381
1 parent 3afcef0 commit ee188f2

3 files changed

Lines changed: 206 additions & 83 deletions

File tree

agentplatform/_genai/evals.py

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,56 @@
4343
logger = logging.getLogger("agentplatform_genai.evals")
4444

4545

46+
def _AgentRunConfig_from_vertex(
47+
from_object: Union[dict[str, Any], object],
48+
parent_object: Optional[dict[str, Any]] = None,
49+
) -> dict[str, Any]:
50+
to_object: dict[str, Any] = {}
51+
if getv(from_object, ["sessionInput"]) is not None:
52+
setv(
53+
to_object,
54+
["session_input"],
55+
_SessionInput_from_vertex(getv(from_object, ["sessionInput"]), to_object),
56+
)
57+
58+
if getv(from_object, ["agentEngine"]) is not None:
59+
setv(to_object, ["agent_engine"], getv(from_object, ["agentEngine"]))
60+
61+
if getv(from_object, ["userSimulatorConfig"]) is not None:
62+
setv(
63+
to_object,
64+
["user_simulator_config"],
65+
getv(from_object, ["userSimulatorConfig"]),
66+
)
67+
68+
return to_object
69+
70+
71+
def _AgentRunConfig_to_vertex(
72+
from_object: Union[dict[str, Any], object],
73+
parent_object: Optional[dict[str, Any]] = None,
74+
) -> dict[str, Any]:
75+
to_object: dict[str, Any] = {}
76+
if getv(from_object, ["session_input"]) is not None:
77+
setv(
78+
to_object,
79+
["sessionInput"],
80+
_SessionInput_to_vertex(getv(from_object, ["session_input"]), to_object),
81+
)
82+
83+
if getv(from_object, ["agent_engine"]) is not None:
84+
setv(to_object, ["agentEngine"], getv(from_object, ["agent_engine"]))
85+
86+
if getv(from_object, ["user_simulator_config"]) is not None:
87+
setv(
88+
to_object,
89+
["userSimulatorConfig"],
90+
getv(from_object, ["user_simulator_config"]),
91+
)
92+
93+
return to_object
94+
95+
4696
def _CreateEvaluationItemParameters_to_vertex(
4797
from_object: Union[dict[str, Any], object],
4898
parent_object: Optional[dict[str, Any]] = None,
@@ -137,6 +187,9 @@ def _CreateEvaluationRunParameters_to_vertex(
137187
[item for item in getv(from_object, ["analysis_configs"])],
138188
)
139189

190+
if getv(from_object, ["dummy_session_input"]) is not None:
191+
_SessionInput_to_vertex(getv(from_object, ["dummy_session_input"]), to_object)
192+
140193
return to_object
141194

142195

@@ -464,7 +517,13 @@ def _EvaluationRunInferenceConfig_from_vertex(
464517
setv(to_object, ["prompt_template"], getv(from_object, ["promptTemplate"]))
465518

466519
if getv(from_object, ["agentRunConfig"]) is not None:
467-
setv(to_object, ["agent_run_config"], getv(from_object, ["agentRunConfig"]))
520+
setv(
521+
to_object,
522+
["agent_run_config"],
523+
_AgentRunConfig_from_vertex(
524+
getv(from_object, ["agentRunConfig"]), to_object
525+
),
526+
)
468527

469528
if getv(from_object, ["agents"]) is not None:
470529
setv(to_object, ["agent_configs"], getv(from_object, ["agents"]))
@@ -487,7 +546,13 @@ def _EvaluationRunInferenceConfig_to_vertex(
487546
setv(to_object, ["promptTemplate"], getv(from_object, ["prompt_template"]))
488547

489548
if getv(from_object, ["agent_run_config"]) is not None:
490-
setv(to_object, ["agentRunConfig"], getv(from_object, ["agent_run_config"]))
549+
setv(
550+
to_object,
551+
["agentRunConfig"],
552+
_AgentRunConfig_to_vertex(
553+
getv(from_object, ["agent_run_config"]), to_object
554+
),
555+
)
491556

492557
if getv(from_object, ["agent_configs"]) is not None:
493558
setv(to_object, ["agents"], getv(from_object, ["agent_configs"]))
@@ -902,6 +967,40 @@ def _RubricBasedMetricSpec_to_vertex(
902967
return to_object
903968

904969

970+
def _SessionInput_from_vertex(
971+
from_object: Union[dict[str, Any], object],
972+
parent_object: Optional[dict[str, Any]] = None,
973+
) -> dict[str, Any]:
974+
to_object: dict[str, Any] = {}
975+
if getv(from_object, ["userId"]) is not None:
976+
setv(to_object, ["user_id"], getv(from_object, ["userId"]))
977+
978+
if getv(from_object, ["sessionState"]) is not None:
979+
setv(to_object, ["state"], getv(from_object, ["sessionState"]))
980+
981+
if getv(from_object, ["parameters", "app_name"]) is not None:
982+
setv(to_object, ["app_name"], getv(from_object, ["parameters", "app_name"]))
983+
984+
return to_object
985+
986+
987+
def _SessionInput_to_vertex(
988+
from_object: Union[dict[str, Any], object],
989+
parent_object: Optional[dict[str, Any]] = None,
990+
) -> dict[str, Any]:
991+
to_object: dict[str, Any] = {}
992+
if getv(from_object, ["user_id"]) is not None:
993+
setv(to_object, ["userId"], getv(from_object, ["user_id"]))
994+
995+
if getv(from_object, ["state"]) is not None:
996+
setv(to_object, ["sessionState"], getv(from_object, ["state"]))
997+
998+
if getv(from_object, ["app_name"]) is not None:
999+
setv(to_object, ["parameters", "app_name"], getv(from_object, ["app_name"]))
1000+
1001+
return to_object
1002+
1003+
9051004
def _UnifiedMetric_from_vertex(
9061005
from_object: Union[dict[str, Any], object],
9071006
parent_object: Optional[dict[str, Any]] = None,
@@ -1174,6 +1273,10 @@ def _create_evaluation_run(
11741273
] = None,
11751274
config: Optional[types.CreateEvaluationRunConfigOrDict] = None,
11761275
analysis_configs: Optional[list[types.AnalysisConfigOrDict]] = None,
1276+
dummy_session_input: Optional[types.evals.SessionInputOrDict] = None,
1277+
dummy_user_simulator_config: Optional[
1278+
types.evals.UserSimulatorConfigOrDict
1279+
] = None,
11771280
) -> types.EvaluationRun:
11781281
"""
11791282
Creates an EvaluationRun.
@@ -1188,6 +1291,8 @@ def _create_evaluation_run(
11881291
inference_configs=inference_configs,
11891292
config=config,
11901293
analysis_configs=analysis_configs,
1294+
dummy_session_input=dummy_session_input,
1295+
dummy_user_simulator_config=dummy_user_simulator_config,
11911296
)
11921297

11931298
request_url_dict: Optional[dict[str, str]]
@@ -3321,6 +3426,10 @@ async def _create_evaluation_run(
33213426
] = None,
33223427
config: Optional[types.CreateEvaluationRunConfigOrDict] = None,
33233428
analysis_configs: Optional[list[types.AnalysisConfigOrDict]] = None,
3429+
dummy_session_input: Optional[types.evals.SessionInputOrDict] = None,
3430+
dummy_user_simulator_config: Optional[
3431+
types.evals.UserSimulatorConfigOrDict
3432+
] = None,
33243433
) -> types.EvaluationRun:
33253434
"""
33263435
Creates an EvaluationRun.
@@ -3335,6 +3444,8 @@ async def _create_evaluation_run(
33353444
inference_configs=inference_configs,
33363445
config=config,
33373446
analysis_configs=analysis_configs,
3447+
dummy_session_input=dummy_session_input,
3448+
dummy_user_simulator_config=dummy_user_simulator_config,
33383449
)
33393450

33403451
request_url_dict: Optional[dict[str, str]]

agentplatform/_genai/types/common.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,13 +2560,13 @@ class AgentRunConfig(_common.BaseModel):
25602560
class AgentRunConfigDict(TypedDict, total=False):
25612561
"""Configuration for an Agent Run."""
25622562

2563-
session_input: Optional[evals_types.SessionInput]
2563+
session_input: Optional[evals_types.SessionInputDict]
25642564
"""The session input to get agent running results."""
25652565

25662566
agent_engine: Optional[str]
25672567
"""The resource name of the Agent Engine."""
25682568

2569-
user_simulator_config: Optional[evals_types.UserSimulatorConfig]
2569+
user_simulator_config: Optional[evals_types.UserSimulatorConfigDict]
25702570
"""Used for multi-turn agent run.
25712571
Contains configuration for a user simulator that
25722572
uses an LLM to generate messages on behalf of the user."""
@@ -2765,6 +2765,12 @@ class _CreateEvaluationRunParameters(_common.BaseModel):
27652765
analysis_configs: Optional[list[AnalysisConfig]] = Field(
27662766
default=None, description=""""""
27672767
)
2768+
dummy_session_input: Optional[evals_types.SessionInput] = Field(
2769+
default=None, description=""""""
2770+
)
2771+
dummy_user_simulator_config: Optional[evals_types.UserSimulatorConfig] = Field(
2772+
default=None, description=""""""
2773+
)
27682774

27692775

27702776
class _CreateEvaluationRunParametersDict(TypedDict, total=False):
@@ -2794,6 +2800,12 @@ class _CreateEvaluationRunParametersDict(TypedDict, total=False):
27942800
analysis_configs: Optional[list[AnalysisConfigDict]]
27952801
""""""
27962802

2803+
dummy_session_input: Optional[evals_types.SessionInputDict]
2804+
""""""
2805+
2806+
dummy_user_simulator_config: Optional[evals_types.UserSimulatorConfigDict]
2807+
""""""
2808+
27972809

27982810
_CreateEvaluationRunParametersOrDict = Union[
27992811
_CreateEvaluationRunParameters, _CreateEvaluationRunParametersDict

agentplatform/_genai/types/evals.py

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,85 @@ class Importance(_common.CaseInSensitiveEnum):
3636
"""Low importance."""
3737

3838

39+
class SessionInput(_common.BaseModel):
40+
"""This field is experimental and may change in future versions.
41+
42+
Input to initialize a session and run an agent, used for agent evaluation.
43+
"""
44+
45+
user_id: Optional[str] = Field(default=None, description="""The user id.""")
46+
state: Optional[dict[str, str]] = Field(
47+
default=None, description="""The state of the session."""
48+
)
49+
app_name: Optional[str] = Field(
50+
default=None,
51+
description="""The name of the app, used for local ADK agent run Runner and Session.""",
52+
)
53+
54+
55+
class SessionInputDict(TypedDict, total=False):
56+
"""This field is experimental and may change in future versions.
57+
58+
Input to initialize a session and run an agent, used for agent evaluation.
59+
"""
60+
61+
user_id: Optional[str]
62+
"""The user id."""
63+
64+
state: Optional[dict[str, str]]
65+
"""The state of the session."""
66+
67+
app_name: Optional[str]
68+
"""The name of the app, used for local ADK agent run Runner and Session."""
69+
70+
71+
SessionInputOrDict = Union[SessionInput, SessionInputDict]
72+
73+
74+
class UserSimulatorConfig(_common.BaseModel):
75+
"""Configuration for a user simulator.
76+
77+
Uses an LLM to generate multi-turn messages that simulate a user.
78+
"""
79+
80+
model_name: Optional[str] = Field(
81+
default=None,
82+
description="""The model name to get next user message for multi-turn agent run.""",
83+
)
84+
model_configuration: Optional[genai_types.GenerateContentConfig] = Field(
85+
default=None, description="""The configuration for the model."""
86+
)
87+
max_turn: Optional[int] = Field(
88+
default=None,
89+
description="""Maximum number of invocations allowed by the multi-turn agent
90+
running. This property allows us to stop a run-off conversation
91+
where the agent and the user simulator get into a never ending loop.
92+
The initial fixed prompt is also counted as an invocation.""",
93+
)
94+
95+
96+
class UserSimulatorConfigDict(TypedDict, total=False):
97+
"""Configuration for a user simulator.
98+
99+
Uses an LLM to generate multi-turn messages that simulate a user.
100+
"""
101+
102+
model_name: Optional[str]
103+
"""The model name to get next user message for multi-turn agent run."""
104+
105+
model_configuration: Optional[genai_types.GenerateContentConfigDict]
106+
"""The configuration for the model."""
107+
108+
max_turn: Optional[int]
109+
"""Maximum number of invocations allowed by the multi-turn agent
110+
running. This property allows us to stop a run-off conversation
111+
where the agent and the user simulator get into a never ending loop.
112+
The initial fixed prompt is also counted as an invocation."""
113+
114+
115+
UserSimulatorConfigOrDict = Union[UserSimulatorConfig, UserSimulatorConfigDict]
116+
117+
39118
class AgentConfig(_common.BaseModel):
40119
"""Represents configuration for an Agent."""
41120

@@ -456,41 +535,6 @@ class AgentInfoDict(TypedDict, total=False):
456535
AgentInfoOrDict = Union[AgentInfo, AgentInfoDict]
457536

458537

459-
class SessionInput(_common.BaseModel):
460-
"""This field is experimental and may change in future versions.
461-
462-
Input to initialize a session and run an agent, used for agent evaluation.
463-
"""
464-
465-
user_id: Optional[str] = Field(default=None, description="""The user id.""")
466-
state: Optional[dict[str, str]] = Field(
467-
default=None, description="""The state of the session."""
468-
)
469-
app_name: Optional[str] = Field(
470-
default=None,
471-
description="""The name of the app, used for local ADK agent run Runner and Session.""",
472-
)
473-
474-
475-
class SessionInputDict(TypedDict, total=False):
476-
"""This field is experimental and may change in future versions.
477-
478-
Input to initialize a session and run an agent, used for agent evaluation.
479-
"""
480-
481-
user_id: Optional[str]
482-
"""The user id."""
483-
484-
state: Optional[dict[str, str]]
485-
"""The state of the session."""
486-
487-
app_name: Optional[str]
488-
"""The name of the app, used for local ADK agent run Runner and Session."""
489-
490-
491-
SessionInputOrDict = Union[SessionInput, SessionInputDict]
492-
493-
494538
class UserScenario(_common.BaseModel):
495539
"""User scenario to help simulate multi-turn agent run results."""
496540

@@ -586,50 +630,6 @@ class UserScenarioGenerationConfigDict(TypedDict, total=False):
586630
]
587631

588632

589-
class UserSimulatorConfig(_common.BaseModel):
590-
"""Configuration for a user simulator.
591-
592-
Uses an LLM to generate multi-turn messages that simulate a user.
593-
"""
594-
595-
model_name: Optional[str] = Field(
596-
default=None,
597-
description="""The model name to get next user message for multi-turn agent run.""",
598-
)
599-
model_configuration: Optional[genai_types.GenerateContentConfig] = Field(
600-
default=None, description="""The configuration for the model."""
601-
)
602-
max_turn: Optional[int] = Field(
603-
default=None,
604-
description="""Maximum number of invocations allowed by the multi-turn agent
605-
running. This property allows us to stop a run-off conversation
606-
where the agent and the user simulator get into a never ending loop.
607-
The initial fixed prompt is also counted as an invocation.""",
608-
)
609-
610-
611-
class UserSimulatorConfigDict(TypedDict, total=False):
612-
"""Configuration for a user simulator.
613-
614-
Uses an LLM to generate multi-turn messages that simulate a user.
615-
"""
616-
617-
model_name: Optional[str]
618-
"""The model name to get next user message for multi-turn agent run."""
619-
620-
model_configuration: Optional[genai_types.GenerateContentConfigDict]
621-
"""The configuration for the model."""
622-
623-
max_turn: Optional[int]
624-
"""Maximum number of invocations allowed by the multi-turn agent
625-
running. This property allows us to stop a run-off conversation
626-
where the agent and the user simulator get into a never ending loop.
627-
The initial fixed prompt is also counted as an invocation."""
628-
629-
630-
UserSimulatorConfigOrDict = Union[UserSimulatorConfig, UserSimulatorConfigDict]
631-
632-
633633
class Event(_common.BaseModel):
634634
"""Represents an event in a conversation between agents and users.
635635

0 commit comments

Comments
 (0)