如何解决如何使用 dialogflow_cx python 库检测 end_interaction 信号?
我使用 DialogFlow CX python library 进行代理集成。通过 detect_intent
发送请求后,我希望能够在响应中检查确定与代理的交互是否已结束的标志。
为此,我在 ResponseMessage
数据类型响应中找到了一个名为 end_interaction
的属性。我用我的一个代理对此进行了测试,一旦交互结束,我就可以在响应消息数组中看到 end_interaction
:
# inspecting query_result.response_messages
[
text {
text: "Goodbye."
},end_interaction {
}
]
现在,问题是我想在收到信号时触发一些操作,但是由于 end_interaction
字段在我尝试检查它时被发送为空,因此返回 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
进行检查,但是它们对于所有场景都返回 True
,因为 ResponseMessage
数据类型始终具有 {{ 1}} 属性。
非常感谢任何有关如何检测和检查此信号的帮助!谢谢!
解决方法
要检测和检查 end_interaction 字段,您可以在您的 detect_intent 中使用以下内容:
any("end_interaction" in d for d in response.query_result.response_messages)
请注意,如果在 response_messages 列表中找到 end_interaction 字段并且 假 如果不是。您可以使用它来确定与代理的交互是否已结束。
以下是带有 end_interaction 字段的detect_intent 的python 示例代码,供您参考:
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)}')
结果如下:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。