检索增强生成处理器
retrieval_augmented_generation 处理器是一种搜索结果处理器,可用于对话式搜索中的检索增强生成(RAG)。该处理器拦截查询结果,从对话记忆中检索之前的消息,并将提示发送给大语言模型(LLM)。处理器收到 LLM 的响应后,会将响应保存到对话记忆中,并同时返回原始的 UDB-SX 查询结果和 LLM 响应。
自 UDB-SX 25.0.0.0 起,
retrieval_augmented_generation处理器仅支持 OpenAI 和 Amazon Bedrock 模型。
请求体字段
下表列出了所有可用的请求字段。
| 字段 | 数据类型 | 描述 |
|---|---|---|
model_id |
String | 管道中使用的模型 ID。必填。 |
context_field_list |
Array | 文档源中包含的字段列表,管道将这些字段用作 RAG 的上下文,必填,更多信息请参见上下文字段列表。 |
system_prompt |
String | 发送给 LLM 的系统提示,用于调整其行为(例如响应语气),可以是角色描述或一组指令,可选。 |
user_instructions |
String | 人工生成的指令,发送给 LLM 以指导其生成结果, |
| tag 字符串 处理器的标识符,可选。 | ||
tag |
String | 处理器的标识符,可选。 |
description |
String | 处理器的描述,可选。 |
上下文字段列表
context_field_list 是文档源中包含的字段列表,管道将这些字段用作 RAG 的上下文。例如,假设您的 UDB-SX 索引包含一组文档,每个文档都包含title和text字段:
{
"_index": "qa_demo",
"_id": "SimKcIoBOVKVCYpk1IL-",
"_source": {
"title": "Abraham Lincoln 2",
"text": "Abraham Lincoln was born on February 12, 1809, the second child of Thomas Lincoln and Nancy Hanks Lincoln, in a log cabin on Sinking Spring Farm near Hodgenville, Kentucky.[2] He was a descendant of Samuel Lincoln, an Englishman who migrated from Hingham, Norfolk, to its namesake, Hingham, Massachusetts, in 1638. The family then migrated west, passing through New Jersey, Pennsylvania, and Virginia.[3] Lincoln was also a descendant of the Harrison family of Virginia; his paternal grandfather and namesake, Captain Abraham Lincoln and wife Bathsheba (née Herring) moved the family from Virginia to Jefferson County, Kentucky.[b] The captain was killed in an Indian raid in 1786.[5] His children, including eight-year-old Thomas, Abraham's father, witnessed the attack.[6][c] Thomas then worked at odd jobs in Kentucky and Tennessee before the family settled in Hardin County, Kentucky, in the early 1800s.[6]\n"
}
}
您可以通过在处理器中设置 "context_field_list": ["text"],指定仅将 text 内容发送给 LLM。
示例
以下示例演示了如何使用包含retrieval_augmented_generation处理器的搜索管道。
创建搜索管道
以下请求创建了一个包含retrieval_augmented_generation处理器的搜索管道,该处理器用于 OpenAI 模型:
PUT /_search/pipeline/rag_pipeline
{
"response_processors": [
{
"retrieval_augmented_generation": {
"tag": "openai_pipeline_demo",
"description": "Demo pipeline Using OpenAI Connector",
"model_id": "gnDIbI0BfUsSoeNT_jAw",
"context_field_list": ["text"],
"system_prompt": "You are a helpful assistant",
"user_instructions": "Generate a concise and informative answer in less than 100 words for the given question"
}
}
]
}
使用搜索管道
将 UDB-SX 查询与ext对象结合使用,该对象存储用于 LLM 的生成式问答参数:
GET /my_rag_test_data/_search?search_pipeline=rag_pipeline
{
"query": {
"match": {
"text": "Abraham Lincoln"
}
},
"ext": {
"generative_qa_parameters": {
"llm_model": "gpt-3.5-turbo",
"llm_question": "Was Abraham Lincoln a good politician",
"memory_id": "iXC4bI0BfUsSoeNTjS30",
"context_size": 5,
"message_size": 5,
"timeout": 15
}
}
}
有关设置对话式搜索的更多信息,请参见基于 RAG 的对话式搜索。