如何在Google ADK Agent中调用Vertex AI微调的Gemini模型
如何在Google ADK Agent中调用Vertex AI微调后的Gemini端点
解决方案概述
Google ADK的Agent类支持传入自定义的genai.Client实例,通过该客户端可连接到Vertex AI上的微调模型端点。需要修改agent.py中的客户端配置和Agent初始化参数,替换原API Key认证方式为Vertex AI身份验证。
修改后的完整agent.py代码
import os from google.adk.agents import Agent from google.adk.tools import google_search from google.adk.tools.agent_tool import AgentTool from google import genai from google.genai import types from google.adk.sessions import InMemorySessionService from google.adk.runners import Runner # --- 配置Vertex AI客户端 --- PROJECT_ID = "9456734556734" LOCATION = "us-central1" # 初始化Vertex AI客户端(依赖GCP身份验证,无需API Key) # 本地运行需设置GOOGLE_APPLICATION_CREDENTIALS指向服务账号JSON文件 client = genai.Client( vertexai=True, project=PROJECT_ID, location=LOCATION, ) # --- 配置生成参数(与微调代码保持一致) --- generation_config = types.GenerateContentConfig( temperature=0.7, top_p=0.95, max_output_tokens=8192, safety_settings=[ types.SafetySetting(category="HARM_CATEGORY_HATE_SPEECH", threshold="OFF"), types.SafetySetting(category="HARM_CATEGORY_DANGEROUS_CONTENT", threshold="OFF"), types.SafetySetting(category="HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold="OFF"), types.SafetySetting(category="HARM_CATEGORY_HARASSMENT", threshold="OFF"), ] ) # --- Root Agent --- root_agent = Agent( name="RootAgent", # 替换为你的微调模型端点路径 model="projects/9456734556734/locations/us-central1/endpoints/5797762931831894471", client=client, description="AI Agent", instruction=""" You are a helpful AI assistant. """, tools=[google_search], generation_config=generation_config ) # --- Root Agent Runner配置 --- APP_NAME = "snack_creations_app" USER_ID = "12345" SESSION_ID = "123344" session_service = InMemorySessionService() session = session_service.create_session( app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID ) runner = Runner( agent=root_agent, app_name=APP_NAME, session_service=session_service ) # Agent交互逻辑 def call_agent(query): content = types.Content(role="user", parts=[types.Part(text=query)]) events = runner.run(user_id=USER_ID, session_id=SESSION_ID, new_message=content) final_response = None for event in events: if event.is_final_response(): final_response = event.content.parts[0].text print("Agent Response: ", final_response) return final_response return final_response # 启动交互式对话 print("Starting the interactive conversation (type 'quit' to exit).") while True: user_query = input("You: ") if user_query.lower() == 'quit': break call_agent(user_query) print("\n") print("Conversation ended.")
关键修改点说明
- 替换认证方式:移除原
GOOGLE_API_KEY配置,改用Vertex AI客户端,依赖GCP身份验证(服务账号或环境默认凭据)。 - 传入自定义客户端:创建
genai.Client实例并传入Agent的client参数,让Agent使用Vertex AI的模型端点。 - 同步生成参数:将微调代码中的
GenerateContentConfig设置到Agent的generation_config参数,保持生成规则一致。 - 指定模型端点:将
Agent的model参数替换为你的Vertex AI微调模型端点路径。
注意事项
- GCP权限配置:确保运行代码的身份(服务账号或用户)拥有
aiplatform.endpoints.predict及Vertex AI相关访问权限。 - 依赖版本:安装最新版依赖避免兼容性问题:
pip install --upgrade google-adk google-genai - 本地身份验证:本地运行时设置环境变量指向服务账号JSON文件:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"
内容的提问来源于stack exchange,提问作者Seth
相关产品推荐
相关产品推荐

