如何使用TeapotLLM進行可靠的問答、RAG和資訊提取

如何使用TeapotLLM進行可靠的問答、RAG和資訊提取

文字生成模型是研究和應用的絕佳工具。文字生成模型的優勢之一在於其能力,而這些能力來自於其架構、訓練和大型資料集。這些特徵決定了模型的工作方式。

TeapotAI 的開源模型就是一個很好的例子,它在 TeapotLLM 方面的工作非常突出。這是一個基於 800M 引數構建的小型語言模型。它還在合成資料上進行了微調,可以在低資源環境(包括智慧手機和 CPU)中提高效率。它是執行各種任務的絕佳工具。該模型只能在給定上下文中執行問答、RAG 和資訊提取。

學習目標

  • 瞭解 TeapotLLM 的功能和獨特之處。
  • 探索 TeapotLLM 的模型架構和訓練過程。
  • 瞭解 TeapotLLM 中的檢索增強生成(RAG) 和抗幻覺功能。
  • 探索 TeapotLLM 在人工智慧驅動任務中的實際應用。
  • 獲得執行 TeapotLLM 進行問答、RAG 和結構化資料提取的實踐經驗。

什麼是TeapotLLM?

TeapotLLM 是最先進的 800M 高精度引數模型。這一小型語言模型旨在生成無幻覺資訊。它附帶有一個全面的 Python 軟體包 TeapotAI,可幫助使用該模型。

該模型基於轉換器架構,可執行各種自然語言處理任務。開發人員使用 Deepseek-V3 生成的 LLM 任務合成資料集,在 flan-t5 基礎上對其進行了微調。

TeapotAI LLM 的特點

該模型有以下幾個特點

檢索增強生成

可以對該模型進行微調,以便使用自定義嵌入模型執行檢索增強生成。然後,該模型可以學習從文件中提取資訊來回答問題。

抗幻覺

Teapot AI 經過訓練可在提供的上下文中生成文字。這有助於它避免在沒有足夠資料的情況下回答問題。

Pydantic提取功能

這一功能意味著 TeapotAI 有一個軟體包,可為模型提供基於 pydantic 的資料提取功能。這使您可以高效、準確地從文字中獲取資料。

Teapot LLM的模型架構

該模型是透過微調 Flan-T5 基礎資料和合成資料建立的。其原理基於變壓器模型;Teapot AI 也是基於編碼器-解碼器架構建立的。

Teapot LLM 是由 Flan-T5-Large 微調而成的專用語言模型,Flan-T5-Large 是 T5(文字到文字轉換器)的一個著名指令微調變體。基礎模型 Flan-T5-Large 是一種基於轉換器的架構,它將每個問題都視為文字到文字問題,從而在各種自然語言處理任務中表現出色。Teapot LLM 在此基礎上,利用 DeepSeek-V3 生成的大型語言模型(LLM)任務合成資料集進行了進一步完善,DeepSeek-V3 是一種先進的生成模型,以生成高質量的合成文字而著稱。

Teapot LLM的模型架構

Source- Click Here

該模型的結構採用了許多轉換器模型常用的編碼器-解碼器結構來生成文字。這兩個元件各司其職。編碼器處理輸入序列,而解碼器對輸出序列進行同樣的處理。

在處理過程中,編碼器將輸入文字轉換為潛在表示。解碼器則將這些表徵轉換為特定任務的反應。

該模型的效能來自於對上下文的高度理解。這一點很容易從它的架構中得到證明,它採用了一些標準的轉換器原理,如轉換器注意機制、多頭自注意層、前饋網路和層歸一化。

Teapot LLM輸入和輸出處理

如何執行Teapot LLM

該模型可用於各種應用,如回答問題、與 RAG 聊天和提取資訊。我們將探討執行該模型以執行這些任務的步驟。

準備環境

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
! pip install teapotai
! pip install teapotai
! pip install teapotai

首先,安裝執行該任務所需的 Python 軟體包。該命令將安裝 TeapotAI,並提供執行抗幻覺任務所需的功能。

匯入基本庫

這一步需要從 TeapotAI 庫中匯入 TeapotAI 類。匯入該類有助於模型執行抗幻覺問答、檢索增強生成(RAG)和 JSON 提取等任務。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from teapotai import TeapotAI
from teapotai import TeapotAI
from teapotai import TeapotAI

背景

提供上下文是執行該模型的另一個重要步驟。這有助於模型獲取執行指定任務所需的資訊。

