NeMo-Guardrails icon indicating copy to clipboard operation
NeMo-Guardrails copied to clipboard

global variable to update user informations for extraction task

Open thanhsang298 opened this issue 1 year ago • 6 comments

Hi, thank for great framework

I want to use 1 variable user_info to init and update user information. My script like this

user_info = {}
USER_INFO_KEYS = ["user_name", "ship_address", "phone_number"]

def init_user_info():
    for info_key in USER_INFO_KEYS:
        user_info[info_key] = "unknown"
init_user_info()

async def update_user_info_details(user_info_details="unknown", key="info"):
    user_info[key] = user_info_details
    return user_info

async def get_user_info_details():
    return user_info


COLANG_CONFIG = """
define user give personal info
  "mình tên Hà, địa chỉ của mình là quận Thủ đức"
  "42 đường Ngô Quyền là địa chỉ, mình tên Sang, sđt 01823737738"
  "Ship về địa chỉ Biên Hòa cho tôi, số điện thoại 096594019"

define bot confirm personal info
  "Thông tin của bạn đã được tiếp nhận: - Tên người mua hàng: $user_name - Địa chỉ: $ship_address - Số điện thoại: $phone_number"

define flow process personal info
  user give personal info

  $user_info = execute get_user_info_details()

  if $user_info.user_name == "unknown"
    ## Extract the name of the Vietnamese user. If not specified, say "unknown"
    $user_name = ...
    if $user_name != "unknown"
      $user_info = execute update_user_info_details(user_info_details=$user_name, key="user_name")

  if $user_info.ship_address == "unknown"
    ## Extract the ship address from the Vietnamese user's query. If not specified, say "unknown"
    $ship_address = ...
    if $ship_address != "unknown"
      $user_info = execute update_user_info_details(user_info_details=$ship_address, key="ship_address")

  if $user_info.phone_number == "unknown"
    ## Extract the phone number from the Vietnamese user's query. If not specified, say "unknown"
    $phone_number = ...
    if $phone_number != "unknown"
      $user_info = execute update_user_info_details(user_info_details=$phone_number, key="phone_number")

  bot confirm personal info
"""

config = RailsConfig.from_content(COLANG_CONFIG)
app = LLMRails(config, llm=llm, verbose=True)
app.runtime.register_action_param("llm", llm)
app.register_action(update_user_info_details, "update_user_info_details")
app.register_action(get_user_info_details, "get_user_info_details")

when user give their info:

messages= [{
    "role": "user",
    "content": "Mình là Ngọc, địa chỉ giao hàng là 42 đường số 3, số điện thoại là 0965326589"
}]
new_message = await app.generate_async(messages=messages)

i got output when print new_message : {'user_name': 'Ngọc', 'ship_address': 'unknown', 'phone_number': 'unknown'} but my expected output is: {'user_name': 'Ngọc', 'ship_address': '42 đường số 3', 'phone_number': '0965326589'}

Can you take a look my problem and shown the misstake in my flow. Thank you so much !!!

thanhsang298 avatar Mar 25 '24 10:03 thanhsang298

Hi @thanhsang298! Can you share the logs? Skimming through quickly, I cannot spot anything wrong. I can't test on my end right now, but maybe I can spot something by looking at the logs. Thanks!

drazvan avatar Mar 26 '24 11:03 drazvan

Can you share the logs? Skimming through quickly, I cannot spot anything wrong. I can't test on my end right now, but maybe I can spot something by looking at the logs. Thanks!

Hi @drazvan , I updated my flow like below:

async def validate_user_info_details(user_info_details):
    status = False
    if user_info_details != "unknown":
        status = True
    return status

user_info = {}
USER_INFO_KEYS = ["user_name", "ship_address", "phone_number"]

def init_user_info():
    for info_key in USER_INFO_KEYS:
        user_info[info_key] = "unknown"
init_user_info()

async def update_user_info_details(user_info_details="unknown", key="info"):
    user_info[key] = user_info_details
    return user_info

async def get_user_info_details():
    return user_info

