LangChain簡(jiǎn)化ChatGPT工程復(fù)雜度使用詳解
什么是LangChain?
使用ChatGPT大家可能都是知道prompt,
(1)想像一下,如果我需要快速讀一本書,想通過(guò)本書作為prompt,使用ChatGPT根據(jù)書本中來(lái)回答問題,我們需要怎么做?
(2)假設(shè)你需要一個(gè)問答任務(wù)用到prompt A,摘要任務(wù)要使用到prompt B,那如何管理這些prompt呢?因此需要用LangChain來(lái)管理這些prompt。
LangChain的出現(xiàn),簡(jiǎn)化了我們?cè)谑褂肅hatGPT的工程復(fù)雜度。
LangChain中的模塊,每個(gè)模塊如何使用?
前提:運(yùn)行一下代碼,需要OPENAI_API_KEY(OpenAI申請(qǐng)的key),同時(shí)統(tǒng)一引入這些庫(kù):
# 導(dǎo)入LLM包裝器 from langchain import OpenAI, ConversationChain from langchain.agents import initialize_agent from langchain.agents import load_tools from langchain.chains import LLMChain from langchain.prompts import PromptTemplate
LLM:從語(yǔ)言模型中輸出預(yù)測(cè)結(jié)果,和直接使用OpenAI的接口一樣,輸入什么就返回什么。
llm = OpenAI(model_name="text-davinci-003", temperature=0.9) // 這些都是OpenAI的參數(shù) text = "What would be a good company name for a company that makes colorful socks?" print(llm(text)) // 以上就是打印調(diào)用OpenAI接口的返回值,相當(dāng)于接口的封裝,實(shí)現(xiàn)的代碼可以看看github.com/hwchase17/langchain/llms/openai.py的OpenAIChat
以上代碼運(yùn)行結(jié)果:
Cozy Colours Socks.
Prompt Templates:管理LLMs的Prompts,就像我們需要管理變量或者模板一樣。
prompt = PromptTemplate( input_variables=["product"], template="What is a good name for a company that makes {product}?", ) // 以上是兩個(gè)參數(shù),一個(gè)輸入變量,一個(gè)模板字符串,實(shí)現(xiàn)的代碼可以看看github.com/hwchase17/langchain/prompts // PromptTemplate實(shí)際是基于StringPromptTemplate,可以支持字符串類型的模板,也可以支持文件類型的模板
以上代碼運(yùn)行結(jié)果:
What is a good name for a company that makes colorful socks?
Chains:將LLMs和prompts結(jié)合起來(lái),前面提到提供了OpenAI的封裝和你需要問的字符串模板,就可以執(zhí)行獲得返回了。
from langchain.chains import LLMChain chain = LLMChain(llm=llm, prompt=prompt) // 通過(guò)LLM的llm變量,Prompt Templates的prompt生成LLMChain chain.run("colorful socks") // 實(shí)際這里就變成了實(shí)際問題:What is a good name for a company that makes colorful socks?
Agents:基于用戶輸入動(dòng)態(tài)地調(diào)用chains,LangChani可以將問題拆分為幾個(gè)步驟,然后每個(gè)步驟可以根據(jù)提供個(gè)Agents做相關(guān)的事情。
# 導(dǎo)入一些tools,比如llm-math # llm-math是langchain里面的能做數(shù)學(xué)計(jì)算的模塊 tools = load_tools(["llm-math"], llm=llm) # 初始化tools,models 和使用的agent agent = initialize_agent( tools, llm, agent="zero-shot-react-description", verbose=True) text = "12 raised to the 3 power and result raised to 2 power?" print("input text: ", text) agent.run(text)
通過(guò)如上的代碼,運(yùn)行結(jié)果(拆分為兩個(gè)部分):
> Entering new AgentExecutor chain... I need to use the calculator for this Action: Calculator Action Input: 12^3 Observation: Answer: 1728 Thought: I need to then raise the previous result to the second power Action: Calculator Action Input: 1728^2 Observation: Answer: 2985984 Thought: I now know the final answer Final Answer: 2985984 > Finished chain.
Memory:就是提供對(duì)話的上下文存儲(chǔ),可以使用Langchain的ConversationChain,在LLM交互中記錄交互的歷史狀態(tài),并基于歷史狀態(tài)修正模型預(yù)測(cè)。
# ConversationChain用法 llm = OpenAI(temperature=0) # 將verbose設(shè)置為True,以便我們可以看到提示 conversation = ConversationChain(llm=llm, verbose=True) print("input text: conversation") conversation.predict(input="Hi there!") conversation.predict( input="I'm doing well! Just having a conversation with an AI.")
通過(guò)多輪運(yùn)行以后,就會(huì)出現(xiàn):
Prompt after formatting:
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.
Current conversation:
Human: Hi there!
AI: Hi there! It's nice to meet you. How can I help you today?
Human: I'm doing well! Just having a conversation with an AI.
AI: That's great! It's always nice to have a conversation with someone new. What would you like to talk about?
具體代碼
如下:
# 導(dǎo)入LLM包裝器 from langchain import OpenAI, ConversationChain from langchain.agents import initialize_agent from langchain.agents import load_tools from langchain.chains import LLMChain from langchain.prompts import PromptTemplate # 初始化包裝器,temperature越高結(jié)果越隨機(jī) llm = OpenAI(temperature=0.9) # 進(jìn)行調(diào)用 text = "What would be a good company name for a company that makes colorful socks?" print("input text: ", text) print(llm(text)) prompt = PromptTemplate( input_variables=["product"], template="What is a good name for a company that makes {product}?", ) print("input text: product") print(prompt.format(product="colorful socks")) chain = LLMChain(llm=llm, prompt=prompt) chain.run("colorful socks") # 導(dǎo)入一些tools,比如llm-math # llm-math是langchain里面的能做數(shù)學(xué)計(jì)算的模塊 tools = load_tools(["llm-math"], llm=llm) # 初始化tools,models 和使用的agent agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True) text = "12 raised to the 3 power and result raised to 2 power?" print("input text: ", text) agent.run(text) # ConversationChain用法 llm = OpenAI(temperature=0) # 將verbose設(shè)置為True,以便我們可以看到提示 conversation = ConversationChain(llm=llm, verbose=True) print("input text: conversation") conversation.predict(input="Hi there!") conversation.predict( input="I'm doing well! Just having a conversation with an AI.")
參考資料
http://www.dbjr.com.cn/article/279343.htm
以上就是LangChain簡(jiǎn)化ChatGPT工程復(fù)雜度使用詳解的詳細(xì)內(nèi)容,更多關(guān)于LangChain簡(jiǎn)化ChatGPT的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python獲取當(dāng)前文件所在目錄、獲取上級(jí)目錄的坑及解決
這篇文章主要介紹了python獲取當(dāng)前文件所在目錄、獲取上級(jí)目錄的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Python 內(nèi)置變量和函數(shù)的查看及說(shuō)明介紹
今天小編就為大家分享一篇Python 內(nèi)置變量和函數(shù)的查看及說(shuō)明介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python+OpenCV圖像處理——實(shí)現(xiàn)直線檢測(cè)
這篇文章主要介紹了Python+OpenCV如何實(shí)現(xiàn)直線檢測(cè),幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-10-10Python基于list的append和pop方法實(shí)現(xiàn)堆棧與隊(duì)列功能示例
這篇文章主要介紹了Python基于list的append和pop方法實(shí)現(xiàn)堆棧與隊(duì)列功能,結(jié)合實(shí)例形式分析了Python使用list定義及使用隊(duì)列的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07python實(shí)現(xiàn)遞歸查找某個(gè)路徑下所有文件中的中文字符
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)遞歸查找某個(gè)路徑下所有文件中的中文字符,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08