11import guidance
22from ._block import block
33
4+ nodisp_start = "<||_#NODISP_||>"
5+ nodisp_end = "<||_/NODISP_||>"
6+ span_start = "<||_html:<span style='background-color: rgba(255, 180, 0, 0.3); border-radius: 3px;'>_||>"
7+ span_end = "<||_html:</span>_||>"
8+
9+
410@guidance
511def role_opener (lm , role_name , debug = False , ** kwargs ):
612 if not hasattr (lm , "get_role_start" ):
7- raise Exception (f"You need to use a chat model in order the use role blocks like `with { role_name } ():`! Perhaps you meant to use the { type (lm ).__name__ } Chat class?" )
13+ raise Exception (
14+ f"You need to use a chat model in order the use role blocks like `with { role_name } ():`! Perhaps you meant to use the { type (lm ).__name__ } Chat class?"
15+ )
16+
17+ # Block start container (centers elements)
818 lm += f"<||_html:<div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); justify-content: center; align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>{ role_name .lower ()} </div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>_||>"
9-
19+
20+ # Start of either debug or HTML no disp block
1021 if debug :
11- lm += f"<||_html:<span style='background-color: rgba(255, 165, 0); border-radius: 3px;'>_||>"
22+ lm += span_start
1223 else :
13- lm += "<||_#NODISP_||>"
24+ lm += nodisp_start
1425
1526 lm += lm .get_role_start (role_name , ** kwargs )
16-
27+
28+ # End of either debug or HTML no disp block
1729 if debug :
18- lm += "<||_html:</span>_||>"
30+ lm += span_end
1931 else :
20- lm += "<||_/NODISP_||>"
32+ lm += nodisp_end
2133
2234 return lm
2335
36+
2437@guidance
2538def role_closer (lm , role_name , debug = False , ** kwargs ):
26-
39+ # Start of either debug or HTML no disp block
2740 if debug :
28- lm += f"<||_html:<span style='background-color: rgba(255, 165, 0); border-radius: 3px;'>_||>"
41+ lm += span_start
2942 else :
30- lm += "<||_#NODISP_||>"
31-
43+ lm += nodisp_start
44+
3245 lm += lm .get_role_end (role_name )
33-
46+
47+ # End of either debug or HTML no disp block
3448 if debug :
35- lm += "<||_html:</span>_||>"
49+ lm += span_end
3650 else :
37- lm += "<||_/NODISP_||>"
51+ lm += nodisp_end
3852
53+ # End of top container
3954 lm += "<||_html:</div></div>_||>"
4055
4156 return lm
4257
58+
4359def role (role_name , text = None , ** kwargs ):
4460 if text is None :
45- return block (opener = role_opener (role_name , ** kwargs ), closer = role_closer (role_name , ** kwargs ))
61+ return block (
62+ opener = role_opener (role_name , ** kwargs ),
63+ closer = role_closer (role_name , ** kwargs ),
64+ )
4665 else :
4766 assert False
48- #return self.append(open_text + text + close_text)
67+ # return self.append(open_text + text + close_text)
68+
4969
5070def system (text = None , ** kwargs ):
5171 return role ("system" , text , ** kwargs )
5272
73+
5374def user (text = None , ** kwargs ):
5475 return role ("user" , text , ** kwargs )
5576
77+
5678def assistant (text = None , ** kwargs ):
5779 return role ("assistant" , text , ** kwargs )
5880
81+
5982def function (text = None , ** kwargs ):
6083 return role ("function" , text , ** kwargs )
6184
85+
6286def instruction (text = None , ** kwargs ):
63- return role ("instruction" , text , ** kwargs )
87+ return role ("instruction" , text , ** kwargs )
0 commit comments