COLANG_CONFIG = """
define user give personal info
  "mình tên Hà, địa chỉ của mình là quận Thủ đức"
  "42 đường Ngô Quyền là địa chỉ, mình tên Sang, sđt 01823737738"
  "Ship về địa chỉ Biên Hòa cho tôi, số điện thoại 096594019"

define bot confirm personal info
  "Thông tin của bạn đã được tiếp nhận: - Tên người mua hàng: $user_name - Địa chỉ: $ship_address - Số điện thoại: $phone_number"

define bot request to user resend user name
  "Hiện tại mình chưa nhận được thông tin về người mua hàng, bạn có thể cung cập lại cho mình được không ạ?"

define bot request to user resend ship address
  "Hiện tại mình chưa nhận được thông tin về địa chỉ giao nhận hàng, bạn có thể cung cập lại cho mình được không ạ?"

define bot request to user resend phone number
  "Hiện tại mình chưa nhận được thông tin về số điện thoại người mua hàng, bạn có thể cung cập lại cho mình được không ạ?"

define flow process personal info
  user give personal info

  $user_info = execute get_user_info_details()

  if $user_info.user_name == "unknown"
    ## Extract the name of the Vietnamese user. If not specified, say "unknown"
    $user_name = ...
    $user_name_status = execute validate_user_info_details(user_info_details=$user_name)
    if $user_name_status
      $user_info = execute update_user_info_details(user_info_details=$user_name, key="user_name")
    else
      bot request to user resend user name

  if $user_info.ship_address == "unknown"
    ## Extract the ship address from the Vietnamese user's query. If not specified, say "unknown"
    $ship_address = ...
    $ship_address_status = execute validate_user_info_details(user_info_details=$ship_address)
    if $ship_address_status
      $user_info = execute update_user_info_details(user_info_details=$ship_address, key="ship_address")
    else
      bot request to user resend ship address

  if $user_info.phone_number == "unknown"
    ## Extract the phone number from the Vietnamese user's query. If not specified, say "unknown"
    $phone_number = ...
    $phone_number_status = execute validate_user_info_details(user_info_details=$phone_number)
    if $phone_number_status
      $user_info = execute update_user_info_details(user_info_details=$phone_number, key="phone_number")
    else
      bot request to user resend phone number

  bot confirm personal info
"""

config = RailsConfig.from_content(COLANG_CONFIG)
app = LLMRails(config, llm=llm, verbose=True)
app.runtime.register_action_param("llm", llm)
app.register_action(validate_user_info_details, "validate_user_info_details")
app.register_action(update_user_info_details, "update_user_info_details")
app.register_action(get_user_info_details, "get_user_info_details")

when user give their info:

messages= [{
    "role": "user",
    "content": "Mình là Ngọc, địa chỉ giao hàng là 42 đường số 3, số điện thoại là 0965326589"
}]
new_message = await app.generate_async(messages=messages)

i got output when print new_message : {'user_name': 'Ngọc', 'ship_address': 'unknown', 'phone_number': 'unknown'} but my expected output is: {'user_name': 'Ngọc', 'ship_address': '42 đường số 3', 'phone_number': '0965326589'}

Here is the logs:

Event UtteranceUserActionFinished {'final_transcript': 'Mình là Ngọc, địa chỉ giao hàng là 42 đường số 3, số điện thoại là 0965326589'}
Event StartInternalSystemAction {'uid': '01c73b80-b1a3-47f0-90a4-496a4e88f97c', 'event_created_at': '2024-03-27T07:50:31.895315+00:00', 'source_uid': 'NeMoGuardrails', 'action_name': 'create_event', 'action_params': {'event': {'_type': 'UserMessage', 'text': '$user_message'}}, 'action_result_key': None, 'action_uid': '69b269fb-ffa9-4fdc-ab0a-1121c2d7c7d0', 'is_system_action': True}
Executing action create_event
Event UserMessage {'uid': '165f93ca-6396-44dd-bd9b-8379a8a6994d', 'event_created_at': '2024-03-27T07:50:31.895755+00:00', 'source_uid': 'NeMoGuardrails', 'text': 'Mình là Ngọc, địa chỉ giao hàng là 42 đường số 3, số điện thoại là 0965326589'}
Event StartInternalSystemAction {'uid': '41994358-1fe6-4e2a-9909-7cf8e3d7492a', 'event_created_at': '2024-03-27T07:50:31.896463+00:00', 'source_uid': 'NeMoGuardrails', 'action_name': 'generate_user_intent', 'action_params': {}, 'action_result_key': None, 'action_uid': '8c06433b-2fce-4db9-b84d-ba0fb3dc24dc', 'is_system_action': True}
Executing action generate_user_intent
Phase 1 Generating user intent
Invocation Params {'model': 'gpt-3.5-turbo', 'stream': False, 'n': 1, 'temperature': 0.0, '_type': 'azure-openai-chat', 'stop': None}
Prompt Messages 
User
"""
Below is a conversation between a helpful AI assistant and a user. The bot is designed to generate human-like text based on the input that it receives. The bot is talkative and provides lots of specific details. If the bot does not know the answer to a question, it truthfully says it does not know.
"""

# This is how a conversation between a user and the bot can go:
user "Hello there!"
  express greeting
bot express greeting
  "Hello! How can I assist you today?"
user "What can you do for me?"
  ask about capabilities
bot respond about capabilities
  "As an AI assistant, I can help you with a wide range of tasks. This includes question answering on various topics, generating text for various purposes and providing suggestions based on your preferences."
user "Tell me a bit about the history of NVIDIA."
  ask general question
bot response for general question
  "NVIDIA is a technology company that specializes in designing and manufacturing graphics processing units (GPUs) and other computer hardware. The company was founded in 1993 by Jen-Hsun Huang, Chris Malachowsky, and Curtis Priem."
user "tell me more"
  request more information
bot provide more information
  "Initially, the company focused on developing 3D graphics processing technology for the PC gaming market. In 1999, NVIDIA released the GeForce 256, the world's first GPU, which was a major breakthrough for the gaming industry. The company continued to innovate in the GPU space, releasing new products and expanding into other markets such as professional graphics, mobile devices, and artificial intelligence."
user "thanks"
  express appreciation
bot express appreciation and offer additional help
  "You're welcome. If you have any more questions or if there's anything else I can help you with, please don't hesitate to ask."


# This is how the user talks:
user "mình tên Hà, địa chỉ của mình là quận Thủ đức"
  give personal info

user "Ship về địa chỉ Biên Hòa cho tôi, số điện thoại 096594019"
  give personal info

user "42 đường Ngô Quyền là địa chỉ, mình tên Sang, sđt 01823737738"
  give personal info



# This is the current conversation between the user and the bot:
# Choose intent from this list: give personal info
user "Hello there!"
  express greeting
bot express greeting
  "Hello! How can I assist you today?"
user "What can you do for me?"
  ask about capabilities
bot respond about capabilities
  "As an AI assistant, I can help you with a wide range of tasks. This includes question answering on various topics, generating text for various purposes and providing suggestions based on your preferences."
user "Mình là Ngọc, địa chỉ giao hàng là 42 đường số 3, số điện thoại là 0965326589"

Output Stats {'token_usage': {'completion_tokens': 3, 'prompt_tokens': 634, 'total_tokens': 637}, 'model_name': 'gpt-35-turbo-16k', 'system_fingerprint': None}
--- LLM call took 1.35 seconds
Event UserIntent {'uid': '8749ca44-874d-4140-a320-904a7d0e0205', 'event_created_at': '2024-03-27T07:50:33.303101+00:00', 'source_uid': 'NeMoGuardrails', 'intent': 'give personal info'}
Event StartInternalSystemAction {'uid': '8189e873-bad4-4fa6-96b4-a38d7096955b', 'event_created_at': '2024-03-27T07:50:33.304364+00:00', 'source_uid': 'NeMoGuardrails', 'action_name': 'get_user_info_details', 'action_params': {}, 'action_result_key': 'user_info', 'action_uid': 'a2d5f22c-095e-47e9-a1ac-0f38c5cb5544', 'is_system_action': False}
Executing action get_user_info_details
Event InternalSystemActionFinished {'uid': '5d7e9914-8bc2-49d4-9eb9-82f7a5099e2a', 'event_created_at': '2024-03-27T07:50:33.305007+00:00', 'source_uid': 'NeMoGuardrails', 'action_uid': 'a2d5f22c-095e-47e9-a1ac-0f38c5cb5544', 'action_name': 'get_user_info_details', 'action_params': {}, 'action_result_key': 'user_info', 'status': 'success', 'is_success': True, 'return_value': {'user_name': 'unknown', 'ship_address': 'unknown', 'phone_number': 'unknown'}, 'events': [], 'is_system_action': False, 'action_finished_at': '2024-03-27T07:50:33.305022+00:00'}
Event StartInternalSystemAction {'uid': '0d5f4cf9-7973-4e35-94f3-24dddcc3fbc8', 'event_created_at': '2024-03-27T07:50:33.308574+00:00', 'source_uid': 'NeMoGuardrails', 'action_name': 'generate_value', 'action_params': {'instructions': '# Extract the name of the Vietnamese user. If not specified, say "unknown"'}, 'action_result_key': 'user_name', 'action_uid': '1490ac0f-c7ff-4f99-af28-bc90270c963e', 'is_system_action': True}
Executing action generate_value
Invocation Params {'model': 'gpt-3.5-turbo', 'stream': False, 'n': 1, 'temperature': 0.0, '_type': 'azure-openai-chat', 'stop': None}
Prompt Messages 
User
"""
Below is a conversation between a helpful AI assistant and a user. The bot is designed to generate human-like text based on the input that it receives. The bot is talkative and provides lots of specific details. If the bot does not know the answer to a question, it truthfully says it does not know.
"""

# This is how a conversation between a user and the bot can go:
user "Hello there!"
  express greeting
bot express greeting
  "Hello! How can I assist you today?"
user "What can you do for me?"
  ask about capabilities
bot respond about capabilities
  "As an AI assistant, I can help you with a wide range of tasks. This includes question answering on various topics, generating text for various purposes and providing suggestions based on your preferences."
user "Tell me a bit about the history of NVIDIA."
  ask general question
bot response for general question
  "NVIDIA is a technology company that specializes in designing and manufacturing graphics processing units (GPUs) and other computer hardware. The company was founded in 1993 by Jen-Hsun Huang, Chris Malachowsky, and Curtis Priem."
user "tell me more"
  request more information
bot provide more information
  "Initially, the company focused on developing 3D graphics processing technology for the PC gaming market. In 1999, NVIDIA released the GeForce 256, the world's first GPU, which was a major breakthrough for the gaming industry. The company continued to innovate in the GPU space, releasing new products and expanding into other markets such as professional graphics, mobile devices, and artificial intelligence."
user "thanks"
  express appreciation
bot express appreciation and offer additional help
  "You're welcome. If you have any more questions or if there's anything else I can help you with, please don't hesitate to ask."


# This is how the bot thinks:


# This is the current conversation between the user and the bot:
user "Hello there!"
  express greeting
bot express greeting
  "Hello! How can I assist you today?"
user "What can you do for me?"
  ask about capabilities
bot respond about capabilities
  "As an AI assistant, I can help you with a wide range of tasks. This includes question answering on various topics, generating text for various purposes and providing suggestions based on your preferences."
user "Mình là Ngọc, địa chỉ giao hàng là 42 đường số 3, số điện thoại là 0965326589"
  give personal info
execute get_user_info_details
# The result was {'user_name': 'unknown', 'ship_address': 'unknown', 'phone_number': 'unknown'}

