1+ package com .embabel .joke .agent ;
2+
3+ import com .embabel .agent .api .annotation .AchievesGoal ;
4+ import com .embabel .agent .api .annotation .Action ;
5+ import com .embabel .agent .api .annotation .Agent ;
6+ import com .embabel .agent .api .annotation .Export ;
7+ import com .embabel .agent .api .common .OperationContext ;
8+ import com .embabel .agent .domain .io .UserInput ;
9+ import com .embabel .agent .prompt .persona .Persona ;
10+ import com .embabel .agent .prompt .persona .RoleGoalBackstory ;
11+ import com .embabel .common .ai .model .LlmOptions ;
12+ import org .springframework .beans .factory .annotation .Value ;
13+ import org .springframework .context .annotation .Profile ;
14+
15+ import java .util .List ;
16+
17+ abstract class JokePersonas {
18+ static final RoleGoalBackstory JOKE_WRITER = RoleGoalBackstory
19+ .withRole ("Professional Comedian" )
20+ .andGoal ("Write funny and clever jokes that make people laugh" )
21+ .andBackstory ("Spent 20 years doing stand-up comedy in clubs around the world" );
22+
23+ static final Persona JOKE_CRITIC = Persona .create (
24+ "Comedy Critic" ,
25+ "Late Night Comedy Show Writer" ,
26+ "Sharp, witty, and constructive" ,
27+ "Evaluate jokes for humor, delivery, and audience appeal"
28+ );
29+ }
30+
31+ record Joke (String setup , String punchline ) {
32+ }
33+
34+ record JokeIdea (String topic , String style ) {
35+ }
36+
37+ record RefinedJoke (
38+ Joke originalJoke ,
39+ Joke improvedJoke ,
40+ String critique
41+ ) {
42+ }
43+
44+ @ Agent (description = "Generate jokes based on user input and refine them" )
45+ @ Profile ("!test" )
46+ class JokeAgent {
47+
48+ private final int maxJokeLength ;
49+
50+ JokeAgent (@ Value ("${maxJokeLength:150}" ) int maxJokeLength ) {
51+ this .maxJokeLength = maxJokeLength ;
52+ }
53+
54+ @ AchievesGoal (
55+ description = "A joke has been generated and refined based on feedback" ,
56+ export = @ Export (remote = true , name = "generateAndRefineJoke" ))
57+ @ Action
58+ RefinedJoke refineJoke (UserInput userInput , Joke joke , OperationContext context ) {
59+ var critique = context
60+ .ai ()
61+ .withAutoLlm ()
62+ .withPromptContributor (JokePersonas .JOKE_CRITIC )
63+ .generateText (String .format ("""
64+ Evaluate this joke. Provide constructive feedback on:
65+ - How funny it is
66+ - Whether the punchline lands well
67+ - If it's appropriate for the intended audience
68+ - Suggestions for improvement
69+
70+ Keep your critique brief and actionable.
71+
72+ # The Joke
73+ Setup: %s
74+ Punchline: %s
75+
76+ # Original user request
77+ %s
78+ """ ,
79+ joke .setup (),
80+ joke .punchline (),
81+ userInput .getContent ()
82+ ).trim ());
83+
84+ var improvedJoke = context
85+ .ai ()
86+ .withLlm (LlmOptions .withAutoLlm ().withTemperature (.8 ))
87+ .withPromptContributor (JokePersonas .JOKE_WRITER )
88+ .createObject (String .format ("""
89+ Based on this critique, create an improved version of the joke.
90+ Keep the total length under %d characters.
91+
92+ # Original Joke
93+ Setup: %s
94+ Punchline: %s
95+
96+ # Critique
97+ %s
98+ """ ,
99+ maxJokeLength ,
100+ joke .setup (),
101+ joke .punchline (),
102+ critique
103+ ).trim (), Joke .class );
104+
105+ return new RefinedJoke (joke , improvedJoke , critique );
106+ }
107+
108+ @ Action
109+ Joke writeJoke (JokeIdea jokeIdea , OperationContext context ) {
110+ return context .ai ()
111+ .withLlm (LlmOptions .withAutoLlm ().withTemperature (.9 ))
112+ .withPromptContributor (JokePersonas .JOKE_WRITER )
113+ .createObject (String .format ("""
114+ Write a joke about: %s
115+ Style: %s
116+
117+ The joke should have a clear setup and punchline.
118+ Keep the total length under %d characters.
119+ Make it clever and funny.
120+ """ ,
121+ jokeIdea .topic (),
122+ jokeIdea .style (),
123+ maxJokeLength
124+ ).trim (), Joke .class );
125+ }
126+
127+ @ Action
128+ JokeIdea brainstormJoke (UserInput userInput , OperationContext context ) {
129+ return context .ai ()
130+ .withAutoLlm ()
131+ .createObject (String .format ("""
132+ Based on the user's input, identify:
133+ 1. A topic for a joke (be specific)
134+ 2. A comedy style (e.g., pun, observational, one-liner, knock-knock, etc.)
135+
136+ If the user didn't specify preferences, choose something appropriate and funny.
137+
138+ # User input
139+ %s
140+ """ ,
141+ userInput .getContent ()
142+ ).trim (), JokeIdea .class );
143+ }
144+ }
0 commit comments