Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b11d049
[llm-document-erichment] Add first revision of the feature
nicolo-rinaldi Mar 25, 2026
dfb27ab
[llm-document-enrichment] First working version
nicolo-rinaldi Mar 26, 2026
827548a
[llm-document-enrichment] Add promptFile feature to DocumentEnrichmen…
nicolo-rinaldi Mar 26, 2026
cc94317
[llm-document-enrichment] Add multiple inputField support + tests
nicolo-rinaldi Mar 27, 2026
c723362
[llm-document-enrchment] Add supprot for:
nicolo-rinaldi Mar 30, 2026
cf0d6bb
[llm-document-enrichment] polished code, added tests and added file f…
nicolo-rinaldi Mar 31, 2026
570c2aa
[llm-document-enrichment] updated supported models + added tests
nicolo-rinaldi Apr 1, 2026
184f579
[llm-document-enrichment] added documentation for 'Document Enrichmen…
nicolo-rinaldi Apr 1, 2026
1b7c972
[llm-document-enrichment] cleanup of DocumentEnrichmentUpdateProcesso…
nicolo-rinaldi Apr 1, 2026
9cf0315
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi Apr 1, 2026
36208af
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi Apr 1, 2026
3d29d16
[llm-document-enrichment] Addressed comments in Anna's review
nicolo-rinaldi Apr 13, 2026
55ff9ab
[llm-document-enrichment] Addressed comments in Anna's review
nicolo-rinaldi Apr 13, 2026
ed40008
[llm-document-enrichment] Fixed broken tests and updated documentation
nicolo-rinaldi Apr 14, 2026
ba27be1
[llm-document-enrichment] Added tests, Exception for multiple output …
nicolo-rinaldi Apr 15, 2026
b092f22
[llm-document-enrichment] Updated documentation
nicolo-rinaldi Apr 15, 2026
6e9525d
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi Apr 17, 2026
4f39e40
[llm-document-enrichment] Pre-check changes
nicolo-rinaldi Apr 17, 2026
b2b51a6
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi Apr 17, 2026
af48a10
[llm-document-enrichment] Post-check changes. Now `./gradlew check` w…
nicolo-rinaldi Apr 17, 2026
f15285e
[llm-document-enrichment] Fix GitHub actions failures
nicolo-rinaldi Apr 17, 2026
2cb28e4
[llm-document-enrichment] Addressed Alessandro's comments. Removed an…
nicolo-rinaldi Apr 24, 2026
157787d
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi Apr 24, 2026
3ae92f6
[llm-document-enrichment] Addressed Alessandro's comments. Removed an…
nicolo-rinaldi Apr 24, 2026
c78d6ba
[llm-document-enrichment] Addressed Alessandro's comments. Removed an…
nicolo-rinaldi Apr 27, 2026
73b2ae2
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi Apr 27, 2026
1d95ee5
[llm-document-enrichment] Parsing logic moved to DocumentEnrichmentUp…
nicolo-rinaldi Apr 27, 2026
5067a3c
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi May 11, 2026
ed6f4a4
[llm-document-enrichment] Updated the code to the new refactoring of …
nicolo-rinaldi May 11, 2026
53acb36
[llm-document-enrichment] fixed javadoc
nicolo-rinaldi May 12, 2026
29fa5b8
[text-to-vector-test-refactoring] fixed test namings and functions to…
nicolo-rinaldi May 12, 2026
9b1855d
[text-to-vector-test-refactoring] added changelog file
nicolo-rinaldi May 12, 2026
b0b4eb4
[text-to-vector-test-refactoring] Removed "Managed" to ManagedTextToV…
nicolo-rinaldi May 13, 2026
07811cd
Merge branch 'text-to-vector-test-refactoring' into llm-document-enri…
nicolo-rinaldi May 13, 2026
0a143d1
[llm-document-enrichment] Updated managed classes namings
nicolo-rinaldi May 13, 2026
e76faad
[llm-document-enrichment] Removed Italian comment
nicolo-rinaldi May 13, 2026
7ea679b
[text-to-vector-test-refactoring] Updated test names + tidy
nicolo-rinaldi May 14, 2026
20621be
Merge branch 'text-to-vector-test-refactoring' into llm-document-enri…
nicolo-rinaldi May 14, 2026
b9908a4
[llm-document-enrichment] Updated test names + tidy
nicolo-rinaldi May 14, 2026
9437c55
[llm-document-enrichment] Fixed tests in TestLargeLanguageModelManage…
nicolo-rinaldi May 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolo-rinaldi Does this come from the pull of the refactoring branch in this branch?

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title: Refactor the tests of the language-model module to accomodate new upcoming LLM oriented features avoiding code duplication.
type: fixed
authors:
- name: Nicolò Rinaldi
- name: Anna Ruggero
- name: Alessandro benedetti
links:
- name: PR#4419
url: https://github.com/apache/solr/pull/4419
7 changes: 7 additions & 0 deletions changelog/unreleased/SOLR-18187-llm-document-enrichment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Add DocumentEnrichmentUpdateProcessorFactory for LLM-based document enrichment at index time
type: added # added, changed, fixed, deprecated, removed, dependency_update, security, other
authors:
- name: Nicolò Rinaldi, Anna Ruggero, Alessandro Benedetti
links:
- name: SOLR-18187
url: https://issues.apache.org/jira/browse/SOLR-18187
5 changes: 4 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ amazon-awssdk-s3 = { module = "software.amazon.awssdk:s3", version.ref = "amazon
amazon-awssdk-sdkcore = { module = "software.amazon.awssdk:sdk-core", version.ref = "amazon-awssdk" }
amazon-awssdk-sts = { module = "software.amazon.awssdk:sts", version.ref = "amazon-awssdk" }
androidx-lifecycle-runtimeCompose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidx-lifecycle" }
androidx-lifecycle-viewmodelCompose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" }
androidx-lifecycle-viewModelNav3 = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-navigation3", version.ref = "androidx-lifecycle" }
androidx-lifecycle-viewmodelCompose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" }
androidx-material3-adaptive = { module = "org.jetbrains.compose.material3.adaptive:adaptive", version.ref = "androidx-adaptive" }
androidx-material3-adaptive-nav3 = { module = "org.jetbrains.compose.material3.adaptive:adaptive-navigation3", version.ref = "androidx-adaptive" }
androidx-navigation3-ui = { module = "org.jetbrains.androidx.navigation3:navigation3-ui", version.ref = "androidx-navigation3" }
Expand Down Expand Up @@ -459,11 +459,14 @@ ktor-client-serialization-json = { module = "io.ktor:ktor-serialization-kotlinx-
ktor-server-cio = { module = "io.ktor:ktor-server-cio" }
ktor-server-core = { module = "io.ktor:ktor-server-core" }
ktor-server-htmlBuilder = { module = "io.ktor:ktor-server-html-builder" }
langchain4j-anthropic = { module = "dev.langchain4j:langchain4j-anthropic" }
langchain4j-bom = { module = "dev.langchain4j:langchain4j-bom", version.ref = "langchain4j-bom" }
langchain4j-cohere = { module = "dev.langchain4j:langchain4j-cohere" }
langchain4j-core = { module = "dev.langchain4j:langchain4j-core" }
langchain4j-google-ai-gemini = { module = "dev.langchain4j:langchain4j-google-ai-gemini" }
langchain4j-hugging-face = { module = "dev.langchain4j:langchain4j-hugging-face" }
langchain4j-mistral-ai = { module = "dev.langchain4j:langchain4j-mistral-ai" }
langchain4j-ollama = { module = "dev.langchain4j:langchain4j-ollama" }
langchain4j-open-ai = { module = "dev.langchain4j:langchain4j-open-ai" }
lmax-disruptor = { module = "com.lmax:disruptor", version.ref = "lmax-disruptor" }
locationtech-spatial4j = { module = "org.locationtech.spatial4j:spatial4j", version.ref = "spatial4j" }
Expand Down
1 change: 1 addition & 0 deletions solr/licenses/langchain4j-anthropic-1.9.1.jar.sha1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
905f570cd38c2ebd94fa159f2e88bca06bbf71c8
1 change: 1 addition & 0 deletions solr/licenses/langchain4j-google-ai-gemini-1.9.1.jar.sha1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
49973974543318de23f3f09b21c79d5e45815d8c
1 change: 1 addition & 0 deletions solr/licenses/langchain4j-ollama-1.9.1.jar.sha1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e3c631fa2fce6e79ad50fc86adb724656992263a
3 changes: 3 additions & 0 deletions solr/modules/language-models/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ dependencies {
implementation libs.apache.lucene.core

implementation libs.langchain4j.core
runtimeOnly libs.langchain4j.anthropic
runtimeOnly libs.langchain4j.cohere
runtimeOnly libs.langchain4j.google.ai.gemini
runtimeOnly libs.langchain4j.hugging.face
runtimeOnly libs.langchain4j.mistral.ai
runtimeOnly libs.langchain4j.ollama
runtimeOnly libs.langchain4j.open.ai

implementation libs.slf4j.api
Expand Down
3 changes: 3 additions & 0 deletions solr/modules/language-models/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ com.tdunning:t-digest:3.3=jarValidation,runtimeClasspath,runtimeLibs,solrPlatfor
commons-cli:commons-cli:1.11.0=jarValidation,runtimeClasspath,runtimeLibs,solrPlatformLibs,testRuntimeClasspath
commons-codec:commons-codec:1.21.0=jarValidation,runtimeClasspath,runtimeLibs,solrPlatformLibs,testRuntimeClasspath
commons-io:commons-io:2.21.0=compileClasspath,jarValidation,runtimeClasspath,runtimeLibs,solrPlatformLibs,testCompileClasspath,testRuntimeClasspath
dev.langchain4j:langchain4j-anthropic:1.9.1=jarValidation,runtimeClasspath,runtimeLibs,testRuntimeClasspath
dev.langchain4j:langchain4j-bom:1.9.1=compileClasspath,jarValidation,runtimeClasspath,runtimeLibs,testCompileClasspath,testRuntimeClasspath
dev.langchain4j:langchain4j-cohere:1.9.1-beta17=jarValidation,runtimeClasspath,runtimeLibs,testRuntimeClasspath
dev.langchain4j:langchain4j-core:1.9.1=compileClasspath,jarValidation,runtimeClasspath,runtimeLibs,testCompileClasspath,testRuntimeClasspath
dev.langchain4j:langchain4j-google-ai-gemini:1.9.1=jarValidation,runtimeClasspath,runtimeLibs,testRuntimeClasspath
dev.langchain4j:langchain4j-http-client-jdk:1.9.1=jarValidation,runtimeClasspath,runtimeLibs,testRuntimeClasspath
dev.langchain4j:langchain4j-http-client:1.9.1=jarValidation,runtimeClasspath,runtimeLibs,testRuntimeClasspath
dev.langchain4j:langchain4j-hugging-face:1.9.1-beta17=jarValidation,runtimeClasspath,runtimeLibs,testRuntimeClasspath
dev.langchain4j:langchain4j-mistral-ai:1.9.1=jarValidation,runtimeClasspath,runtimeLibs,testRuntimeClasspath
dev.langchain4j:langchain4j-ollama:1.9.1=jarValidation,runtimeClasspath,runtimeLibs,testRuntimeClasspath
dev.langchain4j:langchain4j-open-ai:1.9.1=jarValidation,runtimeClasspath,runtimeLibs,testRuntimeClasspath
io.dropwizard.metrics:metrics-annotation:4.2.33=jarValidation,testRuntimeClasspath
io.dropwizard.metrics:metrics-core:4.2.33=jarValidation,runtimeClasspath,runtimeLibs,solrPlatformLibs,testRuntimeClasspath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.languagemodels.model;

import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.request.ResponseFormat;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.RamUsageEstimator;
import org.apache.solr.common.SolrException;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.languagemodels.LanguageModelException;
import org.apache.solr.languagemodels.store.rest.LargeLanguageModelStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This object wraps a {@link dev.langchain4j.model.chat.ChatModel} to some content given a prompt
* and a {@link ResponseFormat}. It's meant to be used as a managed resource with the {@link
* LargeLanguageModelStore}
*/
public class SolrLargeLanguageModel extends SolrLanguageModel implements Accountable {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final long BASE_RAM_BYTES =
RamUsageEstimator.shallowSizeOfInstance(SolrLargeLanguageModel.class);

private static final String THINKING_BUDGET_TOKENS = "thinkingBudgetTokens";
private static final String RANDOM_SEED = "randomSeed";

private final ChatModel chatModel;
private final int hashCode;

public static SolrLargeLanguageModel getInstance(
SolrResourceLoader solrResourceLoader,
String className,
String name,
Map<String, Object> params)
throws LanguageModelException {
try {
/*
* The idea here is to build a {@link dev.langchain4j.model.chat.ChatModel} using inversion
* of control.
* Each model has its own list of parameters we don't know beforehand, but each {@link dev.langchain4j.model.chat.ChatModel} class
* has its own builder that uses setters with the same name of the parameter in input.
* */
ChatModel chatModel;
Class<?> modelClass = solrResourceLoader.findClass(className, ChatModel.class);
var builder = modelClass.getMethod("builder").invoke(null);
if (params != null) {
/*
* This block of code has the responsibility of instantiate a {@link
Comment thread
nicolo-rinaldi marked this conversation as resolved.
* dev.langchain4j.model.chat.ChatModel} using the params provided. Classes have
* params of the specific implementation of {@link
* dev.langchain4j.model.chat.ChatModel}, which is not known beforehand. So we benefit of
* the design choice in langchain4j that each subclass implementing {@link
* dev.langchain4j.model.chat.ChatModel} uses setters with the same name of the
* param.
*/
for (String paramName : params.keySet()) {
/*
* When a param is not primitive, we need to instantiate the object explicitly and then call the
* setter method.
* N.B. when adding support to new models, pay attention to all the parameters they
* support, some of them may require to be handled in here as separate switch cases
*/
switch (paramName) {
case TIMEOUT_PARAM -> builder
.getClass()
.getMethod(paramName, Duration.class)
.invoke(builder, Duration.ofSeconds((Long) params.get(paramName)));

case MAX_RETRIES_PARAM, THINKING_BUDGET_TOKENS, RANDOM_SEED -> builder
.getClass()
.getMethod(paramName, Integer.class)
.invoke(builder, ((Long) params.get(paramName)).intValue());

/*
* For primitive params if there's only one setter available, we call it.
* If there's choice we default to the string one
*/
default -> {
ArrayList<Method> paramNameMatches = new ArrayList<>();
for (var method : builder.getClass().getMethods()) {
if (paramName.equals(method.getName()) && method.getParameterCount() == 1) {
paramNameMatches.add(method);
}
}
if (paramNameMatches.size() == 1) {
paramNameMatches.getFirst().invoke(builder, params.get(paramName));
} else {
try {
builder
.getClass()
.getMethod(paramName, String.class)
.invoke(builder, params.get(paramName).toString());
} catch (NoSuchMethodException e) {
log.error("Parameter {} not supported by model {}", paramName, className);
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e.getMessage(), e);
}
}
}
}
}
}

// Always enforce strict schema adherence where supported. For Anthropic and Google it's
// enabled by default
if (!"dev.langchain4j.model.anthropic.AnthropicChatModel".equals(className)
&& !"dev.langchain4j.model.googleai.GoogleAiGeminiChatModel".equals(className)) {
try {
builder.getClass().getMethod("strictJsonSchema", Boolean.class).invoke(builder, true);
} catch (NoSuchMethodException ignored) {
log.debug(
"Model {} does not have strictJsonSchema param, structured output is not enforced",
className);
}
}
chatModel = (ChatModel) builder.getClass().getMethod("build").invoke(builder);
return new SolrLargeLanguageModel(name, chatModel, params);
} catch (final Exception e) {
throw new LanguageModelException("Model loading failed for " + className, e);
}
}

public SolrLargeLanguageModel(String name, ChatModel chatModel, Map<String, Object> params) {
super(name, params);
this.chatModel = chatModel;
this.hashCode = calculateHashCode();
}

/**
* Sends a structured chat request to the language model and returns the raw text response.
*
* @param prompt the user prompt to send to the language model
* @param responseFormat the format specification that instructs the model to produce structured
* JSON output
* @return the raw text response from the language model
*/
public String generate(String prompt, ResponseFormat responseFormat) {
ChatRequest chatRequest =
ChatRequest.builder()
.responseFormat(responseFormat)
.messages(UserMessage.from(prompt))
.build();
return chatModel.chat(chatRequest).aiMessage().text();
}

@Override
public String toString() {
return getClass().getSimpleName() + "(name=" + getName() + ")";
}

@Override
public long ramBytesUsed() {
return BASE_RAM_BYTES
+ RamUsageEstimator.sizeOfObject(name)
+ RamUsageEstimator.sizeOfObject(chatModel);
}

@Override
public int hashCode() {
return hashCode;
}

private int calculateHashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + Objects.hashCode(name);
result = (prime * result) + Objects.hashCode(chatModel);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof SolrLargeLanguageModel)) return false;
final SolrLargeLanguageModel other = (SolrLargeLanguageModel) obj;
return Objects.equals(chatModel, other.chatModel) && Objects.equals(name, other.name);
}

