|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 Embabel Software, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.embabel.template.agent |
| 17 | + |
| 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.using |
| 22 | +import com.embabel.agent.api.common.OperationContext |
| 23 | +import com.embabel.agent.api.common.create |
| 24 | +import com.embabel.agent.domain.io.UserInput |
| 25 | +import com.embabel.agent.domain.library.HasContent |
| 26 | +import com.embabel.agent.prompt.Persona |
| 27 | +import com.embabel.common.ai.model.LlmOptions |
| 28 | +import com.embabel.common.ai.model.ModelSelectionCriteria.Companion.Auto |
| 29 | +import com.embabel.common.core.types.Timestamped |
| 30 | +import org.springframework.beans.factory.annotation.Value |
| 31 | +import org.springframework.context.annotation.Profile |
| 32 | +import java.time.Instant |
| 33 | +import java.time.ZoneId |
| 34 | +import java.time.format.DateTimeFormatter |
| 35 | + |
| 36 | +val StoryTeller = Persona( |
| 37 | + name = "Roald Dahl", |
| 38 | + persona = "A creative storyteller who loves to weave imaginative tales that are a bit unconventional", |
| 39 | + voice = "Quirky", |
| 40 | + objective = "Create memorable stories that captivate the reader's imagination.", |
| 41 | +) |
| 42 | + |
| 43 | +val Reviewer = Persona( |
| 44 | + name = "Media Book Review", |
| 45 | + persona = "New York Times Book Reviewer", |
| 46 | + voice = "Professional and insightful", |
| 47 | + objective = "Help guide readers toward good stories", |
| 48 | +) |
| 49 | + |
| 50 | +data class Story( |
| 51 | + val text: String, |
| 52 | +) |
| 53 | + |
| 54 | +data class ReviewedStory( |
| 55 | + val story: Story, |
| 56 | + val review: String, |
| 57 | + val reviewer: Persona, |
| 58 | +) : HasContent, Timestamped { |
| 59 | + |
| 60 | + override val timestamp: Instant |
| 61 | + get() = Instant.now() |
| 62 | + |
| 63 | + override val content: String |
| 64 | + get() = """ |
| 65 | + # Story |
| 66 | + ${story.text} |
| 67 | +
|
| 68 | + # Review |
| 69 | + $review |
| 70 | +
|
| 71 | + # Reviewer |
| 72 | + ${reviewer.name}, ${ |
| 73 | + timestamp.atZone(ZoneId.systemDefault()) |
| 74 | + .format(DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy")) |
| 75 | + } |
| 76 | + """.trimIndent() |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +@Agent( |
| 81 | + description = "Generate a story based on user input and review it", |
| 82 | +) |
| 83 | +@Profile("!test") |
| 84 | +class WriteAndReviewAgent( |
| 85 | + @Value("\${storyWordCount:100}") private val storyWordCount: Int, |
| 86 | + @Value("\${reviewWordCount:100}") private val reviewWordCount: Int, |
| 87 | +) { |
| 88 | + |
| 89 | + @Action |
| 90 | + fun craftStory(userInput: UserInput): Story = |
| 91 | + using( |
| 92 | + LlmOptions(criteria = Auto) |
| 93 | + .withTemperature(.9), // Higher temperature for more creative output |
| 94 | + ).withPromptContributor(StoryTeller) |
| 95 | + .create( |
| 96 | + """ |
| 97 | + Craft a short story in $storyWordCount words or less. |
| 98 | + The story should be engaging and imaginative. |
| 99 | + Use the user's input as inspiration if possible. |
| 100 | + If the user has provided a name, include it in the story. |
| 101 | +
|
| 102 | + # User input |
| 103 | + ${userInput.content} |
| 104 | + """.trimIndent() |
| 105 | + ) |
| 106 | + |
| 107 | + @AchievesGoal("The user has been greeted") |
| 108 | + @Action |
| 109 | + fun reviewStory(userInput: UserInput, story: Story, context: OperationContext): ReviewedStory { |
| 110 | + val review = context.promptRunner( |
| 111 | + LlmOptions(criteria = Auto) |
| 112 | + ).withPromptContributor(Reviewer) |
| 113 | + .generateText( |
| 114 | + """ |
| 115 | + You will be given a short story to review. |
| 116 | + Review it in $reviewWordCount words or less. |
| 117 | + Consider whether or not the story is engaging, imaginative, and well-written. |
| 118 | + Also consider whether the story is appropriate given the original user input. |
| 119 | +
|
| 120 | + # Story |
| 121 | + ${story.text} |
| 122 | +
|
| 123 | + # User input that inspired the story |
| 124 | + ${userInput.content} |
| 125 | + """.trimIndent() |
| 126 | + ) |
| 127 | + return ReviewedStory( |
| 128 | + story = story, |
| 129 | + review = review, |
| 130 | + reviewer = Reviewer, |
| 131 | + ) |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments