Apigee AI 网关为 ADK 智能体提供流量治理

通过 Apigee 代理暴露 Gemini 或 Agent Platform 模型端点,可在请求到达模型前执行安全、限流与缓存策略。ApigeeLlm 封装器接入 ADK 智能体,OpenAI 兼容模型则改用 CompletionsHTTPClient 转发。

## Appigee AI ADK 代理的网关[¶](<https://adk.dev/agents/models/apigee/#apigee-ai-gateway-for-adk-agents>) 在 ADKPython v1.18.0Java v0.4.0中支持 [阿皮盖](<https://docs.cloud.google.com/apigee/docs/api-platform/get-started/what-apigee>) 提供强大的 [AI 网关](<https://cloud.google.com/solutions/apigee-ai>),转换您如何管理和治理您的基因的 AI 模型流量。您通过 Apigee 代理来曝光您的 AI 模型端点( 如 Agent Platface 或 Gemini API ) , 您立即获得了企业级能力 : - **示范安全:** 执行保护威胁的 " 装甲模型 " 等安保政策。 - **交通治理:** 强制限制费率和托肯限制,以管理成本和防止滥用。 - **性能 :** 利用语义学缓存和先进的模型路由,提高响应时间和效率. - **监视可见度( V) :** 得到颗粒监测,分析和审计 所有你的AI请求。 那个... `ApigeeLlm` 包装器设计用于代理平台和双子座API(generateContent). 我们正在不断扩大对其他模型和接口的支持. 对于OpenAI兼容模型,包括自托管或其他提供商,使用该软件. `CompletionsHTTPClient` 以通过您的 Apigeee 代理服务器进行路由交通。 ## 执行实例[¶](<https://adk.dev/agents/models/apigee/#implementation-example>) 将Apigee的治理纳入您的代理工作流程 `ApigeeLlm` 包装对象并传递给一个 `LlmAgent` 或其他代理类型。 PythonJava ``` from google.adk.agents import LlmAgent from google.adk.models.apigee_llm import ApigeeLlm # Instantiate the ApigeeLlm wrapper model = ApigeeLlm( # Specify the Apigee route to your model. For more info, check out the ApigeeLlm documentation (https://github.com/google/adk-python/tree/main/contributing/samples/models/hello_world_apigeellm). model="apigee/gemini-flash-latest", # The proxy URL of your deployed Apigee proxy including the base path proxy_url=f"https://{APIGEE_PROXY_URL}", # Pass necessary authentication/authorization headers (like an API key) custom_headers={"foo": "bar"}, # Optional: Pass google-auth credentials if the proxy requires additional OAuth scopes # credentials=my_credentials ) # Pass the configured model wrapper to your LlmAgent agent = LlmAgent( model=model, name="my_governed_agent", instruction="You are a helpful assistant powered by Gemini and governed by Apigee.", # ... other agent parameters ) ``` ``` import com.google.adk.agents.LlmAgent; import com.google.adk.models.ApigeeLlm; import com.google.common.collect.ImmutableMap; ApigeeLlm apigeeLlm = ApigeeLlm.builder() .modelName("apigee/gemini-flash-latest") // Specify the Apigee route to your model. For more info, check out the ApigeeLlm documentation .proxyUrl(APIGEE_PROXY_URL) //The proxy URL of your deployed Apigee proxy including the base path .customHeaders(ImmutableMap.of("foo", "bar")) //Pass necessary authentication/authorization headers (like an API key) .build(); LlmAgent agent = LlmAgent.builder() .model(apigeeLlm) .name("my_governed_agent") .description("my_governed_agent") .instruction("You are a helpful assistant powered by Gemini and governed by Apigee.") // tools will be added next .build(); ``` 使用此配置, 您代理的每个 API 呼叫都会先通过 Apigee 进行路由, 在所有必要的策略( 安全性, 速率限制, 日志) 执行后, 请求才会安全地传输到 AI 模式端点 。 关于使用 Apigee 代理的完整代码示例, 请参见 。 [你好,世界之声LLM](<https://github.com/google/adk-python/tree/main/contributing/samples/models/hello_world_apigeellm>). ## 与 OpenAI 兼容[¶](<https://adk.dev/agents/models/apigee/#compatibility-with-openai>) 那个... `CompletionsHTTPClient` 是一个通用的 HTTP 客户端, 旨在与 OpenAI API 格式兼容。 它允许您通过期望标准 OpenAI 兼容的代理程序( 如 Apigee) 运行请求 。 `/chat/completions` 端点,而不是原生双子座或Vertex AI协议。此客户端处理 : - **有效载荷建造**: 将LlmRequest对象转换为OpenAI兼容API所要求的格式. - **反应处理**:管理代理服务器的流式和非流式响应. - **可靠性**用户 `tenacity` 以重试非流化请求, 但仅在您通过时进行 `retry_options=types.HttpRetryOptions(...)` 切换到构建器。默认情况下,每个请求都会尝试一次,流线请求永远不会被重审 。 - **正常化**: 将响应和流块分析为ADK框架其他部分所期望的标准格式. ### 执行实例[¶](<https://adk.dev/agents/models/apigee/#implementation-example_1>) ``` import asyncio from google.adk.models.apigee_llm import CompletionsHTTPClient from google.adk.models.llm_request import LlmRequest from google.genai import types async def test_client(): # 1. Initialize the client client = CompletionsHTTPClient( base_url="https://your-apigee-proxy-url.com/v1", headers={"Authorization": "Bearer YOUR_API_KEY"} ) # 2. Construct a minimal request request = LlmRequest( model="gpt-4o", # Replace with your target model ID contents=[types.Content(role="user", parts=[types.Part.from_text(text="Hello!")])] ) # 3. Execute a non-streaming generation async for response in client.generate_content_async(request, stream=False): if response.content and response.content.parts: print(f"Response: {response.content.parts[0].text}") if __name__ == "__main__": asyncio.run(test_client()) ```

查看原始来源