LangChain簡化ChatGPT工程復(fù)雜度使用詳解
什么是LangChain?
使用ChatGPT大家可能都是知道prompt,
(1)想像一下,如果我需要快速讀一本書,想通過本書作為prompt,使用ChatGPT根據(jù)書本中來回答問題,我們需要怎么做?
(2)假設(shè)你需要一個問答任務(wù)用到prompt A,摘要任務(wù)要使用到prompt B,那如何管理這些prompt呢?因此需要用LangChain來管理這些prompt。
LangChain的出現(xiàn),簡化了我們在使用ChatGPT的工程復(fù)雜度。
LangChain中的模塊,每個模塊如何使用?
前提:運行一下代碼,需要OPENAI_API_KEY(OpenAI申請的key),同時統(tǒng)一引入這些庫:
# 導(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ù)測結(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)于接口的封裝,實現(xiàn)的代碼可以看看github.com/hwchase17/langchain/llms/openai.py的OpenAIChat
以上代碼運行結(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}?",
)
// 以上是兩個參數(shù),一個輸入變量,一個模板字符串,實現(xiàn)的代碼可以看看github.com/hwchase17/langchain/prompts
// PromptTemplate實際是基于StringPromptTemplate,可以支持字符串類型的模板,也可以支持文件類型的模板
以上代碼運行結(jié)果:
What is a good name for a company that makes colorful socks?
Chains:將LLMs和prompts結(jié)合起來,前面提到提供了OpenAI的封裝和你需要問的字符串模板,就可以執(zhí)行獲得返回了。
from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=prompt) // 通過LLM的llm變量,Prompt Templates的prompt生成LLMChain
chain.run("colorful socks") // 實際這里就變成了實際問題:What is a good name for a company that makes colorful socks?
Agents:基于用戶輸入動態(tài)地調(diào)用chains,LangChani可以將問題拆分為幾個步驟,然后每個步驟可以根據(jù)提供個Agents做相關(guān)的事情。
# 導(dǎo)入一些tools,比如llm-math
# llm-math是langchain里面的能做數(shù)學(xué)計算的模塊
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)
通過如上的代碼,運行結(jié)果(拆分為兩個部分):
> 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:就是提供對話的上下文存儲,可以使用Langchain的ConversationChain,在LLM交互中記錄交互的歷史狀態(tài),并基于歷史狀態(tài)修正模型預(yù)測。
# 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.")
通過多輪運行以后,就會出現(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é)果越隨機
llm = OpenAI(temperature=0.9)
# 進行調(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é)計算的模塊
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簡化ChatGPT工程復(fù)雜度使用詳解的詳細(xì)內(nèi)容,更多關(guān)于LangChain簡化ChatGPT的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python獲取當(dāng)前文件所在目錄、獲取上級目錄的坑及解決
這篇文章主要介紹了python獲取當(dāng)前文件所在目錄、獲取上級目錄的坑及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Python 內(nèi)置變量和函數(shù)的查看及說明介紹
今天小編就為大家分享一篇Python 內(nèi)置變量和函數(shù)的查看及說明介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python+OpenCV圖像處理——實現(xiàn)直線檢測
這篇文章主要介紹了Python+OpenCV如何實現(xiàn)直線檢測,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-10-10
Python基于list的append和pop方法實現(xiàn)堆棧與隊列功能示例
這篇文章主要介紹了Python基于list的append和pop方法實現(xiàn)堆棧與隊列功能,結(jié)合實例形式分析了Python使用list定義及使用隊列的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
python實現(xiàn)遞歸查找某個路徑下所有文件中的中文字符
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)遞歸查找某個路徑下所有文件中的中文字符,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08

