### Running the Embabel Application
Source: https://context7.com/embabel/kotlin-agent-template/llms.txt
Use shell scripts to start the Embabel shell interface for interacting with agents. Includes commands for dynamic execution, predefined shell commands, and running tests.
```bash
# Start the Embabel shell
./scripts/shell.sh
# In the shell, invoke agents dynamically with 'x' command
# shell:> x "Tell me a story about a dragon and a princess"
# Or use predefined shell commands
# shell:> demo
# shell:> animal
# Run tests
mvn test
# Build without tests
./mvnw -Dmaven.test.skip=true package
```
--------------------------------
### Unit Test Embabel Agent
Source: https://github.com/embabel/kotlin-agent-template/blob/main/README.md
Example of unit testing an Embabel agent using FakeOperationContext and FakePromptRunner to mock LLM interactions. Verifies expected responses and prompt content.
```kotlin
val context = FakeOperationContext.create()
context.expectResponse(Story("Once upon a time..."))
val story = agent.craftStory(userInput, context.ai())
val prompt = context.llmInvocations.first().messages.first().content
assertTrue(prompt.contains("knight"))
```
--------------------------------
### Inject AI Capabilities into Spring Components in Kotlin
Source: https://context7.com/embabel/kotlin-agent-template/llms.txt
Inject an Embabel Ai instance into any Spring component to add LLM capabilities. Use JSR-380 validation annotations to constrain generated content. The 'creating' method with a class and examples helps guide the LLM output.
```kotlin
import com.embabel.agent.api.common.Ai
import jakarta.validation.constraints.Pattern
import org.springframework.stereotype.Component
@Component
class InjectedDemo(private val ai: Ai) {
// Data class with validation constraints for generated output
data class Animal(
val name: String,
@field:Pattern(regexp = ".*ox.*", message = "Species must contain 'ox'")
val species: String,
)
fun inventAnimal(): Animal {
return ai
.withDefaultLlm()
.withId("invent-animal")
.creating(Animal::class.java)
.withExample("good example", Animal("Fluffox", "Magicox"))
.withExample("bad example: does not pass validation", Animal("Sparky", "Dragon"))
.fromPrompt("""
You just woke up in a magical forest.
Invent a fictional animal.
The animal should have a name and a species.
""".trimIndent())
}
}
// Usage in a shell command or service
@ShellComponent
class DemoShell(private val injectedDemo: InjectedDemo) {
@ShellMethod("Invent an animal")
fun animal(): String {
return injectedDemo.inventAnimal().toString()
}
}
// Output: Animal(name=Glimmerfox, species=Luminox)
```
--------------------------------
### Run Embabel Project
Source: https://github.com/embabel/kotlin-agent-template/blob/main/docs/llm-docs.md
Execute this script to run the Embabel project after completing the configuration steps.
```bash
./scripts/shell.sh
```
--------------------------------
### Configure OpenAI LLM Provider with Maven
Source: https://context7.com/embabel/kotlin-agent-template/llms.txt
This Maven profile activates the OpenAI starter dependency when the OPENAI_API_KEY environment variable is set. Ensure the embabel-agent.version is correctly defined.
```xml
openai-models
env.OPENAI_API_KEY
com.embabel.agent
embabel-agent-starter-openai
${embabel-agent.version}
```
--------------------------------
### Run Demo Commands in Shell
Source: https://github.com/embabel/kotlin-agent-template/blob/main/README.md
Execute the 'demo' command in the Embabel shell to run the story agent programmatically.
```bash
demo
```
--------------------------------
### Run Animal Demo in Shell
Source: https://github.com/embabel/kotlin-agent-template/blob/main/README.md
Execute the 'animal' command in the Embabel shell to run a simple demo using an injected Embabel Ai instance.
```bash
animal
```
--------------------------------
### Run Maven Tests
Source: https://github.com/embabel/kotlin-agent-template/blob/main/README.md
Execute this command to run all unit tests for the agent project.
```bash
mvn test
```
--------------------------------
### Configure Anthropic LLM Provider with Maven
Source: https://context7.com/embabel/kotlin-agent-template/llms.txt
This Maven profile activates the Anthropic starter dependency when the ANTHROPIC_API_KEY environment variable is set. Ensure the embabel-agent.version is correctly defined.
```xml
anthropic-models
env.ANTHROPIC_API_KEY
com.embabel.agent
embabel-agent-starter-anthropic
${embabel-agent.version}
```
--------------------------------
### Configure Embabel for Amazon Bedrock
Source: https://github.com/embabel/kotlin-agent-template/blob/main/docs/llm-docs.md
Configure your application.properties to use Amazon Bedrock's Claude 3.5 Sonnet model and activate the Bedrock profile.
```properties
embabel.models.default-llm=us.anthropic.claude-3-5-sonnet-20240620-v1:0
embabel.agent.platform.ranking.llm=us.anthropic.claude-3-5-sonnet-20240620-v1:0
spring.ai.bedrock.anthropic.chat.inference-profile-id=us.anthropic.claude-3-5-sonnet-20240620-v1:0
spring.profiles.active=starwars,bedrock
```
--------------------------------
### Configure Application Properties for LLM Providers
Source: https://context7.com/embabel/kotlin-agent-template/llms.txt
These properties configure the default LLM and embedding models, and specific settings for Amazon Bedrock with Anthropic Claude. Uncomment and modify for local Ollama models.
```properties
# application.properties for Amazon Bedrock
embabel.models.default-llm=us.anthropic.claude-3-5-sonnet-20240620-v1:0
embabel.agent.platform.ranking.llm=us.anthropic.claude-3-5-sonnet-20240620-v1:0
spring.ai.bedrock.anthropic.chat.inference-profile-id=us.anthropic.claude-3-5-sonnet-20240620-v1:0
spring.profiles.active=bedrock
# For local Ollama models
#embabel.models.defaultLlm=llama3.1:8b
#embabel.models.defaultEmbeddingModel=nomic-embed-text:latest
```
--------------------------------
### Programmatic Agent Invocation with AgentInvocation
Source: https://context7.com/embabel/kotlin-agent-template/llms.txt
Use AgentInvocation to programmatically invoke agents, specifying the desired output type and input. This is the typical pattern for real applications.
```kotlin
import com.embabel.agent.api.invocation.AgentInvocation
import com.embabel.agent.core.AgentPlatform
import com.embabel.agent.domain.io.UserInput
import org.springframework.shell.standard.ShellComponent
import org.springframework.shell.standard.ShellMethod
@ShellComponent
class DemoShell(
private val agentPlatform: AgentPlatform,
){
@ShellMethod("Demo programmatic agent invocation")
fun demo(): String {
// Invoke agent by specifying desired output type
val reviewedStory = AgentInvocation
.create(agentPlatform, ReviewedStory::class.java)
.invoke(UserInput("Tell me a story about caterpillars"))
return reviewedStory.content
}
}
// The agent platform automatically:
// 1. Finds agents that can produce ReviewedStory
// 2. Executes the action chain (craftStory -> reviewStory)
// 3. Returns the final ReviewedStory object
```
--------------------------------
### Set Environment Variables for Bedrock
Source: https://github.com/embabel/kotlin-agent-template/blob/main/docs/llm-docs.md
Set these environment variables to null when using Bedrock to ensure it doesn't default to other providers like OpenAI or Anthropic.
```bash
export ANTHROPIC_API_KEY=null
export OPENAI_API_KEY=null
```
--------------------------------
### Unit Testing Agents with FakeOperationContext
Source: https://context7.com/embabel/kotlin-agent-template/llms.txt
Test agents without actual LLM calls using FakeOperationContext. Set up expected responses and verify prompt content and LLM configuration.
```kotlin
import com.embabel.agent.domain.io.UserInput
import com.embabel.agent.test.unit.FakeOperationContext
import com.embabel.agent.test.unit.FakePromptRunner
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.time.Instant
internal class WriteAndReviewAgentTest {
@Test
fun testCraftStory() {
// Create agent with configuration
val agent = WriteAndReviewAgent(200, 400)
// Set up fake context with expected response
val context = FakeOperationContext.create()
val promptRunner = context.promptRunner() as FakePromptRunner
context.expectResponse(Story("Once upon a time Sir Galahad..."))
// Execute the action
agent.craftStory(
UserInput("Tell me a story about a brave knight", Instant.now()),
context
)
// Verify prompt contains expected content
val prompt = promptRunner.llmInvocations.first().messages.single().content
assertTrue(prompt.contains("knight"), "Expected prompt to contain 'knight'")
// Verify LLM hyperparameters
val temperature = promptRunner.llmInvocations.first().interaction.llm.temperature
assertEquals(0.7, temperature!!, 0.01, "Expected temperature to be 0.7")
}
@Test
fun testReview() {
val agent = WriteAndReviewAgent(200, 400)
val userInput = UserInput("Tell me a story about a brave knight", Instant.now())
val story = Story("Once upon a time, Sir Galahad...")
val context = FakeOperationContext.create()
context.expectResponse("A thrilling tale of bravery and adventure!")
agent.reviewStory(userInput, story, context)
// Access LLM invocations directly from context
val prompt = context.llmInvocations.single().messages.first().content
assertTrue(prompt.contains("knight"))
assertTrue(prompt.contains("review"))
}
}
```
--------------------------------
### Invoke Story Agent in Shell
Source: https://github.com/embabel/kotlin-agent-template/blob/main/README.md
Use this command within the Embabel shell to invoke the story generation agent with a user-provided topic.
```bash
x "Tell me a story about...[your topic]"
```
--------------------------------
### Define an Agent with @Agent Annotation in Kotlin
Source: https://context7.com/embabel/kotlin-agent-template/llms.txt
Define an agent using the @Agent annotation. Actions are defined with @Action methods, and the final action is marked with @AchievesGoal. Use OperationContext for LLM interactions and withLlm for configuring LLM options.
```kotlin
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.OperationContext
import com.embabel.agent.api.common.create
import com.embabel.agent.domain.io.UserInput
import com.embabel.agent.prompt.persona.RoleGoalBackstory
import com.embabel.common.ai.model.LlmOptions
// Define a persona for prompt enrichment
val StoryTeller = RoleGoalBackstory(
role = "A creative storyteller who loves to weave imaginative tales",
goal = "Create memorable stories that captivate the reader's imagination.",
backstory = "You have been crafting stories for as long as you can remember."
)
// Data class for structured output
data class Story(val text: String)
@Agent(description = "Generate a story based on user input and review it")
class WriteAndReviewAgent(
@param:Value("\\${storyWordCount:100}") private val storyWordCount: Int,
) {
@Action
fun craftStory(userInput: UserInput, context: OperationContext): Story =
context.ai()
.withLlm(LlmOptions.withAutoLlm().withTemperature(0.7))
.withPromptContributor(StoryTeller)
.create("""
Craft a short story in $storyWordCount words or less.
The story should be engaging and imaginative.
# User input
${userInput.content}
""".trimIndent())
@AchievesGoal(
description = "A story has been written",
export = Export(remote = true, name = "writeStory")
)
@Action
fun finalizeStory(story: Story, context: OperationContext): Story = story
```
--------------------------------
### Add Embabel Bedrock Dependency
Source: https://github.com/embabel/kotlin-agent-template/blob/main/docs/llm-docs.md
Include this Maven dependency in your pom.xml to enable Embabel integration with Amazon Bedrock.
```xml
com.embabel.agent
embabel-agent-starter-bedrock
${embabel-agent.version}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.