Skip to content

Commit a443313

Browse files
committed
Upgrade to 0.1.3
1 parent 5210f84 commit a443313

6 files changed

Lines changed: 82 additions & 55 deletions

File tree

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ Run the shell script to start Embabel under Spring Shell:
4040
./scripts/shell.sh
4141
```
4242

43-
There is a single example agent, `WriteAndReviewAgent`.
43+
There is a single example
44+
agent, [WriteAndReviewAgent](./src/main/java/com/embabel/template/agent/WriteAndReviewAgent.java).
4445
It uses one LLM with a high temperature and creative persona to write a story based on your input,
4546
then another LLM with a low temperature and different persona to review the story.
4647

@@ -50,13 +51,13 @@ When the Embabel shell comes up, invoke the story agent like this:
5051
x "Tell me a story about...[your topic]"
5152
```
5253

53-
Try the `InjectedDemo` command to see simple, non-agent use.
54-
Type this in the interactive shell to see a single prompt create
55-
an imaginary animal:
54+
Try the following other shell commands:
5655

57-
```java
58-
animal
59-
```
56+
- `demo`: Runs the same agent, invoked programmatically, instead of dynamically based on user input.
57+
See [DemoCommands.java](./src/main/java/com/embabel/template/DemoShell.java) for the
58+
implementation.
59+
- `animal`: Runs a simple demo using an Embabel injected `Ai` instance to call an LLM.
60+
See [InjectedDemo](./src/main/java/com/embabel/template/injected/InjectedDemo.java).
6061

6162
## Suggested Next Steps
6263

pom.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<properties>
2020
<java.version>21</java.version>
21-
<embabel-agent.version>0.1.2</embabel-agent.version>
21+
<embabel-agent.version>0.1.3</embabel-agent.version>
2222
</properties>
2323

2424
<dependencies>
@@ -30,6 +30,19 @@
3030
<version>${embabel-agent.version}</version>
3131
</dependency>
3232

33+
<dependency>
34+
<groupId>com.embabel.agent</groupId>
35+
<artifactId>embabel-agent-starter-openai</artifactId>
36+
<version>${embabel-agent.version}</version>
37+
</dependency>
38+
39+
<!-- Uncomment to use Anthropic models -->
40+
<!-- <dependency>-->
41+
<!-- <groupId>com.embabel.agent</groupId>-->
42+
<!-- <artifactId>embabel-agent-starter-anthropic</artifactId>-->
43+
<!-- <version>${embabel-agent.version}</version>-->
44+
<!-- </dependency>-->
45+
3346
<dependency>
3447
<groupId>com.embabel.agent</groupId>
3548
<artifactId>embabel-agent-starter-shell</artifactId>

src/main/java/com/embabel/template/DemoShell.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
package com.embabel.template;
22

3+
import com.embabel.agent.api.common.autonomy.AgentInvocation;
4+
import com.embabel.agent.core.AgentPlatform;
5+
import com.embabel.agent.domain.io.UserInput;
6+
import com.embabel.template.agent.WriteAndReviewAgent;
37
import com.embabel.template.injected.InjectedDemo;
48
import org.springframework.shell.standard.ShellComponent;
59
import org.springframework.shell.standard.ShellMethod;
610

