国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
LangChain 完整指南:使用大語(yǔ)言模型構(gòu)建強(qiáng)大的應(yīng)用程序

山行 山行AI

LangChain 是一個(gè)強(qiáng)大的框架,可以簡(jiǎn)化構(gòu)建高級(jí)語(yǔ)言模型應(yīng)用程序的過(guò)程。隨著大模型的熱度不斷攀升,LangChain也獲得了越來(lái)越多的開(kāi)發(fā)人員親睞。

嗨,你好!讓我向你介紹LangChain,這是一個(gè)非常棒的庫(kù),它能讓開(kāi)發(fā)者利用大型語(yǔ)言模型(LLMs)和其他計(jì)算資源來(lái)構(gòu)建強(qiáng)大的應(yīng)用。在這份指南中,我將快速概述LangChain的工作原理,并探討一些很酷的使用案例,例如問(wèn)答系統(tǒng)、聊天機(jī)器人和智能代理。我還會(huì)帶你走過(guò)一個(gè)快速啟動(dòng)指南,幫助你開(kāi)始使用。讓我們開(kāi)始吧!

What is LangChain?

LangChain是一個(gè)強(qiáng)大的框架,旨在幫助開(kāi)發(fā)人員使用語(yǔ)言模型構(gòu)建端到端的應(yīng)用程序。它提供了一套工具、組件和接口,可簡(jiǎn)化創(chuàng)建由大型語(yǔ)言模型 (LLM) 和聊天模型提供支持的應(yīng)用程序的過(guò)程。LangChain 可以輕松管理與語(yǔ)言模型的交互,將多個(gè)組件鏈接在一起,并集成額外的資源,例如 API 和數(shù)據(jù)庫(kù)。

LangChain有很多核心概念:

1. Components and Chains

在 LangChain 中,Component 是模塊化的構(gòu)建塊,可以組合起來(lái)創(chuàng)建強(qiáng)大的應(yīng)用程序。Chain 是組合在一起以完成特定任務(wù)的一系列 Components(或其他 Chain)。例如,一個(gè) Chain 可能包括一個(gè) Prompt 模板、一個(gè)語(yǔ)言模型和一個(gè)輸出解析器,它們一起工作以處理用戶輸入、生成響應(yīng)并處理輸出。

2. Prompt Templates and Values

Prompt Template 負(fù)責(zé)創(chuàng)建 PromptValue,這是最終傳遞給語(yǔ)言模型的內(nèi)容。Prompt Template 有助于將用戶輸入和其他動(dòng)態(tài)信息轉(zhuǎn)換為適合語(yǔ)言模型的格式。PromptValues 是具有方法的類,這些方法可以轉(zhuǎn)換為每個(gè)模型類型期望的確切輸入類型(如文本或聊天消息)。

3. Example Selectors

當(dāng)您想要在 Prompts 中動(dòng)態(tài)包含示例時(shí),Example Selectors 很有用。他們接受用戶輸入并返回一個(gè)示例列表以在提示中使用,使其更強(qiáng)大和特定于上下文。

4. Output Parsers

Output Parsers 負(fù)責(zé)將語(yǔ)言模型響應(yīng)構(gòu)建為更有用的格式。它們實(shí)現(xiàn)了兩種主要方法:一種用于提供格式化指令,另一種用于將語(yǔ)言模型的響應(yīng)解析為結(jié)構(gòu)化格式。這使得在您的應(yīng)用程序中處理輸出數(shù)據(jù)變得更加容易。

5. Indexes and Retrievers

Index 是一種組織文檔的方式,使語(yǔ)言模型更容易與它們交互。檢索器是用于獲取相關(guān)文檔并將它們與語(yǔ)言模型組合的接口。LangChain 提供了用于處理不同類型的索引和檢索器的工具和功能,例如矢量數(shù)據(jù)庫(kù)和文本拆分器。

6. Chat Message History

LangChain 主要通過(guò)聊天界面與語(yǔ)言模型進(jìn)行交互。ChatMessageHistory 類負(fù)責(zé)記住所有以前的聊天交互數(shù)據(jù),然后可以將這些交互數(shù)據(jù)傳遞回模型、匯總或以其他方式組合。這有助于維護(hù)上下文并提高模型對(duì)對(duì)話的理解。

