33from langchain .agents import create_agent
44from langchain_core .messages import HumanMessage
55from langchain_core .tools import tool
6- from langgraph .constants import START , END
6+ from langgraph .constants import END , START
77from langgraph .graph import StateGraph
8+ from middleware import CustomFilterAction , LoggingMiddleware
89from pydantic import BaseModel
910from uipath .core .guardrails import GuardrailScope
1011
11- from middleware import CustomFilterAction , LoggingMiddleware
1212from uipath_langchain .chat import UiPathChat
1313from uipath_langchain .guardrails import (
1414 BlockAction ,
15- PIIDetectionEntity ,
1615 GuardrailExecutionStage ,
16+ HarmfulContentEntity ,
1717 LogAction ,
18+ PIIDetectionEntity ,
1819 UiPathDeterministicGuardrailMiddleware ,
20+ UiPathHarmfulContentMiddleware ,
21+ UiPathIntellectualPropertyMiddleware ,
1922 UiPathPIIDetectionMiddleware ,
20- UiPathPromptInjectionMiddleware ,
23+ UiPathUserPromptAttacksMiddleware ,
2124)
2225from uipath_langchain .guardrails .actions import LoggingSeverityLevel
23- from uipath_langchain .guardrails .enums import PIIDetectionEntityType
26+ from uipath_langchain .guardrails .enums import (
27+ HarmfulContentEntityType ,
28+ IntellectualPropertyEntityType ,
29+ PIIDetectionEntityType ,
30+ )
2431
2532
2633# Define input schema for the agent
2734class Input (BaseModel ):
2835 """Input schema for the joke agent."""
36+
2937 topic : str
3038
3139
3240class Output (BaseModel ):
3341 """Output schema for the joke agent."""
42+
3443 joke : str
3544
3645
@@ -57,6 +66,7 @@ def analyze_joke_syntax(joke: str) -> str:
5766
5867 return f"Words number: { word_count } \n Letters: { letter_count } "
5968
69+
6070# System prompt based on agent1.json
6171SYSTEM_PROMPT = """You are an AI assistant designed to generate family-friendly jokes. Your process is as follows:
6272
@@ -104,12 +114,25 @@ def analyze_joke_syntax(joke: str) -> str:
104114 tools = [analyze_joke_syntax ],
105115 enabled_for_evals = False ,
106116 ),
107- * UiPathPromptInjectionMiddleware (
108- name = "Prompt Injection Detection" ,
117+ * UiPathUserPromptAttacksMiddleware (
118+ name = "User Prompt Attacks Detection" ,
109119 action = BlockAction (),
110- threshold = 0.5 ,
111120 enabled_for_evals = False ,
112121 ),
122+ * UiPathHarmfulContentMiddleware (
123+ name = "Harmful Content Detection" ,
124+ scopes = [GuardrailScope .AGENT , GuardrailScope .LLM ],
125+ action = BlockAction (),
126+ entities = [
127+ HarmfulContentEntity (HarmfulContentEntityType .VIOLENCE , threshold = 2 ),
128+ ],
129+ ),
130+ * UiPathIntellectualPropertyMiddleware (
131+ name = "Intellectual Property Detection" ,
132+ scopes = [GuardrailScope .LLM ],
133+ action = LogAction (severity_level = LoggingSeverityLevel .WARNING ),
134+ entities = [IntellectualPropertyEntityType .TEXT ],
135+ ),
113136 # Custom FilterAction example: demonstrates how developers can implement their own actions
114137 * UiPathDeterministicGuardrailMiddleware (
115138 tools = [analyze_joke_syntax ],
@@ -142,7 +165,7 @@ def analyze_joke_syntax(joke: str) -> str:
142165 ),
143166 stage = GuardrailExecutionStage .POST ,
144167 name = "Joke Content Always Filter" ,
145- )
168+ ),
146169 ],
147170)
148171
@@ -152,7 +175,9 @@ async def joke_node(state: Input) -> Output:
152175 """Convert topic to messages, call agent, and extract joke."""
153176 # Convert topic to messages format
154177 messages = [
155- HumanMessage (content = f"Generate a family-friendly joke based on the topic: { state .topic } " )
178+ HumanMessage (
179+ content = f"Generate a family-friendly joke based on the topic: { state .topic } "
180+ )
156181 ]
157182
158183 # Call the agent with messages
0 commit comments