context = """The Eiffel Tower is a wrought iron lattice tower in Paris, France. It was designed by Gustave Eiffel and completed in 1889.It stands at a height of 330 meters and is one of the most recognizable structures in the world."""

這種上下文通常以多行字串的形式出現,如上圖所示,資訊用三層引號封裝。

模型初始化和查詢

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
teapot_ai = TeapotAI()
answer = teapot_ai.query(
query="What is the height of the Eiffel Tower?",
context=context
)
teapot_ai = TeapotAI() answer = teapot_ai.query( query="What is the height of the Eiffel Tower?", context=context )
 teapot_ai = TeapotAI()
answer = teapot_ai.query(
query="What is the height of the Eiffel Tower?",
context=context
)

程式碼初始化了 Teapotai,並使用它根據前面提到的上下文請求資訊。為了得到答案,我們要列印(結果),如下所示;

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
print (answer)
print (answer)
print (answer)

下面是根據上下文給出的答案。

根據上下文給出的答案

在回答包含許多文件的問題時,將此模型用作聊天工具。讓我們看看如何利用這一功能執行 Teapot。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from teapotai import TeapotAI
from teapotai import TeapotAI
 from teapotai import TeapotAI

與第一個任務一樣,該程式碼也會匯入必要的庫。

上下文

這裡可以提供 RAG 應用程式回答問題所依據的上下文;可以是長篇文章或文件。下面是一個示例;

documents = [   "The Eiffel Tower is located in Paris, France. It was built in 1889 and stands 330 meters tall.",   "The Great Wall of China is a historic fortification that stretches over 13,000 miles.",   "The Amazon Rainforest is the largest tropical rainforest in the world, covering over 5.5 million square kilometers.",   "The Grand Canyon is a natural landmark located in Arizona, USA, carved by the Colorado River.",   "Mount Everest is the tallest mountain on Earth, located in the Himalayas along the border between Nepal and China.",   "The Colosseum in Rome, Italy, is an ancient amphitheater known for its gladiator battles.",   "The Sahara Desert is the largest hot desert in the world, located in North Africa.",   "The Nile River is the longest river in the world, flowing through northeastern Africa.",   "The Empire State Building is an iconic skyscraper in New York City that was completed in 1931 and stands at 1454 feet tall."]

這段程式碼定義了一個名為“documents”的列表,其中每個元素都是一個包含事實資訊的字串。

用文件初始化Teapot以實現RAG

該初始化確保 TeapotAI 可以使用這些文件進行檢索增強生成(RAG),根據給定資訊回答問題,而不是根據常識生成回覆。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
teapot_ai = TeapotAI(documents=documents)
teapot_ai = TeapotAI(documents=documents)
teapot_ai = TeapotAI(documents=documents)

使用RAG獲取答案

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
answer = teapot_ai.chat([
{
"role":"system",
"content": "You are an agent designed to answer facts about famous landmarks."
},
{
"role":"user",
"content": "What landmark was constructed in the 1800s?"
}
])
answer = teapot_ai.chat([ { "role":"system", "content": "You are an agent designed to answer facts about famous landmarks." }, { "role":"user", "content": "What landmark was constructed in the 1800s?" } ])
 answer = teapot_ai.chat([
{
"role":"system",
"content": "You are an agent designed to answer facts about famous landmarks."
},
{
"role":"user",
"content": "What landmark was constructed in the 1800s?"
}
])

這段程式碼使用 TeapotAI 的“chat”方法生成結構化對話和回覆。輸入將是 “role”:”system” 和 “role”:”user” 欄位中顯示的資訊。因此,答案將僅基於上述名為“documents”的列表中的給定上下文。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
print(answer)
print(answer)
print(answer)

下面是根據檔案給出的答案。

根據檔案給出的答案

使用Teapot提取資訊

該模型可使用 JSON 結構從上下文中提取資訊。提取方法使用 Pydantic 模型來確保 Teapot 以正確的格式檢索資料。它可以根據欄位名稱推斷欄位,並在提供描述時加以利用。這種方法可與 RAG 和查詢功能無縫整合,以增強資料提取功能。

匯入必要的庫

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from teapotai import TeapotAI
from pydantic import BaseModel, Field
from teapotai import TeapotAI from pydantic import BaseModel, Field
from teapotai import TeapotAI
from pydantic import BaseModel, Field

這些庫有助於驗證資料結構,如 pydantic 模型。BaseModel 和 Field 對於執行正確的資料格式至關重要。它們共同確保從文字中提取準確和結構化的資訊。

