LangChain yog cov cuab yeej txiav-ntug thiab muaj zog tsim los siv lub zog ntawm Cov Qauv Lus Loj (LLMs).
Cov LLMs no muaj peev xwm ua tau zoo kawg thiab tuaj yeem daws tau ntau yam haujlwm. Txawm li cas los xij, nws tseem ceeb heev uas yuav tsum nco ntsoov tias lawv lub zog nyob hauv lawv qhov xwm txheej ntau dua li qhov kev paub tob tob. Nws cov koob meej tau loj hlob sai heev txij li thaum pib ntawm GPT-4.
Thaum LLMs ua tau zoo ntawm kev tuav ntau yam dej num, lawv yuav ntsib kev txwv thaum nws los txog rau kev muab cov lus teb tshwj xeeb lossis daws cov haujlwm uas yuav tsum tau muaj kev paub tob. Xav txog, piv txwv li, siv LLM los teb cov lus nug lossis ua haujlwm hauv cov haujlwm tshwj xeeb xws li tshuaj lossis kev cai lij choj.
Thaum LLM tuaj yeem teb cov lus nug dav dav txog cov haujlwm no, nws yuav nyuaj los muab cov lus qhia ntxaws ntxiv lossis cov lus teb tsis txaus ntseeg uas yuav tsum muaj kev paub tshwj xeeb lossis kev txawj ntse.
Qhov no yog vim LLMs raug cob qhia ntau ntawm cov ntawv nyeem los ntawm ntau qhov chaw, ua rau lawv kawm cov qauv, nkag siab cov ntsiab lus, thiab tsim cov lus teb sib txuam. Txawm li cas los xij, lawv txoj kev cob qhia tsis feem ntau koom nrog kev paub tshwj xeeb lossis kev paub tshwj xeeb kom tau txais tib yam li tib neeg cov kws tshaj lij hauv cov haujlwm ntawd.
Yog li ntawd, thaum LangChain, ua ke nrog LLMs, tuaj yeem yog cov cuab yeej muaj txiaj ntsig rau ntau txoj haujlwm, nws tseem ceeb heev kom paub tias kev txawj ntse hauv kev sib sib zog nqus tseem yuav tsim nyog hauv qee qhov xwm txheej. Tib neeg cov kws tshaj lij uas muaj kev paub tshwj xeeb tuaj yeem muab qhov tsim nyog qhov tob, kev nkag siab tsis txaus ntseeg, thiab cov ntsiab lus tshwj xeeb kev nkag siab uas tej zaum yuav dhau ntawm lub peev xwm ntawm LLMs ib leeg.
Peb xav qhia saib LangChain cov ntaub ntawv lossis GitHub repository rau kev nkag siab ntau dua ntawm nws cov kev siv raug siv. Nws raug nquahu kom tau txais daim duab loj dua ntawm cov pob no.
Nws Ua Haujlwm Li Cas?
Txhawm rau nkag siab lub hom phiaj thiab kev ua haujlwm ntawm LangChain, cia peb xav txog qhov piv txwv zoo. Peb paub tias GPT-4 muaj kev paub dav dav dav dav thiab tuaj yeem muab cov lus teb txhim khu kev qha rau ntau cov lus nug.
Txawm li cas los xij, yuav ua li cas yog tias peb xav tau cov ntaub ntawv tshwj xeeb los ntawm peb tus kheej cov ntaub ntawv, xws li cov ntaub ntawv tus kheej, phau ntawv, PDF, lossis cov ntaub ntawv ntiag tug?
LangChain tso cai rau peb mus txuas ib qauv lus loj zoo li GPT-4 rau peb tus kheej cov ntaub ntawv. Nws mus dhau qhov tsuas yog pasting ib ntu ntawm cov ntawv nyeem rau hauv kev sib tham sib tham. Hloov chaw, peb tuaj yeem siv tag nrho cov ntaub ntawv sau nrog peb tus kheej cov ntaub ntawv.
Thaum peb tau txais cov ntaub ntawv xav tau, LangChain tuaj yeem pab peb ua haujlwm tshwj xeeb. Piv txwv li, peb tuaj yeem qhia nws kom xa email uas muaj qee yam ntsiab lus.
Txhawm rau ua tiav qhov no, peb ua raws li txoj hauv kev siv LangChain. Ua ntej, peb muab cov ntaub ntawv peb xav tau qauv lus los siv thiab muab faib ua me me chunks. Cov chunks no ces cia li embeddings, uas yog vector sawv cev ntawm cov ntawv nyeem, hauv Vector Database.
Nrog rau qhov kev teeb tsa no, peb tuaj yeem tsim cov qauv kev siv lus uas ua raws li tus qauv kav dej: tus neeg siv nug cov lus nug thawj zaug, uas yog tom qab ntawd xa mus rau cov qauv lus. Cov lus nug tus sawv cev vector yog siv los ua qhov kev tshawb fawb zoo sib xws hauv Vector Database, khaws cov ntaub ntawv tseem ceeb.
Cov chunks no tau muab rov qab rau cov qauv lus, ua kom nws muab cov lus teb lossis ua qhov xav tau.
LangChain pab txhawb kev txhim kho cov ntawv thov uas paub txog cov ntaub ntawv, raws li peb tuaj yeem siv peb tus kheej cov ntaub ntawv hauv lub khw vector, thiab qhov tseeb, vim tias lawv tuaj yeem ua dhau los teb cov lus nug. T
nws qhib ntau qhov kev siv tau zoo, tshwj xeeb tshaj yog nyob rau hauv kev pab tus kheej, qhov twg tus qauv lus loj tuaj yeem ua haujlwm xws li booking flights, hloov nyiaj, lossis pab cuam txog cov teeb meem se.
Tsis tas li ntawd, qhov cuam tshuam rau kev kawm thiab kawm cov kev kawm tshiab yog qhov tseem ceeb, vim hais tias hom lus tuaj yeem siv rau tag nrho cov lus qhia thiab ua kom cov txheej txheem kawm sai. Coding, kev txheeb xyuas cov ntaub ntawv, thiab cov ntaub ntawv tshawb fawb tseem xav tias yuav cuam tshuam los ntawm cov kev nce qib no.
Ib qho kev cia siab tshaj plaws yog txuas cov qauv lus loj rau cov ntaub ntawv tuam txhab uas twb muaj lawm, xws li cov ntaub ntawv cov neeg siv khoom lossis cov ntaub ntawv lag luam. Qhov kev koom ua ke nrog APIs siab heev xws li Meta's API lossis Google's API tau cog lus tias yuav muaj kev vam meej hauv cov ntaub ntawv txheeb xyuas thiab cov ntaub ntawv tshawb fawb.
Yuav Ua Li Cas Tsim Ib Lub Vev Xaib (Demo)
Tam sim no, Langchain muaj nyob ua Python thiab JavaScript pob.
Peb tuaj yeem tsim qhov ua qauv qhia Web App siv Streamlit, LangChain, thiab OpenAI GPT-3 qauv los siv lub tswv yim LangChain.
Tab sis ua ntej, peb yuav tsum nruab ob peb qhov kev vam khom, suav nrog Streamlit, LangChain, thiab OpenAI.
Pre-requisites
Streamlit: Ib pob Python nrov rau kev tsim cov ntaub ntawv tshawb fawb txog lub vev xaib
OpenAI: Kev nkag mus rau OpenAI's GPT-3 hom lus yog xav tau.
Txhawm rau nruab cov kev vam meej, siv cov lus txib hauv qab no hauv cmd:
pip install streamlit
pip install langchain
pip install openai
Ntshuam pob
Peb pib los ntawm kev xa cov pob khoom xav tau, xws li OpenAI, LangChain, thiab Streamlit. Peb cov lus qauv chains raug txhais thiab ua tiav siv peb chav kawm los ntawm LangChain: LLMChain, SimpleSequentialChain, thiab PromptTemplate.
import streamlit as st
from langchain.chains import LLMChain, SimpleSequentialChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
Teeb Yeem
Lub hauv paus txheej txheem ntawm peb qhov project tau muab tso rau siv Streamlit syntax. Peb tau muab lub npe rau lub npe "Dab tsi yog qhov tseeb: Siv Simple Sequential Chain" thiab suav nrog qhov txuas txuas mus rau GitHub qhov chaw cia khoom uas ua haujlwm raws li lub app kev tshoov siab.
import streamlit as st
from langchain.chains import LLMChain, SimpleSequentialChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
Front-End Widgets
Peb teeb tsa lub app nrog ob peb cov ntaub ntawv tseem ceeb, siv Streamlit syntax yooj yim:
# 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")
Ntxiv rau pem hauv ntej-kawg widgets
Tsis tas li ntawd, peb yuav tsum muab ib qho widget tso cai rau peb cov neeg siv nkag mus rau cov lus nug.
# 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?",
)
Txhua yam ua tiav! Cov chains tau nce thiab khiav!
Peb ntiav ntau yam chains ntawm kev ua haujlwm ua ke nrog SimpleSequentialChain
los teb rau tus neeg siv cov lus nug. Cov chains yog nqa tawm nyob rau hauv ib theem zuj zus thaum tus neeg siv xaiv lub "Tell me about it"
khawm:
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
: uas yog thawj kauj ruam hauv peb cov raj xa dej, tau txais cov neeg siv cov lus nug raws li kev tawm tswv yim thiab tso tawm. Tus neeg siv cov lus nug ua haujlwm raws li cov saw hlau tus qauv.- Raws li ib nqe lus txuas nrog cov lus nug, lub
assumptions_chain
generates ib tug mos txwv daim ntawv teev cov assumpions siv cov zis los ntawm lubquestion_chain
ua input. CovLLMChain
thiabOpenAI
qauv los ntawm LangChain tau siv los tsim cov nqe lus. Tus neeg siv tau ua haujlwm los tsim cov npe ntawm cov kev xav uas tau tsim los tsim cov lus uas siv tus qauv rau cov saw no. - Raws li cov outputs los ntawm lub
question_chain
thiabassumptions_chain
, lubfact_checker_chain
tsim ib daim ntawv teev cov lus pov thawj nyob rau hauv daim ntawv ntawm cov mos txwv cov ntsiab lus. Cov ntawv thov yog tsim los siv covOpenAI
qauv thiabLLMChain
los ntawm LangChain. Tus neeg siv yog lub luag haujlwm los txiav txim siab seb txhua qhov kev thov puas yog lossis tsis raug thiab muab kev ncaj ncees rau cov uas yog. - cov
answer_chain
siv cov txiaj ntsig los ntawm kev sivquestion_chain
,assumptions_chain
, Thiabfact_checker_chain
raws li cov khoom siv los tsim cov lus teb rau cov neeg siv cov lus nug uas siv cov ntaub ntawv tsim los ntawm cov saw hlau ua ntej. Cov qauv rau cov saw no thov kom tus neeg siv teb rau thawj cov lus nug uas siv qhov tseeb uas tau tsim. - Txhawm rau muab cov lus teb kawg rau tus neeg siv qhov kev nug raws li cov ntaub ntawv tsim los ntawm cov saw hlau ua ntej, peb muab cov saw hlau no tso rau hauv cov saw hlau tag nrho. Tom qab cov chains tiav, peb siv
st.success()
los qhia cov neeg siv cov kev daws teeb meem.
xaus
Peb tuaj yeem yooj yim sib txuas ua ke cov qauv lus sib txawv los tsim cov kav dej sib txawv los ntawm kev siv cov SimpleSequentialChain
module ntawm LangChain. Rau ntau yam NLP daim ntawv thov, suav nrog chatbots, cov lus nug-thiab-lus teb, thiab cov cuab yeej txhais lus, qhov no yuav pab tau zoo.
Lub ci ntsa iab ntawm LangChain tau pom nyob rau hauv nws lub peev xwm ua kom pom tseeb, uas ua rau tus neeg siv tuaj yeem mloog zoo rau qhov teeb meem tam sim no es tsis yog qhov tshwj xeeb ntawm kev ua qauv lus.
LangChain ua rau cov txheej txheem ntawm kev tsim cov qauv lus uas muaj txiaj ntsig zoo rau cov neeg siv los ntawm kev muab cov qauv ua ntej kev cob qhia thiab xaiv cov qauv.
Nws muab koj cov kev xaiv los kho cov qauv lus zoo siv lawv tus kheej cov ntaub ntawv, ua kom yooj yim los kho cov qauv lus. Qhov no ua rau txoj kev loj hlob ntawm cov neeg muaj meej dua, cov qauv tshwj xeeb uas, rau ib txoj hauj lwm, ua tau zoo dua cov qauv kev cob qhia.
cov SimpleSequentialChain
module thiab lwm yam nta ntawm LangChain ua rau nws yog ib qho cuab yeej zoo rau kev tsim kho sai sai thiab siv cov txheej txheem NLP sophisticated.
Sau ntawv cia Ncua