Reasoning Engine API

Reasoning Engine API 为生成式 AI 应用中的自定义代理工作流提供托管式运行时。您可以使用 LangChain 等编排框架创建应用,并使用 Reasoning Engine 进行部署。此服务具有 Vertex AI 集成的所有安全性、隐私性、可观测性和可伸缩性优势。

如需详细了解 Reasoning Engine 的概念性信息,请参阅部署应用

局限性

  • Reasoning Engine API 仅支持 Python 编排框架。
  • us-central1 区域支持 Reasoning Engine API。

示例语法

用于创建和注册推理引擎资源的语法。

Python

class SimpleAdditionApp:
    def query() -> str:
        """
           ...

        """

        return
...

reasoning_engine = reasoning_engines.ReasoningEngine.create(
    SimpleAdditionApp(),
    display_name="",
    description="",
    requirements=[...],
    extra_packages=[...],
)

参数列表

参数
display_name

必需:string

ReasoningEngine 的显示名称。

description

可选:string

ReasoningEngine 的说明。

spec

必需:ReasoningEngineSpec

ReasoningEngine 的配置。

package_spec

必需:PackageSpec

用户提供的软件包规范,例如序列化的对象和软件包要求。

class_methods

可选:protobuf.Struct

对象类方法的声明。

PackageSpec

PackageSpec 包含对存储 OpenAPI YAML 文件的 Cloud Storage URI 的引用。

参数
pickle_object_gcs_uri

可选:string

序列化的 Python 对象的 Cloud Storage URI。

dependency_files_gcs_uri

可选:string

扩展名为 tar.gz 的依赖项文件的 Cloud Storage URI。

requirements_gcs_uri

可选:string

requirements.txt 文件的 Cloud Storage URI。

python_version

可选:string

Python 版本。支持的版本包括 Python 3.83.93.103.11。 如果未指定,则默认值为 3.10

QueryReasoningEngine

参数
input

protobuf.struct

input 中的参数应与创建步骤中定义的 query 类方法一致。

示例

部署基本应用配置

以下示例使用了一个将两个整数相加的应用和一个带有 Reasoning Engine 的远程应用:

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档

import vertexai
from vertexai.preview import reasoning_engines

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# staging_bucket = "gs://YOUR_BUCKET_NAME"

vertexai.init(
    project=project_id, location="us-central1", staging_bucket=staging_bucket
)

class SimpleAdditionApp:
    def query(self, a: int, b: int) -> str:
        """Query the application.

        Args:
            a: The first input number
            b: The second input number

        Returns:
            int: The additional result.
        """

        return f"{int(a)} + {int(b)} is {int(a + b)}"

# Locally test
app = SimpleAdditionApp()
app.query(a=1, b=2)

# Create a remote app with reasoning engine.
# This may take 1-2 minutes to finish.
reasoning_engine = reasoning_engines.ReasoningEngine.create(
    SimpleAdditionApp(),
    display_name="Demo Addition App",
    description="A simple demo addition app",
    requirements=[],
    extra_packages=[],
)

部署高级应用配置

以下是一个高级示例,它使用了 LangChain 的链、提示模板和 Gemini API:

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档


from typing import List

import vertexai
from vertexai.preview import reasoning_engines

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "us-central1"
# staging_bucket = "gs://YOUR_BUCKET_NAME"

vertexai.init(project=project_id, location=location, staging_bucket=staging_bucket)

class LangchainApp:
    def __init__(self, project: str, location: str) -> None:
        self.project_id = project
        self.location = location

    def set_up(self) -> None:
        from langchain_core.prompts import ChatPromptTemplate
        from langchain_google_vertexai import ChatVertexAI

        system = (
            "You are a helpful assistant that answers questions "
            "about Google Cloud."
        )
        human = "{text}"
        prompt = ChatPromptTemplate.from_messages(
            [("system", system), ("human", human)]
        )
        chat = ChatVertexAI(project=self.project_id, location=self.location)
        self.chain = prompt | chat

    def query(self, question: str) -> Union[str, List[Union[str, Dict]]]:
        """Query the application.

        Args:
            question: The user prompt.

        Returns:
            str: The LLM response.
        """
        return self.chain.invoke({"text": question}).content

# Locally test
app = LangchainApp(project=project_id, location=location)
app.set_up()
print(app.query("What is Vertex AI?"))

# Create a remote app with reasoning engine
# This may take 1-2 minutes to finish because it builds a container and turn up HTTP servers.
reasoning_engine = reasoning_engines.ReasoningEngine.create(
    LangchainApp(project=project_id, location=location),
    requirements=[
        "google-cloud-aiplatform==1.50.0",
        "langchain-google-vertexai",
        "langchain-core",
    ],
    display_name="Demo LangChain App",
    description="This is a simple LangChain app.",
    # sys_version="3.10",  # Optional
    extra_packages=[],
)

查询 Reasoning Engine

查询推理引擎。

此示例使用的是部署基本应用配置示例中的 SimpleAdditionApp 类。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的项目 ID
  • LOCATION:处理请求的区域。必须为 us-central1
  • REASONING_ENGINE_ID:推理引擎的 ID。
  • INPUTprotobuf.struct: input 中的参数应与部署基本应用配置期间定义的 def query(self, question: str) 方法内的参数相匹配。

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID:query

请求 JSON 正文:

{
  "input": {
    INPUT
  }
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID:query"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID:query" | Select-Object -Expand Content

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档

import vertexai
from vertexai.preview import reasoning_engines

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# reasoning_engine_id = "REASONING_ENGINE_ID"

vertexai.init(project=project_id, location="us-central1")
reasoning_engine = reasoning_engines.ReasoningEngine(reasoning_engine_id)

# Replace with kwargs for `.query()` method.
response = reasoning_engine.query(a=1, b=2)
print(response)

列出 Reasoning Engine

列出项目中的推理引擎。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的项目 ID
  • PROJECT_ID:您的项目 ID
  • LOCATION:处理请求的区域。必须为 us-central1

HTTP 方法和网址:

GET https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines" | Select-Object -Expand Content

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档

import vertexai
from vertexai.preview import reasoning_engines

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"

vertexai.init(project=project_id, location="us-central1")

reasoning_engine_list = reasoning_engines.ReasoningEngine.list()
print(reasoning_engine_list)

获取 Reasoning Engine

获取推理引擎的详细信息。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的项目 ID
  • PROJECT_ID:您的项目 ID
  • LOCATION:处理请求的区域。必须为 us-central1
  • REASONING_ENGINE_ID:推理引擎的 ID。

HTTP 方法和网址:

GET https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID" | Select-Object -Expand Content

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档

import vertexai
from vertexai.preview import reasoning_engines

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# reasoning_engine_id = "REASONING_ENGINE_ID"

vertexai.init(project=project_id, location="us-central1")

reasoning_engine = reasoning_engines.ReasoningEngine(reasoning_engine_id)
print(reasoning_engine)

删除 Reasoning Engine

删除推理引擎。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的项目 ID
  • LOCATION:处理请求的区域。必须为 us-central1
  • REASONING_ENGINE_ID:推理引擎的 ID。

HTTP 方法和网址:

DELETE https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X DELETE \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID" | Select-Object -Expand Content

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档

import vertexai
from vertexai.preview import reasoning_engines

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# reasoning_engine_id = "REASONING_ENGINE_ID"

vertexai.init(project=project_id, location="us-central1")

reasoning_engine = reasoning_engines.ReasoningEngine(reasoning_engine_id)
reasoning_engine.delete()

后续步骤