This example shows how to use Apache Camel with Milvus as a vector database to perform similarity search on European cities based on geographic and demographic features.
OpenAI (via Ollama) is used to generate city data (country, latitude, longitude, population in millions) which is stored as a 3-dimensional feature vector in Milvus. A similarity search then finds cities with the most similar characteristics.
The pipeline works end-to-end:
-
Insert: LLM returns raw city data (
Rome: Italy;40.41;12.51;2.81) →VectorUtilsnormalizes to 0-1 range → stored in Milvus as 3D vector -
Search: LLM returns raw Rome coordinates → normalized → Milvus L2 similarity search → finds Rome, Milan, Zurich as the 3 closest cities
-
Explain: The results are sent back to the LLM which provides a natural language explanation with approximate distances
-
A running Milvus instance (using Camel CLI):
camel infra run milvus
-
A running Ollama instance with the required model:
ollama serve ollama pull granite4:3b
When the application starts, it executes the following pipeline:
-
Create Collection - Creates a
citiescollection in Milvus with four fields:id(Int64 primary key),city(VarChar),country(VarChar), andfeatures(3-dimensional FloatVector). Thefeaturesvector encodes each city as[latitude, longitude, population_millions], normalized to 0-1 range. An IVF_FLAT index with L2 distance metric is created on the vector field. -
Insert Cities - Reads city names from
input/cities.txt. For each city, it asks the LLM viaopenai:chat-completionto provide the country, latitude, longitude, and population (in millions). The response is parsed using Camel Simple expressions and normalized to a float vector viaVectorUtils, then inserted into Milvus. -
Similarity Search - Searches for the 3 cities most similar to Rome based on L2 distance of their feature vectors. The search vector is also generated by the LLM.
-
Explain - The search results are sent back to the LLM which provides a natural language explanation with approximate distances.
Creating Milvus collection: cities
Collection created successfully
Index created successfully
Processing cities from: cities.txt
City data for Rome: Italy;41.90;12.50;2.87
Inserted Rome into Milvus
City data for Paris: France;48.86;2.35;2.16
Inserted Paris into Milvus
...
All cities indexed. Starting similarity search...
Searching for three closest cities to: Rome
Search vector for Rome: 41.90;12.50;2.87
Closest cities to Rome: [{rank=1, city=Rome, country=Italy}, {rank=2, city=Milan, country=Italy}, {rank=3, city=Zurich, country=Switzerland}].
Answer: Rome: 0 km, Milan: 200 km, Zurich: 400 km
|
Note
|
The exact similarity results may vary depending on the LLM responses. |
Edit src/main/resources/application.properties to configure:
-
camel.component.milvus.host- Milvus server host (default: localhost) -
camel.component.milvus.port- Milvus gRPC port (default: 19530) -
camel.component.openai.base-url- OpenAI-compatible API base URL (default: Ollama at http://localhost:11434/v1) -
camel.component.openai.model- Chat completion model (default: granite4:3b)
To use OpenAI instead of Ollama, change the base URL and set your API key:
camel.component.openai.base-url=https://api.openai.com/v1
camel.component.openai.api-key=${OPENAI_API_KEY}
camel.component.openai.model=gpt-4o-mini
If you hit any problem using Camel or have some feedback, then please let us know.
We also love contributors, so get involved :-)
The Camel riders!