-
Notifications
You must be signed in to change notification settings - Fork 825
SOLR-18187: Document enrichment with LLMs #4259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nicolo-rinaldi
wants to merge
40
commits into
apache:main
Choose a base branch
from
SeaseLtd:llm-document-enrichment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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 dfb27ab
[llm-document-enrichment] First working version
nicolo-rinaldi 827548a
[llm-document-enrichment] Add promptFile feature to DocumentEnrichmen…
nicolo-rinaldi cc94317
[llm-document-enrichment] Add multiple inputField support + tests
nicolo-rinaldi c723362
[llm-document-enrchment] Add supprot for:
nicolo-rinaldi cf0d6bb
[llm-document-enrichment] polished code, added tests and added file f…
nicolo-rinaldi 570c2aa
[llm-document-enrichment] updated supported models + added tests
nicolo-rinaldi 184f579
[llm-document-enrichment] added documentation for 'Document Enrichmen…
nicolo-rinaldi 1b7c972
[llm-document-enrichment] cleanup of DocumentEnrichmentUpdateProcesso…
nicolo-rinaldi 9cf0315
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi 36208af
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi 3d29d16
[llm-document-enrichment] Addressed comments in Anna's review
nicolo-rinaldi 55ff9ab
[llm-document-enrichment] Addressed comments in Anna's review
nicolo-rinaldi ed40008
[llm-document-enrichment] Fixed broken tests and updated documentation
nicolo-rinaldi ba27be1
[llm-document-enrichment] Added tests, Exception for multiple output …
nicolo-rinaldi b092f22
[llm-document-enrichment] Updated documentation
nicolo-rinaldi 6e9525d
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi 4f39e40
[llm-document-enrichment] Pre-check changes
nicolo-rinaldi b2b51a6
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi af48a10
[llm-document-enrichment] Post-check changes. Now `./gradlew check` w…
nicolo-rinaldi f15285e
[llm-document-enrichment] Fix GitHub actions failures
nicolo-rinaldi 2cb28e4
[llm-document-enrichment] Addressed Alessandro's comments. Removed an…
nicolo-rinaldi 157787d
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi 3ae92f6
[llm-document-enrichment] Addressed Alessandro's comments. Removed an…
nicolo-rinaldi c78d6ba
[llm-document-enrichment] Addressed Alessandro's comments. Removed an…
nicolo-rinaldi 73b2ae2
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi 1d95ee5
[llm-document-enrichment] Parsing logic moved to DocumentEnrichmentUp…
nicolo-rinaldi 5067a3c
Merge branch 'main' into llm-document-enrichment
nicolo-rinaldi ed6f4a4
[llm-document-enrichment] Updated the code to the new refactoring of …
nicolo-rinaldi 53acb36
[llm-document-enrichment] fixed javadoc
nicolo-rinaldi 29fa5b8
[text-to-vector-test-refactoring] fixed test namings and functions to…
nicolo-rinaldi 9b1855d
[text-to-vector-test-refactoring] added changelog file
nicolo-rinaldi b0b4eb4
[text-to-vector-test-refactoring] Removed "Managed" to ManagedTextToV…
nicolo-rinaldi 07811cd
Merge branch 'text-to-vector-test-refactoring' into llm-document-enri…
nicolo-rinaldi 0a143d1
[llm-document-enrichment] Updated managed classes namings
nicolo-rinaldi e76faad
[llm-document-enrichment] Removed Italian comment
nicolo-rinaldi 7ea679b
[text-to-vector-test-refactoring] Updated test names + tidy
nicolo-rinaldi 20621be
Merge branch 'text-to-vector-test-refactoring' into llm-document-enri…
nicolo-rinaldi b9908a4
[llm-document-enrichment] Updated test names + tidy
nicolo-rinaldi 9437c55
[llm-document-enrichment] Fixed tests in TestLargeLanguageModelManage…
nicolo-rinaldi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
changelog/unreleased/PR#4419-lenguage-model-test-refactoring.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 905f570cd38c2ebd94fa159f2e88bca06bbf71c8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 49973974543318de23f3f09b21c79d5e45815d8c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| e3c631fa2fce6e79ad50fc86adb724656992263a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
...language-models/src/java/org/apache/solr/languagemodels/model/SolrLargeLanguageModel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
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(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?