Skip to content

Commit acb6cab

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: GenAI Client(evals) - Add loading agent info util function
PiperOrigin-RevId: 828342302
1 parent 37fa3ce commit acb6cab

File tree

4 files changed

+101
-11
lines changed

4 files changed

+101
-11
lines changed

tests/unit/vertexai/genai/replays/test_create_evaluation_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_create_eval_run_data_source_evaluation_set(client):
7676
LLM_METRIC,
7777
],
7878
agent_info=types.evals.AgentInfo(
79-
agent="project/123/locations/us-central1/reasoningEngines/456",
79+
agent_resource_name="project/123/locations/us-central1/reasoningEngines/456",
8080
name="agent-1",
8181
instruction="agent-1 instruction",
8282
tool_declarations=[tool],

tests/unit/vertexai/genai/test_evals.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,40 @@ def test_agent_info_creation(self):
27822782
assert agent_info.description == "description1"
27832783
assert agent_info.tool_declarations == [tool]
27842784

2785+
@mock.patch.object(genai_types.FunctionDeclaration, "from_callable_with_api_option")
2786+
def test_load_from_agent(self, mock_from_callable):
2787+
def my_search_tool(query: str) -> str:
2788+
"""Searches for information."""
2789+
return f"search result for {query}"
2790+
2791+
mock_function_declaration = mock.Mock(spec=genai_types.FunctionDeclaration)
2792+
mock_from_callable.return_value = mock_function_declaration
2793+
2794+
mock_agent = mock.Mock()
2795+
mock_agent.name = "mock_agent"
2796+
mock_agent.instruction = "mock instruction"
2797+
mock_agent.description = "mock description"
2798+
mock_agent.tools = [my_search_tool]
2799+
2800+
agent_info = vertexai_genai_types.evals.AgentInfo.load_from_agent(
2801+
agent=mock_agent,
2802+
agent_resource_name="projects/123/locations/abc/reasoningEngines/456",
2803+
)
2804+
2805+
assert agent_info.name == "mock_agent"
2806+
assert agent_info.instruction == "mock instruction"
2807+
assert agent_info.description == "mock description"
2808+
assert (
2809+
agent_info.agent_resource_name
2810+
== "projects/123/locations/abc/reasoningEngines/456"
2811+
)
2812+
assert len(agent_info.tool_declarations) == 1
2813+
assert isinstance(agent_info.tool_declarations[0], genai_types.Tool)
2814+
assert agent_info.tool_declarations[0].function_declarations == [
2815+
mock_function_declaration
2816+
]
2817+
mock_from_callable.assert_called_once_with(callable=my_search_tool)
2818+
27852819

27862820
class TestEvent:
27872821
"""Unit tests for the Event class."""

vertexai/_genai/evals.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,11 +1405,11 @@ def create_evaluation_run(
14051405
tools=agent_info.tool_declarations,
14061406
)
14071407
)
1408-
if agent_info.agent:
1408+
if agent_info.agent_resource_name:
14091409
labels = labels or {}
1410-
labels["vertex-ai-evaluation-agent-engine-id"] = agent_info.agent.split(
1411-
"reasoningEngines/"
1412-
)[-1]
1410+
labels["vertex-ai-evaluation-agent-engine-id"] = (
1411+
agent_info.agent_resource_name.split("reasoningEngines/")[-1]
1412+
)
14131413
if not name:
14141414
name = f"evaluation_run_{uuid.uuid4()}"
14151415

@@ -2252,11 +2252,11 @@ async def create_evaluation_run(
22522252
tools=agent_info.tool_declarations,
22532253
)
22542254
)
2255-
if agent_info.agent:
2255+
if agent_info.agent_resource_name:
22562256
labels = labels or {}
2257-
labels["vertex-ai-evaluation-agent-engine-id"] = agent_info.agent.split(
2258-
"reasoningEngines/"
2259-
)[-1]
2257+
labels["vertex-ai-evaluation-agent-engine-id"] = (
2258+
agent_info.agent_resource_name.split("reasoningEngines/")[-1]
2259+
)
22602260
if not name:
22612261
name = f"evaluation_run_{uuid.uuid4()}"
22622262

vertexai/_genai/types/evals.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Importance(_common.CaseInSensitiveEnum):
3939
class AgentInfo(_common.BaseModel):
4040
"""The agent info of an agent, used for agent eval."""
4141

42-
agent: Optional[str] = Field(
42+
agent_resource_name: Optional[str] = Field(
4343
default=None,
4444
description="""The agent engine used to run agent. Agent engine resource name in str type, with format
4545
`projects/{project}/locations/{location}/reasoningEngines/{reasoning_engine_id}`.""",
@@ -57,11 +57,67 @@ class AgentInfo(_common.BaseModel):
5757
default=None, description="""List of tools used by the Agent."""
5858
)
5959

60+
@staticmethod
61+
def _get_tool_declarations_from_agent(agent: Any) -> genai_types.ToolListUnion:
62+
"""Gets tool declarations from an agent.
63+
64+
Args:
65+
agent: The agent to get the tool declarations from. Data type is google.adk.agents.LLMAgent type, use Any to avoid dependency on ADK.
66+
67+
Returns:
68+
The tool declarations of the agent.
69+
"""
70+
tool_declarations: genai_types.ToolListUnion = []
71+
for tool in agent.tools:
72+
tool_declarations.append(
73+
{
74+
"function_declarations": [
75+
genai_types.FunctionDeclaration.from_callable_with_api_option(
76+
callable=tool
77+
)
78+
]
79+
}
80+
)
81+
return tool_declarations
82+
83+
@classmethod
84+
def load_from_agent(
85+
cls, agent: Any, agent_resource_name: Optional[str] = None
86+
) -> "AgentInfo":
87+
"""Loads agent info from an agent.
88+
89+
Args:
90+
agent: The agent to get the agent info from, data type is google.adk.agents.LLMAgent type, use Any to avoid dependency on ADK.
91+
agent_resource_name: Optional. The agent engine resource name.
92+
93+
Returns:
94+
The agent info of the agent.
95+
96+
Example:
97+
```
98+
from vertexai._genai import types
99+
100+
# Assuming 'my_agent' is an instance of google.adk.agents.LLMAgent
101+
102+
agent_info = types.evals.AgentInfo.load_from_agent(
103+
agent=my_agent,
104+
agent_resource_name="projects/123/locations/us-central1/reasoningEngines/456"
105+
)
106+
```
107+
"""
108+
return cls( # pytype: disable=missing-parameter
109+
name=agent.name,
110+
agent_resource_name=agent_resource_name,
111+
instruction=agent.instruction,
112+
description=agent.description,
113+
tool_declarations=AgentInfo._get_tool_declarations_from_agent(agent),
114+
)
115+
60116

61117
class AgentInfoDict(TypedDict, total=False):
62118
"""The agent info of an agent, used for agent eval."""
63119

64-
agent: Optional[str]
120+
agent_resource_name: Optional[str]
65121
"""The agent engine used to run agent. Agent engine resource name in str type, with format
66122
`projects/{project}/locations/{location}/reasoningEngines/{reasoning_engine_id}`."""
67123

0 commit comments

Comments
 (0)