如何使用 dialogflow_cx python 库检测 end_interaction 信号?
How to detect the end_interaction signal using the dialogflow_cx python library?
我正在使用 DialogFlow CX python library 进行代理集成。通过 detect_intent
发送请求后,我希望能够检查响应中的标志,以确定与代理的交互是否已结束。
为此,我在 ResponseMessage
数据类型响应中找到了一个 属性,名为 end_interaction
。我用我的一个代理人对此进行了测试,一旦交互结束,我就可以在响应消息数组中看到 end_interaction
:
# inspecting query_result.response_messages
[
text {
text: "Goodbye."
},
end_interaction {
}
]
现在,问题是我想在收到信号时触发一些操作,但由于 end_interaction
字段在我尝试检查它时被发送为空 return 是我 False
:
# Both response messages from the example above return False under a bool
bool(query_result.response_messages[0].end_interaction) == False # text message response
bool(query_result.response_messages[1].end_interaction) == False # end_interaction signal
我已经尝试了很多事情,比如用 isinstance
和 hasattr
进行检查,但是它们 return True
适用于自 ResponseMessage
数据以来的所有场景type 总是有一个 end_interaction
属性。
非常感谢您提供有关如何检测和检查此信号的任何帮助!谢谢!
要检测和检查 end_interaction 字段,您可以在 detect_intent 中使用以下内容:
任意("end_interaction" in d for d in response.query_result.response_messages)
请注意,如果 end_interaction 字段在 response_messages 列表中找到,如果不在 False 中。您可以使用它来确定与代理的交互是否已结束。
这是detect_intent的python示例代码,其中包含end_interaction字段供您参考:
def detect_intent_texts(agent, session_id, texts, language_code):
session_path = f'{agent}/sessions/{session_id}'
print(f"Session path: {session_path}\n")
client_options = None
agent_components = AgentsClient.parse_agent_path(agent)
location_id = agent_components["location"]
if location_id != "global":
api_endpoint = f"{location_id}-dialogflow.googleapis.com:443"
print(f"API Endpoint: {api_endpoint}\n")
client_options = {"api_endpoint": api_endpoint}
session_client = SessionsClient(client_options=client_options)
for text in texts:
text_input = session.TextInput(text=text)
query_input = session.QueryInput(text=text_input, language_code=language_code)
request = session.DetectIntentRequest(
session=session_path, query_input=query_input
)
response = session_client.detect_intent(request=request)
print("=" * 20)
print(f"Query text: {response.query_result.text}")
response_messages = [
" ".join(msg.text.text) for msg in response.query_result.response_messages
]
print(f"Response Messages:\n {response.query_result.response_messages}")
print(f'End of interaction: {any("end_interaction" in d for d in response.query_result.response_messages)}')
结果如下:
我正在使用 DialogFlow CX python library 进行代理集成。通过 detect_intent
发送请求后,我希望能够检查响应中的标志,以确定与代理的交互是否已结束。
为此,我在 ResponseMessage
数据类型响应中找到了一个 属性,名为 end_interaction
。我用我的一个代理人对此进行了测试,一旦交互结束,我就可以在响应消息数组中看到 end_interaction
:
# inspecting query_result.response_messages
[
text {
text: "Goodbye."
},
end_interaction {
}
]
现在,问题是我想在收到信号时触发一些操作,但由于 end_interaction
字段在我尝试检查它时被发送为空 return 是我 False
:
# Both response messages from the example above return False under a bool
bool(query_result.response_messages[0].end_interaction) == False # text message response
bool(query_result.response_messages[1].end_interaction) == False # end_interaction signal
我已经尝试了很多事情,比如用 isinstance
和 hasattr
进行检查,但是它们 return True
适用于自 ResponseMessage
数据以来的所有场景type 总是有一个 end_interaction
属性。
非常感谢您提供有关如何检测和检查此信号的任何帮助!谢谢!
要检测和检查 end_interaction 字段,您可以在 detect_intent 中使用以下内容:
任意("end_interaction" in d for d in response.query_result.response_messages)
请注意,如果 end_interaction 字段在 response_messages 列表中找到,如果不在 False 中。您可以使用它来确定与代理的交互是否已结束。
这是detect_intent的python示例代码,其中包含end_interaction字段供您参考:
def detect_intent_texts(agent, session_id, texts, language_code):
session_path = f'{agent}/sessions/{session_id}'
print(f"Session path: {session_path}\n")
client_options = None
agent_components = AgentsClient.parse_agent_path(agent)
location_id = agent_components["location"]
if location_id != "global":
api_endpoint = f"{location_id}-dialogflow.googleapis.com:443"
print(f"API Endpoint: {api_endpoint}\n")
client_options = {"api_endpoint": api_endpoint}
session_client = SessionsClient(client_options=client_options)
for text in texts:
text_input = session.TextInput(text=text)
query_input = session.QueryInput(text=text_input, language_code=language_code)
request = session.DetectIntentRequest(
session=session_path, query_input=query_input
)
response = session_client.detect_intent(request=request)
print("=" * 20)
print(f"Query text: {response.query_result.text}")
response_messages = [
" ".join(msg.text.text) for msg in response.query_result.response_messages
]
print(f"Response Messages:\n {response.query_result.response_messages}")
print(f'End of interaction: {any("end_interaction" in d for d in response.query_result.response_messages)}')
结果如下: