Class Introduction

Function

Implements retrieval of parallel inference text-to-text dialog chains. It inherits the SingleText2TextChain base class to reduce the retrieval delay.

Prototype

from mx_rag.chain import ParallelText2TextChain
class ParallelText2TextChain(SingleText2TextChain)

Parameters

Parameter

Data Type

Required/Optional

Description

llm

Text2TextLLM

Required

LLM object. For details, see Text2TextLLM.

retriever

Retriever

Required

Retriever object. For details, see Retriever.

reranker

Reranker

Optional

Reranker object, which is used to re-rank the retrieved documents. The default value is None. For details, see Reranker.

prompt

String

Optional

When adding knowledge search content, you can add system prompts to more accurately control an LLM. The default value is "Answer users' questions in a concise and professional manner based on the preceding known information. If you cannot obtain the answer from the known information, answer the question based on your own experience." If you need to customize a prompt, add it by referring to the prompt project of your LLM.

The length range is [1, 1024 × 1024].

sys_messages

List[dict]

Optional

System message, defaulting to None. The list can contain up to 16 messages. Each dictionary within the list can have a maximum length of 16. The maximum length for dictionary key strings is 16, and the maximum length for value strings is 4 × 1024 × 1024. The reference format is as follows: [{"role": "system", "content": "You are a friendly assistant"}].

source

Bool

Optional

Whether to return the retrieved documents during a dialog. The key value in the dictionary returned by Chain is source_documents. The default value is True.

user_content_builder

Callable

Optional

Callback function, defaulting to _user_content_builder. The return value must be a string with a length up to 4 × 1024 × 1024. The function is to integrate the original question, retrieved document list, and user prompts to generate a text that can be directly used as the content for a user role in model dialogues (that is, {"role": "user", "content": generation result}).

  • Default function of user_content_builder:
def _user_content_builder(query: str, docs: List[Document], prompt: str) -> str:
    """
       Default combination logic for user input
       Parameter description:
       ----------
       query : str
           Original question
           For example, "Summarize the key points based on the following materials."
       docs : List[Document]
           List of document objects returned by the retriever.
           Each document usually contains the following information:
           - page_content: document content;
           - metadata: metadata (such as the source, title, and score).
       prompt : str
           System prompt. The default value is "Answer users' questions in a concise and professional manner based on the preceding known information.
           If you cannot obtain the answer from the known information, answer the question based on your own experience."
       Return:
       -----
       str: complete prompt after concatenation, which is used as the model input.
       """
    final_prompt = ""
    document_separator: str = "\n\n"
    if len(docs) != 0:
        if prompt != "":
            last_doc = docs[-1]
            last_doc.page_content = (last_doc.page_content
                                     + f"{document_separator}{prompt}")
            docs[-1] = last_doc
        final_prompt = document_separator.join(x.page_content for x in docs)
    if final_prompt != "":
        final_prompt += document_separator
    final_prompt += query
    return final_prompt