如何在 Django Python 中使用检测意图(文本)?
How to use Detect Intent(Text) in Django Python?
我正在使用 Dialogflow 和 Django 创建一个聊天机器人-python。现在,我已经在 Dialogflow 中创建了一个代理,已经有了一个 Django 应用程序,使用了 ngork 和其他必要的东西,除了将 Dialogflow 连接到 Django 应用程序的 fulfillment/and 调用 API。
我偶然发现了这份文档 https://github.com/googleapis/dialogflow-python-client-v2 并成功完成了所有需要的步骤。在文档的最后一部分,似乎我最终需要做的是使用 dialogflow detect intent text ,所以我复制它并将其放入我的 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
对象中获取它,然后您需要提取它。
提取文本后,您需要使用 text
和 session_id
调用 dialogflow 的 detect_intent()
函数(具有相同 session_id 的文本请求将被视为对话的同一部分)。
此外,您需要从 GCP 控制台获取 json 文件以验证对话流请求。您可以阅读更多相关信息 here。
这里是示例代码,您可以根据自己的用途进行扩展:
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
希望对您有所帮助。
我正在使用 Dialogflow 和 Django 创建一个聊天机器人-python。现在,我已经在 Dialogflow 中创建了一个代理,已经有了一个 Django 应用程序,使用了 ngork 和其他必要的东西,除了将 Dialogflow 连接到 Django 应用程序的 fulfillment/and 调用 API。
我偶然发现了这份文档 https://github.com/googleapis/dialogflow-python-client-v2 并成功完成了所有需要的步骤。在文档的最后一部分,似乎我最终需要做的是使用 dialogflow detect intent text ,所以我复制它并将其放入我的 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
对象中获取它,然后您需要提取它。
提取文本后,您需要使用 text
和 session_id
调用 dialogflow 的 detect_intent()
函数(具有相同 session_id 的文本请求将被视为对话的同一部分)。
此外,您需要从 GCP 控制台获取 json 文件以验证对话流请求。您可以阅读更多相关信息 here。
这里是示例代码,您可以根据自己的用途进行扩展:
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
希望对您有所帮助。