# # Extract the name of the Vietnamese user. If not specified, say "unknown"
$user_name =
Output Stats {'token_usage': {'completion_tokens': 4, 'prompt_tokens': 581, 'total_tokens': 585}, 'model_name': 'gpt-35-turbo-16k', 'system_fingerprint': None}
--- LLM call took 0.54 seconds
Event InternalSystemActionFinished {'uid': '949ff09a-1da3-4d30-b6b8-2e885f131d58', 'event_created_at': '2024-03-27T07:50:33.869666+00:00', 'source_uid': 'NeMoGuardrails', 'action_uid': '1490ac0f-c7ff-4f99-af28-bc90270c963e', 'action_name': 'generate_value', 'action_params': {'instructions': '# Extract the name of the Vietnamese user. If not specified, say "unknown"'}, 'action_result_key': 'user_name', 'status': 'success', 'is_success': True, 'return_value': 'Ngọc', 'events': [], 'is_system_action': True, 'action_finished_at': '2024-03-27T07:50:33.869681+00:00'}
Event StartInternalSystemAction {'uid': '784eb72b-1399-4fd3-8f9c-5337ba0a379a', 'event_created_at': '2024-03-27T07:50:33.870800+00:00', 'source_uid': 'NeMoGuardrails', 'action_name': 'validate_user_info_details', 'action_params': {'user_info_details': '$user_name'}, 'action_result_key': 'user_name_status', 'action_uid': '19f24996-daa3-4025-a23c-a6101e7161c2', 'is_system_action': False}
Executing action validate_user_info_details
Event InternalSystemActionFinished {'uid': '82d41195-a0f8-455e-8c39-96ffd37e948a', 'event_created_at': '2024-03-27T07:50:33.871187+00:00', 'source_uid': 'NeMoGuardrails', 'action_uid': '19f24996-daa3-4025-a23c-a6101e7161c2', 'action_name': 'validate_user_info_details', 'action_params': {'user_info_details': '$user_name'}, 'action_result_key': 'user_name_status', 'status': 'success', 'is_success': True, 'return_value': True, 'events': [], 'is_system_action': False, 'action_finished_at': '2024-03-27T07:50:33.871199+00:00'}
Event StartInternalSystemAction {'uid': 'b9b4cf80-a27e-41a8-b6f1-f8a1994bd396', 'event_created_at': '2024-03-27T07:50:33.872144+00:00', 'source_uid': 'NeMoGuardrails', 'action_name': 'update_user_info_details', 'action_params': {'user_info_details': '$user_name', 'key': 'user_name'}, 'action_result_key': 'user_info', 'action_uid': 'f81beb22-c59d-4af0-a54b-0d51073a5aad', 'is_system_action': False}
Executing action update_user_info_details
Event InternalSystemActionFinished {'uid': '568be6bf-8dc3-4ebe-a144-645e7ec01bc3', 'event_created_at': '2024-03-27T07:50:33.872512+00:00', 'source_uid': 'NeMoGuardrails', 'action_uid': 'f81beb22-c59d-4af0-a54b-0d51073a5aad', 'action_name': 'update_user_info_details', 'action_params': {'user_info_details': '$user_name', 'key': 'user_name'}, 'action_result_key': 'user_info', 'status': 'success', 'is_success': True, 'return_value': {'user_name': 'Ngọc', 'ship_address': 'unknown', 'phone_number': 'unknown'}, 'events': [], 'is_system_action': False, 'action_finished_at': '2024-03-27T07:50:33.872532+00:00'}

Thank for your attention :D

thanhsang298 avatar Mar 27 '24 07:03 thanhsang298

@thanhsang298: did you find a solution? Is this still of interest? I remember I managed to reproduce a couple of weeks ago, but had limited time to investigate. I can look again if needed.

drazvan avatar Apr 15 '24 14:04 drazvan

I am having the same issue, trying to extract user values

grupocopa avatar Sep 01 '24 19:09 grupocopa

@grupocopa : can you share a minimal snippet to reproduce? Thanks!

drazvan avatar Sep 03 '24 10:09 drazvan

Yeah of course! Its on this link.

Thank you @drazvan!

grupocopa avatar Sep 05 '24 22:09 grupocopa