1515 */
1616package com .embabel .template .agent ;
1717
18- import com .embabel .agent .api .annotation .AchievesGoal ;
19- import com .embabel .agent .api .annotation .Action ;
20- import com .embabel .agent .api .annotation .Agent ;
21- import com .embabel .agent .api .annotation .Export ;
18+ import com .embabel .agent .api .annotation .*;
2219import com .embabel .agent .api .common .Ai ;
2320import com .embabel .agent .domain .io .UserInput ;
2421import com .embabel .agent .domain .library .HasContent ;
@@ -90,52 +87,29 @@ public String getContent() {
9087 }
9188 }
9289
93- private final int storyWordCount ;
94- private final int reviewWordCount ;
90+ @ State
91+ interface Stage {
92+ }
93+
94+ record Properties (
95+ int storyWordCount ,
96+ int reviewWordCount
97+ ) {
98+ }
99+
100+ private final Properties properties ;
95101
96102 WriteAndReviewAgent (
97103 @ Value ("${storyWordCount:100}" ) int storyWordCount ,
98104 @ Value ("${reviewWordCount:100}" ) int reviewWordCount
99105 ) {
100- this .storyWordCount = storyWordCount ;
101- this .reviewWordCount = reviewWordCount ;
106+ this .properties = new Properties (storyWordCount , reviewWordCount );
102107 }
103108
104- @ AchievesGoal (
105- description = "The story has been crafted and reviewed by a book reviewer" ,
106- export = @ Export (remote = true , name = "writeAndReviewStory" ))
107- @ Action
108- ReviewedStory reviewStory (UserInput userInput , Story story , Ai ai ) {
109- var review = ai
110- .withAutoLlm ()
111- .withPromptContributor (Personas .REVIEWER )
112- .generateText (String .format ("""
113- You will be given a short story to review.
114- Review it in %d words or less.
115- Consider whether or not the story is engaging, imaginative, and well-written.
116- Also consider whether the story is appropriate given the original user input.
117-
118- # Story
119- %s
120-
121- # User input that inspired the story
122- %s
123- """ ,
124- reviewWordCount ,
125- story .text (),
126- userInput .getContent ()
127- ).trim ());
128-
129- return new ReviewedStory (
130- story ,
131- review ,
132- Personas .REVIEWER
133- );
134- }
135109
136110 @ Action
137- Story craftStory (UserInput userInput , Ai ai ) {
138- return ai
111+ AssessStory craftStory (UserInput userInput , Ai ai ) {
112+ var draft = ai
139113 // Higher temperature for more creative output
140114 .withLlm (LlmOptions
141115 .withAutoLlm () // You can also choose a specific model or role here
@@ -151,8 +125,135 @@ Story craftStory(UserInput userInput, Ai ai) {
151125 # User input
152126 %s
153127 """ ,
154- storyWordCount ,
128+ properties . storyWordCount ,
155129 userInput .getContent ()
156130 ).trim (), Story .class );
131+ return new AssessStory (userInput , draft , properties );
132+ }
133+
134+ /**
135+ * We prompt the user for this
136+ *
137+ * @param comments
138+ */
139+ record HumanFeedback (String comments ) {
140+ }
141+
142+ private record AssessmentOfHumanFeedback (boolean acceptable ) {
157143 }
144+
145+ @ State
146+ record AssessStory (UserInput userInput , Story story , Properties properties ) implements Stage {
147+
148+ @ Action
149+ HumanFeedback getFeedback () {
150+ return WaitFor .formSubmission ("""
151+ Please provide feedback on the story
152+ %s
153+ """ .formatted (story .text ),
154+ HumanFeedback .class );
155+ }
156+
157+ @ Action
158+ Stage assess (HumanFeedback feedback , Ai ai ) {
159+ var assessment = ai .withDefaultLlm ().createObject ("""
160+ Based on the following human feedback, determine if the story is acceptable.
161+ Return true if the story is acceptable, false otherwise.
162+
163+ # Story
164+ %s
165+
166+ # Human feedback
167+ %s
168+
169+
170+ Return your answer as a JSON object with a single field "acceptable" set to true or false.
171+ """ .formatted (
172+ story .text (),
173+ feedback .comments
174+ )
175+ , AssessmentOfHumanFeedback .class );
176+ if (assessment .acceptable ) {
177+ return new Done (userInput , story , properties );
178+ } else {
179+ return new ReviseStory (userInput , story , feedback , properties );
180+ }
181+ }
182+
183+ }
184+
185+ @ State
186+ record ReviseStory (UserInput userInput , Story story , HumanFeedback humanFeedback ,
187+ Properties properties ) implements Stage {
188+
189+ @ Action
190+ AssessStory reviseStory (Ai ai ) {
191+ var draft = ai
192+ // Higher temperature for more creative output
193+ .withLlm (LlmOptions
194+ .withAutoLlm () // You can also choose a specific model or role here
195+ .withTemperature (.7 )
196+ )
197+ .withPromptContributor (Personas .WRITER )
198+ .createObject (String .format ("""
199+ Revise a short story in %d words or less.
200+ The story should be engaging and imaginative.
201+ Use the user's input as inspiration if possible.
202+ If the user has provided a name, include it in the story.
203+
204+ # User input
205+ %s
206+
207+ # Previous story
208+ %s
209+
210+ # Revision instructions
211+ %s
212+ """ ,
213+ properties .storyWordCount ,
214+ userInput .getContent (),
215+ story .text (),
216+ humanFeedback .comments
217+ ).trim (), Story .class );
218+ return new AssessStory (userInput , draft , properties );
219+ }
220+ }
221+
222+ @ State
223+ record Done (UserInput userInput , Story story , Properties properties ) implements Stage {
224+
225+ @ AchievesGoal (
226+ description = "The story has been crafted and reviewed by a book reviewer" ,
227+ export = @ Export (remote = true , name = "writeAndReviewStory" ))
228+ @ Action
229+ ReviewedStory reviewStory (Ai ai ) {
230+ var review = ai
231+ .withAutoLlm ()
232+ .withPromptContributor (Personas .REVIEWER )
233+ .generateText (String .format ("""
234+ You will be given a short story to review.
235+ Review it in %d words or less.
236+ Consider whether or not the story is engaging, imaginative, and well-written.
237+ Also consider whether the story is appropriate given the original user input.
238+
239+ # Story
240+ %s
241+
242+ # User input that inspired the story
243+ %s
244+ """ ,
245+ properties .reviewWordCount ,
246+ story .text (),
247+ userInput .getContent ()
248+ ).trim ());
249+
250+ return new ReviewedStory (
251+ story ,
252+ review ,
253+ Personas .REVIEWER
254+ );
255+ }
256+ }
257+
258+
158259}
0 commit comments