自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

推理增強(qiáng)生成ReAG,讓RAG效果更上一層樓

發(fā)布于 2025-2-25 13:03
瀏覽
0收藏

在檢索增強(qiáng)生成(RAG)技術(shù)嶄露頭角之際,業(yè)界對(duì)其賦予厚望,期待它能夠推動(dòng)AI邁向新的智能高度。

然而,實(shí)踐中RAG暴露出諸多缺陷,極大地限制了其應(yīng)用效果與AI的發(fā)展進(jìn)程。在此背景下,推理增強(qiáng)生成(ReAG)技術(shù)應(yīng)運(yùn)而生。ReAG憑借其獨(dú)特的技術(shù)架構(gòu)與運(yùn)行邏輯,為解決 RAG 問題提供新思路和可行方案,在AI升級(jí)之路上潛力巨大。

1.傳統(tǒng)RAG的 “槽點(diǎn)”

傳統(tǒng) RAG 系統(tǒng)就好比記憶力差的圖書管理員,看似在努力找資料,實(shí)則狀況百出:

  • 語義搜索“缺根弦”:找文檔只看表面,像搜“空氣污染”,就只知道“汽車尾氣排放”,像《城市肺部疾病趨勢(shì)》這種相關(guān)研究就被無視了。
  • 基礎(chǔ)架構(gòu)“找麻煩”:分塊、嵌入、向量數(shù)據(jù)庫把流程復(fù)雜化,還容易出問題,索引過時(shí)、分割錯(cuò)誤經(jīng)常有。
  • 知識(shí)更新“慢吞吞”:醫(yī)學(xué)、金融數(shù)據(jù)變得快,RAG更新索引卻很慢,新知識(shí)進(jìn)不來,根本沒法用。

你問“北極熊為啥變少”,RAG只說“海冰融化”,關(guān)鍵的覓食問題卻不提,這就是RAG的缺陷。

2.ReAG來襲,告別傳統(tǒng)檢索模式

RAG的問題不少,ReAG則帶來全新思路。它跳過RAG的預(yù)處理流程,直接把原始材料(文本文件、電子表格、網(wǎng)址等)喂給語言模型。