711
@ShellComponent
8-
record DemoShell(InjectedDemo injectedDemo) {
12+
record DemoShell(InjectedDemo injectedDemo, AgentPlatform agentPlatform) {
13+
14+
@ShellMethod("Demo")
15+
String demo() {
16+
// Illustrate calling an agent programmatically,
17+
// as most often occurs in real applications.
18+
var reviewedStory = AgentInvocation
19+
.create(agentPlatform, WriteAndReviewAgent.ReviewedStory.class)
20+
.invoke(new UserInput("Tell me a story about caterpillars"));
21+
return reviewedStory.getContent();
22+
}
923

1024
@ShellMethod("Invent an animal")
1125
String animal() {

src/main/java/com/embabel/template/agent/WriteAndReviewAgent.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,46 +48,47 @@ abstract class Personas {
4848
);
4949
}
5050

51-
record Story(String text) {
52-
}
5351

54-
record ReviewedStory(
55-
Story story,
56-
String review,
57-
Persona reviewer
58-
) implements HasContent, Timestamped {
52+
@Agent(description = "Generate a story based on user input and review it")
53+
@Profile("!test")
54+
public class WriteAndReviewAgent {
5955

60-
@Override
61-
@NonNull
62-
public Instant getTimestamp() {
63-
return Instant.now();
56+
public record Story(String text) {
6457
}
6558

66-
@Override
67-
@NonNull
68-
public String getContent() {
69-
return String.format("""
70-
# Story
71-
%s
72-
73-
# Review
74-
%s
75-
76-
# Reviewer
77-
%s, %s
78-
""",
79-
story.text(),
80-
review,
81-
reviewer.getName(),
82-
getTimestamp().atZone(ZoneId.systemDefault())
83-
.format(DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy"))
84-
).trim();
85-
}
86-
}
59+
public record ReviewedStory(
60+
Story story,
61+
String review,
62+
Persona reviewer
63+
) implements HasContent, Timestamped {
8764

88-
@Agent(description = "Generate a story based on user input and review it")
89-
@Profile("!test")
90-
class WriteAndReviewAgent {
65+
@Override
66+
@NonNull
67+
public Instant getTimestamp() {
68+
return Instant.now();
69+
}
70+
71+
@Override
72+
@NonNull
73+
public String getContent() {
74+
return String.format("""
75+
# Story
76+
%s
77+
78+
# Review
79+
%s
80+
81+
# Reviewer
82+
%s, %s
83+
""",
84+
story.text(),
85+
review,
86+
reviewer.getName(),
87+
getTimestamp().atZone(ZoneId.systemDefault())
88+
.format(DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy"))
89+
).trim();
90+
}
91+
}
9192

9293
private final int storyWordCount;
9394
private final int reviewWordCount;

src/test/java/com/embabel/template/agent/WriteAndReviewAgentIntegrationTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.embabel.agent.api.common.autonomy.AgentInvocation;
44
import com.embabel.agent.domain.io.UserInput;
55
import com.embabel.agent.testing.integration.EmbabelMockitoIntegrationTest;
6-
import org.junit.jupiter.api.Test;
76
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
88

99
import static org.junit.jupiter.api.Assertions.*;
1010

@@ -25,17 +25,17 @@ static void setUp() {
2525
void shouldExecuteCompleteWorkflow() {
2626
var input = new UserInput("Write about artificial intelligence");
2727

28-
var story = new Story("AI will transform our world...");
29-
var reviewedStory = new ReviewedStory(story, "Excellent exploration of AI themes.", Personas.REVIEWER);
28+
var story = new WriteAndReviewAgent.Story("AI will transform our world...");
29+
var reviewedStory = new WriteAndReviewAgent.ReviewedStory(story, "Excellent exploration of AI themes.", Personas.REVIEWER);
3030

31-
whenCreateObject(prompt -> prompt.contains("Craft a short story"), Story.class)
31+
whenCreateObject(prompt -> prompt.contains("Craft a short story"), WriteAndReviewAgent.Story.class)
3232
.thenReturn(story);
3333

3434
// The second call uses generateText
3535
whenGenerateText(prompt -> prompt.contains("You will be given a short story to review"))
3636
.thenReturn(reviewedStory.review());
3737

38-
var invocation = AgentInvocation.create(agentPlatform, ReviewedStory.class);
38+
var invocation = AgentInvocation.create(agentPlatform, WriteAndReviewAgent.ReviewedStory.class);
3939
var reviewedStoryResult = invocation.invoke(input);
4040

4141
assertNotNull(reviewedStoryResult);
@@ -44,7 +44,7 @@ void shouldExecuteCompleteWorkflow() {
4444
assertEquals(reviewedStory, reviewedStoryResult,
4545
"Expected review to match: " + reviewedStoryResult);
4646

47-
verifyCreateObjectMatching(prompt -> prompt.contains("Craft a short story"), Story.class,
47+
verifyCreateObjectMatching(prompt -> prompt.contains("Craft a short story"), WriteAndReviewAgent.Story.class,
4848
llm -> llm.getLlm().getTemperature() == 0.7 && llm.getToolGroups().isEmpty());
4949
verifyGenerateTextMatching(prompt -> prompt.contains("You will be given a short story to review"));
5050
verifyNoMoreInteractions();

src/test/java/com/embabel/template/agent/WriteAndReviewAgentTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@
33
import com.embabel.agent.domain.io.UserInput;
44
import com.embabel.agent.testing.unit.FakeOperationContext;
55
import com.embabel.agent.testing.unit.FakePromptRunner;
6-
import com.embabel.agent.testing.unit.UnitTestUtils;
76
import org.junit.jupiter.api.Test;
87

98
import java.time.Instant;
109

11-
import static org.junit.jupiter.api.Assertions.assertEquals;
1210
import static org.junit.jupiter.api.Assertions.assertTrue;
1311

1412
class WriteAndReviewAgentTest {
15-
13+
1614
@Test
1715
void testWriteAndReviewAgent() {
1816
var context = FakeOperationContext.create();
1917
var promptRunner = (FakePromptRunner) context.promptRunner();
20-
context.expectResponse(new Story("One upon a time Sir Galahad . . "));
18+
context.expectResponse(new WriteAndReviewAgent.Story("One upon a time Sir Galahad . . "));
2119

2220
var agent = new WriteAndReviewAgent(200, 400);
2321
agent.craftStory(new UserInput("Tell me a story about a brave knight", Instant.now()), context);
@@ -31,7 +29,7 @@ void testWriteAndReviewAgent() {
3129
void testReview() {
3230
var agent = new WriteAndReviewAgent(200, 400);
3331
var userInput = new UserInput("Tell me a story about a brave knight", Instant.now());
34-
var story = new Story("Once upon a time, Sir Galahad...");
32+
var story = new WriteAndReviewAgent.Story("Once upon a time, Sir Galahad...");
3533
var context = FakeOperationContext.create();
3634
context.expectResponse("A thrilling tale of bravery and adventure!");
3735
agent.reviewStory(userInput, story, context);

0 commit comments

Comments
 (0)