简介
本页面为开发者提供构建中间件的指导与指南,用于将GPT Action连接到特定应用程序。在继续之前,请确保您已熟悉以下信息:
这个特定的GPT Action提供了如何构建Google Cloud Function的概述,这是Google基于云的函数构建器。本文档帮助用户设置一个受OAuth保护的Google Cloud Function,以连接到GPT Action和示例应用程序。
价值与示例商业应用场景
价值: 用户现在可以利用ChatGPT的自然语言能力直接连接到Google云函数。这可以通过以下几种方式实现:
- GPT Actions中的10万字符限制:用户可以使用中间件对API返回的文本响应进行预处理。例如,您可以在中间件中使用OpenAI的API对文本进行摘要处理,然后再将其发送回ChatGPT。
- 通常对于操作,用户依赖SaaS API返回文本。您可以将供应商API的响应转换为易于理解的文本,并且它可以处理不同的数据类型,例如结构化和非结构化数据。
- 它可以返回文件而不仅仅是文本。这对于展示用于数据分析的CSV文件很有用,或者返回PDF文件后ChatGPT会将其视为上传内容。
示例用例:
- 用户需要查询Google Cloud SQL,但需要在ChatGPT和Google Cloud SQL之间搭建一个中间件应用
- 用户已在Google Cloud函数中连续构建了多个步骤,需要通过ChatGPT来启动该流程
应用信息
应用密钥链接
在开始之前,请查看应用程序中的这些链接:
- 应用网站: https://cloud.google.com/functions/docs
- 应用API文档:https://cloud.google.com/functions/docs/writing/write-http-functions
应用前提条件
在开始之前,请确保您已在应用程序环境中完成以下步骤:
- 具有创建Google Cloud Functions和Google Cloud APIs权限的Google Cloud Console(设置OAuth客户端时需要此权限)
应用设置
安装应用
创建和部署Google Cloud Functions有3种选项
- IDE - 使用您喜欢的IDE创建,例如VS Code
- Google Cloud Console - 通过浏览器创建
- Google Cloud CLI (gcloud) - 通过命令行创建
您可以在此处here查阅支持的运行时环境
选项1:使用IDE(VSCode)
请参阅Google的文档此处了解如何使用VSCode进行部署。如果您熟悉这种方法,可以随意使用。
选项2:直接在Google云控制台中
查看此处文档了解如何使用Google云控制台进行部署。
选项3:使用Google Cloud CLI(gcloud)
请参阅此处的文档了解如何使用Google云控制台进行部署。下面我们将逐步演示一个示例。
第一部分:安装并初始化Google Cloud CLI (gcloud)
按照此处与您运行的操作系统相关的步骤进行操作。此过程的最后一步是运行gcloud init并登录您的Google账户
第二部分:设置本地开发环境
在本示例中,我们将设置一个Node.js环境。
mkdir <directory_name>
cd <directory_name>初始化Node.js项目
npm init接受npm init的默认值
Part 3: 创建函数
创建index.js文件
const functions = require('@google-cloud/functions-framework');
const axios = require('axios');
const TOKENINFO_URL = 'https://oauth2.googleapis.com/tokeninfo';
// Register an HTTP function with the Functions Framework that will be executed
// when you make an HTTP request to the deployed function's endpoint.
functions.http('executeGCPFunction', async (req, res) => {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).send('Unauthorized: No token provided');
}
const token = authHeader.split(' ')[1];
if (!token) {
return res.status(401).send('Unauthorized: No token provided');
}
try {
const tokenInfo = await validateAccessToken(token);
res.json("You have connected as an authenticated user to Google Functions");
} catch (error) {
res.status(401).send('Unauthorized: Invalid token');
}
});
async function validateAccessToken(token) {
try {
const response = await axios.get(TOKENINFO_URL, {
params: {
access_token: token,
},
});
return response.data;
} catch (error) {
throw new Error('Invalid token');
}
}第四部分:部署函数
以下步骤将在您的package.json文件中安装并添加必要的依赖项
npm install @google-cloud/functions-framework
npm install axiosnpx @google-cloud/functions-framework --target=executeGCPFunctiongcloud functions deploy gcp-function-for-chatgpt \
--gen2 \
--runtime=nodejs20 \
--region=us-central1 \
--source=. \
--entry-point=executeGCPFunction \
--trigger-http \
--allow-unauthenticatedChatGPT 步骤
自定义GPT指令
创建自定义GPT后,请将以下文本复制到指令面板中。有问题吗?查看入门示例详细了解此步骤的操作方法。
When the user asks you to test the integration, you will make a call to the custom action and display the resultsOpenAPI 规范
创建自定义GPT后,在操作面板中复制以下文本。有问题吗?查看入门示例了解此步骤的详细操作方式。
以下是连接到此中间件的示例。您需要在此部分插入您的应用程序和函数信息。
openapi: 3.1.0
info:
title: {insert title}
description: {insert description}
version: 1.0.0
servers:
- url: {url of your Google Cloud Function}
description: {insert description}
paths:
/{your_function_name}:
get:
operationId: {create an operationID}
summary: {insert summary}
responses:
'200':
description: {insert description}
content:
text/plain:
schema:
type: string
example: {example of response}认证说明
以下是设置与这个第三方应用程序进行身份验证的说明。有问题吗?查看入门示例以更详细地了解此步骤的工作原理。
在Google云控制台中
在Google Cloud控制台中,您需要创建OAuth客户端ID凭据。要导航到正确的页面,请在Google Cloud控制台中搜索"Credentials"或在浏览器中输入https://console.cloud.google.com/apis/credentials?project=。您可以在此处了解更多信息。
点击"创建凭证"并选择"Oauth客户端ID"。在"应用类型"中选择"Web应用程序",然后输入您的应用程序名称(见下文)。

在"OAuth客户端已创建"模态对话框中,请注意
- 客户端ID
- 客户端密钥
在ChatGPT中(参考入门示例中的步骤2)
在ChatGPT中,点击"认证"并选择"OAuth"。输入以下信息。
- 客户端ID: 见上一步
- 客户端密钥: 见上一步
- 授权URL:
https://accounts.google.com/o/oauth2/auth - 令牌URL:
https://oauth2.googleapis.com/token - 范围:
https://www.googleapis.com/auth/userinfo.email
返回Google Cloud控制台(同时参考入门示例中的步骤4)
编辑您之前在Google Cloud中创建的OAuth 2.0客户端ID,并添加创建自定义操作后收到的回调URL。

测试GPT
你现在可以开始测试GPT了。你可以输入一个简单的提示,比如"测试集成",然后预期会看到以下内容:
- 请求登录Google
- 允许请求访问您的Google函数
- 来自ChatGPT的响应显示您函数的返回结果 - 例如"您已作为认证用户连接到Google Functions"
是否有您希望我们优先考虑的集成方案?我们的集成是否存在错误?请在GitHub上提交PR或问题,我们会尽快查看。