Writing Your First Agent
The easiest way to create your first agent is to use the Java or Kotlin template repositories.
Example: WriteAndReviewAgent
Section titled “Example: WriteAndReviewAgent”The template includes a WriteAndReviewAgent that demonstrates key concepts:
@Agent(description = "Agent that writes and reviews stories")public class WriteAndReviewAgent {
@Action public Story writeStory(UserInput userInput, OperationContext context) { return context.ai() .withAutoLlm() .createObject(""" You are a creative writer who aims to delight and surprise. Write a story about %s """.formatted(userInput.getContent()), Story.class); }
@AchievesGoal(description = "Review a story") @Action public ReviewedStory reviewStory(Story story, OperationContext context) { return context.ai() .withLlmByRole("reviewer") .createObject(""" You are a meticulous editor. Carefully review this story: %s """.formatted(story.text), ReviewedStory.class); }}@Agent(description = "Agent that writes and reviews stories")class WriteAndReviewAgent {
@Action fun writeStory(userInput: UserInput, context: OperationContext): Story { return context.ai() .withAutoLlm() .createObject(""" You are a creative writer who aims to delight and surprise. Write a story about ${userInput.content} """, Story::class.java) }
@AchievesGoal(description = "Review a story") @Action fun reviewStory(story: Story, context: OperationContext): ReviewedStory { return context.ai() .withLlmByRole("reviewer") .createObject(""" You are a meticulous editor. Carefully review this story: ${story.text} """, ReviewedStory::class.java) }}Key Concepts Demonstrated
Section titled “Key Concepts Demonstrated”Multiple LLMs with Different Configurations:
- Writer LLM uses high temperature (0.8) for creativity
- Reviewer LLM uses low temperature (0.2) for analytical review
- Different personas guide the model behavior
Actions and Goals:
@Actionmethods are the steps the agent can take@AchievesGoalmarks the final action that completes the agent’s work
Domain Objects:
StoryandReviewedStoryare strongly-typed domain objects- Help structure the interaction between actions
Running Your Agent
Section titled “Running Your Agent”Set your API keys and run the shell:
export OPENAI_API_KEY="your_key_here"./scripts/shell.shIn the shell, try:
x "Tell me a story about a robot learning to paint"The agent will:
- Generate a creative story using the writer LLM
- Review and improve it using the reviewer LLM
- Return the final reviewed story
Next Steps
Section titled “Next Steps”- Explore the examples repository for more complex agents
- Read the Reference Documentation for detailed API information
- Try building your own domain-specific agents