7. Agents and Toolkits

Agent 是在 LangChain 中推動(dòng)決策制定的實(shí)體。他們可以訪問(wèn)一套工具,并可以根據(jù)用戶輸入決定調(diào)用哪個(gè)工具。Tookits 是一組工具,當(dāng)它們一起使用時(shí),可以完成特定的任務(wù)。代理執(zhí)行器負(fù)責(zé)使用適當(dāng)?shù)墓ぞ哌\(yùn)行代理。

通過(guò)理解和利用這些核心概念,您可以利用 LangChain 的強(qiáng)大功能來(lái)構(gòu)建適應(yīng)性強(qiáng)、高效且能夠處理復(fù)雜用例的高級(jí)語(yǔ)言模型應(yīng)用程序。

What is a LangChain Agent?

LangChain Agent 是框架中驅(qū)動(dòng)決策制定的實(shí)體。它可以訪問(wèn)一組工具,并可以根據(jù)用戶的輸入決定調(diào)用哪個(gè)工具。代理幫助構(gòu)建復(fù)雜的應(yīng)用程序,這些應(yīng)用程序需要自適應(yīng)和特定于上下文的響應(yīng)。當(dāng)存在取決于用戶輸入和其他因素的未知交互鏈時(shí),它們特別有用。

如何使用 LangChain?

要使用 LangChain,開(kāi)發(fā)人員首先要導(dǎo)入必要的組件和工具,例如 LLMs, chat models, agents, chains, 內(nèi)存功能。這些組件組合起來(lái)創(chuàng)建一個(gè)可以理解、處理和響應(yīng)用戶輸入的應(yīng)用程序。

LangChain 為特定用例提供了多種組件,例如個(gè)人助理、文檔問(wèn)答、聊天機(jī)器人、查詢表格數(shù)據(jù)、與 API 交互、提取、評(píng)估和匯總。

What’s a LangChain model?

LangChain model 是一種抽象,表示框架中使用的不同類型的模型。LangChain 中的模型主要分為三類:

1.LLM(大型語(yǔ)言模型):這些模型將文本字符串作為輸入并返回文本字符串作為輸出。它們是許多語(yǔ)言模型應(yīng)用程序的支柱。2.聊天模型( Chat Model):聊天模型由語(yǔ)言模型支持,但具有更結(jié)構(gòu)化的 API。他們將聊天消息列表作為輸入并返回聊天消息。這使得管理對(duì)話歷史記錄和維護(hù)上下文變得容易。3.文本嵌入模型(Text Embedding Models):這些模型將文本作為輸入并返回表示文本嵌入的浮點(diǎn)列表。這些嵌入可用于文檔檢索、聚類和相似性比較等任務(wù)。

開(kāi)發(fā)人員可以為他們的用例選擇合適的 LangChain 模型,并利用提供的組件來(lái)構(gòu)建他們的應(yīng)用程序。

LangChain 的主要特點(diǎn)

LangChain 旨在為六個(gè)主要領(lǐng)域的開(kāi)發(fā)人員提供支持:

1.LLM 和提示:LangChain 使管理提示、優(yōu)化它們以及為所有 LLM 創(chuàng)建通用界面變得容易。此外,它還包括一些用于處理 LLM 的便捷實(shí)用程序。2.鏈(Chain):這些是對(duì) LLM 或其他實(shí)用程序的調(diào)用序列。LangChain 為鏈提供標(biāo)準(zhǔn)接口,與各種工具集成,為流行應(yīng)用提供端到端的鏈。3.數(shù)據(jù)增強(qiáng)生成:LangChain 使鏈能夠與外部數(shù)據(jù)源交互以收集生成步驟的數(shù)據(jù)。例如,它可以幫助總結(jié)長(zhǎng)文本或使用特定數(shù)據(jù)源回答問(wèn)題。4.Agents:Agents 讓 LLM 做出有關(guān)行動(dòng)的決定,采取這些行動(dòng),檢查結(jié)果,并繼續(xù)前進(jìn)直到工作完成。LangChain 提供了代理的標(biāo)準(zhǔn)接口,多種代理可供選擇,以及端到端的代理示例。5.內(nèi)存:LangChain 有一個(gè)標(biāo)準(zhǔn)的內(nèi)存接口,有助于維護(hù)鏈或代理調(diào)用之間的狀態(tài)。它還提供了一系列內(nèi)存實(shí)現(xiàn)和使用內(nèi)存的鏈或代理的示例。6.評(píng)估:很難用傳統(tǒng)指標(biāo)評(píng)估生成模型。這就是為什么 LangChain 提供提示和鏈來(lái)幫助開(kāi)發(fā)者自己使用 LLM 評(píng)估他們的模型。

使用示例

LangChain 支持大量用例,例如:

·針對(duì)特定文檔的問(wèn)答:根據(jù)給定的文檔回答問(wèn)題,使用這些文檔中的信息來(lái)創(chuàng)建答案?!?strong>聊天機(jī)器人:構(gòu)建可以利用 LLM 的功能生成文本的聊天機(jī)器人?!?strong>Agents:開(kāi)發(fā)可以決定行動(dòng)、采取這些行動(dòng)、觀察結(jié)果并繼續(xù)執(zhí)行直到完成的代理。

快速入門(mén)指南:使用 LangChain 構(gòu)建端到端語(yǔ)言模型應(yīng)用程序

·安裝
首先,安裝 LangChain。只需運(yùn)行以下命令:

pip install langchain

·環(huán)境設(shè)置
現(xiàn)在,由于 LangChain 經(jīng)常需要與模型提供者、數(shù)據(jù)存儲(chǔ)、API 等集成,我們將設(shè)置我們的環(huán)境。在這個(gè)例子中,我們將使用 OpenAI 的 API,因此我們需要安裝他們的 SDK:

pip install openai

接下來(lái),讓我們?cè)诮K端中設(shè)置環(huán)境變量:

export OPENAI_API_KEY = '...'

或者,如果您更喜歡在 Jupyter notebook 或 Python 腳本中工作,您可以像這樣設(shè)置環(huán)境變量:

import os os .environ[ 'OPENAI_API_KEY' ] = '...'

·構(gòu)建語(yǔ)言模型應(yīng)用程序:LLM
安裝好 LangChain 并設(shè)置好環(huán)境后,我們就可以開(kāi)始構(gòu)建我們的語(yǔ)言模型應(yīng)用程序了。LangChain 提供了一堆模塊,您可以使用它們來(lái)創(chuàng)建語(yǔ)言模型應(yīng)用程序。您可以將這些模塊組合起來(lái)用于更復(fù)雜的應(yīng)用程序,或者將它們單獨(dú)用于更簡(jiǎn)單的應(yīng)用程序。·構(gòu)建語(yǔ)言模型應(yīng)用程序:Chat Model
除了 LLM,您還可以使用聊天模型。這些是語(yǔ)言模型的變體,它們?cè)诘讓邮褂谜Z(yǔ)言模型但具有不同的界面。聊天模型使用聊天消息作為輸入和輸出,而不是“文本輸入、文本輸出”API。聊天模型 API 的使用還比較新,所以大家都還在尋找最佳抽象使用方式。

要完成聊天,您需要將一條或多條消息傳遞給聊天模型。LangChain 目前支持 AIMessage、HumanMessage、SystemMessage 和 ChatMessage 類型。您將主要使用 HumanMessage、AIMessage 和 SystemMessage。
下面是使用聊天模型的示例:

from langchain.chat_models import ChatOpenAIfrom langchain.schema import ( AIMessage, HumanMessage, SystemMessage)

chat = ChatOpenAI(temperature=0)

您可以通過(guò)傳遞一條消息來(lái)完成:

chat([HumanMessage(content='Translate this sentence from English to French. I love programming.')])# -> AIMessage(content='J'aime programmer.', additional_kwargs={})

或者傳遞多條消息給 OpenAI 的 gpt-3.5-turbo 和 gpt-4 models:

messages = [ SystemMessage(content='You are a helpful assistant that translates English to Chinese.'), HumanMessage(content='Translate this sentence from English to Chinese. I love programming.')]chat(messages)# -> AIMessage(content='我喜歡編程。(Wǒ xǐhuān biānchéng.)', additional_kwargs={})

您還可以使用 generate 為多組消息生成完成。這將返回一個(gè)帶有附加消息參數(shù)的 LLMResult:

