Skip to content

soujava/mongodb-ai-planning-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongodb-ai-planning-pattern

A Java example demonstrating the Planning Pattern with AI, MongoDB, Jakarta EE, and tool calling through a travel itinerary assistant.

The application showcases how Large Language Models (LLMs) can interact with enterprise systems using tools rather than relying exclusively on model knowledge. Instead of generating answers from training data alone, the AI plans a sequence of actions, invokes tools to retrieve information from MongoDB, and then produces recommendations based on real data.

The sample uses a travel assistant domain where users can explore cities, discover attractions, and generate travel itineraries. The AI agent interacts with the application through tool calling, demonstrating how deterministic software and AI capabilities can work together.

Architecture

The application combines a traditional Jakarta EE architecture with AI planning capabilities. The user interacts through a JSF and PrimeFaces interface, while LangChain4j orchestrates the AI workflow. Rather than directly accessing MongoDB, the model uses tools that expose application capabilities. These tools delegate to services and repositories, ensuring the AI remains isolated from implementation details and only operates through well-defined actions.

graph LR
subgraph UI["User Interface"]
JSF["JSF + PrimeFaces"]
end

subgraph AI["AI Layer"]
Agent["TravelService"]
Planner["Planning Pattern"]
end

subgraph Tools["Tool Layer"]
CityTools["CityTools"]
AttractionTools["AttractionTools"]
end

subgraph Services["Application Layer"]
CityService["CityService"]
AttractionService["AttractionService"]
end

subgraph Data["Data Layer"]
CityRepository["CityRepository"]
AttractionRepository["AttractionRepository"]
MongoDB[("MongoDB")]
end

JSF -->|"User Prompt"| Agent
Agent --> Planner

Planner -->|"Uses"| CityTools
Planner -->|"Uses"| AttractionTools

CityTools --> CityService
AttractionTools --> AttractionService

CityService --> CityRepository
AttractionService --> AttractionRepository

CityRepository --> MongoDB
AttractionRepository --> MongoDB

style JSF fill:#F8F7F7,stroke:#1D5183,stroke-width:2px,color:#1D5183
style Agent fill:#1D5183,stroke:#019DDC,stroke-width:2px,color:#F8F7F7
style Planner fill:#F8F7F7,stroke:#019DDC,stroke-width:2px,color:#1D5183
style CityTools fill:#F8F7F7,stroke:#1D5183,stroke-width:2px,color:#1D5183
style AttractionTools fill:#F8F7F7,stroke:#1D5183,stroke-width:2px,color:#1D5183
style CityService fill:#F8F7F7,stroke:#019DDC,stroke-width:2px,color:#1D5183
style AttractionService fill:#F8F7F7,stroke:#019DDC,stroke-width:2px,color:#1D5183
style CityRepository fill:#F8F7F7,stroke:#019DDC,stroke-width:2px,color:#1D5183
style AttractionRepository fill:#F8F7F7,stroke:#019DDC,stroke-width:2px,color:#1D5183
style MongoDB fill:#1D5183,stroke:#019DDC,stroke-width:2px,color:#F8F7F7
Loading

Planning Pattern

Traditional software typically follows a static execution flow where developers explicitly define each step through method calls, conditions, and workflows. AI systems introduce a different paradigm. Instead of following a predetermined path, the model evaluates a goal and dynamically determines which actions should be executed.

The Planning Pattern enables the model to decompose a request into smaller tasks, select the appropriate tools, retrieve information, and compose a final response. This allows enterprise applications to combine deterministic business logic with the flexibility of AI-driven decision-making.

For example, when a user asks:

%%{init: {
  "theme": "base",
  "themeVariables": {
    "primaryColor": "#F8F7F7",
    "primaryTextColor": "#1D5183",
    "primaryBorderColor": "#1D5183",
    "lineColor": "#019DDC",
    "secondaryColor": "#F8F7F7",
    "secondaryBorderColor": "#019DDC",
    "tertiaryColor": "#1D5183",
    "tertiaryTextColor": "#F8F7F7"
  }
}}%%

sequenceDiagram

    actor User

    participant UI as JSF + PrimeFaces
    participant Agent as AI Travel Agent<br/>(TravelService)
    participant CityTools as CityTools
    participant AttractionTools as AttractionTools
    participant MongoDB as MongoDB

    User->>UI: Create a historical itinerary in Portugal

    UI->>Agent: chat(userMessage)

    Note over Agent: Analyze goal and create a plan

    Agent->>CityTools: citiesByCountry("Portugal")

    CityTools->>MongoDB: Find cities
    MongoDB-->>CityTools: Lisbon, Porto
    CityTools-->>Agent: Available cities

    Agent->>AttractionTools: attractionsByType("Lisbon", HISTORICAL)

    AttractionTools->>MongoDB: Find attractions
    MongoDB-->>AttractionTools: Historical attractions
    AttractionTools-->>Agent: Lisbon attractions

    Agent->>AttractionTools: attractionsByType("Porto", HISTORICAL)

    AttractionTools->>MongoDB: Find attractions
    MongoDB-->>AttractionTools: Historical attractions
    AttractionTools-->>Agent: Porto attractions

    Note over Agent: Build itinerary from tool results

    Agent-->>UI: Travel recommendation

    UI-->>User: Render recommendation
Loading

Technologies

Java Jakarta EE Jakarta Faces (JSF) PrimeFaces LangChain4j Jakarta Data Jakarta NoSQL MongoDB Maven Embedded GlassFish

Running the Application

Build the application:

mvn clean install

Run the application:

mvn embedded-glassfish

Open the application:

http://localhost:8080

Example Prompts

Show me available cities to travel Show me attractions in Lisbon Create a historical itinerary in Portugal Create a museum-focused trip in Europe Create a food and culture itinerary

What You Will Learn

How to implement the Planning Pattern How AI agents interact with enterprise applications through tools How to integrate LangChain4j with Jakarta EE How to expose application capabilities through tool calling How to use MongoDB with Jakarta Data and Jakarta NoSQL How to combine deterministic software with AI-driven workflows

About

A Java example demonstrating the Planning Pattern with AI, MongoDB, and tool calling through a travel itinerary assistant.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors