Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/colang meta decorator #425

Merged
merged 6 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,13 @@
"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"
}
},
{
"name": "Debug story very verbose (current directory)",
"type": "debugpy",
"request": "launch",
"console": "integratedTerminal",
"module": "nemoguardrails",
"args": [
"chat",
"--config=${fileDirname}",
"--verbose",
"--debug-level=DEBUG"
],
"justMyCode": true,
"env": {
"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"
}
},
{
"name": "Debug story verbose (current directory)",
"type": "debugpy",
"request": "launch",
"console": "integratedTerminal",
"module": "nemoguardrails",
"args": [
"chat",
"--config=${fileDirname}",
"--verbose",
"--verbose-llm-calls",
"--debug-level=INFO"
],
"args": ["chat", "--config=${fileDirname}", "--debug-level=DEBUG"],
"justMyCode": true,
"env": {
"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"
Expand All @@ -52,7 +29,7 @@
"request": "launch",
"console": "integratedTerminal",
"module": "nemoguardrails",
"args": ["chat", "--config=${fileDirname}", "--verbose"],
"args": ["chat", "--config=${fileDirname}", "--debug-level=INFO"],
"justMyCode": true,
"env": {
"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"
Expand All @@ -64,7 +41,7 @@
"request": "launch",
"console": "integratedTerminal",
"module": "nemoguardrails",
"args": ["chat", "--config=${fileDirname}", "--verbose-llm-calls"],
"args": ["chat", "--config=${fileDirname}", "--verbose"],
"justMyCode": true,
"env": {
"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"
Expand Down
15 changes: 15 additions & 0 deletions nemoguardrails/actions/llm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,19 @@ def get_colang_history(
action_group: List[InternalEvent] = []
current_intent: Optional[str] = None

previous_event = None
for event in events:
if not isinstance(event, InternalEvent):
# Skip non-internal events
continue

if (
event.name == InternalEvents.USER_ACTION_LOG
and previous_event
and events_to_dialog_history([previous_event])
== events_to_dialog_history([event])
):
# Remove duplicated user action log events that stem from the same user event as the previous event
continue

if (
Expand All @@ -191,6 +202,8 @@ def get_colang_history(

action_group.append(event)
current_intent = event.arguments["intent_flow_id"]

previous_event = event
elif (
event.name == InternalEvents.BOT_INTENT_LOG
or event.name == InternalEvents.USER_INTENT_LOG
Expand All @@ -215,6 +228,8 @@ def get_colang_history(
action_group.clear()
current_intent = None

previous_event = event

if action_group:
new_history.append(events_to_dialog_history(action_group))

Expand Down
18 changes: 6 additions & 12 deletions nemoguardrails/actions/v2_x/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,9 @@ async def _init_flows_index(self) -> None:
if colang_flow:
assert isinstance(flow, Flow)
# Check if we need to exclude this flow.
# TODO: deprecate the use of the "# meta: " comment in favor of
# @meta(llm_exclude=True)
if "# meta: exclude from llm" in colang_flow or (
"exclude_from_llm" not in flow.file_info
or flow.file_info["exclude_from_llm"]
or (
"meta" in flow.decorators
and flow.decorators["meta"].parameters.get("llm_exclude")
)
if flow.file_info.get("exclude_from_llm") or (
"meta" in flow.decorators
and flow.decorators["meta"].parameters.get("llm_exclude")
):
continue

Expand Down Expand Up @@ -194,7 +188,7 @@ async def generate_user_intent(
if isinstance(flow_id, str) and (
flow_config is None
or (
"# meta: user intent" in flow_config.source_code
flow_config.has_meta_tag("user_intent")
and flow_id not in potential_user_intents
)
):
Expand Down Expand Up @@ -453,8 +447,8 @@ async def generate_flow_continuation(
return {
"name": flow_name,
"parameters": flow_parameters,
"body": f"flow {flow_name}\n"
+ f' # meta: bot intent = "{intent}"\n'
"body": f'@meta(bot_intent="{intent}")\n'
+ f"flow {flow_name}\n"
+ "\n".join([" " + l.strip(" ") for l in lines]),
}

Expand Down
10 changes: 6 additions & 4 deletions nemoguardrails/colang/v2_x/lang/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def _flow_def(self, children: dict, meta: Meta) -> Flow:
)

def _remove_source_code_comments(self, source: str) -> str:
pattern = r"#(?! (llm:|meta:))[^\n]*"
pattern = r"#[^\n]*"
return re.sub(pattern, "", source)

def _spec_op(self, children: list, meta: Meta) -> SpecOp:
Expand Down Expand Up @@ -558,9 +558,11 @@ def __default__(self, data, children: list, meta: Meta) -> dict:

# Transform tokens to dicts
children = [
child
if not isinstance(child, Token)
else {"_type": child.type, "elements": [child.value]}
(
child
if not isinstance(child, Token)
else {"_type": child.type, "elements": [child.value]}
)
for child in children
]

Expand Down
48 changes: 24 additions & 24 deletions nemoguardrails/colang/v2_x/library/avatars.co
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# meta: exclude from llm

################################################################
# COLANG 2.0 AVATARS CORE LIBRARY
# VERSION 0.2.0
Expand Down Expand Up @@ -35,13 +33,15 @@
#-------
################################################################

# meta: exclude from llm

# -----------------------------------
# User UMIM event wrapper flows
# DON'T CHANGE! Currently, hard-wired with LLM prompt generation
# -----------------------------------

@meta(user_action='user said "{$transcript}"')
flow user said $text -> $transcript
# meta: user action
match UtteranceUserAction.Finished(final_transcript=$text) as $event
$transcript = $event.final_transcript

Expand Down Expand Up @@ -71,8 +71,8 @@ flow user saying something -> $transcript
#send UserActionLog(flow_id="user saying", parameter=$event.interim_transcript, intent_flow_id="user saying something")
$transcript = $event.interim_transcript

@meta(user_action='selected choice "{$choice}"')
flow user selected choice $choice_id -> $choice
# meta: user action
match VisualChoiceSceneAction.ChoiceUpdated(current_choice=[$choice_id]) as $event
$choice = $event.current_choice

Expand All @@ -86,9 +86,9 @@ flow unhandled user intent -> $intent
$intent = $event.flow_id

@loop("user_was_silent")
@meta(user_intent=True)
flow user was silent $time_s
"""Triggers when user was silent for $time_s seconds."""
# meta: user intent
while True
start wait $time_s as $timer_ref
when $timer_ref.Finished()
Expand All @@ -100,9 +100,9 @@ flow user was silent $time_s
send $timer_ref.Stop()

@loop("user_did_not_respond")
@meta(user_intent=True)
flow user didnt respond $time_s
"""Triggers when user was silent for $time_s seconds while bot was silent."""
# meta: user intent
while True
start wait $time_s as $timer_ref
when $timer_ref.Finished()
Expand All @@ -116,9 +116,9 @@ flow user didnt respond $time_s
orwhen UtteranceUserAction.Finished() or UtteranceBotAction.Finished()
send $timer_ref.Stop()

@meta(user_intent=True)
flow user interrupted bot talking $sentence_length=15
"""Triggers when the user talked while bot is speaking."""
# meta: user intent
global $bot_talking_state
while True
if $bot_talking_state
Expand Down Expand Up @@ -197,15 +197,15 @@ flow bot started a posture -> $posture
match FlowStarted(flow_id="bot posture") as $event
$posture = $event.posture

@meta(bot_action=True)
flow bot started an action -> $action
# meta: bot action
match bot started saying something as $action
or bot started a gesture as $action
or bot started a posture as $action

@loop("bot_was_silent")
@meta(bot_intent=True)
flow bot was silent $time_s
# meta: bot intent
while True
start wait $time_s as $timer_ref
when $timer_ref.Finished()
Expand All @@ -225,70 +225,70 @@ flow _bot_say $text
"""It's an internal helper for higher semantic level flows"""
await UtteranceBotAction(script=$text) as $action

@meta(bot_action=True)
flow bot gesture $gesture
# meta: bot action
await GestureBotAction(gesture=$gesture) as $action

@meta(bot_action=True)
flow bot gesture with delay $gesture $delay
# meta: bot action
wait $delay
bot gesture $gesture

@meta(bot_action=True)
flow bot posture $posture
# meta: bot action
await PostureBotAction(posture=$posture) as $action

@meta(bot_action=True)
flow scene show choice $prompt
# meta: bot action
await VisualChoiceSceneAction(prompt=$prompt,choice_type="selection", allow_multiple_choices=False) as $action

@meta(bot_action=True)
flow scene show textual information $title $text $header_image
# meta: bot action
await VisualInformationSceneAction(title=$title, support_prompts=[], content=[{"image":$header_image},{"text":$text}]) as $action

@meta(bot_action=True)
flow scene show short information $info
# meta: bot action
await VisualInformationSceneAction(title=$info, support_prompts=[], content=[]) as $action

@meta(bot_action=True)
flow scene show form $prompt
# meta: bot action
await VisualInformationSceneAction(prompt=$prompt) as $action

# ----------------------------------
# Bot action semantic wrapper flows
# DON'T CHANGE! Currently, hard-wired with LLM prompt generation
# -----------------------------------

@meta(bot_action=True)
flow bot say $text
# meta: bot action
await _bot_say $text

flow bot say something like $text
$variation = i"Return a single string that is a new variation of: {$text}"
await bot say $variation

@meta(bot_action=True)
flow bot inform $text
# meta: bot action
await _bot_say $text

@meta(bot_action=True)
flow bot ask $text
# meta: bot action
await _bot_say $text

@meta(bot_action=True)
flow bot express $text
# meta: bot action
await _bot_say $text

@meta(bot_action=True)
flow bot respond $text
# meta: bot action
await _bot_say $text

@meta(bot_action=True)
flow bot clarify $text
# meta: bot action
await _bot_say $text

@meta(bot_action=True)
flow bot suggest $text
# meta: bot action
await _bot_say $text

# ----------------------------------
Expand Down Expand Up @@ -529,8 +529,8 @@ flow execute llm instruction $instructions
match $event_ref.flow.Finished()
await RemoveFlowsAction(flow_ids=[$flow_info.name])

@meta(user_intent=True)
flow user requested a task
# meta: user intent
user said "do something"
or user said "can you do something"
or user said "please do"
Expand Down
4 changes: 2 additions & 2 deletions nemoguardrails/colang/v2_x/library/core.co
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@meta(user_action='user said "{$transcript}"')
flow user said $text -> $transcript
# meta: user action
match UtteranceUserAction.Finished(final_transcript=$text) as $event
$transcript = $event.final_transcript

Expand All @@ -16,6 +16,6 @@ flow _bot_say $text
"""It's an internal helper for higher semantic level flows"""
await UtteranceBotAction(script=$text) as $action

@meta(bot_action=True)
flow bot say $text
# meta: bot action
await _bot_say $text
Loading
Loading