batch_messages = [    [        SystemMessage(content='You are a helpful assistant that translates English to Chinese.'),        HumanMessage(content='Translate this sentence from English to Chinese. I love programming.')    ],    [        SystemMessage(content='You are a helpful assistant that translates English to Chinese.'),        HumanMessage(content='Translate this sentence from English to Chinese. I love artificial intelligence.')    ],]result = chat.generate(batch_messages)result# -> LLMResult(generations=[[ChatGeneration(text='我喜歡編程。(Wǒ xǐhuān biānchéng.)', generation_info=None, message=AIMessage(content='我喜歡編程。(Wǒ xǐhuān biānchéng.)', additional_kwargs={}))], [ChatGeneration(text='我喜愛(ài)人工智能。(Wǒ xǐ'ài rén gōng zhì néng.)', generation_info=None, message=AIMessage(content='我喜愛(ài)人工智能。(Wǒ xǐ'ài rén gōng zhì néng.)', additional_kwargs={}))]], llm_output={'token_usage': {'prompt_tokens': 71, 'completion_tokens': 18, 'total_tokens': 89}})

您還可以從 LLMResult 中提取 tokens 使用等信息:

result.llm_output['token_usage']# -> {'prompt_tokens': 71, 'completion_tokens': 18, 'total_tokens': 89}

對(duì)于聊天模型,您還可以通過(guò)使用 MessagePromptTemplate 來(lái)使用模板。您可以從一個(gè)或多個(gè) MessagePromptTemplates 創(chuàng)建 ChatPromptTemplate。ChatPromptTemplate 的方法format_prompt返回一個(gè) PromptValue,您可以將其轉(zhuǎn)換為字符串或 Message 對(duì)象,具體取決于您是否要使用格式化值作為 LLM 或聊天模型的輸入。

以下是一個(gè)例子:

from langchain.chat_models import ChatOpenAIfrom langchain.prompts.chat import (    ChatPromptTemplate,    SystemMessagePromptTemplate,    HumanMessagePromptTemplate,)
chat = ChatOpenAI(temperature=0)template='You are a helpful assistant that translates {input_language} to {output_language}.'system_message_prompt = SystemMessagePromptTemplate.from_template(template)human_template='{text}'human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])# get a chat completion from the formatted messageschat(chat_prompt.format_prompt(input_language='English', output_language='Chinese', text='I love programming.').to_messages())# -> AIMessage(content='我喜歡編程。(Wǒ xǐhuān biānchéng.)', additional_kwargs={})

您也可以將 LLMChain 與 Chat Model 一起使用:

from langchain.chat_models import ChatOpenAIfrom langchain import LLMChainfrom langchain.prompts.chat import (    ChatPromptTemplate,    SystemMessagePromptTemplate,    HumanMessagePromptTemplate,)
chat = ChatOpenAI(temperature=0)template='You are a helpful assistant that translates {input_language} to {output_language}.'system_message_prompt = SystemMessagePromptTemplate.from_template(template)human_template='{text}'human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])chain = LLMChain(llm=chat, prompt=chat_prompt)chain.run(input_language='English', output_language='Chinese', text='I love programming.')# -> '我喜歡編程。(Wǒ xǐhuān biānchéng.)'

您還可以將代理與聊天模型一起使用。使用 AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION 作為代理類型初始化 Agent

from langchain.agents import load_toolsfrom langchain.agents import initialize_agentfrom langchain.agents import AgentTypefrom langchain.chat_models import ChatOpenAIfrom langchain.llms import OpenAI
# First, let's load the language model we're going to use to control the agent.chat = ChatOpenAI(temperature=0)# Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.llm = OpenAI(temperature=0)tools = load_tools(['serpapi', 'llm-math'], llm=llm)# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.agent = initialize_agent(tools, chat, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)# Now let's test it out!agent.run('Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?')

在此示例中,代理將以交互的方式執(zhí)行搜索和計(jì)算以提供最終答案。

最后,讓我們探索將內(nèi)存與使用聊天模型初始化的鏈和代理一起使用。這與 Memory for LLMs 的主要區(qū)別在于我們可以將以前的消息保留為它們自己唯一的內(nèi)存對(duì)象,而不是將它們壓縮成一個(gè)字符串。

