提取用户提供的值#

概述#

本指南将教您如何从用户话语中提取用户提供的值(例如,姓名、日期、查询)并将它们存储在上下文变量中。 然后,您可以使用这些值来生成 Bot 的响应或执行后续逻辑。

一般语法如下

# Comment with instructions on how to extract the value.
# Can span multiple lines.
$variable_name = ...

注意... 在这里不是占位符;它是实际的语法,即省略号。

在流程中的任何时刻,您都可以包含 $variable_name = ...,指示 LLM 计算变量的值。

单个值或列表#

您可以提取单个值。

user provide name
# Extract the name of the user.
$name = ...

或者,您也可以指示 LLM 提取值列表。

define flow add to cart
  user request add items to cart

  # Generate a list of the menu items that the user requested to be added to the cart
  # e.g. ["french fries", "double protein burger", "lemonade"].
  # If user specifies no menu items, just leave this empty, i.e. [].

  $item_list = ...

多个值#

如果您从同一个用户输入中提取多个变量的值。

define user request book flight
  "I want to book a flight."
  "I want to fly from Bucharest to San Francisco."
  "I want a flight to Paris."

define flow
  user request book flight

  # Extract the origin from the user's request. If not specified, say "unknown".
  $origin_city = ...

  # Extract the destination city from the user's request. If not specified, say "unknown".
  $destination_city = ...

上下文查询#

这种机制可以应用于启用上下文查询。 例如,假设您想使用 Wolfram Alpha 回答数学问题,并支持以下流程

user "What is the largest prime factor for 1024?"
bot "The largest prime factor is 2."
user "And its square root?"
bot "The square root for 1024 is 32"

为了实现这一点,您可以使用以下流程

define flow
  user ask math question

  # Extract the math question from the user's input.
  $math_query = ...

  execute wolfram alpha request(query=$math_query)
  bot respond to math question