【RAG】表格場景RAG怎么做?TableRAG:一種增強大規模表格理解框架
前面很多期介紹了密集文檔場景的RAG方法,今天來看看大量表格場景的RAG怎么做的。
現有結合大模型的方法通常需要將整個表格作為輸入,這會導致一些挑戰,比如位置偏差、上下文長度限制等,尤其是在處理大型表格時。為了解決這些問題,文章提出了TableRAG框架,該框架利用查詢擴展結合模式和單元格檢索,以在向LLM提供信息之前精確定位關鍵信息。這種方法能夠更高效地編碼數據和精確檢索,顯著減少提示長度并減輕信息丟失。
表提示技術在LLM中的應用比較
(a) Read Table
語言模型讀取整個表格。這是最直接的方法,但往往不可行,因為大型表格會超出模型的處理能力。陰影區域表示提供給語言模型的數據,包括所有行和列。對于大型表格,這種方法不現實,因為會超過模型的令牌限制。
(b) Read Schema
語言模型只讀取表格的模式(schema),即列名和數據類型。只包含列名和數據類型的信息,不包含表格內容的具體信息。這種方法會導致表格內容的信息丟失。
(c) Row-Column Retrieval
對行和列進行編碼,然后根據它們與問題的相似性進行選擇。只有行和列的交集被呈現給語言模型。 編碼后,基于與問題的相關性選擇行和列。 對于大型表格,編碼所有行和列仍然不可行。
(d) Schema-Cell Retrieval (Ours)
編碼列名和單元格,并根據它們與語言模型生成的關于問題查詢的相關性進行檢索。只有檢索到的模式和單元格提供給語言模型。 包括檢索到的列名和單元格值。 提高了編碼和推理的效率。
(e) Retrieval Performance on ArcadeQA
展示了在 ArcadeQA 數據集上不同方法的檢索結果。TableRAG 在列和單元格檢索方面都優于其他方法,從而提高了后續表格推理過程的性能。
方法
TableRAG Example
核心思想是結合模式檢索和單元格檢索,獲得解決問題的必要信息,通過程序輔助的LLM。實際上,沒必要將整個表格給LLM。相反,關鍵信息通常位于與問題直接相關的特定列名、數據類型和單元格值中。例如,考慮一個問題“錢包的平均價格是多少?”為了解決這個問題,程序可能只需要提取與“錢包”相關的行,然后從價格列計算平均值。僅知道相關列名以及表中“錢包”的表示方式就足以編寫程序。因此,TableRAG解決了RAG的上下文長度限制。
圖片
TableRAG流程圖:表格被用來構建Schema和單元格數據庫。然后通過LLM將問題擴展成多個模式和單元格查詢。這些查詢依次用于Schema檢索和列-單元格對。每個查詢的前K個候選項被組合起來,輸入到LLM求解器的提示中以回答問題。
TableRAG核心組件
- Tabular Query Expansion(表格查詢擴展)
為了有效地操作表格,關鍵是要精確地找出查詢所需的列名和單元格值。與之前的方法不同,TableRAG 不僅使用問題本身作為單一查詢,而是為模式和單元格值生成單獨的查詢。例如,對于問題 "What is the average price for wallets?",模型被提示生成針對列名(如 "product" 和 "price")以及相關單元格值(如 "wallet")的潛在查詢。然后,這些查詢被用來從表格中檢索相關的模式和單元格值。 - Schema Retrieval(Schema檢索)
在生成查詢后,Schema檢索會使用預訓練的編碼器fenc
來獲取相關的列名。編碼器將查詢與編碼的列名進行匹配,以確定相關性。檢索到的模式數據包括列名、數據類型和示例值。對于被識別為數值或日期時間類型的列,會顯示最小值和最大值作為示例值;對于分類列,會展示三個最常見的類別作為示例值。通過這種方式,檢索到的模式為表格的格式和內容提供了結構化的概覽,這將用于更有針對性的數據提取。
相關prompt如下:
========================================= Prompt =========================================
Given a large table regarding "amazon seller order status prediction orders data", I want
to answer a question: "What is the average price for leather wallets?"
Since I cannot view the table directly, please suggest some column names that might contain
the necessary data to answer this question.
Please answer with a list of column names in JSON format without any additional explanation
.
Example:
["column1", "column2", "column3"]
======================================= Completion =======================================
["product_name", "category", "price"]
- Cell Retrieval(單元格檢索)
在Schema檢索之后,進行單元格檢索以提取回答查詢所需的特定單元格值。這涉及到構建一個由表格 T 中的不同列-值對組成的數據庫,表示為 $ V = {(C_j, v_{ij})} $,其中 $ C_j $ 是第 $ j $ 列的列名。在實踐中,不同值的數量通常遠小于單元格的總數,這顯著提高了單元格檢索的效率。
單元格檢索在 TableRAG 中起著至關重要的作用:
相關prompt如下:
========================================= Prompt =========================================
Given a large table regarding "amazon seller order status prediction orders data", I want
to answer a question: "What is the average price for leather wallets?"
Please extract some keywords which might appear in the table cells and help answer the
question.
The keywords should be categorical values rather than numerical values.
The keywords should be contained in the question.
Please answer with a list of keywords in JSON format without any additional explanation.
Example:
["keyword1", "keyword2", "keyword3"]
======================================= Completion =======================================
["leather wallets", "average price", "amazon seller", "order status prediction", "orders
data"]
- 單元格識別:它允許語言模型準確地檢測表格中特定關鍵詞的存在,這對于有效的索引至關重要。
- 單元格-列關聯:它還使語言模型能夠將特定單元格與其相關的列名關聯起來,這在問題涉及特定屬性時至關重要。
- Cell Retrieval with Encoding Budget
在最壞的情況下,不同值的數量可能與單元格的總數相匹配。為了保持 TableRAG 在這種情況下的可行性,引入了一個單元格編碼預算$ B $。如果不同值的數量超過$ B $,編碼過程將限制在出現頻率最高的 $ B $ 對,從而在處理大型表格時提高效率。 - Program-Aided Solver(程序輔助求解器)
在獲得與問題相關的列名和單元格值后,語言模型可以使用這些信息有效地與表格交互。TableRAG 與可以以編程方式與表格交互的語言模型代理兼容。在這項工作中,作者考慮了 ReAct,這是一種流行的擴展語言模型功能的方法,已在最近的文獻中用于在表格 QA 基準測試中取得最先進的結果。
相關prompt如下:
========================================= Prompt =========================================
You are working with a pandas dataframe regarding "amazon seller order status prediction
orders data" in Python. The name of the dataframe is ‘df‘. Your task is to use ‘
python_repl_ast‘ to answer the question: "What is the average price for leather wallets?"
Tool description:
- ‘python_repl_ast‘: A Python interactive shell. Use this to execute python commands. Input
should be a valid single line python command.
Since you cannot view the table directly, here are some schemas and cell values retrieved
from the table.
Schema Retrieval Results:
{"column_name": "item_total", "dtype": "object", "cell_examples": [’$449.00’, ’$399.00’, ’
$549.00’]}
{"column_name": "quantity", "dtype": "int64", "min": 1, "max": 4}
{"column_name": "order_no", "dtype": "object", "cell_examples": [’402-4845680-8041921’,
’405-9763961-5211537’, ’404-3964908-7850720’]}
...
Cell Retrieval Queries: leather wallets, average price, order status, prediction, amazon
seller
Cell Retrieval Results:
{"column_name": "order_status", "cell_value": "Delivered to buyer"}
{"column_name": "description", "cell_value": "Pure Leather Camel Color Gent’s Wallet with
Coin Compartment and Card Holders | Men’s Ultra Slim Money Organiser (1 pc)"}
...
Strictly follow the given format to respond:
Thought: you should always think about what to do
Action: the single line Python command to execute
Observation: the result of the action
... (this Thought/Action/Observation can repeat N times)
Thought: before giving the final answer, you should think about the observations
Final Answer: the final answer to the original input question (Answer1, Answer2, ...)
Notes:
- Do not use markdown or any other formatting in your responses.
- Ensure the last line is only "Final Answer: Answer1, Answer2, ..." form, no other form.
- Directly output the Final Answer rather than outputting by Python.
- Ensure to have a concluding thought that verifies the table, observations and the
question before giving the final answer.
Now, given a table regarding "amazon seller order status prediction orders data", please
use ‘python_repl_ast‘ with the column names and cell values above to answer the question: "
What is the average price for leather wallets?"
Begin!
======================================= Completion =======================================
Thought: I need to remove the comma in the item_total values before converting them to
float.
Action: df[’item_total’] = df[’item_total’].str.replace(’$’,’’).str.replace(’,’,’’).astype(
float)
Observation: success!
Thought: Now, I can filter out the rows with leather wallets and calculate the average
price.
Action: df[df[’description’].str.contains(’wallet’, case=False)][’item_total’].mean()
Observation: 442.7916666666667
Thought: before giving the final answer, I should consider the observations and ensure that
the calculations are accurate.
Final Answer: The average price for leather wallets is $442.79.
實驗結果
數據集:為了驗證TableRAG在大規模表格上的可擴展性,作者構建了兩個新的基準數據集:ArcadeQA和BirdQA,分別源自Arcade和BIRD-SQL數據集。此外,作者還從TabFact數據集中生成了合成數據,將表格擴展到更大的規模。
并且比較了四種不同的方法,包括ReadTable、ReadSchema、RandRowSampling和RowColRetrieval。所有方法都基于相同的PyReAct求解器實現。
圖片
圖片
圖片
TableRAG的檢索設計顯著減少了計算成本和token使用,同時保持了高性能。
參考文獻
TableRAG: Million-Token Table Understanding with Language Models,https://arxiv.org/abs/2410.04739v1