使用输出上下文响应操作特定上下文

manipulating a specific context using output context response

我目前正在尝试使用 webhook 响应和我 found 更改我的上下文之一的特定值,以下应该有效:

       {
            "fulfillmentText": ${textToSpeech},
            "fulfillmentMessages": [{ "text": { "text": [${text}] } }],
            "payload": {
                "google": {
                    "expectUserResponse": true,
                    "richResponse": {
                        "items": [
                            {
                                "simpleResponse": {
                                    "textToSpeech": ${textToSpeech},
                                    "displayText": ${text}
                                }
                            }
                        ],
                        "suggestions": ${suggestions},
                        "linkOutSuggestion": {
                            "destinationName": "Feedback",
                            "url": ${feedbackURL}
                        }
                    }
                }
            },
            "outputContexts": [
                {
                  "name": "projects/${projectID}/agent/sessions/${conversationID}/contexts/${context}",
                  "lifespanCount": 15,
                  "parameters": { 
                    "param":"value"
                   }
                }]
        }

但是,这不会更改在该上下文中指定的任何参数。我是不是做错了什么,或者是否有更好的方法来使用 webhook 响应更改输出上下文的参数?

您可能想检查传入上下文的命名方式。

名称可以有 either of the following formats:

  • projects/<Project ID>/agent/sessions/<Session ID>/contexts/<Context ID>
  • projects/<Project ID>/agent/environments/<Environment ID>/users/<User ID>/sessions/<Session ID>/contexts/<Context ID>

如果上下文使用第二种格式(包括环境和用户 ID)传入,则您需要创建具有相似名称的上下文。

具体来说,/contexts/<Context ID> 之前的部分应该与 WebhookRequest 中提供的完整会话字符串相匹配,您猜对了,它与以下两种模式之一相匹配:

  • projects/<Project ID>/agent/sessions/<Session ID>
  • projects/<Project ID>/agent/environments/<Environment ID>/users/<User ID>/sessions/<Session ID>

刚刚设法解决了问题。 我没有尝试创建自己的输出上下文,而是操纵了 req.body.queryResult.outputContexts 中的值。

例如: req.body.queryResult.outputContexts[0].parameters.param="value"

然后发送带有原始 outputContext 的响应

{
            "fulfillmentText": ${textToSpeech},
            "fulfillmentMessages": [{ "text": { "text": [${text}] } }],
            "payload": {
                "google": {
                    "expectUserResponse": true,
                    "richResponse": {
                        "items": [
                            {
                                "simpleResponse": {
                                    "textToSpeech": ${textToSpeech},
                                    "displayText": ${text}
                                }
                            }
                        ],
                        "suggestions": ${suggestions},
                        "linkOutSuggestion": {
                            "destinationName": "Feedback",
                            "url": ${feedbackURL}
                        }
                    }
                }
            },
            "outputContexts": ${req.body.queryResult.outputContexts}
        }