Skip to content

Commit 95f6ff9

Browse files
committed
Camel Spring AI Example
1 parent 977b76c commit 95f6ff9

File tree

12 files changed

+621
-1
lines changed

12 files changed

+621
-1
lines changed

README.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ readme's instructions.
2727
=== Examples
2828

2929
// examples: START
30-
Number of Examples: 67 (0 deprecated)
30+
Number of Examples: 68 (0 deprecated)
3131

3232
[width="100%",cols="4,2,4",options="header"]
3333
|===
3434
| Example | Category | Description
3535

36+
| link:ai-agent/README.adoc[Ai Agent] (ai-agent) | AI | An example showing how to work with Camel Spring AI for chat, tools, and vector store
37+
3638
| link:aot-basic/readme.adoc[Aot Basic] (aot-basic) | AOT | Example on how to leverage Spring Boot AOT in Camel Spring Boot
3739

3840
| link:endpointdsl/readme.adoc[Endpointdsl] (endpointdsl) | Beginner | Using type-safe Endpoint DSL

ai-agent/README.adoc

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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!

ai-agent/input/camel-intro.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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.

ai-agent/input/spring-ai-intro.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Spring AI provides abstractions for AI services including chat models, embeddings, and vector stores. It enables developers to build AI-powered applications using familiar Spring patterns and practices.

ai-agent/pom.xml

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one or more
5+
contributor license agreements. See the NOTICE file distributed with
6+
this work for additional information regarding copyright ownership.
7+
The ASF licenses this file to You under the Apache License, Version 2.0
8+
(the "License"); you may not use this file except in compliance with
9+
the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21+
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>org.apache.camel.springboot.example</groupId>
26+
<artifactId>examples</artifactId>
27+
<version>4.17.0-SNAPSHOT</version>
28+
</parent>
29+
30+
<artifactId>camel-example-spring-boot-ai-agent</artifactId>
31+
<name>Camel SB Examples :: Spring AI Agent</name>
32+
<description>An example showing how to work with Camel Spring AI for chat, tools, and vector store</description>
33+
34+
<properties>
35+
<category>AI</category>
36+
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
39+
<spring-ai-version>1.1.0</spring-ai-version>
40+
</properties>
41+
42+
<dependencyManagement>
43+
<dependencies>
44+
<!-- Camel BOM -->
45+
<dependency>
46+
<groupId>org.apache.camel.springboot</groupId>
47+
<artifactId>camel-spring-boot-bom</artifactId>
48+
<version>${project.version}</version>
49+
<type>pom</type>
50+
<scope>import</scope>
51+
</dependency>
52+
<!-- Spring Boot BOM -->
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-dependencies</artifactId>
56+
<version>${spring-boot-version}</version>
57+
<type>pom</type>
58+
<scope>import</scope>
59+
</dependency>
60+
<!-- Spring AI BOM -->
61+
<dependency>
62+
<groupId>org.springframework.ai</groupId>
63+
<artifactId>spring-ai-bom</artifactId>
64+
<version>${spring-ai-version}</version>
65+
<type>pom</type>
66+
<scope>import</scope>
67+
</dependency>
68+
</dependencies>
69+
</dependencyManagement>
70+
71+
<dependencies>
72+
73+
<!-- Spring Boot -->
74+
<dependency>
75+
<groupId>org.springframework.boot</groupId>
76+
<artifactId>spring-boot-starter</artifactId>
77+
</dependency>
78+
79+
<!-- Camel -->
80+
<dependency>
81+
<groupId>org.apache.camel.springboot</groupId>
82+
<artifactId>camel-spring-boot-starter</artifactId>
83+
</dependency>
84+
<dependency>
85+
<groupId>org.apache.camel.springboot</groupId>
86+
<artifactId>camel-spring-ai-chat-starter</artifactId>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.apache.camel.springboot</groupId>
90+
<artifactId>camel-spring-ai-vector-store-starter</artifactId>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.apache.camel.springboot</groupId>
94+
<artifactId>camel-spring-ai-tools-starter</artifactId>
95+
</dependency>
96+
<dependency>
97+
<groupId>org.apache.camel.springboot</groupId>
98+
<artifactId>camel-file-starter</artifactId>
99+
</dependency>
100+
<dependency>
101+
<groupId>org.apache.camel.springboot</groupId>
102+
<artifactId>camel-http-starter</artifactId>
103+
</dependency>
104+
<dependency>
105+
<groupId>org.apache.camel.springboot</groupId>
106+
<artifactId>camel-jq-starter</artifactId>
107+
</dependency>
108+
109+
<!-- Spring AI Vector Store - Qdrant -->
110+
<dependency>
111+
<groupId>org.springframework.ai</groupId>
112+
<artifactId>spring-ai-starter-vector-store-qdrant</artifactId>
113+
</dependency>
114+
115+
<!-- test -->
116+
<dependency>
117+
<groupId>org.springframework.boot</groupId>
118+
<artifactId>spring-boot-starter-test</artifactId>
119+
<scope>test</scope>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.apache.camel</groupId>
123+
<artifactId>camel-test-spring-junit5</artifactId>
124+
<version>${camel-version}</version>
125+
<scope>test</scope>
126+
</dependency>
127+
128+
</dependencies>
129+
130+
<profiles>
131+
<profile>
132+
<id>default</id>
133+
<activation>
134+
<activeByDefault>true</activeByDefault>
135+
</activation>
136+
<dependencies>
137+
<!-- Spring AI Ollama -->
138+
<dependency>
139+
<groupId>org.springframework.ai</groupId>
140+
<artifactId>spring-ai-starter-model-ollama</artifactId>
141+
</dependency>
142+
</dependencies>
143+
</profile>
144+
<profile>
145+
<id>prod</id>
146+
<dependencies>
147+
<!-- Spring AI OpenAI -->
148+
<dependency>
149+
<groupId>org.springframework.ai</groupId>
150+
<artifactId>spring-ai-starter-model-openai</artifactId>
151+
</dependency>
152+
</dependencies>
153+
</profile>
154+
</profiles>
155+
156+
<build>
157+
<plugins>
158+
<plugin>
159+
<groupId>org.springframework.boot</groupId>
160+
<artifactId>spring-boot-maven-plugin</artifactId>
161+
<version>${spring-boot-version}</version>
162+
<executions>
163+
<execution>
164+
<goals>
165+
<goal>repackage</goal>
166+
</goals>
167+
</execution>
168+
</executions>
169+
</plugin>
170+
</plugins>
171+
</build>
172+
</project>

0 commit comments

Comments
 (0)