MCPcopy Index your code
hub / github.com/google/adk-python / chat

Function chat

contributing/samples/integrations/gcp_auth/client/main.py:127–317  ·  view source on GitHub ↗
(request: ChatRequest, response: Response)

Source from the content-addressed store, hash-verified

125# Endpoint for querying the agent.
126@app.post("/chat")
127async def chat(request: ChatRequest, response: Response):
128 session_id = request.session_id or str(uuid.uuid4())
129 current_agent = None
130 client = None
131
132 client = vertexai.Client(
133 project=request.project_id,
134 location=request.location,
135 )
136 remote_agent_name = (
137 f"projects/{request.project_id}/locations/{request.location}"
138 f"/reasoningEngines/{request.agent_id}"
139 )
140 try:
141 current_agent = client.agent_engines.get(name=remote_agent_name)
142 except Exception as e:
143 import traceback
144
145 tb_str = traceback.format_exc()
146 err_str = str(e)
147
148 async def error_generator():
149 err_data = {
150 "error": f"Failed to load remote agent: {err_str}",
151 "traceback": tb_str,
152 }
153 yield f"data: {json.dumps(err_data)}\n\n"
154
155 return StreamingResponse(error_generator(), media_type="text/event-stream")
156
157 if not request.session_id and current_agent:
158 try:
159 if hasattr(current_agent, "async_create_session"):
160 print(f"DEBUG: Creating async session for {request.user_id}")
161 session_obj = await current_agent.async_create_session(
162 user_id=request.user_id
163 )
164 else:
165 session_obj = current_agent.create_session(user_id=request.user_id)
166 session_id = (
167 session_obj.id
168 if hasattr(session_obj, "id")
169 else session_obj.get("id")
170 )
171
172 client = vertexai.Client(
173 project=request.project_id,
174 location=request.location,
175 )
176 current_agent = client.agent_engines.get(name=remote_agent_name)
177 except Exception as e:
178 import traceback
179
180 print(f"Failed to create session: {e}")
181 tb_str = traceback.format_exc()
182 err_str = str(e)
183
184 async def error_generator():

Callers

nothing calls this directly

Calls 4

error_generatorFunction · 0.85
event_generatorFunction · 0.85
getMethod · 0.45
create_sessionMethod · 0.45

Tested by

no test coverage detected