Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_create_eval_run_data_source_evaluation_set(client):
LLM_METRIC,
],
agent_info=types.evals.AgentInfo(
agent="project/123/locations/us-central1/reasoningEngines/456",
agent_resource_name="project/123/locations/us-central1/reasoningEngines/456",
name="agent-1",
instruction="agent-1 instruction",
tool_declarations=[tool],
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/vertexai/genai/test_evals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,40 @@ def test_agent_info_creation(self):
assert agent_info.description == "description1"
assert agent_info.tool_declarations == [tool]

@mock.patch.object(genai_types.FunctionDeclaration, "from_callable_with_api_option")
def test_load_from_agent(self, mock_from_callable):
def my_search_tool(query: str) -> str:
"""Searches for information."""
return f"search result for {query}"

mock_function_declaration = mock.Mock(spec=genai_types.FunctionDeclaration)
mock_from_callable.return_value = mock_function_declaration

mock_agent = mock.Mock()
mock_agent.name = "mock_agent"
mock_agent.instruction = "mock instruction"
mock_agent.description = "mock description"
mock_agent.tools = [my_search_tool]

agent_info = vertexai_genai_types.evals.AgentInfo.load_from_agent(
agent=mock_agent,
agent_resource_name="projects/123/locations/abc/reasoningEngines/456",
)

assert agent_info.name == "mock_agent"
assert agent_info.instruction == "mock instruction"
assert agent_info.description == "mock description"
assert (
agent_info.agent_resource_name
== "projects/123/locations/abc/reasoningEngines/456"
)
assert len(agent_info.tool_declarations) == 1
assert isinstance(agent_info.tool_declarations[0], genai_types.Tool)
assert agent_info.tool_declarations[0].function_declarations == [
mock_function_declaration
]
mock_from_callable.assert_called_once_with(callable=my_search_tool)


class TestEvent:
"""Unit tests for the Event class."""
Expand Down
16 changes: 8 additions & 8 deletions vertexai/_genai/evals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,11 +1405,11 @@ def create_evaluation_run(
tools=agent_info.tool_declarations,
)
)
if agent_info.agent:
if agent_info.agent_resource_name:
labels = labels or {}
labels["vertex-ai-evaluation-agent-engine-id"] = agent_info.agent.split(
"reasoningEngines/"
)[-1]
labels["vertex-ai-evaluation-agent-engine-id"] = (
agent_info.agent_resource_name.split("reasoningEngines/")[-1]
)
if not name:
name = f"evaluation_run_{uuid.uuid4()}"

Expand Down Expand Up @@ -2252,11 +2252,11 @@ async def create_evaluation_run(
tools=agent_info.tool_declarations,
)
)
if agent_info.agent:
if agent_info.agent_resource_name:
labels = labels or {}
labels["vertex-ai-evaluation-agent-engine-id"] = agent_info.agent.split(
"reasoningEngines/"
)[-1]
labels["vertex-ai-evaluation-agent-engine-id"] = (
agent_info.agent_resource_name.split("reasoningEngines/")[-1]
)
if not name:
name = f"evaluation_run_{uuid.uuid4()}"

Expand Down
60 changes: 58 additions & 2 deletions vertexai/_genai/types/evals.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Importance(_common.CaseInSensitiveEnum):
class AgentInfo(_common.BaseModel):
"""The agent info of an agent, used for agent eval."""

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

@staticmethod
def _get_tool_declarations_from_agent(agent: Any) -> genai_types.ToolListUnion:
"""Gets tool declarations from an agent.

Args:
agent: The agent to get the tool declarations from. Data type is google.adk.agents.LLMAgent type, use Any to avoid dependency on ADK.

Returns:
The tool declarations of the agent.
"""
tool_declarations: genai_types.ToolListUnion = []
for tool in agent.tools:
tool_declarations.append(
{
"function_declarations": [
genai_types.FunctionDeclaration.from_callable_with_api_option(
callable=tool
)
]
}
)
return tool_declarations

@classmethod
def load_from_agent(
cls, agent: Any, agent_resource_name: Optional[str] = None
) -> "AgentInfo":
"""Loads agent info from an agent.

Args:
agent: The agent to get the agent info from, data type is google.adk.agents.LLMAgent type, use Any to avoid dependency on ADK.
agent_resource_name: Optional. The agent engine resource name.

Returns:
The agent info of the agent.

Example:
```
from vertexai._genai import types

# Assuming 'my_agent' is an instance of google.adk.agents.LLMAgent

agent_info = types.evals.AgentInfo.load_from_agent(
agent=my_agent,
agent_resource_name="projects/123/locations/us-central1/reasoningEngines/456"
)
```
"""
return cls( # pytype: disable=missing-parameter
name=agent.name,
agent_resource_name=agent_resource_name,
instruction=agent.instruction,
description=agent.description,
tool_declarations=AgentInfo._get_tool_declarations_from_agent(agent),
)


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

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

Expand Down
Loading