This implementation of watsonx Assistant with watsonx Discovery (Elasticsearch) consists of the following features,
- Conversation history
- Query rewrite
- Semantic search
- Answer generation
Edit the notebook watsonx-rag-elasticsearch.ipynb
, and update the required variables.
This notebook demonstrates,
- how to have fine grain control over the ingesting process such as chunking
- using pipelines to offload embedding text to Elasticsearch
- create an Elasticsearch index
- search for query terms using pipelines
- deploy and test prompts on Watson Machine Learning Deployment spaces
query
of text typecontext
of any typehistory
of any type
-
Elasticsearch
-
watsonx.ai
This action consists of 3 steps for the RAG process,
- Rewrite query based on conversational history
- Retreive information using semantic search
- Generate answer based on retrieved information
Set history
variable to the following expression,
${history} = ${system_session_history}.transform("role", "<|start_header_id|>user<|end_header_id|>\n\n", "<|start_header_id|>assistant<|end_header_id|>\n\n").joinToArray("%e.role%%e.content%").join("<|eot_id|>") + "<|eot_id|>"
In the And then section
, select use an extension.
- Select the watsonx.ai extension that was created in Step 2.2.
- Select the
Text Generation
operation. - Under parameters,
- set the
deployment_id
forrewrite-prompt
that was create in Step 1 version
to2023-05-29
- set the
- Under optional parameters, set
parameters.prompt_variables.history
to thehistory
variable. - Under stream response, set
text
toresults[0].generated_text
Set query
variable to the following expression,
${query} = ${step_950_result_2.body.results}[0]["generated_text"]
Edit expression using the editor to select the previous step and select body.results
.
The final expression should be like the below image.
In the And then section
, select use an extension.
- Select the elasticsearch extension that was created in Step 2.2.
- Select the
Search request
operation. - Under parameters, fill in the
index_name
that was used in Step 1. - Under optional parameters, fill in the
knn
,fields
andsource
fields.
For knn
,
{
"field": "text_embedding",
"query_vector_builder": {
"text_embedding": {"model_id": "intfloat__multilingual-e5-base", "model_text": ${query}}
},
"k": 5,
"num_candidates": 50
}
Note: set model_id
according to the embedding model used in Step 1.
For fields
, fill in ["text"]
Note: ["text"]
will depend on what field names was used in Step 1.
For source
, select False.
Click on New step +
Set context
variable to the below expression,
${context} = "\n<documents><document>\n" + ${step_181_result_1.body.hits.hits}.joinToArray("%e.fields.text[0]%").join("\n</document>\n\n<document>\n") + "\n</document></documents>\n"
Edit expression using the editor to select the previous step and select body.hits.hits
.
Set history
variable to the below expression,
${history} = ${system_session_history}.transform("role", "<|start_header_id|>user<|end_header_id|>\n\n", "<|start_header_id|>assistant<|end_header_id|>\n\n").joinToArray("%e.role%%e.content%").join("<|eot_id|>") + "<|eot_id|>"
In the And then section
, select use an extension.
- Select the watsonx.ai extension that was created in Step 2.2.
- Select the
Text Generation Stream
operation. Under parameters,- set
deployment_id
forconversational-prompt
that was create in Step 1 - set
version
to2023-05-29
.
- set
- Under optional parameters, set
parameters.prompt_variables
to the expression{ "context": ${context}, "history": ${history} }
. - Under stream response, set
text
toresults[0].generated_text