Skip to content

Commit 971c995

Browse files
committed
Add InjectedDemo as in Java template
1 parent 3c4a3bd commit 971c995

3 files changed

Lines changed: 83 additions & 2 deletions

File tree

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ Add your magic here!
1717

1818
Illustrates:
1919

20-
- A simple agent using the Embabel `@Agent` and `@Action` annotations
20+
- An injected demo showing how any Spring component can be injected with an Embabel `Ai` instance to enable it to
21+
perform LLM operations.
22+
- A simple agent
2123
- Unit tests for an agent verifying prompts and hyperparameters
2224

2325
> For the Java equivalent, see
@@ -42,11 +44,19 @@ When the Embabel shell comes up, invoke the story agent like this:
4244
x "Tell me a story about...[your topic]"
4345
```
4446

47+
Try the following other shell commands:
48+
49+
- `demo`: Runs the same agent, invoked programmatically, instead of dynamically based on user input.
50+
See [DemoCommands.kt](./src/main/kotlin/com/embabel/template/DemoShell.kt) for the
51+
implementation.
52+
- `animal`: Runs a simple demo using an Embabel injected `Ai` instance to call an LLM.
53+
See [InjectedDemo](./src/main/kotlin/com/embabel/template/injected/InjectedDemo.kt).
54+
4555
## Suggested Next Steps
4656

4757
To get a feel for working with Embabel, try the following:
4858

49-
- Modify the prompts in `WriteAndReviewAgent`.
59+
- Modify the prompts in `WriteAndReviewAgent` and `InjectedDemo`.
5060
- Experiment with different models and hyperparameters by modifying `withLlm` calls.
5161
- Integrate your own services, injecting them with Spring. All Embabel `@Agent` classes are Spring beans.
5262
- Run the tests with `mvn test` and modify them to experiment with prompt verification.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.embabel.template
2+
3+
import com.embabel.agent.api.invocation.AgentInvocation
4+
import com.embabel.agent.core.AgentPlatform
5+
import com.embabel.agent.domain.io.UserInput
6+
import com.embabel.template.agent.ReviewedStory
7+
import com.embabel.template.injected.InjectedDemo
8+
import org.springframework.shell.standard.ShellComponent
9+
import org.springframework.shell.standard.ShellMethod
10+
11+
@ShellComponent
12+
class DemoShell(
13+
private val injectedDemo: InjectedDemo,
14+
private val agentPlatform: AgentPlatform,
15+
) {
16+
17+
@ShellMethod("Demo")
18+
fun demo(): String {
19+
// Illustrate calling an agent programmatically,
20+
// as most often occurs in real applications.
21+
val reviewedStory = AgentInvocation
22+
.create(agentPlatform, ReviewedStory::class.java)
23+
.invoke(UserInput("Tell me a story about caterpillars"))
24+
return reviewedStory.content
25+
}
26+
27+
@ShellMethod("Invent an animal")
28+
fun animal(): String {
29+
return injectedDemo.inventAnimal().toString()
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.embabel.template.injected
2+
3+
import com.embabel.agent.api.common.Ai
4+
import jakarta.validation.constraints.Pattern
5+
import org.springframework.stereotype.Component
6+
7+
/**
8+
* Demonstrate injection of Embabel's OperationContext into a Spring component.
9+
*
10+
* @param ai Embabel AI helper, injected by Spring
11+
*/
12+
@Component
13+
class InjectedDemo(private val ai: Ai) {
14+
15+
/**
16+
* Demonstrates use of JSR-380 validation annotations on record fields
17+
* to constrain generated content.
18+
*/
19+
data class Animal(
20+
val name: String,
21+
@field:Pattern(regexp = ".*ox.*", message = "Species must contain 'ox'")
22+
val species: String,
23+
)
24+
25+
fun inventAnimal(): Animal {
26+
return ai
27+
.withDefaultLlm()
28+
.withId("invent-animal")
29+
.creating(Animal::class.java)
30+
.withExample("good example", Animal("Fluffox", "Magicox"))
31+
.withExample("bad example: does not pass validation", Animal("Sparky", "Dragon"))
32+
.fromPrompt(
33+
"""
34+
You just woke up in a magical forest.
35+
Invent a fictional animal.
36+
The animal should have a name and a species.
37+
""".trimIndent(),
38+
)
39+
}
40+
}

0 commit comments

Comments
 (0)