面向机器人构建者、开发者和维护者的高级脚本 API。
Python Dialogflow CX 脚本 API (DFCX SCRAPI) 是一个高级 API,扩展了 Dialogflow CX 的官方 Google Python 客户端。对于机器人构建者、开发者和维护者来说,SCRAPI 使 DFCX 的使用变得更容易、更友好、更 Pythonic。
SCRAPI --> Python Dialogflow CX
as
Keras --> Tensorflow
借助 DFCX SCRAPI,您可以大规模执行许多机器人构建和维护操作,包括但不限于:
身份验证可能会有所不同,具体取决于您与 SCRAPI 交互的方式和位置。
如果您将 SCRAPI 与 Google Colab 笔记本一起使用,您可以将以下内容添加到笔记本顶部以便于身份验证:
project_id = '<YOUR_GCP_PROJECT_ID>'
# this will launch an interactive prompt that allows you to auth with GCP in a browser
!g cloud auth application - default login - - no - launch - browser
# this will set your active project to the `project_id` above
!g cloud auth application - default set - quota - project $ project_id
运行上述命令后,Colab 将从环境中获取您的凭据并将其直接传递给 SCRAPI。无需使用服务帐户密钥!然后您可以像这样简单地使用 SCRAPI:
from dfcx_scrapi . core . intents import Intents
agent_id = '<YOUR_AGENT_ID>'
i = Intents () # <-- Creds will be automatically picked up from the environment
intents_map = i . get_intents_map ( agent_id )
如果您将 SCRAPI 与 Cloud Functions 或 Cloud Run 结合使用,SCRAPI 可以获取这些服务使用的默认环境凭据,而无需任何其他配置!
dfcx-scrapi
添加到您的requirements.txt
文件中一旦完成上述设置,您的函数代码就可以像这样轻松使用:
from dfcx_scrapi . core . intents import Intents
agent_id = '<YOUR_AGENT_ID>'
i = Intents () # <-- Creds will be automatically picked up from the environment
intents_map = i . get_intents_map ( agent_id )
与 Cloud Functions / Cloud Run 类似,如果您使用 gcloud CLI,则 SCRAPI 可以获取您的本地身份验证凭据。
gcloud init
。gcloud auth login
gcloud auth list
以确保您的主要账户处于活动状态。这将使用 gcloud CLI 对您的主要 GCP 帐户进行身份验证,并且 SCRAPI 可以从此处获取凭据。
SCRAPI 中的一些类仍然依赖于服务帐户密钥,特别是 DataframeFunctions 类,因为它如何使用 Google Sheets 进行身份验证。
为了使用这些功能,您需要一个对您的 GCP 项目具有适当访问权限的服务帐号。
有关更多信息并查看服务帐户的官方文档,请参阅创建和管理 GCP 服务帐户。
获得具有适当权限的服务帐户密钥后,您可以按如下方式使用它:
from dfcx_scrapi . core . intents import Intents
from dfcx_scrapi . tools . dataframe_functions import DataframeFunctions
agent_id = '<YOUR_AGENT_ID>'
creds_path = '<PATH_TO_YOUR_SERVICE_ACCOUNT_JSON_FILE>'
i = Intents ( creds_path = creds_path )
dffx = DataframeFunctions ( creds_path = creds_path )
df = i . bulk_intent_to_df ( agent_id )
dffx . dataframe_to_sheets ( 'GOOGLE_SHEET_NAME' , 'TAB_NAME' , df )
设置 Google Cloud Platform 凭据并安装依赖项。
gcloud auth login
gcloud auth application-default login
gcloud config set project < project name >
python3 -m venv venv
source ./venv/bin/activate
pip install -r requirements.txt
要运行一段简单的代码,您可以执行以下操作:
dfcx_scrapi.core
导入类 from dfcx_scrapi . core . intents import Intents
creds_path = '<PATH_TO_YOUR_SERVICE_ACCOUNT_JSON_FILE>'
agent_path = '<FULL_DFCX_AGENT_ID_PATH>'
# DFCX Agent ID paths are in this format:
# 'projects/<project_id>/locations/<location_id>/agents/<agent_id>'
# Instantiate your class object and pass in your credentials
i = Intents ( creds_path , agent_id = agent_path )
# Retrieve all Intents and Training Phrases from an Agent and push to a Pandas DataFrame
df = i . bulk_intent_to_df ()
以下是 SCRAPI 库的结构以及该结构背后的动机的简要概述。
Core 文件夹与 DFCX 代理中的核心资源类型(代理、意图、流等)同义。
Tools 文件夹包含各种自定义工具包,可让您执行更复杂的机器人管理任务,例如
Builders 文件夹包含用于在 Dialogflow CX 中构建底层原型的简单方法
我们欢迎您提交任何贡献或功能请求!
根据 Apache 2.0 许可证分发。请参阅许可证了解更多信息。
帕特里克·马洛 - [email protected] - @kmaphoenix
米拉德·塔布里兹 - [email protected] - @MRyderOC
项目链接:https://github.com/GoogleCloudPlatform/dfcx-scrapi
Dialogflow CX Python 客户端库
拥抱脸 - 飞马座释义