-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathWriteAndReviewAgent.java
More file actions
157 lines (137 loc) · 5.64 KB
/
Copy pathWriteAndReviewAgent.java
File metadata and controls
157 lines (137 loc) · 5.64 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright 2024-2025 Embabel Software, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.embabel.template.agent;
import com.embabel.agent.api.annotation.AchievesGoal;
import com.embabel.agent.api.annotation.Action;
import com.embabel.agent.api.annotation.Agent;
import com.embabel.agent.api.annotation.Export;
import com.embabel.agent.api.common.Ai;
import com.embabel.agent.domain.io.UserInput;
import com.embabel.agent.domain.library.HasContent;
import com.embabel.agent.prompt.persona.Persona;
import com.embabel.agent.prompt.persona.RoleGoalBackstory;
import com.embabel.common.ai.model.LlmOptions;
import com.embabel.common.core.types.Timestamped;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.NonNull;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
abstract class Personas {
static final RoleGoalBackstory WRITER = new RoleGoalBackstory(
"Creative Storyteller",
"Write engaging and imaginative stories",
"Has a PhD in French literature; used to work in a circus");
static final Persona REVIEWER = new Persona(
"Media Book Review",
"New York Times Book Reviewer",
"Professional and insightful",
"Help guide readers toward good stories"
);
}
@Agent(description = "Generate a story based on user input and review it")
public class WriteAndReviewAgent {
public record Story(String text) {
}
public record ReviewedStory(
Story story,
String review,
Persona reviewer
) implements HasContent, Timestamped {
@Override
@NonNull
public Instant getTimestamp() {
return Instant.now();
}
@Override
@NonNull
public String getContent() {
return String.format("""
# Story
%s
# Review
%s
# Reviewer
%s, %s
""",
story.text(),
review,
reviewer.getName(),
getTimestamp().atZone(ZoneId.systemDefault())
.format(DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy"))
).trim();
}
}
private final int storyWordCount;
private final int reviewWordCount;
WriteAndReviewAgent(
@Value("${storyWordCount:100}") int storyWordCount,
@Value("${reviewWordCount:100}") int reviewWordCount
) {
this.storyWordCount = storyWordCount;
this.reviewWordCount = reviewWordCount;
}
@AchievesGoal(
description = "The story has been crafted and reviewed by a book reviewer",
export = @Export(remote = true, name = "writeAndReviewStory"))
@Action
ReviewedStory reviewStory(UserInput userInput, Story story, Ai ai) {
var review = ai
.withAutoLlm()
.withPromptContributor(Personas.REVIEWER)
.generateText(String.format("""
You will be given a short story to review.
Review it in %d words or less.
Consider whether or not the story is engaging, imaginative, and well-written.
Also consider whether the story is appropriate given the original user input.
# Story
%s
# User input that inspired the story
%s
""",
reviewWordCount,
story.text(),
userInput.getContent()
).trim());
return new ReviewedStory(
story,
review,
Personas.REVIEWER
);
}
@Action
Story craftStory(UserInput userInput, Ai ai) {
return ai
// Higher temperature for more creative output
.withLlm(LlmOptions
.withAutoLlm() // You can also choose a specific model or role here
.withTemperature(.7)
)
.withPromptContributor(Personas.WRITER)
.creating(Story.class)
.fromPrompt(String.format("""
Craft a short story in %d words or less.
The story should be engaging and imaginative.
Use the user's input as inspiration if possible.
If the user has provided a name, include it in the story.
# User input
%s
""",
storyWordCount,
userInput.getContent()
).trim());
}
}