structure saas with tools
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
from typing import Optional
|
||||
|
||||
from ...agents.base_agent import BaseAgent
|
||||
from ...agents.llm_agent import LlmAgent
|
||||
|
||||
__all__ = [
|
||||
'create_empty_state',
|
||||
]
|
||||
|
||||
|
||||
def _create_empty_state(agent: BaseAgent, all_state: dict[str, Any]):
|
||||
for sub_agent in agent.sub_agents:
|
||||
_create_empty_state(sub_agent, all_state)
|
||||
|
||||
if (
|
||||
isinstance(agent, LlmAgent)
|
||||
and agent.instruction
|
||||
and isinstance(agent.instruction, str)
|
||||
):
|
||||
for key in re.findall(r'{([\w]+)}', agent.instruction):
|
||||
all_state[key] = ''
|
||||
|
||||
|
||||
def create_empty_state(
|
||||
agent: BaseAgent, initialized_states: Optional[dict[str, Any]] = None
|
||||
) -> dict[str, Any]:
|
||||
"""Creates empty str for non-initialized states."""
|
||||
non_initialized_states = {}
|
||||
_create_empty_state(agent, non_initialized_states)
|
||||
for key in initialized_states or {}:
|
||||
if key in non_initialized_states:
|
||||
del non_initialized_states[key]
|
||||
return non_initialized_states
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,54 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
logger = logging.getLogger(__file__)
|
||||
|
||||
|
||||
def _walk_to_root_until_found(folder, filename) -> str:
|
||||
checkpath = os.path.join(folder, filename)
|
||||
if os.path.exists(checkpath) and os.path.isfile(checkpath):
|
||||
return checkpath
|
||||
|
||||
parent_folder = os.path.dirname(folder)
|
||||
if parent_folder == folder: # reached the root
|
||||
return ''
|
||||
|
||||
return _walk_to_root_until_found(parent_folder, filename)
|
||||
|
||||
|
||||
def load_dotenv_for_agent(
|
||||
agent_name: str, agent_parent_folder: str, filename: str = '.env'
|
||||
):
|
||||
"""Lods the .env file for the agent module."""
|
||||
|
||||
# Gets the folder of agent_module as starting_folder
|
||||
starting_folder = os.path.abspath(
|
||||
os.path.join(agent_parent_folder, agent_name)
|
||||
)
|
||||
dotenv_file_path = _walk_to_root_until_found(starting_folder, filename)
|
||||
if dotenv_file_path:
|
||||
load_dotenv(dotenv_file_path, override=True, verbose=True)
|
||||
logger.info(
|
||||
'Loaded %s file for %s at %s',
|
||||
filename,
|
||||
agent_name,
|
||||
dotenv_file_path,
|
||||
)
|
||||
else:
|
||||
logger.info('No %s file found for %s', filename, agent_name)
|
||||
@@ -0,0 +1,93 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from typing import Any
|
||||
|
||||
from ...sessions.session import Session
|
||||
|
||||
|
||||
def convert_session_to_eval_format(session: Session) -> list[dict[str, Any]]:
|
||||
"""Converts a session data into eval format.
|
||||
|
||||
Args:
|
||||
session: The session that should be converted.
|
||||
|
||||
Returns:
|
||||
list: A single evaluation dataset in the required format.
|
||||
"""
|
||||
eval_case = []
|
||||
events = session.events if session and session.events else []
|
||||
|
||||
for event in events:
|
||||
if event.author == 'user':
|
||||
if not event.content or not event.content.parts:
|
||||
continue
|
||||
|
||||
# Extract user query
|
||||
content = event.content
|
||||
parts = content.parts
|
||||
|
||||
query = parts[0].text or ''
|
||||
|
||||
# Find the corresponding tool usage or response for the query
|
||||
expected_tool_use = []
|
||||
intermediate_agent_responses = []
|
||||
|
||||
# Check subsequent events to extract tool uses or responses for this turn.
|
||||
for subsequent_event in events[events.index(event) + 1 :]:
|
||||
event_author = subsequent_event.author or 'agent'
|
||||
if event_author == 'user':
|
||||
# We found an event where the author was the user. This means that a
|
||||
# new turn has started. So close this turn here.
|
||||
break
|
||||
|
||||
if not subsequent_event.content or not subsequent_event.content.parts:
|
||||
continue
|
||||
|
||||
for subsequent_part in subsequent_event.content.parts:
|
||||
# Some events have both function call and reference
|
||||
|
||||
if subsequent_part.function_call:
|
||||
tool_name = subsequent_part.function_call.name or ''
|
||||
tool_input = subsequent_part.function_call.args or {}
|
||||
expected_tool_use.append({
|
||||
'tool_name': tool_name,
|
||||
'tool_input': tool_input,
|
||||
})
|
||||
elif subsequent_part.text:
|
||||
# Also keep track of all the natural language responses that
|
||||
# agent (or sub agents) generated.
|
||||
intermediate_agent_responses.append(
|
||||
{'author': event_author, 'text': subsequent_part.text}
|
||||
)
|
||||
|
||||
# If we are here then either we are done reading all the events or we
|
||||
# encountered an event that had content authored by the end-user.
|
||||
# This, basically means an end of turn.
|
||||
# We assume that the last natural language intermediate response is the
|
||||
# final response from the agent/model. We treat that as a reference.
|
||||
eval_case.append({
|
||||
'query': query,
|
||||
'expected_tool_use': expected_tool_use,
|
||||
'expected_intermediate_agent_responses': intermediate_agent_responses[
|
||||
:-1
|
||||
],
|
||||
'reference': (
|
||||
intermediate_agent_responses[-1]['text']
|
||||
if intermediate_agent_responses
|
||||
else ''
|
||||
),
|
||||
})
|
||||
|
||||
return eval_case
|
||||
@@ -0,0 +1,72 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
LOGGING_FORMAT = (
|
||||
'%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s'
|
||||
)
|
||||
|
||||
|
||||
def log_to_stderr(level=logging.INFO):
|
||||
logging.basicConfig(
|
||||
level=level,
|
||||
format=LOGGING_FORMAT,
|
||||
)
|
||||
|
||||
|
||||
def log_to_tmp_folder(
|
||||
level=logging.INFO,
|
||||
*,
|
||||
sub_folder: str = 'agents_log',
|
||||
log_file_prefix: str = 'agent',
|
||||
log_file_timestamp: str = time.strftime('%Y%m%d_%H%M%S'),
|
||||
):
|
||||
"""Logs to system temp folder, instead of logging to stderr.
|
||||
|
||||
Args
|
||||
sub_folder: str = 'agents_log',
|
||||
log_file_prefix: str = 'agent',
|
||||
log_file_timestamp: str = time.strftime('%Y%m%d_%H%M%S'),
|
||||
|
||||
Returns
|
||||
the log file path.
|
||||
"""
|
||||
log_dir = os.path.join(tempfile.gettempdir(), sub_folder)
|
||||
log_filename = f'{log_file_prefix}.{log_file_timestamp}.log'
|
||||
log_filepath = os.path.join(log_dir, log_filename)
|
||||
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
|
||||
file_handler = logging.FileHandler(log_filepath, mode='w')
|
||||
file_handler.setLevel(level)
|
||||
file_handler.setFormatter(logging.Formatter(LOGGING_FORMAT))
|
||||
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(level)
|
||||
root_logger.handlers = [] # Clear handles to disable logging to stderr
|
||||
root_logger.addHandler(file_handler)
|
||||
|
||||
print(f'Log setup complete: {log_filepath}')
|
||||
|
||||
latest_log_link = os.path.join(log_dir, f'{log_file_prefix}.latest.log')
|
||||
if os.path.islink(latest_log_link):
|
||||
os.unlink(latest_log_link)
|
||||
os.symlink(log_filepath, latest_log_link)
|
||||
|
||||
print(f'To access latest log: tail -F {latest_log_link}')
|
||||
return log_filepath
|
||||
Reference in New Issue
Block a user