python - 如何在 Django Python 中使用检测意图(文本)?
问题描述
我正在使用 Dialogflow 和 Django-python 创建一个聊天机器人。现在,我已经在 Dialogflow 中创建了一个代理,已经有一个 Django 应用程序,使用了 ngork 和其他必要的东西,除了将 Dialogflow 连接到 Django 应用程序的实现/和调用 API。
我偶然发现了这个文档https://github.com/googleapis/dialogflow-python-client-v2并成功地完成了所有需要的步骤。在文档的最后一部分,似乎我最终需要做的是使用对话流检测意图文本,因此我将其复制并放入我的 Django 应用程序(views.py)中。
def detect_intent_texts(project_id, session_id, texts, language_code):
"""Returns the result of detect intent with texts as inputs.
Using the same `session_id` between requests allows continuation
of the conversation."""
import dialogflow_v2 as dialogflow
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session))
for text in texts:
text_input = dialogflow.types.TextInput(
text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(
session=session, query_input=query_input)
print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
print('Detected intent: {} (confidence: {})\n'.format(
response.query_result.intent.display_name,
response.query_result.intent_detection_confidence))
print('Fulfillment text: {}\n'.format(
response.query_result.fulfillment_text))
现在,我不知道下一步该做什么。不幸的是,我不知道如何使用它或它是如何工作的,因为我还是新手。我已经在网上搜索了答案,但我没有清楚地了解它并且有点不知所措(因为这似乎有很多事情要做)。我希望我能得到一个例子或一步一步的过程。
解决方案
我假设您知道 Django 的基本功能,并且您使用的是 Dialogflow 的 v2。
您需要将用户文本请求从前端传递到views.py
. 你会在request
对象中得到它,然后你需要提取它。
提取文本后,您需要使用and调用 dialogflow 的detect_intent()
函数(具有相同 session_id 的文本请求将被视为对话的同一部分)。 text
session_id
此外,您需要从 GCP 控制台获取一个 json 文件,以便对对话流请求进行身份验证。您可以在此处阅读更多相关信息。
以下是示例代码,您可以根据自己的使用进行扩展:
import dialogflow
from django.http import HttpResponse
def your_view(request):
text = request.POST.get("text_request")
session_id = 'some_session_id'
res = detect_intent(text)
return HttpResponse(res)
def detect_intent(text, session_id):
language_code = 'en'
project_id = 'your_dialogflow_project_id'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path_to_your_json_file'
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(session=session, query_input=query_input)
print('Query text: {}'.format(response.query_result.query_text))
print('Detected intent: {} (confidence: {})\n'.format(
response.query_result.intent.display_name,
response.query_result.intent_detection_confidence))
print('Fulfillment text: {}\n'.format(
response.query_result.fulfillment_text))
return response.query_result.fulfillment_text
希望能帮助到你。
推荐阅读
- javascript - 如何使用 --outDir TypeScript compilerOptions
- neo4j - 在 neo4j 中获取用户时间轴上的聚合消息
- sql - SQL - SAP HANA - 仅使用列表中的第一个条目(基于日期/时间)
- javascript - 间隔太慢
- python - 尝试使用 Selenium 从网站上的所有产品生成链接
- java - How to add a value in object in catch part?
- three.js - 将物理学应用于复杂形状的方法
- sql - DB2 使用标记的持续时间对多个日期间隔无效
- hyperledger-fabric - Hyperledger Fabric 如何执行背书,谁是 Hyperledger Fabric 的客户
- r - 在R中对齐下标轴标签