大語言模型具體這么做:

  • 完整讀取文檔:無需分塊、嵌入,文檔上下文完整保留。
  • 精準(zhǔn)篩選內(nèi)容:先判斷文檔是否有用(相關(guān)性檢查),再確定哪些部分重要(內(nèi)容提?。?。
  • 智能合成答案:像專業(yè)人員一樣整合信息,即便關(guān)鍵詞不匹配,也能找出聯(lián)系。

比如問“北極熊為啥減少”,ReAG分析《海冰的熱動(dòng)力學(xué)》報(bào)告時(shí),就算沒“北極熊”字樣,也能找到海冰減少影響其覓食的關(guān)鍵內(nèi)容,給出答案。

3.ReAG工作原理

ReAG 是如何 “工作” 的?給大家拆解一下它的技術(shù)流程,一看就懂:

  • 直接攝取原始文檔:不管是Markdown、PDF,還是網(wǎng)址,ReAG都不做預(yù)處理,直接使用。
  • 并行分析文檔:大語言模型同時(shí)對(duì)每份文檔進(jìn)行相關(guān)性檢查和內(nèi)容提取,效率超高。
  • 動(dòng)態(tài)合成答案:剔除不相關(guān)文檔,用篩選后的內(nèi)容生成答案。

ReAG 的技術(shù)流程簡(jiǎn)潔高效,具有較高的技術(shù)價(jià)值。

4.ReAG更勝一籌:優(yōu)勢(shì)與權(quán)衡

4.1 ReAG優(yōu)勢(shì)

  • 動(dòng)態(tài)數(shù)據(jù)處理快:實(shí)時(shí)新聞、市場(chǎng)數(shù)據(jù)這類不斷變化的數(shù)據(jù),ReAG能即時(shí)處理,無需重新嵌入,效率超高。
  • 復(fù)雜查詢有一手:像探究監(jiān)管政策對(duì)社區(qū)銀行的影響這類難題,ReAG挖掘間接聯(lián)系的能力比RAG強(qiáng),解題更在行。
  • 多模態(tài)分析超方便:圖表、表格、文本,ReAG能一起分析,還不用額外預(yù)處理。

4.2 ReAG短板

  • 成本較高:處理100份文檔,ReAG需調(diào)用100次大語言模型,RAG向量搜索成本則低很多。
  • 大規(guī)模處理慢:面對(duì)海量文檔,ReAG速度欠佳,RAG和ReAG混合使用效果更佳。

ReAG優(yōu)勢(shì)突出但也有局限,使用時(shí)按需選擇!

5.ReAG技術(shù)棧揭秘

ReAG表現(xiàn)亮眼,其技術(shù)棧暗藏玄機(jī),下面詳細(xì)羅列:

5.1 技術(shù)組件解析

  1. GROQ + Llama-3.3–70B-Versatile
  • 職責(zé):負(fù)責(zé)相關(guān)性評(píng)估,初步篩選文檔。
  • 優(yōu)勢(shì):推理快,每秒處理500多令牌;700億參數(shù)精準(zhǔn)評(píng)分;12.8萬令牌大窗口。
  • 示例:能識(shí)別無關(guān)鍵詞重疊的《海冰的熱動(dòng)力學(xué)》與“北極熊減少”相關(guān)。
  1. Ollama + DeepSeek-R1:14B
  • 任務(wù):進(jìn)行響應(yīng)合成,推理出答案。
  • 長(zhǎng)處:輕量省錢,針對(duì)提取總結(jié)優(yōu)化;可本地運(yùn)行保隱私、降成本;12.8萬令牌窗口。
  • 應(yīng)用:從文檔提取關(guān)鍵信息,如無冰期覓食窗口變化數(shù)據(jù)。
  1. LangChain
  • 功能:編排流程、實(shí)現(xiàn)自動(dòng)化。
  • 特點(diǎn):并行GROQ和Ollama任務(wù);管理文檔、處理錯(cuò)誤、聚合輸出。

5.2 技術(shù)棧優(yōu)勢(shì)

  • 成本合理:GROQ處理重任務(wù),Ollama本地處理輕量任務(wù),節(jié)省成本。
  • 擴(kuò)展性好:GROQ的LPU能處理大量并發(fā)評(píng)估。
  • 靈活多變:可更換模型,無需重寫管道。
  • 經(jīng)驗(yàn)之談:處理超50頁文檔,用大上下文窗口的大語言模型配合ReAG更好。

6.ReAG代碼實(shí)現(xiàn)

安裝所需依賴項(xiàng)

!pip install langchain langchain_groq langchain_ollama langchain_community pymupdf pypdf

下載數(shù)據(jù)

!mkdir ./data
!mkdir ./chunk_caches
!wget "https://www.binasss.sa.cr/int23/8.pdf" -O "./data/fibromyalgia.pdf"

設(shè)置大語言模型

from langchain_groq import ChatGroq
from langchain_ollama import ChatOllama
import os
os.environ["GROQ_API_KEY"] = "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"

llm_relevancy = ChatGroq(
    model="llama-3.3-70b-versatile",
    temperature=0,
)

llm = ChatOllama(
    model="deepseek-r1:14b",
    temperature=0.6,
    max_tokens=3000,
)

定義系統(tǒng)提示

REAG_SYSTEM_PROMPT = """
# 角色和目標(biāo)
你是一個(gè)智能知識(shí)檢索助手。你的任務(wù)是分析提供的文檔或網(wǎng)址,為用戶查詢提取最相關(guān)的信息。

# 指令
1. 仔細(xì)分析用戶的查詢,確定關(guān)鍵概念和要求。
2. 在提供的來源中搜索相關(guān)信息,并在“content”字段中輸出相關(guān)部分。
3. 如果你在文檔中找不到必要的信息,返回“isIrrelevant: true”,否則返回“isIrrelevant: false”。

# 約束
- 不要超出可用數(shù)據(jù)進(jìn)行假設(shè)
- 明確指出是否未找到相關(guān)信息
- 在選擇來源時(shí)保持客觀
"""

定義RAG提示詞

rag_prompt = """你是一個(gè)問答任務(wù)助手。使用以下檢索到的上下文片段來回答問題。如果你不知道答案,就說不知道。最多用三句話,保持回答簡(jiǎn)潔。
問題:{question} 
上下文:{context} 
答案:
"""

定義響應(yīng)模式

from pydantic import BaseModel, Field
from typing import List
from langchain_core.output_parsers import JsonOutputParser

class ResponseSchema(BaseModel):
    content: str = Field(..., description="文檔中與回答所提問題相關(guān)或足以回答問題的頁面內(nèi)容")
    reasoning: str = Field(..., description="針對(duì)所提問題選擇該頁面內(nèi)容的原因")
    is_irrelevant: bool = Field(..., description="如果文檔中的內(nèi)容不足以或與回答所提問題無關(guān),指定為“True”;如果上下文或頁面內(nèi)容與回答問題相關(guān),則指定為“False”")

class RelevancySchemaMessage(BaseModel):
    source: ResponseSchema

relevancy_parser = JsonOutputParser(pydantic_object=RelevancySchemaMessage)

加載并處理輸入文檔

from langchain_community.document_loaders import PyMuPDFLoader

file_path = "./data/fibromyalgia.pdf"
loader = PyMuPDFLoader(file_path)
docs = loader.load()
print(len(docs))
print(docs[0].metadata)

響應(yīng)

8
{'producer': 'Acrobat Distiller 6.0 for Windows',
'creator': 'Elsevier',
'creationdate': '2023-01-20T09:25:19-06:00',
'source': './data/fibromyalgia.pdf',
'file_path': './data/fibromyalgia.pdf',
'total_pages': 8,
'format': 'PDF 1.7',
'title': 'Fibromyalgia: Diagnosis and Management',
'author': 'Bradford T. Winslow MD',
'subject': 'American Family Physician, 107 (2023) 137-144',
'keywords': '',
'moddate': '2023-02-27T15:02:12+05:30',
'trapped': '',
'modDate': "D:20230227150212+05'30'",
'creationDate': "D:20230120092519-06'00'",
'page': 0}

格式化文檔的輔助函數(shù)

from langchain.schema import Document

def format_doc(doc: Document) -> str:
    return f"Document_Title: {doc.metadata['title']}\nPage: {doc.metadata['page']}\nContent: {doc.page_content}"

提取相關(guān)上下文的輔助函數(shù)

from langchain_core.prompts import PromptTemplate

def extract_relevant_context(question, documents):
    result = []
    for doc in documents:
        formatted_documents = format_doc(doc)
        system = f"{REAG_SYSTEM_PROMPT}\n\n# Available source\n\n{formatted_documents}"
        prompt = f"""Determine if the 'Avaiable source' content supplied is sufficient and relevant to ANSWER the QUESTION asked.
        QUESTION: {question}
        #INSTRUCTIONS TO FOLLOW
        1. Analyze the context provided thoroughly to check its relevancy to help formulizing a response for the QUESTION asked.
        2, STRICTLY PROVIDE THE RESPONSE IN A JSON STRUCTURE AS DESCRIBED BELOW:
            ```json
               {{"content":<<The page content of the document that is relevant or sufficient to answer the question asked>>,
                 "reasoning":<<The reasoning for selecting The page content with respect to the question asked>>,
                 "is_irrelevant":<<Specify 'True' if the content in the document is not sufficient or relevant.Specify 'False' if the page content is sufficient to answer the QUESTION>>
                 }}
            ```
         """
        messages =[ {"role": "system", "content": system},
                       {"role": "user", "content": prompt},
                    ]
        response = llm_relevancy.invoke(messages)    
        print(response.content)
        formatted_response = relevancy_parser.parse(response.content)
        result.append(formatted_response)
    final_context = []
    for items in result:
        if (items['is_irrelevant'] == False) or ( items['is_irrelevant'] == 'false') or (items['is_irrelevant'] == 'False'):
            final_context.append(items['content'])
    return final_context

調(diào)用函數(shù)檢索相關(guān)上下文

question = "What is Fibromyalgia?"
final_context = extract_relevant_context(question, docs)
print(len(final_context))

生成響應(yīng)的輔助函數(shù)

def generate_response(question, final_context):
    prompt = PromptTemplate(template=rag_prompt,
                                     input_variables=["question","context"],)
    chain  = prompt | llm
    response = chain.invoke({"question":question,"context":final_context})
    print(response.content.split("\n\n")[-1])
    return response.content.split("\n\n")[-1]

生成響應(yīng)

final_response = generate_response(question, final_context)
final_response

完整響應(yīng)

'Fibromyalgia is a chronic condition characterized by widespread musculoskeletal pain, fatigue, disrupted sleep, and cognitive difficulties like "fibrofog." It is often associated with heightened sensitivity to pain due to altered nervous system processing. Diagnosis considers symptoms such as long-term pain, fatigue, and sleep issues without underlying inflammation or injury.'

問題2

question =  "What are the causes of Fibromyalgia?"
final_context = extract_relevant_context(question, docs)
final_response = generate_response(question, final_context)

完整響應(yīng)

Fibromyalgia likely results from disordered central pain processing leading to heightened sensitivity (hyperalgesia and allodynia). Possible causes include dysfunction of the hypothalamic-pituitary-adrenal axis, inflammation, glial activation, small fiber neuropathy, infections like Epstein-Barr virus or Lyme disease, and a genetic component. Other conditions, such as infections or medication side effects, may also contribute to similar symptoms.

問題3

question =  "Do people suffering from rheumatologic conditions may have fibromyalgia?"
final_context = extract_relevant_context(question, docs)
final_response = generate_response(question, final_context)

完整響應(yīng)

Yes, people with rheumatologic conditions, such as rheumatoid arthritis or psoriatic arthritis, may also have fibromyalgia. This is because they share overlapping symptoms, making diagnosis challenging.

問題4

question =  "Mention the nonpharmacologic treatment for fibromyalgia?"
final_context = extract_relevant_context(question, docs)
final_response = generate_response(question, final_context)

完整響應(yīng)

Nonpharmacologic treatments for fibromyalgia include patient education, exercise, and cognitive behavior therapy (CBT).

問題5

question =  "According to 2016 American College of Rheumatology Fibromyalgia what is the Diagnostic Criteria for Fibromyalgia?"
final_context = extract_relevant_context(question, docs)
final_response = generate_response(question, final_context)

完整響應(yīng)

The 2016 American College of Rheumatology diagnostic criteria for fibromyalgia require generalized pain in at least four of five body regions for at least three months. Additionally, patients must meet either a Widespread Pain Index (WPI) score of ≥7 with a Symptom Severity Scale (SSS) score of ≥5 or a WPI score of ≥4 with an SSS score of ≥9. Other disorders that could explain the symptoms must be ruled out.

問題6

question =  "What is the starting dosage of Amitriptyline?"
final_context = extract_relevant_context(question, docs)
final_response = generate_response(question, final_context)

完整響應(yīng)

The starting dosage of Amitriptyline for adults is usually between 25 to 50 mg per day, often beginning with a lower dose of 5 to 10 mg at night to minimize side effects before gradually increasing.

問題7

question = "What has been mentioned about AAPT 2019 Diagnostic Criteria for Fibromyalgia"
final_context = extract_relevant_context(question, docs)
final_response = generate_response(question, final_context)

完整響應(yīng)

The AAPT 2019 criteria for fibromyalgia include multisite pain in at least six of nine specified areas, moderate to severe sleep problems or fatigue, and symptoms lasting three months or more.

問題8

question =  "What are the medications and doses for Fibromyalgia?"
final_context = extract_relevant_context(question, docs)
print(final_context)
final_response = generate_response(question, final_context)

輸出結(jié)果

['Duloxetine, milnacipran, pregabalin, and amitriptyline are potentially effective medications for fibromyalgia. Nonsteroidal anti-inflammatory drugs and opioids have not demonstrated benefits for fibromyalgia and have significant limitations.',
 'Amitriptyline, cyclobenzaprine, duloxetine (Cymbalta), milnacipran (Savella), and pregabalin (Lyrica) are effective for pain in fibromyalgia.43,46-48,50,52,54',
 'Amitriptyline (tricyclic antidepressant) - 5 to 10 mg at night, 20 to 30 mg at night. Cyclobenzaprine (muscle relaxant; tricyclic derivative) - 5 to 10 mg at night, 10 to 40 mg daily in 1 to 3 divided doses. Duloxetine (Cymbalta; serotonin-norepinephrine reuptake inhibitor) - 20 to 30 mg every morning, 60 mg every morning. Milnacipran (Savella; serotonin-norepinephrine reuptake inhibitor) - 12.5 mg every morning, 50 mg twice daily. Pregabalin (Lyrica; gabapentinoid) - 25 to 50 mg at bedtime, 150 to 450 mg at bedtime.',
 'Fibromyalgia is often treated with medications such as pregabalin (Lyrica) and duloxetine (Cymbalta). Pregabalin can be started at a dose of 75 mg twice daily, with a maximum dose of 450 mg/day. Duloxetine can be initiated at a dose of 30 mg once daily, with a target dose of 60 mg/day.',
 'Fibromyalgia is often treated with medications such as pregabalin (Lyrica) and duloxetine (Cymbalta). Pregabalin can be started at a dose of 75 mg twice daily, with a maximum dose of 450 mg/day. Duloxetine can be initiated at a dose of 30 mg once daily, with a target dose of 60 mg/day.']

最終響應(yīng)

The medications commonly used to treat fibromyalgia include:

1. **Amitriptyline**: A tricyclic antidepressant typically taken at night in doses ranging from 5 to 30 mg.

2. **Cyclobenzaprine**: A muscle relaxant and tricyclic derivative, usually administered in doses up to 40 mg daily in divided doses.

3. **Duloxetine (Cymbalta)**: A serotonin-norepinephrine reuptake inhibitor taken in the morning, starting at 20-30 mg and increasing to 60 mg if needed.

4. **Milnacipran (Savella)**: Another serotonin-norepinephrine reuptake inhibitor, starting at 12.5 mg in the morning and potentially increased to 50 mg twice daily.

5. **Pregabalin (Lyrica)**: A gabapentinoid taken at bedtime, beginning with 75 mg twice daily and up to a maximum of 450 mg/day.

These medications are effective for managing pain associated with fibromyalgia. It's important to note that dosages should be adjusted under medical supervision, starting low and increasing as necessary. Additionally, NSAIDs and opioids are not recommended for treating fibromyalgia due to limited effectiveness and potential side effects.

7.實(shí)際應(yīng)用

  • 醫(yī)學(xué)研究:從原始臨床試驗(yàn)數(shù)據(jù)和期刊中綜合見解。
  • 金融市場(chǎng):分析實(shí)時(shí)收益報(bào)告和美國(guó)證券交易委員會(huì)(SEC)文件,制定實(shí)時(shí)投資策略。
  • 法律分析:剖析復(fù)雜的判例法,識(shí)別先例之間的聯(lián)系。

8.ReAG的未來發(fā)展

  • 混合系統(tǒng):先使用檢索增強(qiáng)生成(RAG)進(jìn)行初步篩選,然后利用推理增強(qiáng)生成(ReAG)進(jìn)行深度分析。
  • 低成本模型:開源大語言模型(如 DeepSeek)和量化技術(shù)將降低成本。
  • 更大的上下文窗口:未來的模型將能夠處理包含十億個(gè)標(biāo)記的文檔,這會(huì)使推理增強(qiáng)生成(ReAG)更加強(qiáng)大。

9.結(jié)語

在 AI 技術(shù)不斷迭代的當(dāng)下,ReAG 為我們展現(xiàn)了一種全新思路。ReAG 無意取代 RAG,而是從根本上重塑 AI 與知識(shí)的交互邏輯。

ReAG 將檢索巧妙轉(zhuǎn)化為推理任務(wù),精準(zhǔn)復(fù)刻人類研究的全面性、細(xì)致性和上下文關(guān)聯(lián)性。在醫(yī)學(xué)研究中,它能高效梳理臨床數(shù)據(jù);于金融領(lǐng)域,又可敏銳洞察市場(chǎng)動(dòng)態(tài)。這種獨(dú)特的優(yōu)勢(shì),使其在多領(lǐng)域已嶄露頭角。

隨著技術(shù)發(fā)展,ReAG 有望解鎖更多應(yīng)用場(chǎng)景,深度賦能各行業(yè)。讓我們一同期待,它在未來 AI 發(fā)展浪潮中創(chuàng)造更多可能,重塑更多領(lǐng)域的發(fā)展格局。

本文轉(zhuǎn)載自 ??AI科技論談??,作者: AI科技論談

標(biāo)簽
收藏
回復(fù)
舉報(bào)
回復(fù)
相關(guān)推薦