如何从dialogflow API访问protobuf响应中的信息
我在访问访问 googleDialogflow api 后收到的 protobuf 对象的值时遇到严重问题
(... create google cloud session object ...)
text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(request={"session": session, "query_input": query_input})
from google.protobuf import json_format
response_json = json_format.MessageToDict(response)
错误是:
AttributeError: 'MapComposite' object has no attribute 'DESCRIPTOR'
基本上我有两个问题
: )我无法将 protobuf 转换为 json (在这里我可以轻松地看到如何访问我正在寻找的信息)
b)我无法理解 protobof 数据结构。有字典吗?
a) 或 b) 中的任何帮助者均表示赞赏。
顺便说一句:我正在寻找一种访问 API 响应中的 response.parameters
的方法。
为 Scott 编辑:
response.parameters 是一个对象:
参数对象的描述如下: https://cloud.google.com/dialogflow/ es/docs/reference/rest/v2/DetectIntentResponse#QueryResult
这对您有意义吗?因为我不明白如何访问这个对象中的值。
I am having serious trouble accessing the values from a protobuf object I receive after accessing the google dialogflow api
(... create google cloud session object ...)
text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(request={"session": session, "query_input": query_input})
from google.protobuf import json_format
response_json = json_format.MessageToDict(response)
The error is:
AttributeError: 'MapComposite' object has no attribute 'DESCRIPTOR'
Basically I have two issues:
a) I am not able to convert the protobuf to json (here I am easily able to see how to access the infos I am looking for)
b) I am not able to understand the protobof data structure. Is there a dictionary?
Any helper in a) or b) are appreciated.
btw: I am looking for a way to access response.parameters
in the API response.
Edit for Scott:
response.parameters is an object:
<proto.marshal.collections.maps.MapComposite object at 0x7fe7323f7da0>
The parameters object is described here:
https://cloud.google.com/dialogflow/es/docs/reference/rest/v2/DetectIntentResponse#QueryResult
Does it make sense to you? Because I don't understand how to access the values in this object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
MessageToDict
方法(如下所示)将 protobuf 序列化为字典,并使用response._pb
而不是response
。现在,您现在可以在字典中导航到您想要访问的
parameters
,如下所示,因为parameters
位于queryResult
内部/之下(基于关于结构)。You may use
MessageToDict
approach, as shown below, to serialize protobuf to a dictionary and useresponse._pb
instead ofresponse
.Now, you can now navigate through the dictionary for the
parameters
that you want to access as shown below sinceparameters
is inside/underqueryResult
(based on the structure).