成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

推理增強生成ReAG,讓RAG效果更上一層樓

發布于 2025-2-25 13:03
瀏覽
0收藏

在檢索增強生成(RAG)技術嶄露頭角之際,業界對其賦予厚望,期待它能夠推動AI邁向新的智能高度。

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

1.傳統RAG的 “槽點”

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

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

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

2.ReAG來襲,告別傳統檢索模式

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

大語言模型具體這么做:

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

比如問“北極熊為啥減少”,ReAG分析《海冰的熱動力學》報告時,就算沒“北極熊”字樣,也能找到海冰減少影響其覓食的關鍵內容,給出答案。

3.ReAG工作原理

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

  • 直接攝取原始文檔:不管是Markdown、PDF,還是網址,ReAG都不做預處理,直接使用。
  • 并行分析文檔:大語言模型同時對每份文檔進行相關性檢查和內容提取,效率超高。
  • 動態合成答案:剔除不相關文檔,用篩選后的內容生成答案。

ReAG 的技術流程簡潔高效,具有較高的技術價值。

4.ReAG更勝一籌:優勢與權衡

4.1 ReAG優勢

  • 動態數據處理快:實時新聞、市場數據這類不斷變化的數據,ReAG能即時處理,無需重新嵌入,效率超高。
  • 復雜查詢有一手:像探究監管政策對社區銀行的影響這類難題,ReAG挖掘間接聯系的能力比RAG強,解題更在行。
  • 多模態分析超方便:圖表、表格、文本,ReAG能一起分析,還不用額外預處理。

4.2 ReAG短板

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

ReAG優勢突出但也有局限,使用時按需選擇!

5.ReAG技術棧揭秘

ReAG表現亮眼,其技術棧暗藏玄機,下面詳細羅列:

5.1 技術組件解析

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

5.2 技術棧優勢

  • 成本合理:GROQ處理重任務,Ollama本地處理輕量任務,節省成本。
  • 擴展性好:GROQ的LPU能處理大量并發評估。
  • 靈活多變:可更換模型,無需重寫管道。
  • 經驗之談:處理超50頁文檔,用大上下文窗口的大語言模型配合ReAG更好。

6.ReAG代碼實現

安裝所需依賴項

!pip install langchain langchain_groq langchain_ollama langchain_community pymupdf pypdf

下載數據

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

設置大語言模型

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,
)

定義系統提示

REAG_SYSTEM_PROMPT = """
# 角色和目標
你是一個智能知識檢索助手。你的任務是分析提供的文檔或網址,為用戶查詢提取最相關的信息。

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

# 約束
- 不要超出可用數據進行假設
- 明確指出是否未找到相關信息
- 在選擇來源時保持客觀
"""

定義RAG提示詞

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

定義響應模式

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

class ResponseSchema(BaseModel):
    content: str = Field(..., description="文檔中與回答所提問題相關或足以回答問題的頁面內容")
    reasoning: str = Field(..., description="針對所提問題選擇該頁面內容的原因")
    is_irrelevant: bool = Field(..., description="如果文檔中的內容不足以或與回答所提問題無關,指定為“True”;如果上下文或頁面內容與回答問題相關,則指定為“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)

響應

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}

格式化文檔的輔助函數

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}"

提取相關上下文的輔助函數

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

調用函數檢索相關上下文

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

生成響應的輔助函數

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]

生成響應

final_response = generate_response(question, final_context)
final_response

完整響應

'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)

完整響應

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)

完整響應

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)

完整響應

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)

完整響應

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)

完整響應

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)

完整響應

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)

輸出結果

['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.']

最終響應

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.實際應用

  • 醫學研究:從原始臨床試驗數據和期刊中綜合見解。
  • 金融市場:分析實時收益報告和美國證券交易委員會(SEC)文件,制定實時投資策略。
  • 法律分析:剖析復雜的判例法,識別先例之間的聯系。

8.ReAG的未來發展

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

9.結語

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

ReAG 將檢索巧妙轉化為推理任務,精準復刻人類研究的全面性、細致性和上下文關聯性。在醫學研究中,它能高效梳理臨床數據;于金融領域,又可敏銳洞察市場動態。這種獨特的優勢,使其在多領域已嶄露頭角。

隨著技術發展,ReAG 有望解鎖更多應用場景,深度賦能各行業。讓我們一同期待,它在未來 AI 發展浪潮中創造更多可能,重塑更多領域的發展格局。

本文轉載自 ??AI科技論談??,作者: AI科技論談

標簽
收藏
回復
舉報
回復
相關推薦
主站蜘蛛池模板: 爱综合| 精品国产三级 | 亚洲精品自在在线观看 | 亚洲精品久久久久久宅男 | 欧美精品a∨在线观看不卡 国产精品久久国产精品 | 久久国内精品 | 草草网 | 色毛片 | 天天射网站 | 亚洲国产精品久久久久 | 国产高清精品一区二区三区 | 中文在线视频 | 国精产品一区二区三区 | 可以看黄的视频 | 亚洲高清av在线 | 欧美精品欧美精品系列 | 中文字幕一区二区三区不卡 | 久草福利 | 久久91精品国产一区二区三区 | 中文字幕在线免费观看 | 亚洲精品白浆高清久久久久久 | 欧美无乱码久久久免费午夜一区 | 久久久91精品国产一区二区三区 | 天天欧美 | 欧美一区免费 | 国产精品国产三级国产aⅴ中文 | 欧美色图另类 | 色接久久 | 国产一区亚洲 | 久久久久久久久久毛片 | 91精品国产综合久久久久久蜜臀 | 一级黄a视频 | 久久se精品一区精品二区 | www.欧美.com | 国产乱码精品一区二区三区忘忧草 | 天天欧美| 国产在线网站 | 99九九视频 | 中文字幕在线视频免费观看 | 国产精品视频综合 | 久久99蜜桃综合影院免费观看 |