LangChain 是一種尖端且強大的工具,旨在利用大型語言模型 (LLM) 的力量。
這些 LLM 擁有卓越的能力,可以有效地處理各種各樣的任務。 然而,重要的是要注意,他們的優勢在於他們的一般性質,而不是深入的領域專業知識。 自 GPT-4 推出以來,它的受歡迎程度迅速增長。
雖然法學碩士擅長處理各種任務,但在提供具體答案或處理需要深厚領域知識的任務時,他們可能會面臨局限性。 例如,考慮利用法學碩士在醫學或法律等專業領域回答問題或執行任務。
雖然法學碩士當然可以回答有關這些領域的一般查詢,但它可能難以提供需要專業知識或專業知識的更詳細或細微的答案。
這是因為 LLM 接受過來自不同來源的大量文本數據的訓練,使他們能夠學習模式、理解上下文並生成連貫的響應。 但是,他們的培訓通常不會像這些領域的人類專家那樣涉及特定領域或專業知識的獲取。
因此,雖然 LangChain 與 LLM 相結合,可以成為廣泛任務的寶貴工具,但重要的是要認識到,在某些情況下,深厚的領域專業知識可能仍然是必要的。 具有專業知識的人類專家可以提供必要的深度、細緻入微的理解和針對特定情境的見解,這可能超出了 LLM 本身的能力。
我們建議查看 LangChain 的文檔或 GitHub上 存儲庫,以便更透徹地了解其典型用例。 強烈建議全面了解此捆綁包。
它是如何工作的?
為了理解 LangChain 的目的和工作,讓我們考慮一個實際的例子。 我們知道 GPT-4 具有令人印象深刻的常識,可以為範圍廣泛的問題提供可靠的答案。
但是,如果我們想從自己的數據中獲取特定信息,例如個人文檔、書籍、PDF 文件或專有數據庫,該怎麼辦?
LangChain 允許我們連接一個 大型語言模型 比如 GPT-4 到我們自己的數據源。 它不僅僅是將一段文本粘貼到聊天界面。 相反,我們可以引用一個充滿我們自己數據的整個數據庫。
一旦我們獲得了想要的信息,LangChain 就可以協助我們採取具體的行動。 例如,我們可以指示它發送包含特定詳細信息的電子郵件。
為了實現這一目標,我們遵循使用 LangChain 的管道方法。 首先,我們獲取我們想要的文檔 語言模型 引用並將其分成更小的塊。 然後將這些塊存儲為嵌入,即 矢量數據庫中文本的矢量表示.
使用此設置,我們可以構建遵循標準管道的語言模型應用程序:用戶提出初始問題,然後將其發送到語言模型。 問題的矢量表示用於在矢量數據庫中執行相似性搜索,檢索相關信息塊。
然後將這些塊反饋給語言模型,使其能夠提供答案或採取所需的操作。
LangChain 促進了數據感知應用程序的開發,因為我們可以在向量存儲中引用我們自己的數據,並且是真實的,因為它們可以採取超越回答問題的行動。 噸
他開闢了許多實際用例,特別是在個人協助方面,大型語言模型可以處理預訂航班、轉賬或協助處理稅務相關事宜等任務。
此外,學習新科目的意義重大,因為語言模型可以參考整個教學大綱並加快學習過程。 編碼、數據分析和數據科學也有望受到這些進步的巨大影響。
最令人興奮的前景之一是將大型語言模型連接到現有的公司數據,例如客戶信息或營銷數據。 這種與高級 API(如 Meta 的 API 或 Google 的 API)的集成有望在數據分析和數據科學方面取得指數級進步。
如何構建網頁(演示)
目前,Langchain 可作為 Python 和 JavaScript 包使用。
我們可以使用 Streamlit、LangChain 和 OpenAI GPT-3 模型創建一個演示 Web 應用程序來實現 LangChain 概念。
但首先,我們必須安裝一些依賴項,包括 Streamlit、LangChain 和 OpenAI。
先決條件
精簡: 一個流行的 Python 包,用於創建與數據科學相關的 Web 應用程序
開放人工智能: 需要訪問 OpenAI 的 GPT-3 語言模型。
要安裝這些依賴項,請在 cmd 中使用以下命令:
pip install streamlit
pip install langchain
pip install openai
導入包
我們首先導入所需的包,例如 OpenAI、LangChain 和 Streamlit。 我們的語言模型鍊是使用 LangChain 中的三個類定義和執行的:LLMChain、SimpleSequentialChain 和 PromptTemplate。
import streamlit as st
from langchain.chains import LLMChain, SimpleSequentialChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
基本設置
然後使用 Streamlit 語法建立了我們項目的結構基礎。 我們為該應用程序命名為“什麼是真實的:使用簡單的順序鏈”,並包含一個指向作為該應用程序靈感的 GitHub 存儲庫的降價鏈接。
import streamlit as st
from langchain.chains import LLMChain, SimpleSequentialChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
前端小部件
我們使用簡單的 Streamlit 語法使用少量相關信息設置應用程序:
# If an API key has been provided, create an OpenAI language model instance
if API:
llm = OpenAI(temperature=0.7, openai_api_key=API)
else:
# If an API key hasn't been provided, display a warning message
st.warning("Enter your OPENAI API-KEY. Get your OpenAI API key from [here](https://platform.openai.com/account/api-keys).\n")
添加前端小部件
此外,我們需要提供一個輸入小部件以允許我們的用戶輸入任何問題。
# Add a text input box for the user's question
user_question = st.text_input(
"Enter Your Question : ",
placeholder = "Cyanobacteria can perform photosynthetsis , are they considered as plants?",
)
全做完了! 鏈條已啟動並運行!
我們採用各種運營鏈 SimpleSequentialChain
以響應用戶的查詢。 當用戶選擇 "Tell me about it"
按鈕:
if st.button("Tell me about it", type="primary"):
# Chain 1: Generating a rephrased version of the user's question
template = """{question}\n\n"""
prompt_template = PromptTemplate(input_variables=["question"], template=template)
question_chain = LLMChain(llm=llm, prompt=prompt_template)
# Chain 2: Generating assumptions made in the statement
template = """Here is a statement:
{statement}
Make a bullet point list of the assumptions you made when producing the above statement.\n\n"""
prompt_template = PromptTemplate(input_variables=["statement"], template=template)
assumptions_chain = LLMChain(llm=llm, prompt=prompt_template)
assumptions_chain_seq = SimpleSequentialChain(
chains=[question_chain, assumptions_chain], verbose=True
)
# Chain 3: Fact checking the assumptions
template = """Here is a bullet point list of assertions:
{assertions}
For each assertion, determine whether it is true or false. If it is false, explain why.\n\n"""
prompt_template = PromptTemplate(input_variables=["assertions"], template=template)
fact_checker_chain = LLMChain(llm=llm, prompt=prompt_template)
fact_checker_chain_seq = SimpleSequentialChain(
chains=[question_chain, assumptions_chain, fact_checker_chain], verbose=True
)
# Final Chain: Generating the final answer to the user's question based on the facts and assumptions
template = """In light of the above facts, how would you answer the question '{}'""".format(
user_question
)
template = """{facts}\n""" + template
prompt_template = PromptTemplate(input_variables=["facts"], template=template)
answer_chain = LLMChain(llm=llm, prompt=prompt_template)
overall_chain = SimpleSequentialChain(
chains=[question_chain, assumptions_chain, fact_checker_chain, answer_chain],
verbose=True,
)
# Running all the chains on the user's question and displaying the final answer
st.success(overall_chain.run(user_question))
question_chain
:這是我們管道中的第一步,接收用戶的問題作為輸入和輸出。 用戶的查詢作為鏈的模板。- 根據與問題相關的陳述,
assumptions_chain
使用來自的輸出生成假設的要點列表question_chain
作為輸入。 這LLMChain
和OpenAI
來自 LangChain 的模型被用來構建聲明。 用戶的任務是創建一個假設列表,這些假設是為了使用該鏈的模板生成語句。 - 基於從輸出
question_chain
和assumptions_chain
是,fact_checker_chain
以要點的形式生成斷言列表。 聲明是使用OpenAI
模型和LLMChain
來自朗鏈。 用戶的任務是確定每個聲明是否準確或不正確,並為這些聲明提供理由。 -
answer_chain
使用來自的輸出question_chain
,assumptions_chain
和fact_checker_chain
作為輸入,使用早期鏈產生的數據創建對用戶問題的響應。 該鏈的模板要求用戶使用創建的事實響應第一個查詢。 - 為了根據早期鏈產生的信息對用戶的查詢提供最終的響應,我們將這些鏈整合到整體鏈中。 鏈完成後,我們使用
st.success()
向用戶展示解決方案。
結論
我們可以簡單地將不同的語言模型操作鏈接在一起,通過使用 SimpleSequentialChain
LangChain的模塊。 對於各種各樣的 NLP 應用程序,包括聊天機器人、問答系統和語言翻譯工具,這可能會很有幫助。
LangChain 的卓越之處在於其抽象能力,這使用戶能夠專注於當前問題,而不是語言建模的細節。
LangChain 通過提供預訓練模型和精選模板,使創建複雜語言模型的過程更加人性化。
它為您提供了使用自己的數據微調語言模型的選項,從而使自定義語言模型變得簡單。 這使得能夠開發更精確的、特定領域的模型,對於給定的工作,這些模型的性能優於經過訓練的模型。
SimpleSequentialChain
LangChain 的模塊和其他功能使其成為快速開發和部署複雜 NLP 系統的有效工具。
發表評論