Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Pada umumnya, Anda mengonfigurasi konteks pada waktu desain
(saat mem-build agen). Misalnya, jika Anda memiliki frasa pelatihan seperti:
"Saya ingin menambahkan pizza ke keranjang belanja saya".
Dalam beberapa skenario lanjutan,
Anda mungkin juga ingin menulis kode yang mendapatkan dan menetapkan beberapa konteks saat runtime
(selama percakapan langsung). Contohnya, Anda mendeteksi lokasi perangkat
dan telah menambahkannya ke konteks, sehingga Anda dapat merujuk ke lokasi tersebut nanti.
Dokumen ini menjelaskan cara mendapatkan dan menetapkan konteks
pada waktu desain dan runtime menggunakan
Konsol Dialogflow,
API,
atau fulfillment.
Penamaan konteks
Aturan berikut berlaku untuk konteks penamaan:
Aturan
Contoh
Gunakan nama alfanumerik.
mycontext1
Gunakan - atau _, bukan spasi.
my-context-1
Nama tidak peka huruf besar/kecil.
Abc123 dan abc123 dianggap setara.
Semua nama konteks menggunakan huruf kecil saat menggunakan API.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-03 UTC."],[[["\u003cp\u003eContexts are primarily configured at design-time during agent building, using training phrases to set them up.\u003c/p\u003e\n"],["\u003cp\u003eContexts can be dynamically managed at runtime during live conversations, such as adding a device location to a context, using code.\u003c/p\u003e\n"],["\u003cp\u003eContexts can be set at design-time using the Dialogflow Console by navigating to the Intents section and adding input or output contexts, or through the API.\u003c/p\u003e\n"],["\u003cp\u003eAt runtime, contexts can be set using the \u003ccode\u003eoutputContexts\u003c/code\u003e field in a \u003ccode\u003eWebhookResponse\u003c/code\u003e or through the \u003ccode\u003equeryParameters.contexts\u003c/code\u003e field in a \u003ccode\u003edetectIntent\u003c/code\u003e API call.\u003c/p\u003e\n"],["\u003cp\u003eAt runtime, you can retrieve active contexts via the \u003ccode\u003equeryResult.outputContexts\u003c/code\u003e field in a \u003ccode\u003eWebhookRequest\u003c/code\u003e or in a \u003ccode\u003edetectIntent\u003c/code\u003e API response, and can be managed with create, delete, update, get, and list methods.\u003c/p\u003e\n"]]],[],null,["# Manage contexts\n\nIn most cases, you configure contexts at design-time\n(when you are building an agent). For example, when you have a training phrase like:\n\"I would like to add pizza to my shopping cart.\"\n\nIn some advanced scenarios,\nyou may also want to write code that gets and sets some contexts at runtime\n(during a live conversation). An example could be, you detected a device location\nand you have added it to the context, so you can refer to the location at a later moment.\n\nThis document describes how to get and set contexts\nat design-time and runtime by using the\n[Dialogflow Console](/dialogflow/docs/console),\n[the API](/dialogflow/docs/api-overview),\nor [fulfillment](/dialogflow/docs/fulfillment-overview).\n\nContext naming\n--------------\n\nThe following rules apply to naming contexts:\n\nSet contexts at design-time\n---------------------------\n\n### Console\n\n1. Go to the [Dialogflow ES console](https://dialogflow.cloud.google.com).\n2. Select an agent.\n3. Select **Intents** in the left sidebar menu.\n4. Expand the **Contexts** section of the intent data.\n5. Click the **Add output context** or **Add input context** field in intent data.\n6. Enter a name for the context and press **Return**.\n7. For output contexts, optionally click the lifespan circle and change the lifespan.\n8. Add more contexts as needed.\n9. Click **Save**.\n\n### API\n\nSee the\n[Intents reference](/dialogflow/docs/reference/common-types#intents).\n\nSet contexts at runtime\n-----------------------\n\n### Fulfillment\n\nWhen your webhook service sends a\n[`WebhookResponse`](/dialogflow/docs/reference/common-types#webhookresponse),\nset the `outputContexts` field to desired active contexts.\n\n### API\n\nThe request for a\n[`Sessions`](/dialogflow/docs/reference/common-types#sessions)\ntype `detectIntent` call contains a `queryParameters.contexts` field,\nwhich is used to set active contexts.\n\nThe following is a REST JSON example of a `detectIntent` request:\n\n`POST https://dialogflow.googleapis.com/v2/{session=projects/*/agent/sessions/*}:detectIntent` \n\n```\n{\n \"queryInput\": {\n \"text\": {\n \"languageCode\": \"en-US\",\n \"text\": \"I would like to add pizza to my shopping cart.\"\n }\n },\n \"queryParams\": {\n \"contexts\": [{\n \"name\": \"projects/project-id/agent/sessions/session-id/contexts/product-chosen\",\n \"lifespanCount\": 5,\n \"parameters\": {\n \"product\": \"Pizza\",\n \"device-location\" \"@52.3377871,4.8698096,17z\"\n }\n }]\n }\n}\n```\n\nAt any time,\nyou can also call the create, delete, and update methods on the\n[`Contexts`](/dialogflow/docs/reference/common-types#contexts)\ntype.\n\nGet contexts at runtime\n-----------------------\n\n### Fulfillment\n\nWhen your webhook service receives a\n[`WebhookRequest`](/dialogflow/docs/reference/common-types#webhookrequest),\nthe `queryResult.outputContexts` field contains the active contexts.\n\n### API\n\nThe response to a\n[`Sessions`](/dialogflow/docs/reference/common-types#sessions)\ntype `detectIntent` call contains a\n`queryResult.outputContexts` field,\nwhich provides the list of active contexts.\n\nThe following is a REST JSON example of a `detectIntent` response: \n\n```\n{\n \"responseId\": \"response-id\",\n \"queryResult\": {\n \"queryText\": \"I would like to add pizza to my shopping cart.\",\n \"parameters\": {\n \"product\": \"pizza\"\n },\n \"allRequiredParamsPresent\": true,\n \"fulfillmentText\": \"The product has been added.\",\n \"fulfillmentMessages\": [\n {\n \"text\": {\n \"text\": [\n \"The product has been added.\"\n ]\n }\n }\n ],\n \"outputContexts\": [\n {\n \"name\": \"projects/project-id/agent/sessions/session-id/contexts/product-chosen\",\n \"lifespanCount\": 5,\n \"parameters\": {\n \"product\": \"Pizza\",\n \"device-location\" \"@52.3377871,4.8698096,17z\"\n }\n }\n ],\n \"intent\": {\n \"name\": \"projects/project-id/agent/intents/intent-id\",\n \"displayName\": \"buy-product\"\n },\n \"intentDetectionConfidence\": 0.8057143,\n \"languageCode\": \"en\",\n }\n}\n```\n\nAt any time,\nyou can also call the get and list methods on the\n[`Contexts`](/dialogflow/docs/reference/common-types#contexts)\ntype."]]