You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在IBM Watson Assistant中存储用户自由文本为变量?

Storing Free-Form Text as a Variable in IBM Watson Assistant

Absolutely! Capturing free-text inputs (like a user's name) as context variables in Watson Assistant is way simpler than the regex-based number extraction you’ve been using. Let’s break down the methods you can use:

1. Capture the Entire User Input Directly

If you want to store the full text the user entered (e.g., if they just type "Alice" when asked for their name), you can assign the raw input directly to a context variable.

In your node’s JSON Editor, add this to the context object:

"context": {
  "user_name": "<? input.text ?>"
}

This takes whatever the user types in that turn and saves it to the user_name variable for use later in the conversation.

2. Extract Specific Text from a Longer Input

If users might phrase their response with extra words (like "My name is Bob" or "我叫李四"), you can tweak the regex extraction to grab just the relevant part. For example:

For English responses:

"context": {
  "user_name": "<? input.text.extract('My name is (.*)', 1) ?>"
}

For Chinese responses:

"context": {
  "user_name": "<? input.text.extract('我叫(.*)', 1) ?>"
}

The 1 in the extract function tells Watson to grab the first captured group (the part inside the parentheses) instead of the full matched string.

3. Handle Empty or Invalid Inputs (Optional)

To avoid storing blank values if a user submits an empty input, you can add a quick conditional check using a ternary operator:

"context": {
  "user_name": "<? input.text.trim() != '' ? input.text.trim() : 'Unknown User' ?>"
}

This trims whitespace from the input and defaults to "Unknown User" if the input is empty.

Quick Tips:

  • Make sure your variable names follow Watson’s rules: no spaces, special characters, or reserved words.
  • You can also set these variables directly in the Context tab of your dialogue node (just use the expression input.text in the "Value" field instead of hardcoding text).

内容的提问来源于stack exchange,提问作者pepefrog69

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:13:04