@Override
public String getModelClassName() {
return chatModel.getClass().getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
import org.apache.solr.common.SolrException;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.languagemodels.LanguageModelException;
import org.apache.solr.languagemodels.store.rest.ManagedTextToVectorModelStore;
import org.apache.solr.languagemodels.store.rest.TextToVectorModelStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This object wraps a {@link EmbeddingModel} to encode text to vector. It's meant to be used as a
* managed resource with the {@link ManagedTextToVectorModelStore}
* managed resource with the {@link TextToVectorModelStore}
*/
public class SolrTextToVectorModel extends SolrLanguageModel implements Accountable {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.languagemodels.model.SolrTextToVectorModel;
import org.apache.solr.languagemodels.store.rest.ManagedTextToVectorModelStore;
import org.apache.solr.languagemodels.store.rest.TextToVectorModelStore;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.rest.ManagedResource;
import org.apache.solr.rest.ManagedResourceObserver;
Expand All @@ -44,7 +44,7 @@
public class TextToVectorQParserPlugin extends QParserPlugin
implements ResourceLoaderAware, ManagedResourceObserver {
public static final String EMBEDDING_MODEL_PARAM = "model";
private ManagedTextToVectorModelStore modelStore = null;
private TextToVectorModelStore modelStore = null;

@Override
public QParser createParser(
Expand All @@ -55,14 +55,14 @@ public QParser createParser(
@Override
public void inform(ResourceLoader loader) throws IOException {
final SolrResourceLoader solrResourceLoader = (SolrResourceLoader) loader;
ManagedTextToVectorModelStore.registerManagedTextToVectorModelStore(solrResourceLoader, this);
TextToVectorModelStore.registerManagedTextToVectorModelStore(solrResourceLoader, this);
}

@Override
public void onManagedResourceInitialized(NamedList<?> args, ManagedResource res)
throws SolrException {
if (res instanceof ManagedTextToVectorModelStore) {
modelStore = (ManagedTextToVectorModelStore) res;
if (res instanceof TextToVectorModelStore) {
modelStore = (TextToVectorModelStore) res;
}
if (modelStore != null) {
modelStore.loadStoredModels();
Expand Down Expand Up @@ -105,7 +105,7 @@ public Query parse() throws SyntaxError {
"The model requested '"
+ embeddingModelName
+ "' can't be found in the store: "
+ ManagedTextToVectorModelStore.REST_END_POINT);
+ TextToVectorModelStore.REST_END_POINT);
}
}
}
Expand Down
Loading