Repository navigation
Expand file tree
/
Copy pathwhen.py
More file actions
67 lines (45 loc) · 1.79 KB
/
Copy pathwhen.py
File metadata and controls
67 lines (45 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from lingo import Lingo, Context, Engine
from lingo.cli import loop
from lingo.llm import Message
from dotenv import load_dotenv
load_dotenv()
# Initialize the bot
bot = Lingo(
name="HelpDesk", description="A helpful customer support agent for a SaaS platform."
)
# --- PATTERN 1: PREEMPTIVE GUARDRAIL ---
# If this triggers, we reply and STOP. The main skill never runs.
@bot.when("The user is asking for illegal actions or using abusive language")
async def security_guardrail(ctx: Context, engine: Engine):
print(">>> [GUARDRAIL TRIGGERED] Blocking request.")
# 1. Reply to the user directly
await engine.reply(
ctx, "I cannot assist with that request as it violates our policy."
)
# 2. STOP execution immediately.
# This raises StopFlow, preventing the main 'support_skill' from running.
engine.stop()
# --- PATTERN 2: PASSIVE CONTEXT INJECTION ---
# If this triggers, we just add info to the context and let the flow continue.
@bot.when("The user seems frustrated or angry")
async def sentiment_analyzer(ctx: Context, engine: Engine):
print(">>> [SENTIMENT DETECTED] Injecting guidance.")
# We do NOT reply to the user. We add a system instruction for the Agent.
# Because we do NOT call engine.stop(), the flow continues to the main skill.
ctx.append(
Message.system(
"SYSTEM NOTE: The user is frustrated. "
"Prioritize empathy and keep responses concise."
)
)
# --- MAIN SKILL ---
# This only runs if the Guardrail didn't stop the flow.
@bot.skill
async def support_skill(ctx: Context, engine: Engine):
# Standard ReAct behavior or simple reply
await engine.reply(ctx, "How can I help you with your account today?")
# --- SIMULATION ---
def main():
loop(bot)
if __name__ == "__main__":
main()