Replies: 1 comment 1 reply
-
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: The Hello! That's a great question about streamlining local development with ADK. The For local development, the To connect your UI to this local server, you should use a standard HTTP client to send requests to the appropriate endpoint. The equivalent of the Here is an example of how you could adapt your code to connect to the local server using the import httpx
import json
# Configuration for your local agent
LOCAL_AGENT_URL = "http://localhost:8000/run_sse"
AGENT_APP_NAME = "your_agent_directory_name" # Change this to your agent's app name
async def stream_local_agent(user_id: str, session_id: str, message: str):
"""
Connects to a local adk api_server and streams the response.
"""
payload = {
"app_name": AGENT_APP_NAME,
"user_id": user_id,
"session_id": session_id,
"message": message,
}
headers = {
"Content-Type": "application/json",
"Accept": "text/event-stream",
}
try:
async with httpx.AsyncClient(timeout=None) as client:
async with client.stream("POST", LOCAL_AGENT_URL, json=payload, headers=headers) as response:
# Ensure the request was successful
response.raise_for_status()
print("Successfully connected to the local agent stream.")
async for line in response.aiter_lines():
if line.startswith("data:"):
try:
# The event data is in a JSON string, remove "data:" prefix
event_data = json.loads(line[5:])
# Now you can process the event like you did before
print(event_data)
# ... your event processing logic here ...
except json.JSONDecodeError:
print(f"Could not decode JSON from line: {line}")
except httpx.RequestError as e:
print(f"An error occurred while requesting {e.request.url!r}.")
print("Please ensure your 'adk api_server' is running.")
# Example usage:
# import asyncio
# asyncio.run(stream_local_agent(user_id="test_user", session_id="test_session", message="Hello agent!"))By using a conditional check in your code (e.g., based on an environment variable), you can easily switch between the I hope this helps you accelerate your development workflow! References:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using the vertexai.Client to connect my UI code to my adk agent running on vertex agent engine.
However, for development iteration speed, I would like to connect the same UI code to an agent running locally (presumably with
adk api_server).How should I adjust the client code to connect to the api_server?
Beta Was this translation helpful? Give feedback.
All reactions