上下文

在這裡,我們提供了要從中提取資訊的描述:一間公寓的詳細資訊。

apartment_description = """This spacious 2-bedroom apartment is available for rent in downtown New York. The monthly rent is $2500.It includes 1 bathrooms and a fully equipped kitchen with modern appliances. There is also a swimming pool at the backyard and beside the building.Pets are welcome!Please reach out to us at 555-123-4567 or john@realty.com"""

使用Pydantic模型提取資料

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class ApartmentInfo(BaseModel):
rent: float = Field(..., description="the monthly rent in dollars")
bedrooms: int = Field(..., description="the number of bedrooms")
bathrooms: int = Field(..., description="the number of bathrooms")
phone_number: str
class ApartmentInfo(BaseModel): rent: float = Field(..., description="the monthly rent in dollars") bedrooms: int = Field(..., description="the number of bedrooms") bathrooms: int = Field(..., description="the number of bathrooms") phone_number: str
class ApartmentInfo(BaseModel):
rent: float = Field(..., description="the monthly rent in dollars")
bedrooms: int = Field(..., description="the number of bedrooms")
bathrooms: int = Field(..., description="the number of bathrooms")
phone_number: str

該程式碼使用 Pydantic 定義了“ApartmentInfo”模型,以確保結構化資料提取。各個欄位闡明瞭每項描述,因此模型可以驗證和組織提取的資訊。

初始化Teapot

這將初始化 TeapotAI 模型,並允許訪問結構化資料提取功能。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
teapot_ai = TeapotAI()
teapot_ai = TeapotAI()
teapot_ai = TeapotAI()

提取公寓詳細資訊

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
extracted_info = teapot_ai.extract(
ApartmentInfo,
context=apartment_description
)
print(extracted_info)
extracted_info = teapot_ai.extract( ApartmentInfo, context=apartment_description ) print(extracted_info)
extracted_info = teapot_ai.extract(
ApartmentInfo,
context=apartment_description
)
print(extracted_info)

在這裡,我們使用 Teapot AI 模型從“ApartmentInfor”中提取結構資料,識別租金、電話號碼和房間數量等關鍵細節。

結果如下:

提取公寓詳細資訊

TeapotLLLM的抗幻覺能力

該模型為確保準確效能而採用的一項基本技術是抗幻覺性。它允許模型僅在所提供檔案或資訊的背景下提供答案。

讓我們用輸出結果來舉例說明。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from teapotai import teapotAI
context = """
The Great Pyramid of Giza, built around 2560 BCE, is the oldest of the Seven Wonders of the Ancient World and the only one still standing.
"""
from teapotai import teapotAI context = """ The Great Pyramid of Giza, built around 2560 BCE, is the oldest of the Seven Wonders of the Ancient World and the only one still standing. """
from teapotai import teapotAI
context = """
The Great Pyramid of Giza, built around 2560 BCE, is the oldest of the Seven Wonders of the Ancient World and the only one still standing.
"""

TeapotLLM的實際應用

讓我們重點介紹一下該模型在現代生活中的一些常見用例。

  • 人工智慧驅動的聊天機器人和虛擬助手是應用該模型功能的絕佳範例。您可以根據特定上下文生成答案,從而讓使用者獲得更準確、更正確的資訊。
  • 這種模式還可以透過總結冗長的文件和檢索關鍵細節,為部落格、報告和營銷資料生成內容。
  • 許多行業都依賴於資料驅動型系統。TeapotLLM 可以幫助從房地產文件、金融和法律系統中提取詳細資訊。您可以訪問合同、法律文件或原始資料。

小結

這個功能強大的開源模型專為可靠的問答、檢索增強生成(RAG)和結構化資訊提取而設計。它的 800M 引數變換器架構使其在低資源環境中也能保持高效率,同時還能保持高精確度。

TeapotLLM 能夠抵禦幻覺並提供結構化輸出,這使其成為人工智慧驅動型應用(從聊天機器人到文件分析)中的重要工具。

  • TeapotLLM 擁有8億個引數和架構,因此重量輕,適用於CPU和智慧手機等低資源環境。
  • 該模型的抗幻覺能力使其更具情境感知能力,減少了答案不準確的可能性。
  • 該模型使用 Pydantic 提取預定義格式的資訊,因此非常適合房地產列表、金融文件和法律文字處理等應用。

資源

評論留言