下面是使用 a 的示例ConversationChain

from langchain.prompts import (    ChatPromptTemplate,     MessagesPlaceholder,     SystemMessagePromptTemplate,     HumanMessagePromptTemplate)from langchain.chains import ConversationChainfrom langchain.chat_models import ChatOpenAIfrom langchain.memory import ConversationBufferMemory
prompt = ChatPromptTemplate.from_messages([ SystemMessagePromptTemplate.from_template('The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.'), MessagesPlaceholder(variable_name='history'), HumanMessagePromptTemplate.from_template('{input}')])llm = ChatOpenAI(temperature=0)memory = ConversationBufferMemory(return_messages=True)conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm)conversation.predict(input='Hi there!')# -> 'Hello! How can I assist you today?'conversation.predict(input='I'm doing well! Just having a conversation with an AI.')# -> 'That sounds like fun! I'm happy to chat with you. Is there anything specific you'd like to talk about?'conversation.predict(input='Tell me about yourself.')# -> 'Sure! I am an AI language model created by OpenAI. I was trained on a large dataset of text from the internet, which allows me to understand and generate human-like language. I can answer questions, provide information, and even have conversations like this one. Is there anything else you'd like to know about me?'

在此示例中,我們使用 aConversationChain來(lái)維護(hù)跨與 AI 的多次交互的對(duì)話上下文。

就是這樣!現(xiàn)在您已經(jīng)對(duì)如何使用 LangChain 構(gòu)建端到端的語(yǔ)言模型應(yīng)用有了深入的了解。通過(guò)遵循這些示例,您可以使用 LLM、聊天模型、代理、鏈和內(nèi)存功能開(kāi)發(fā)強(qiáng)大的語(yǔ)言模型應(yīng)用程序。

結(jié)論

總之,LangChain 是一個(gè)強(qiáng)大的框架,它通過(guò)提供模塊化和靈活的方法簡(jiǎn)化了構(gòu)建高級(jí)語(yǔ)言模型應(yīng)用程序的過(guò)程。通過(guò)了解組件、鏈、提示模板、輸出解析器、索引、檢索器、聊天消息歷史記錄和代理等核心概念,您可以創(chuàng)建適合您特定需求的自定義解決方案。LangChain 的適應(yīng)性和易用性使其成為開(kāi)發(fā)人員的寶貴工具,使他們能夠釋放語(yǔ)言模型的全部潛力,并在廣泛的用例中創(chuàng)建智能的、上下文感知的應(yīng)用程序。

原文鏈接[1]

附錄

目前github上有幾個(gè)比較火的與LangChain相關(guān)的項(xiàng)目,大家可以選用:

·langchain本尊:https://github.com/hwchase17/langchain·基于LangChain構(gòu)建本地知識(shí)庫(kù):GitHub - imClumsyPanda/langchain-ChatGLM: langchain-ChatGLM, local knowledge based ChatGLM with langchain | 基于本地知識(shí)庫(kù)的 ChatGLM 問(wèn)答[2]·意如其名:https://github.com/imClumsyPanda/langchain-ChatGLM-webui

References

[1] 原文鏈接: https://link.zhihu.com/?target=https%3A//levelup.gitconnected.com/a-complete-guide-to-langchain-building-powerful-applications-with-large-language-models-53ad54a1e7eb
[2] GitHub - imClumsyPanda/langchain-ChatGLM: langchain-ChatGLM, local knowledge based ChatGLM with langchain | 基于本地知識(shí)庫(kù)的 ChatGLM 問(wèn)答:
https://github.com/imClumsyPanda/langchain-ChatGLM

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
面向小白的本地部署大模型完整教程:LangChain Streamlit LLama
解讀LangChain
從?模型到 AI 應(yīng)?落地,工程實(shí)現(xiàn)有多復(fù)雜?
探索未知:LLM Agent 應(yīng)用開(kāi)發(fā)的全新時(shí)代
吳恩達(dá)限免AI課再更新:如何基于LangChain使用LLM構(gòu)建私有數(shù)據(jù)的問(wèn)答系統(tǒng)和聊天機(jī)器人
花了一個(gè)小時(shí),刷完了吳恩達(dá)的新課
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服