欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

LangChain簡(jiǎn)化ChatGPT工程復(fù)雜度使用詳解

 更新時(shí)間:2023年03月29日 10:12:42   作者:周末程序猿  
這篇文章主要為大家介紹了LangChain簡(jiǎn)化ChatGPT工程復(fù)雜度使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

什么是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.")

參考資料

note.com/npaka/n/n15…

http://www.dbjr.com.cn/article/279343.htm

github.com/hwchase17/l…

以上就是LangChain簡(jiǎn)化ChatGPT工程復(fù)雜度使用詳解的詳細(xì)內(nèi)容,更多關(guān)于LangChain簡(jiǎn)化ChatGPT的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論