-
Notifications
You must be signed in to change notification settings - Fork 140
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
Use Search Pipeline processors, Remote Inference and HttpConnector to enable Retrieval Augmented Generation (RAG) #1195
Merged
ylwu-amzn
merged 15 commits into
opensearch-project:feature/conversation
from
austintlee:feature/conversation
Sep 1, 2023
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0d4c700
Use Search Pipeline processors, Remote Inference and HttpConnector to
austintlee 5666781
Address test coverage.
austintlee 614efae
Merge branch 'opensearch-project:feature/conversation' into feature/c…
austintlee a01453f
Fix/update imports due to changes coming from core.
austintlee f4408c8
Update license header.
austintlee 9ae1617
Address comments.
austintlee dcabac9
Use List for context fields so we can pull contexts from multiple fie…
austintlee 8d78a06
Address review comments.
austintlee 14505a0
Merge branch 'feature/conversation' into feature/conversation
austintlee 9909bc2
Fix spotless issue.
austintlee 6363b32
Update README.
austintlee 3ad7120
Fix ml-client shadowJar implicit dependency issue.
austintlee 342c7fa
Add a wrapper client for ML predict.
austintlee 1dbd99d
Add tests for the internal ML client.
austintlee 13a5cfb
Merge branch 'feature/conversation' into feature/conversation
austintlee 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
This file contains 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 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 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,95 @@ | ||
# conversational-search-processors | ||
OpenSearch search processors providing conversational search capabilities | ||
======= | ||
# Plugin for Conversations Using Search Processors in OpenSearch | ||
This repo is a WIP plugin for handling conversations in OpenSearch ([Per this RFC](https://github.com/opensearch-project/ml-commons/issues/1150)). | ||
|
||
Conversational Retrieval Augmented Generation (RAG) is implemented via Search processors that combine user questions and OpenSearch query results as input to an LLM, e.g. OpenAI, and return answers. | ||
|
||
## Creating a search pipeline with the GenerativeQAResponseProcessor | ||
|
||
``` | ||
PUT /_search/pipeline/<search pipeline name> | ||
{ | ||
"response_processors": [ | ||
{ | ||
"retrieval_augmented_generation": { | ||
"tag": <tag>, | ||
"description": <description>, | ||
"model_id": "<model_id>", | ||
"context_field_list": [<field>] (e.g. ["text"]) | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
The 'model_id' parameter here needs to refer to a model of type REMOTE that has an HttpConnector instance associated with it. | ||
|
||
## Making a search request against an index using the above processor | ||
``` | ||
GET /<index>/_search\?search_pipeline\=<search pipeline name> | ||
{ | ||
"_source": ["title", "text"], | ||
"query" : { | ||
"neural": { | ||
"text_vector": { | ||
"query_text": <query string>, | ||
"k": <integer> (e.g. 10), | ||
"model_id": <model_id> | ||
} | ||
} | ||
}, | ||
"ext": { | ||
"generative_qa_parameters": { | ||
"llm_model": <LLM model> (e.g. "gpt-3.5-turbo"), | ||
"llm_question": <question string> | ||
} | ||
austintlee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
``` | ||
|
||
## Retrieval Augmented Generation response | ||
``` | ||
{ | ||
"took": 3, | ||
"timed_out": false, | ||
"_shards": { | ||
"total": 3, | ||
"successful": 3, | ||
"skipped": 0, | ||
"failed": 0 | ||
}, | ||
"hits": { | ||
"total": { | ||
"value": 110, | ||
"relation": "eq" | ||
}, | ||
"max_score": 0.55129033, | ||
"hits": [ | ||
{ | ||
"_index": "...", | ||
"_id": "...", | ||
"_score": 0.55129033, | ||
"_source": { | ||
"text": "...", | ||
"title": "..." | ||
} | ||
}, | ||
{ | ||
... | ||
} | ||
... | ||
{ | ||
... | ||
} | ||
] | ||
}, // end of hits | ||
"ext": { | ||
"retrieval_augmented_generation": { | ||
"answer": "..." | ||
} | ||
} | ||
} | ||
``` | ||
The RAG answer is returned as an "ext" to SearchResponse following the "hits" array. | ||
austintlee marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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,74 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed 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. | ||
*/ | ||
plugins { | ||
id 'java' | ||
id 'jacoco' | ||
id "io.freefair.lombok" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
mavenLocal() | ||
} | ||
|
||
dependencies { | ||
|
||
compileOnly group: 'org.opensearch', name: 'opensearch', version: "${opensearch_version}" | ||
implementation 'org.apache.commons:commons-lang3:3.12.0' | ||
//implementation project(':opensearch-ml-client') | ||
implementation project(':opensearch-ml-common') | ||
implementation group: 'org.opensearch', name: 'common-utils', version: "${common_utils_version}" | ||
// https://mvnrepository.com/artifact/org.apache.httpcomponents.core5/httpcore5 | ||
implementation group: 'org.apache.httpcomponents.core5', name: 'httpcore5', version: '5.2.1' | ||
implementation("com.google.guava:guava:32.0.1-jre") | ||
implementation group: 'org.json', name: 'json', version: '20230227' | ||
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.10.0' | ||
testImplementation "org.opensearch.test:framework:${opensearch_version}" | ||
} | ||
|
||
test { | ||
include '**/*Tests.class' | ||
systemProperty 'tests.security.manager', 'false' | ||
} | ||
|
||
jacocoTestReport { | ||
dependsOn /*integTest,*/ test | ||
reports { | ||
xml.required = true | ||
html.required = true | ||
} | ||
} | ||
|
||
jacocoTestCoverageVerification { | ||
violationRules { | ||
rule { | ||
limit { | ||
counter = 'LINE' | ||
minimum = 0.65 //TODO: increase coverage to 0.90 | ||
} | ||
limit { | ||
counter = 'BRANCH' | ||
minimum = 0.55 //TODO: increase coverage to 0.85 | ||
} | ||
} | ||
} | ||
dependsOn jacocoTestReport | ||
} | ||
|
||
check.dependsOn jacocoTestCoverageVerification | ||
//jacocoTestCoverageVerification.dependsOn jacocoTestReport |
36 changes: 36 additions & 0 deletions
36
...ensearch/searchpipelines/questionanswering/generative/GenerativeQAProcessorConstants.java
This file contains 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,36 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed 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.opensearch.searchpipelines.questionanswering.generative; | ||
|
||
public class GenerativeQAProcessorConstants { | ||
|
||
// Identifier for the generative QA request processor | ||
public static final String REQUEST_PROCESSOR_TYPE = "question_rewrite"; | ||
|
||
// Identifier for the generative QA response processor | ||
public static final String RESPONSE_PROCESSOR_TYPE = "retrieval_augmented_generation"; | ||
|
||
// The model_id of the model registered and deployed in OpenSearch. | ||
public static final String CONFIG_NAME_MODEL_ID = "model_id"; | ||
|
||
// The name of the model supported by an LLM, e.g. "gpt-3.5" in OpenAI. | ||
public static final String CONFIG_NAME_LLM_MODEL = "llm_model"; | ||
|
||
// The field in search results that contain the context to be sent to the LLM. | ||
public static final String CONFIG_NAME_CONTEXT_FIELD_LIST = "context_field_list"; | ||
} |
69 changes: 69 additions & 0 deletions
69
...opensearch/searchpipelines/questionanswering/generative/GenerativeQARequestProcessor.java
This file contains 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,69 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed 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.opensearch.searchpipelines.questionanswering.generative; | ||
|
||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.ingest.ConfigurationUtils; | ||
import org.opensearch.search.pipeline.AbstractProcessor; | ||
import org.opensearch.search.pipeline.Processor; | ||
import org.opensearch.search.pipeline.SearchRequestProcessor; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Defines the request processor for generative QA search pipelines. | ||
*/ | ||
public class GenerativeQARequestProcessor extends AbstractProcessor implements SearchRequestProcessor { | ||
|
||
private String modelId; | ||
austintlee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
protected GenerativeQARequestProcessor(String tag, String description, boolean ignoreFailure, String modelId) { | ||
super(tag, description, ignoreFailure); | ||
this.modelId = modelId; | ||
} | ||
|
||
@Override | ||
public SearchRequest processRequest(SearchRequest request) throws Exception { | ||
|
||
// TODO Use chat history to rephrase the question with full conversation context. | ||
|
||
return request; | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return GenerativeQAProcessorConstants.REQUEST_PROCESSOR_TYPE; | ||
} | ||
|
||
public static final class Factory implements Processor.Factory<SearchRequestProcessor> { | ||
|
||
@Override | ||
public SearchRequestProcessor create( | ||
Map<String, Processor.Factory<SearchRequestProcessor>> processorFactories, | ||
String tag, | ||
String description, | ||
boolean ignoreFailure, | ||
Map<String, Object> config, | ||
PipelineContext pipelineContext | ||
) throws Exception { | ||
return new GenerativeQARequestProcessor(tag, description, ignoreFailure, | ||
ConfigurationUtils.readStringProperty(GenerativeQAProcessorConstants.REQUEST_PROCESSOR_TYPE, tag, config, GenerativeQAProcessorConstants.CONFIG_NAME_MODEL_ID) | ||
); | ||
} | ||
} | ||
} |
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.
@msfroh We want to release these behind a feature flag for v2.10.0. I am thinking of returning empty Lists/Maps if the flag is false. What do you think? Is there a cleaner way to handle this?
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.
I think that's a very clean way of handling it.
In particular, feature flags are loaded on startup and the set of available processors are loaded on startup. Saying that the processors are available if and only if the feature flag is set sounds correct to me.