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
12 changes: 9 additions & 3 deletions veadk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .version import VERSION
from typing import TYPE_CHECKING

from veadk.version import VERSION

if TYPE_CHECKING:
from veadk.agent import Agent
from veadk.runner import Runner


# Lazy loading for `Agent` class
def __getattr__(name):
if name == "Agent":
from .agent import Agent
from veadk.agent import Agent

return Agent
if name == "Runner":
from .runner import Runner
from veadk.runner import Runner

return Runner
raise AttributeError(f"module 'veadk' has no attribute '{name}'")
Expand Down
17 changes: 10 additions & 7 deletions veadk/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Optional

from google.adk.agents import LlmAgent, RunConfig
from google.adk.agents.base_agent import BaseAgent
from google.adk.agents.llm_agent import ToolUnion
from google.adk.agents.run_config import StreamingMode
from google.adk.models.lite_llm import LiteLlm
Expand All @@ -39,7 +40,6 @@
from veadk.tracing.base_tracer import BaseTracer
from veadk.utils.logger import get_logger
from veadk.utils.patches import patch_asyncio
from google.adk.agents.base_agent import BaseAgent

patch_asyncio()
logger = get_logger(__name__)
Expand Down Expand Up @@ -70,9 +70,7 @@ class Agent(LlmAgent):
model_api_base: str = getenv("MODEL_AGENT_API_BASE", DEFAULT_MODEL_AGENT_API_BASE)
"""The api base of the model for agent running."""

model_api_key: str = Field(
..., default_factory=lambda: getenv("MODEL_AGENT_API_KEY")
)
model_api_key: str = Field(default_factory=lambda: getenv("MODEL_AGENT_API_KEY"))
"""The api key of the model for agent running."""

tools: list[ToolUnion] = []
Expand Down Expand Up @@ -244,8 +242,13 @@ async def run(
user_id=user_id,
session_id=session_id,
)
await self.long_term_memory.add_session_to_memory(session)
logger.info(f"Add session `{session.id}` to your long-term memory.")
if session:
await self.long_term_memory.add_session_to_memory(session)
logger.info(f"Add session `{session.id}` to your long-term memory.")
else:
logger.error(
f"Session {session_id} not found in session service, cannot save to long-term memory."
)

