一、LangChain介紹
LangChain是一個強大的用于開發大模型應用程序的框架,為開發提供豐富的工具和組件,使得構造復雜的自然語言處理變得更加高效和便捷。它允許開發者將大語言模型與其他數據源工具集成,從而創建出能處理各種任務的智能體應用,如問答系統,聊天機器人,文本生成。
二、LangChain核心模塊介紹
模型(Models)
在LangChain中,模型接口是與大語言模型進行交互的橋梁。它支持多種類型的模型,包含大語言模型(LLMs)和聊天模型(Chat Models)。
提示詞模板(PromptTemplates)
提示詞用于定義輸入給語言模型的提示信息,幫助模型生成更符合預期的輸出。它可以包含變量,通過填充變量來生成具體的提示。
鏈(Chains)
鏈是LangChain中一個重要的概念,它將多個組件組合在一起,按照一定的順序執行,以實現更復雜的任務。常見的鏈類型包含順序鏈(SequnentialChain)、并行鏈等。
工具(Tools)
工具是LangChain中用于執行特定任務的組件,如搜索引擎工具、數據庫查詢工具。開發者可以將工具集成到鏈中,以擴展應用的功能。
記憶(Menory)
在對話式應用中,記憶組件用于保存和管理對話中的歷史信息,使模型能工具上下文進行更連貫的回復。LangChain提供了多種類型的記憶,如短期記憶,長期記憶等。
三、LangChain安裝
安裝環境要求
- python版本:建議使用python 3.7及以上版本
- 包管理工具:推薦pip
安裝步驟
安裝LangChain和相關依賴
pip install langchain==0.3.7
pip install langchain-core==0.3.43
pip install langchain-openai==0.3.1
四、LangChain實戰
1、基本調用:簡單問答機器人
演示任何使用LangChain調用大語言模型實現簡單的問答功能
知識點:大模型配置和調用,以及SystemMessgae和HumanMessage。
在LangChain中,SystemMessage用于設置系統級別的提示詞,HumanMessage用于表示用戶的輸入
from langchain.chains.conversation.base import ConversationChain
from langchain.chains.llm import LLMChain
from langchain.chains.sequential import SequentialChain
from langchain.memory import ConversationBufferMemory
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI# 創建一個大模型對象
llm = ChatOpenAI(openai_api_base="https://ark.cn-beijing.volces.com/api/v3",api_key="",temperature=0.7,max_tokens=10240,model_name="",
)# 簡單的調用
def basic_demo():result = llm.invoke([SystemMessage(content="你是一個翻譯小助手,請將輸入的英文翻譯成中文。"),HumanMessage(content="I love programming.")])print(result)
2、內存記憶:多輪聊天機器人
展示如何使用LangChain搭建一個聊天機器人,支持多輪聊天
知識點:ConversationBuffMemory、PromptTemplate、Memory
def multi_chat_demo():# 創建一個會話內存memory = ConversationBufferMemory()# 創建一個會話對象conversation = ConversationChain(llm=llm, memory=memory)while True:user_input = input("請輸入:")if user_input == "退出":breakresult = conversation.predict(input=user_input)print(f"大模型輸出:{result}")
3、單節點鏈調用:日報生成器
演示如何搭建一個簡單的鏈(工作流)
# 單節點鏈路調用(工作流)
def basic_chat_demo():# 定義提示詞模板prompt_template = PromptTemplate(input_variables=["job", "type"],template="你是一個職場文檔專家,可以幫我生成一個{job}崗位的{type}文檔,字數不要超過300")# 定義一個鏈(工作流),verbose= True將他調試信息全部打印出來chain = LLMChain(llm=llm, prompt=prompt_template, verbose=True, output_key="content")# 執行工作流result = chain.invoke({"job": "前端", "type": "工作日報"})print(result)
4、順序鏈調用:美食推薦
實現美食推薦鏈路,根據用戶輸入的城市,推薦該城市的特殊菜譜。
知識點:順序鏈、多節點參數傳遞
# 多節點鏈路調用
def sequential_chain_demo():# 第一第一個節點提示詞prompt_templeate_1 = PromptTemplate(input_variables=["city"],template="你是一個美食家,可以告訴用戶{city}地區特色菜是什么,只需要說一個菜名就可以,不需要對菜進行解釋")# 定義第一個節點city_chain = LLMChain(llm=llm, prompt=prompt_templeate_1, verbose=True, output_key="food")prompt_templeate_2 = PromptTemplate(input_variables=["food"],template="你是一個廚師,可以輸出一個{food}的菜譜,只需要輸出一個{food}的菜譜,不需要對菜進行解釋")food_chain = LLMChain(llm=llm, prompt=prompt_templeate_2, verbose=True, output_key="info")# 創建一個順序鏈,把兩個節點按照順序組裝all_chain = SequentialChain(chains=[city_chain, food_chain],input_variables=["city"],output_variables=["city","food","info"],verbose=True)result = all_chain.invoke({"city": "上海"})print(result)