網(wǎng)站后臺代碼常用的網(wǎng)絡(luò)營銷推廣方法有哪些
大型語言模型(LLM)已經(jīng)徹底改變了自然語言處理(NLP)任務(wù)。它們改變了我們與文本數(shù)據(jù)交互和處理的方式。這些強大的AI模型,如OpenAI的GPT-4,改變了理解、生成人類類似文本的方式,導(dǎo)致各種行業(yè)出現(xiàn)了眾多突破性應(yīng)用。
LangChain是一個用于構(gòu)建基于大型語言模型(如GPT)的應(yīng)用程序的開源框架。它使應(yīng)用程序能夠?qū)⒄Z言模型連接到其他數(shù)據(jù)源,并允許語言模型與其環(huán)境進行交互。
????在這篇博客中,我們將討論LangChain在基于LLM的應(yīng)用開發(fā)中的應(yīng)用。通過提示LLM,現(xiàn)在比以前任何時候都可以更快地開發(fā)AI應(yīng)用程序?;贚LM的應(yīng)用需要多次提示和輸出解析,因此我們需要為此編寫大量代碼。LangChain通過利用NLP應(yīng)用開發(fā)中發(fā)現(xiàn)的基本抽象,使得這一開發(fā)過程變得更加容易。本博客的內(nèi)容主要基于短課程LangChain用于LLM應(yīng)用開發(fā)。
LangChain框架概述LangChain是一個用于開發(fā)應(yīng)用程序的開源框架。它將大型語言模型(如GPT-4)與外部數(shù)據(jù)相結(jié)合。LangChain有Python或JavaScript(TypeScript)包可用。LangChain注重組合和模塊化。它具有模塊化組件,其中單個組件可以相互結(jié)合使用,也可以單獨使用。LangChain可以應(yīng)用于多個用例,并可以組合其模塊化組件以實現(xiàn)更完整的端到端應(yīng)用程序。
LangChain的關(guān)鍵組件LangChain強調(diào)靈活性和模塊化。它將自然語言處理流程劃分為獨立的模塊化組件,使開發(fā)人員能夠根據(jù)需要定制工作流程。LangChain框架可以分為六個模塊,每個模塊允許與LLM進行不同方面的交互。
模型:
-
LLMs — 20+集成
-
Chat Models
-
Text Embedding Models — 10+集成?提示:
-
提示模板
-
輸出解析器 — 5+集成
-
示例選擇器 — 10+集成?索引:
-
文檔加載器: 50+集成
-
文本拆分器: 10+集成
-
向量空間: 10+集成
-
檢索器: 5+集成/實現(xiàn)?鏈:
-
Prompt + LLM + Output parsing
-
可用作更長鏈的構(gòu)建塊
-
更多特定于應(yīng)用的鏈:20+類型
-
檢索器: 5+集成/實現(xiàn)?代理:
-
代理是一種端到端用例類型,將模型用作推理引擎
-
代理類型: 5+類型
-
代理工具包: 10+實現(xiàn)
模型模型是任何語言模型應(yīng)用的核心元素。模型指的是支持LLM的語言模型。LangChain提供了與任何語言模型接口和集成的構(gòu)建塊。LangChain為兩種類型的模型提供接口和集成:
LLMs — 以文本字符串作為輸入并返回文本字符串的模型Chat Models — 由語言模型支持但以聊天消息列表作為輸入并返回聊天消息的模型。
# This is langchain's abstraction for chatGPT API Endpoint
from langchain.chat_models import ChatOpenAI??????
# To control the randomness and creativity of the generated text by an LLM,
# use temperature = 0.0
chat = ChatOpenAI(temperature=0.0)
Prompts是編程模型的新方式。提示是指創(chuàng)建輸入以傳遞到模型的風(fēng)格。提示通常由多個組件構(gòu)成。提示模板和示例選擇器提供了主要類和函數(shù),以便輕松構(gòu)建和使用提示。
我們將定義一個模板字符串,并使用該模板字符串和ChatPromptTemplate從LangChain創(chuàng)建提示模板。
提示模板
# Define a template string
template_string = """Translate the text that is delimited by triple backticks \
into a style that is {style}. text: ```{text}```
"""
# Create a prompt template using above template string
from langchain.prompts import ChatPromptTemplate
prompt_template = ChatPromptTemplate.from_template(template_string
上述的prompt_template有兩個字段,即style和text。我們也可以從此提示模板中提取原始模板字符串?,F(xiàn)在,如果我們想要將文本翻譯為某種其他樣式,我們需要定義我們的翻譯樣式和文本。
customer_style = """American English in a calm and respectful tone
"""customer_email = """
Arrr, I be fuming that me blender lid flew off and splattered me kitchen walls \
with smoothie! And to make matters worse, the warranty don't cover the cost of \
cleaning up me kitchen. I need yer help right now, matey!
"""
在這里,我們將風(fēng)格設(shè)置為美國英語,語氣平靜且尊重。我們使用帶有將被三個反引號括起來的文本翻譯為特定風(fēng)格的f-string指令來指定提示,然后將上述樣式(客戶風(fēng)格)和文本(客戶電子郵件)傳遞給LLM進行文本翻譯。
# customer_message will generate the prompt and it will be passed into
# the llm to get a response.
customer_messages = prompt_template.format_messages(style=customer_style,text=customer_email)# Call the LLM to translate to the style of the customer message.
customer_response = chat(customer_messages)
當(dāng)我們構(gòu)建復(fù)雜的應(yīng)用程序時,提示可以變得相當(dāng)長和詳細(xì)。我們不使用f字符串,而是使用提示模板,因為提示模板是有用的抽象,可以幫助我們重用好的提示。我們可以創(chuàng)建提示模板并重用這些提示模板,并為模型指定輸出樣式和文本以供工作。
LangChain提供了一些常見操作的提示,例如摘要或回答問題,或者連接到SQL數(shù)據(jù)庫或連接到不同的API。因此,通過使用LangChain的一些內(nèi)置提示,我們可以快速獲得一個正在運行的應(yīng)用程序,而無需自行設(shè)計提示。
輸出解析器LangChain的提示庫的另一個方面是它還支持輸出解析。輸出解析器有助于從語言模型的輸出中獲取結(jié)構(gòu)化信息。輸出解析器涉及將模型的輸出解析為更結(jié)構(gòu)化的格式,以便我們可以使用輸出執(zhí)行下游任務(wù)。
當(dāng)我們使用LLMs構(gòu)建復(fù)雜應(yīng)用程序時,我們經(jīng)常指示LLM以特定格式生成其輸出,例如使用特定的關(guān)鍵字。LangChain的庫函數(shù)假設(shè)LLM將使用某些關(guān)鍵字來解析其輸出。
我們可以有一個LLM輸出JSON,我們將使用LangChain解析該輸出,如下所示:
我們需要首先定義我們希望如何格式化LLM輸出。在這種情況下,我們定義了一個具有提及產(chǎn)品是否為禮物、交付所需的天數(shù)以及價格是否可負(fù)擔(dān)的字段的Python字典。
# Following is one example of the desired output.
{"gift": False,"delivery_days": 5,"price_value": "pretty affordable!"
}
我們可以在下面提到的三個反引號中包含客戶評論。我們可以定義以下評論模板:??????????????
# This is an example of customer review and a template that try to get the desired output
customer_review = """\
Need to be actual review
"""review_template = """\
For the following text, extract the following information:gift: Was the item purchased as a gift for someone else? \
Answer True if yes, False if not or unknown.delivery_days: How many days did it take for the product \
to arrive? If this information is not found, output -1.price_value: Extract any sentences about the value or price,\
and output them as a comma separated Python list.Format the output as JSON with the following keys:
gift
delivery_days
price_valuetext: {text}
"""# This is an example of customer review and a template that try to get the desired output
customer_review = """\
Need to be actual review
"""review_template = """\
For the following text, extract the following information:gift: Was the item purchased as a gift for someone else? \
Answer True if yes, False if not or unknown.delivery_days: How many days did it take for the product \
to arrive? If this information is not found, output -1.price_value: Extract any sentences about the value or price,\
and output them as a comma separated Python list.Format the output as JSON with the following keys:
gift
delivery_days
price_valuetext: {text}
"""# We will wrap all review template, customer review in langchain to get output
# in desired format. We will have prompt template created from review template.from langchain.prompts import ChatPromptTemplateprompt_template = ChatPromptTemplate.from_template(review_template)
print(prompt_template)# Create messages using prompt templates created earlier and customer review.
# Finally, we pass messgaes to OpenAI endpoint to get response.messages = prompt_template.format_messages(text=customer_review)
chat = ChatOpenAI(temperature=0.0)
response = chat(messages)
print(response.content)
上述響應(yīng)仍然不是字典,而是字符串。我們需要使用Python字典將LLM輸出字符串解析為字典。我們需要為Python字典中的每個字段項定義ResponseSchema。為了簡潔起見,我沒有提供這些代碼片段。它們可以在我的GitHub筆記本中找到。這是一種非常有效的方法,可以將LLM輸出解析為Python字典,使其更易于在下游處理中使用。
ReAct框架
在上述示例中,LLM使用諸如“思想”、“行動”和“觀察”等關(guān)鍵詞,使用名為ReAct的框架執(zhí)行思維推理鏈。“思想”是LLM所想的,通過給LLM思考的空間,LLM可以得到更準(zhǔn)確的結(jié)論?!靶袆印笔且粋€關(guān)鍵詞來執(zhí)行特定的行動,而“觀察”是一個關(guān)鍵詞來展示LLM從特定行動中所學(xué)到的內(nèi)容。如果我們有一個指示LLM使用這些特定關(guān)鍵詞(如思想、行動和觀察)的提示,那么這些關(guān)鍵詞可以與解析器結(jié)合使用,以提取標(biāo)記有這些關(guān)鍵詞的文本。
記憶大型語言模型無法記住之前的對話。
當(dāng)你與這些模型互動時,它們自然不會記得你之前說的話或之前的所有對話,這在你構(gòu)建一些應(yīng)用程序(如聊天機器人)并希望與它們進行對話時是一個問題。
通過模型、提示和解析器,我們可以重用我們自己的提示模板,與他人共享提示模板,或使用LangChain內(nèi)置的提示模板,這些模板可以與輸出解析器結(jié)合使用,以便我們獲得特定格式的輸出,并讓解析器解析該輸出并將其存儲在特定字典或其他數(shù)據(jù)結(jié)構(gòu)中,從而使下游處理更容易。
我將在下一篇博客中討論鏈和代理。我還將在我的另一篇博客中討論如何在我們的數(shù)據(jù)中進行問題回答。最后,我們可以看到,通過提示LLM或大型語言模型,現(xiàn)在比以前任何時候都更有可能開發(fā)出更快的AI應(yīng)用程序。但是一個應(yīng)用程序可能需要多次提示LLM并解析其輸出,因此需要編寫大量的粘合代碼。Langchain有助于簡化這個過程。