if collect_runtime_data:
eval_set_recorder = EvalSetRecorder(session_service, eval_set_id)
Expand All @@ -254,6 +257,6 @@ async def run(

if self.tracers:
for tracer in self.tracers:
tracer.dump(user_id, session_id)
tracer.dump(user_id=user_id, session_id=session_id)

return final_output
95 changes: 0 additions & 95 deletions veadk/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,70 +131,6 @@ def init():
set_variable_in_file(target_dir / "deploy.py", setting_values)


# @app.command()
# def web(
# path: str = typer.Option(".", "--path", help="Agent project path"),
# ):
# from google.adk.cli import cli_tools_click

# def my_decorator(func):
# @wraps(func)
# def wrapper(*args, **kwargs):
# adk_app: FastAPI = func(*args, **kwargs)
# import importlib.util
# import mimetypes

# from fastapi.staticfiles import StaticFiles

# mimetypes.add_type("application/javascript", ".js", True)
# mimetypes.add_type("text/javascript", ".js", True)

# spec = importlib.util.find_spec("veadk.cli.browser")
# if spec is not None:
# ANGULAR_DIST_PATH = spec.submodule_search_locations[0]
# logger.info(f"Static source path: {ANGULAR_DIST_PATH}")
# else:
# raise Exception("veadk.cli.browser not found")

# # ----- 8< Unmount app -----
# from starlette.routing import Mount

# for index, route in enumerate(adk_app.routes):
# if isinstance(route, Mount) and route.path == "/dev-ui":
# del adk_app.routes[index]
# break
# # ----- 8< Mount our app -----

# adk_app.mount(
# "/dev-ui/",
# StaticFiles(directory=ANGULAR_DIST_PATH, html=True),
# name="static",
# )

# from fastapi.middleware.cors import CORSMiddleware

# adk_app.add_middleware(
# CORSMiddleware,
# allow_origins=["*"],
# allow_credentials=True,
# allow_methods=["*"],
# allow_headers=["*"],
# )
# return adk_app

# return wrapper

# # Monkey patch
# fast_api.get_fast_api_app = my_decorator(fast_api.get_fast_api_app)

# # reload cli_tools_click
# importlib.reload(cli_tools_click)

# agents_dir = str(Path(path).resolve())
# logger.info(f"Agents dir is {agents_dir}")
# cli_tools_click.cli_web.main(args=[agents_dir])


@app.command()
def web(
session_service_uri: str = typer.Option(
Expand Down Expand Up @@ -325,19 +261,6 @@ def prompt(
ap.optimize(agents=agents, feedback=feedback, model_name=model_name)


# @app.command()
# def studio():
# import os

# # pre-load
# from veadk import Agent # noqa

# os.environ["VEADK_STUDIO_AGENTS_DIR"] = os.getcwd()
# app_path = os.path.join(os.path.dirname(__file__), "../../app/app.py")

# os.system(f"streamlit run {app_path}")


@app.command()
def deploy(
access_key: str = typer.Option(..., "--access-key", help="Access Key"),
Expand All @@ -352,24 +275,6 @@ def deploy(
vefaas.deploy(name=name, path=path)


@app.command()
def log(
access_key: str = typer.Option(..., "--access-key", help="Access Key"),
secret_key: str = typer.Option(..., "--secret-key", help="Secret Key"),
query: str = typer.Option(..., "--query", help="Query statement"),
topic_id: str = typer.Option(..., "--topic-id", help="Topic ID in VeTLS"),
dump_path: str = typer.Option(
".", "--dump-path", help="Local path for log storage file"
),
):
path = Path(dump_path).resolve()

from veadk.cli.services.vetls import VeTLS

vetls = VeTLS(access_key, secret_key, dump_path=str(path))
vetls.query(topic_id=topic_id, query=query)


@app.command()
def version():
print(f"VeADK {VERSION}")
Expand Down
3 changes: 1 addition & 2 deletions veadk/cli/services/agentpilot/agentpilot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@


class AgentPilot:
def __init__(self, api_key: str, path: str = "", task_id: str = None) -> None:
def __init__(self, api_key: str, path: str = "") -> None:
self.api_key = api_key
self.path = path
self.task_id = task_id

def optimize(
self,
Expand Down
1 change: 1 addition & 0 deletions veadk/cli/services/vefaas/vefaas.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def find_app_id_by_name(self, name: str):
for app in apps:
if app["Name"] == name:
return app["Id"]
logger.warning(f"Application with name {name} not found.")
return None

def delete(self, app_id: str):
Expand Down
17 changes: 0 additions & 17 deletions veadk/cli/services/vetls/__init__.py

This file was deleted.

87 changes: 0 additions & 87 deletions veadk/cli/services/vetls/vetls.py

This file was deleted.

4 changes: 4 additions & 0 deletions veadk/cloud/cloud_agent_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ def remove(self, app_name: str):
return
else:
app_id = self._vefaas_service.find_app_id_by_name(app_name)
if not app_id:
raise ValueError(
f"Cloud app {app_name} not found, cannot delete it. Please check the app name."
)
self._vefaas_service.delete(app_id)

def update_function_code(
Expand Down
23 changes: 19 additions & 4 deletions veadk/cloud/cloud_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
from typing import Any
from uuid import uuid4

import json
import httpx
from a2a.client import A2ACardResolver, A2AClient
from a2a.types import AgentCard, Message, MessageSendParams, SendMessageRequest
Expand Down Expand Up @@ -82,11 +82,18 @@ def _get_vefaas_endpoint(
from veadk.cli.services.vefaas.vefaas import VeFaaS

vefaas_client = VeFaaS(access_key=volcengine_ak, secret_key=volcengine_sk)

app = vefaas_client.get_application_details(
app_id=self.vefaas_application_id,
app_name=self.vefaas_application_name,
)

if not app:
raise ValueError(
f"VeFaaS CloudAPP with application_id `{self.vefaas_application_id}` or application_name `{self.vefaas_application_name}` not found."
)
cloud_resource = json.loads(app["CloudResource"])

try:
vefaas_endpoint = cloud_resource["framework"]["url"]["system_url"]
except Exception as e:
Expand Down Expand Up @@ -180,11 +187,19 @@ async def message_send(
id=uuid4().hex,
params=MessageSendParams(**send_message_payload),
)

res = await a2a_client.send_message(
message_send_request,
http_kwargs={"timeout": httpx.Timeout(timeout)},
)
return res.root.result

logger.debug(
f"Message sent to cloud app {self.vefaas_application_name} with response: {res}"
)

# we ignore type checking here, because the response
# from CloudApp will not be `Task` type
return res.root.result # type: ignore
except Exception as e:
# TODO(floritange): show error log on VeFaaS function
print(e)
Expand All @@ -194,7 +209,7 @@ async def message_send(
def get_message_id(message: Message):
"""Get the messageId of the a2a message"""
if getattr(message, "messageId", None):
# Compatible with the messageId of the old version
return message.messageId
# Compatible with the messageId of the old a2a-python version (<0.3.0) in cloud app
return message.messageId # type: ignore
else:
return message.message_id
Loading