您可以将 AI 代理实现为 Cloud Run 服务,用于编排一组异步任务,并通过多次请求-响应交互向用户提供信息。
Cloud Run 上的 AI 代理架构
Cloud Run 上部署的典型 AI 代理架构可能包括来自 Google Cloud 以及 Google Cloud外部的多个组件。
服务和编排:Cloud Run 服务作为可扩缩的 API 端点,可通过自动、按需、快速扩缩容来处理多个并发用户请求。此服务运行核心代理逻辑,通常采用 LangGraph 或智能体开发套件 (ADK) 等 AI 编排框架。此层用于协调对其他组件的调用。Cloud Run 支持使用 WebSockets 将 HTTP 响应流式传输回用户。Cloud Run 的内置服务身份可提供安全且自动的凭证,以便在无需管理 API 密钥的情况下调用 Google Cloud API。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Host AI apps and agents on Cloud Run\n\nThis page highlights some use cases for using Cloud Run as a\nhosting platform for the following AI use cases:\n\n- [AI applications](#ai-apps)\n- [AI agents](#ai-agents)\n\nHost AI applications on Cloud Run\n---------------------------------\n\nAI applications use AI models to operate or perform a specific task.\nFor example, an AI application can use an AI model to summarize documents, or be a chat interface that uses a vector database to retrieve more context.\n\nCloud Run is one of the [application hosting infrastructures](/docs/generative-ai/choose-models-infra-for-ai) that provides a fully managed environment for your AI application workloads.\nCloud Run integrates with AI models such as [Gemini API](/vertex-ai/generative-ai/docs/model-reference/inference), [Vertex AI endpoints](/vertex-ai/docs/general/deployment), or models hosted on [a GPU-enabled Cloud Run service](/run/docs/configuring/services/gpu).\nCloud Run also integrates with [Cloud SQL for PostgreSQL](/sql/docs/postgres/connect-run) and [AlloyDB for PostgreSQL](/alloydb/docs/quickstart/integrate-cloud-run), which are two databases offering the `pgvector` extension for Retrieval-Augmented Generation (RAG).\n\nHost AI Agents on Cloud Run\n---------------------------\n\nAI agents combine the intelligence of advanced AI models, with access to tools,\nto take actions on behalf of the user and under the user's control.\n\nYou can implement AI agents as Cloud Run services to orchestrate a set\nof asynchronous tasks and provide information to users, through involving multiple\nrequest-response interactions.\n\n### AI agent on Cloud Run architecture\n\nA typical AI agent architecture deployed on Cloud Run can involve\nseveral components from Google Cloud and outside of Google Cloud:\n\n1. **Serving and Orchestration:** A Cloud Run service acts as a scalable API endpoint, and can handle multiple concurrent users through automatic, on-demand, rapid scaling of instances. This service runs the core agent logic, often using an AI orchestration framework like [LangGraph](https://www.langchain.com/langgraph) or [Agent Development Kit (ADK)](https://google.github.io/adk-docs/). This layer coordinates calls to other components. Cloud Run supports [streaming HTTP responses](/run/docs/triggering/https-request#streaming) back to the user using [WebSockets](/run/docs/triggering/websockets). Cloud Run's built-in [service identity](/run/docs/securing/service-identity) provides secure and automatic credentials for calling Google Cloud APIs without managing API keys.\n\n2. **AI Models:** The orchestration layer calls models for reasoning capabilities. These can be:\n\n - The [Gemini API](/vertex-ai/generative-ai/docs/model-reference/inference)\n - Custom models or other foundation models deployed on [Vertex AI endpoints](/vertex-ai/docs/general/deployment)\n - Your own fine-tuned models served from a separate [GPU-enabled-Cloud Run service](/run/docs/configuring/services/gpu)\n3. **Memory:** Agents often need memory to retain context and learn from past interactions.\n\n - **Short-term memory** can be implemented by [connecting Cloud Run to Memorystore for Redis](/memorystore/docs/redis/connect-redis-instance-cloud-run).\n - **Long-term memory** for storing the conversational history or remembering the user's preferences can be implemented by connecting Cloud Run to [Firestore](/firestore/docs), a scalable, serverless NoSQL database.\n4. **Databases and Retrieval:** For Retrieval-Augmented Generation (RAG) or fetching structured data:\n\n - Query specific entity information or perform similarity searches over embeddings by connecting Cloud Run to vector databases like [Cloud SQL for PostgreSQL](/sql/docs/postgres/connect-run) or [AlloyDB for PostgreSQL](/alloydb/docs/quickstart/integrate-cloud-run) with the `pgvector` extension.\n5. **Tools:** The orchestrator uses tools to perform specific tasks that models are not suited for or to interact with external services, APIs, or websites. This can include:\n\n - Basic utilities: Precise math calculations, time conversions, or other similar utilities can run in the orchestrating Cloud Run service.\n - API calling: Make calls to other internal or third-party APIs (read or write access).\n - Image or chart generation: Use image generation models or run chart libraries to quickly and effectively create visual content.\n - Browser and OS automation: Run a headless or a full graphical Operating System within container instances to allow the agent to browse the web, extract information from websites, or perform actions using clicks and keyboard input. The Cloud Run service returns pixels of screens. Use libraries like [Puppeteer](https://pptr.dev/) to control the browser.\n - Code execution: Cloud Run provides a [secure environment with multi-layered sandboxing](/run/docs/securing/security#compute-security) and can be configured to the code execution service with minimal or no [IAM permissions](/run/docs/securing/service-identity). A [Cloud Run job](/run/docs/create-jobs) can be used to execute code asynchronously and a [Cloud Run service](/run/docs/deploying) with a [concurrency of 1](/run/docs/configuring/concurrency) can be used for synchronous execution.\n\nWhat's next\n-----------\n\n- Watch [Build AI agents on Cloud Run](https://www.youtube.com/watch?v=GwL8e5Z1tl4).\n- Try the [codelab](https://codelabs.developers.google.com/codelabs/build-and-deploy-a-langchain-app-on-cloud-run) for learning how to build and deploy a LangChain app to Cloud Run.\n- Learn how to [deploy Agent Development Kit (ADK) to Cloud Run](https://google.github.io/adk-docs/deploy/cloud-run/).\n- Find ready-to-use agent samples in [Agent Development Kit (ADK) samples](https://github.com/google/adk-samples).\n- [Host Model Context Protocol (MCP) servers on Cloud Run](/run/docs/host-mcp-servers)."]]