|
| 1 | +== Spring Boot Example with Spring AI Agent |
| 2 | + |
| 3 | +=== Introduction |
| 4 | + |
| 5 | +This example demonstrates how to use Apache Camel with Spring AI to build AI-powered integration applications. The example showcases three key capabilities: |
| 6 | + |
| 7 | +1. *Chat* - Basic conversational AI using the Spring AI Chat component |
| 8 | +2. *Tools* - Function calling where the AI can invoke Camel routes as tools |
| 9 | +3. *Vector Store* - Semantic search using document embeddings with Qdrant |
| 10 | + |
| 11 | +The example uses Ollama as the AI model provider with the granite4:3b model for chat and embeddinggemma:300m for embeddings, and Qdrant as the vector database. |
| 12 | + |
| 13 | +==== Design Principles |
| 14 | + |
| 15 | +This example follows Apache Camel Spring AI's design philosophy of separating concerns: |
| 16 | + |
| 17 | +* *Business Logic in Camel Routes* - The route definitions focus purely on integration logic and data flow |
| 18 | +* *Infrastructure Configuration in application.yaml* - All non-functional requirements such as AI model selection, connection parameters, temperature settings, and external service endpoints are externalized to configuration files |
| 19 | + |
| 20 | +This separation enables: |
| 21 | + |
| 22 | +* Easy switching between different AI providers (e.g., Ollama, OpenAI, Azure) without changing route code |
| 23 | +* Environment-specific configurations (dev, staging, production) with different models or endpoints |
| 24 | +* Clear distinction between what the integration does (routes) and how it connects (configuration) |
| 25 | + |
| 26 | +=== Prerequisites |
| 27 | + |
| 28 | +==== Camel JBang |
| 29 | +Install Camel JBang to easily run infrastructure services: |
| 30 | + |
| 31 | + $ curl -Ls https://sh.jbang.dev | bash -s - trust add https://github.com/apache/camel/ |
| 32 | + $ curl -Ls https://sh.jbang.dev | bash -s - app install --fresh --force camel@apache/camel |
| 33 | + |
| 34 | +Or if you already have JBang installed: |
| 35 | + |
| 36 | + $ jbang app install camel@apache/camel |
| 37 | + |
| 38 | +==== Ollama |
| 39 | +You need to have Ollama installed and running locally. Install Ollama from https://ollama.ai and then pull the required models: |
| 40 | + |
| 41 | + $ ollama pull granite4:3b |
| 42 | + $ ollama pull embeddinggemma:300m |
| 43 | + |
| 44 | +The granite4:3b model is used for chat and the embeddinggemma:300m model is used for embeddings. |
| 45 | + |
| 46 | +Make sure Ollama is running on `http://localhost:11434` (default port) or update the `application.yaml` accordingly. |
| 47 | + |
| 48 | +NOTE: While you can use `camel infra run ollama` to run Ollama via Docker, be aware of performance limitations. On macOS, the Docker image cannot utilize GPU acceleration, making it extremely slow. Additionally, when running the Camel Ollama test-infra, both the Ollama Docker image and the default model (granite4, ~3GB) need to be downloaded on first run, which can be very time-consuming. For better performance, it's recommended to install Ollama natively. |
| 49 | + |
| 50 | +==== Qdrant |
| 51 | +Use Camel JBang to run Qdrant: |
| 52 | + |
| 53 | + $ camel infra run qdrant |
| 54 | + |
| 55 | +Qdrant will be available on `http://localhost:6333`. |
| 56 | + |
| 57 | +=== Components Used |
| 58 | + |
| 59 | +* `camel-spring-ai-chat-starter` - Camel Spring AI chat integration |
| 60 | +* `camel-spring-ai-vector-store-starter` - Camel Spring AI vector store integration |
| 61 | +* `camel-spring-ai-tools-starter` - Camel Spring AI tools/function calling |
| 62 | +* `spring-ai-starter-model-ollama` - Spring AI Ollama model support |
| 63 | +* `spring-ai-starter-vector-store-qdrant` - Qdrant vector store integration |
| 64 | + |
| 65 | +=== Routes Overview |
| 66 | + |
| 67 | +NOTE: The routes use timer delays to ensure proper startup sequence. The chat route executes immediately on startup, the vector store query executes after a 10 second delay, and the tool chat route executes after a 20 second delay. |
| 68 | + |
| 69 | +==== ChatRoute |
| 70 | +Demonstrates basic chat functionality. Sends questions to the AI model and logs the responses. Executes immediately on startup. |
| 71 | + |
| 72 | +==== ToolRoute |
| 73 | +Shows how to define tools (functions) that the AI can call. The example includes a weather tool that the AI can invoke when asked about weather information. The tool is defined using the `spring-ai-tools` component with parameters. The tool chat route executes after a 20 second delay to ensure all services are ready. |
| 74 | + |
| 75 | +==== VectorStoreRoute |
| 76 | +Demonstrates semantic search using vector embeddings with Qdrant: |
| 77 | + |
| 78 | +* Watches the `input` directory for `.txt` files and adds them to the Qdrant vector store |
| 79 | +* Performs a sample similarity search query after a 10 second delay |
| 80 | +* Documents are automatically embedded and stored in Qdrant |
| 81 | +* Returns the top 3 most similar documents with a similarity threshold of 0.5 |
| 82 | + |
| 83 | +=== Build |
| 84 | + |
| 85 | +You can build this example using: |
| 86 | + |
| 87 | + $ mvn package |
| 88 | + |
| 89 | +=== Run |
| 90 | + |
| 91 | +You can run this example using: |
| 92 | + |
| 93 | + $ mvn spring-boot:run |
| 94 | + |
| 95 | +=== Usage |
| 96 | + |
| 97 | +1. The chat route will automatically execute and ask questions about Apache Camel |
| 98 | +2. The tool route will demonstrate AI function calling with the weather tool (executes after 20 seconds) |
| 99 | +3. The vector store route will: |
| 100 | + * Add sample documents from the `input` folder to the Qdrant vector store on startup |
| 101 | + * Execute a sample similarity search query after 10 seconds |
| 102 | + * You can add more `.txt` files to the `input` folder to expand the knowledge base |
| 103 | + |
| 104 | +=== Expected Output |
| 105 | + |
| 106 | +You should see in the console: |
| 107 | + |
| 108 | +* AI responses to the chat questions |
| 109 | +* Weather tool being called when the AI needs weather information |
| 110 | +* Vector store adding documents to Qdrant and performing a similarity search |
| 111 | +* The most relevant documents with similarity scores for the query |
| 112 | + |
| 113 | +The example will continue running and watching the `input` directory for new documents. Press Ctrl+C to stop. |
| 114 | + |
| 115 | +[source] |
| 116 | +---- |
| 117 | +2025-11-25T17:24:21.770+01:00 INFO 51706 --- [ - timer://chat] chatRoute : Sending question to AI: What is Apache Camel? |
| 118 | +2025-11-25T17:24:29.858+01:00 INFO 51706 --- [ - timer://chat] chatRoute : AI Response: Apache Camel is an open-source integration framework based on known Java connectivity standards... |
| 119 | +
|
| 120 | +
|
| 121 | +2025-11-25T17:24:21.771+01:00 INFO 51706 --- [ - file://input] addDocuments : Adding document camel-intro.txt to vector store |
| 122 | +2025-11-25T17:24:22.048+01:00 INFO 51706 --- [ - file://input] addDocuments : Successfully added camel-intro.txt to vector store |
| 123 | +2025-11-25T17:24:22.048+01:00 INFO 51706 --- [ - file://input] addDocuments : Adding document spring-ai-intro.txt to vector store |
| 124 | +2025-11-25T17:24:22.183+01:00 INFO 51706 --- [ - file://input] addDocuments : Successfully added spring-ai-intro.txt to vector store |
| 125 | +2025-11-25T17:34:08.998+01:00 INFO 52622 --- [mer://queryDocs] queryDocuments : Querying vector store with: What is Apache Camel and what does it do? |
| 126 | +2025-11-25T17:34:09.206+01:00 INFO 52622 --- [mer://queryDocs] queryDocuments : Retrieved 1 documents from vector store |
| 127 | +2025-11-25T17:34:09.217+01:00 INFO 52622 --- [mer://queryDocs] queryDocuments : Document text: Apache Camel is an open-source integration framework based on Enterprise Integration Patterns. It provides a rule-based routing and mediation engine which enables you to define routing and mediation rules in various domain-specific languages. |
| 128 | +
|
| 129 | +
|
| 130 | +2025-11-25T17:51:41.445+01:00 INFO 54308 --- [imer://toolChat] toolChatRoute : Sending question with tool capability to AI: What is the weather like in Rome, Italy? Answer with English slang |
| 131 | +2025-11-25T17:51:43.520+01:00 INFO 54308 --- [imer://toolChat] weatherTool : Weather tool called for location: Rome |
| 132 | +2025-11-25T17:51:43.739+01:00 INFO 54308 --- [imer://toolChat] weatherTool : Geocoding result - Latitude: 41.89193, Longitude: 12.51133 |
| 133 | +2025-11-25T17:51:43.858+01:00 INFO 54308 --- [imer://toolChat] weatherTool : Weather tool response: The weather in is 12.4 degrees Celsius with 87% humidity, 100% cloud cover, wind speed 2.9 km/h from 7 degrees, and 0.2 mm precipitation. |
| 134 | +2025-11-25T17:51:47.786+01:00 INFO 54308 --- [imer://toolChat] toolChatRoute : AI Response with tool usage: So, Rome's got it pretty mild out there right now – about **12.4°C**, feels like a cool breeze with that high humidity hanging around at **87%**. No rain in sight (just **0.2mm**), and the sky’s practically overcast (**100% cloud cover**) thanks to those gentle winds from the north‑west at **7°** – perfect for grabbing an espresso outside while you ponder life, love, or just how many more layers you need before it gets chilly again! |
| 135 | +---- |
| 136 | + |
| 137 | +=== Configuration |
| 138 | + |
| 139 | +The AI configuration can be modified in `src/main/resources/application.yaml`: |
| 140 | + |
| 141 | +* Change the Ollama base URL if running on a different host/port |
| 142 | +* Switch to a different model (ensure it's pulled in Ollama first) |
| 143 | +* Adjust the temperature for more/less creative responses |
| 144 | +* Modify Qdrant host/port if running on a different location |
| 145 | +* Change the collection name for different use cases |
| 146 | +* Modify topK in VectorStoreRoute for more/fewer search results |
| 147 | + |
| 148 | +==== Production Profile with OpenAI |
| 149 | + |
| 150 | +The example includes a production profile that uses OpenAI instead of Ollama. To use it, activate the `prod` Maven profile and set the required environment variables: |
| 151 | + |
| 152 | +[source,bash] |
| 153 | +---- |
| 154 | +export OPENAI_CHAT_MODEL=gpt-4o |
| 155 | +export OPENAI_CHAT_BASE_URL=https://api.openai.com |
| 156 | +export OPENAI_CHAT_API_KEY=your-openai-api-key |
| 157 | +export OPENAI_EMBEDDING_MODEL=text-embedding-3-small |
| 158 | +export OPENAI_EMBEDDING_BASE_URL=https://api.openai.com |
| 159 | +export OPENAI_EMBEDDING_API_KEY=your-openai-api-key |
| 160 | +
|
| 161 | +mvn spring-boot:run -Pprod -Dspring-boot.run.profiles=prod |
| 162 | +---- |
| 163 | + |
| 164 | +The production profile configuration is in `src/main/resources/application-prod.yaml` and uses environment variables for flexibility. You can customize the models and base URLs to use different OpenAI-compatible endpoints (e.g., Azure OpenAI, local OpenAI-compatible servers). |
| 165 | + |
| 166 | +=== Help and contributions |
| 167 | + |
| 168 | +If you hit any problem using Camel or have some feedback, then please |
| 169 | +https://camel.apache.org/community/support/[let us know]. |
| 170 | + |
| 171 | +We also love contributors, so |
| 172 | +https://camel.apache.org/community/contributing/[get involved] :-) |
| 173 | + |
| 174 | +The Camel riders! |
0 commit comments