Langchain集成管理prompt功能詳解
發(fā)布日期:2023/6/14 0:16:56 瀏覽量:
Langchain集成管理prompt功能詳解
Langchain主要的功能就是集成管理prompt。
安裝
|
1
|
pip install langchain
|
一、需要大語言模型
使用langchain需要使用一個(gè)大語言模型。這個(gè)模型可以用openai的gpt-turbo-3.5,也可以用Hugging face hub里面的大模型。
用這些大模型就需要調(diào)用他們的api,所以就要去這些網(wǎng)站生成相應(yīng)的token。
二、LangChain的模塊
LangChain提供了許多模塊,可以用于構(gòu)建語言模型應(yīng)用程序。這些模塊可以組合在一起創(chuàng)建更復(fù)雜的應(yīng)用程序,也可以單獨(dú)用于簡單的應(yīng)用程序。
LangChain主要有以下模塊
1. LLM:從語言模型中輸出預(yù)測結(jié)果
- 例子:基于公司產(chǎn)品生成公司名稱
|
1
2
3
4
5
6
7
|
# 導(dǎo)入LLM包裝器。
fromlangchain.llmsimportOpenAI
# 初始化包裝器,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(llm(text))
|
2. Prompt Templates: 管理LLMs的Prompts
一般來說我們不會直接把輸入給模型,而是將輸入和一些別的句子連在一起,形成prompts之后給模型。
例如之前根據(jù)產(chǎn)品取名的用例,在實(shí)際服務(wù)中我們可能只想輸入"socks",那么"What would be a good company name for a company that makes"就是我們的template。
|
1
2
3
4
5
|
fromlangchain.promptsimportPromptTemplate
prompt=PromptTemplate(
input_variables=["product"],
template="What is a good name for a company that makes {product}?",
)
|
那么,對于模型來說,真正的輸入就是
|
1
2
|
print(prompt.format(product="colorful socks"))
Whatisa good namefora company that makes colorful socks?
|
3. Chains:將LLMs和prompts結(jié)合起來
很容易想到,我們的模型有很多,prompts也有很多,那么需要把他們組裝起來,這就是Chains做的事情。
一個(gè)Chain包含一個(gè)Template和一個(gè)模型。例如LLMChain,就包含一個(gè)PromptTemplate和一個(gè)LLM。
這樣我們的例子就可以
|
1
2
3
4
5
6
7
|
fromlangchain.promptsimportPromptTemplate
fromlangchain.llmsimportOpenAI
llm=OpenAI(temperature=0.9)
prompt=PromptTemplate(
input_variables=["product"],
template="What is a good name for a company that makes {product}?",
)
|
我們可以創(chuàng)建一個(gè)LLMChain,然后將llm和prompt給chain。
|
1
2
|
fromlangchain.chainsimportLLMChain
chain=LLMChain(llm=llm, prompt=prompt)
|
然后可以運(yùn)行這個(gè)chain
|
1
2
|
chain.run("colorful socks")
Socktastic!’
|
4. Agents:基于用戶輸入動態(tài)地調(diào)用chains
關(guān)于Agents,需要理解以下的概念:
- Tool:輸入是一個(gè)string,輸出是一個(gè)string,作用是做某個(gè)特定任務(wù)。這個(gè)任務(wù)可以是做搜索、查數(shù)據(jù)庫或者Python REPL.
- LLM:語言模型
- Agent:要使用的代理。這應(yīng)該是一個(gè)字符串,引用一個(gè)支持代理類。這里就是調(diào)用其他服務(wù)的API。
這里有一個(gè)例子。假設(shè)想知道Taylor Swift的男友是誰,并且求出他的年齡的3次方。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
fromlangchain.agentsimportlaod_tools
fromlangchain.agentsimportinitialize_agent
fromlangchain.llmsimportOpenAI
importos
os.environ["OPENAI_API_KEY"]="xxxxxxxx"
os.environ["SERPAPI_API_KEY"]="yyyyyyyy"
# 導(dǎo)入llm模型
llm=OpenAI(temperature=0)
# 導(dǎo)入一些tools,這里倒入serpapi和llm-math
# SerpApi是一個(gè)付費(fèi)提供搜索結(jié)果API的第三方服務(wù)提供商。它允許用戶通過簡單的API調(diào)用訪問各種搜索引擎的搜索結(jié)果,包括Google、Bing、Yahoo、Yandex等。
# llm-math是langchain里面的能做數(shù)學(xué)計(jì)算的模塊
tools=load_tools(["serpapi","llm-math"], llm=llm)
# 初始化tools,models 和使用的agent
agent=initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
# 輸出結(jié)果
agent.run("Who isTaylor’s boyfriend? What is his current age raised to the 3 power?")
|
輸出
> Entering new AgentExecutor chain...
I need to find out who Taylor Swift’s boyfriend is and then calculate his age raised to the 3 power.
Action: Search
Action Input: "Taylor Swift boyfriend"
Observation: Taylor Swift’s romance with actor Joe Alwyn is her most serious yet secretive to date. Since 2016, their famously private relationship has ...
Thought: I need to find out Joe Alwyn’s age.
Action: Search
Action Input: "Joe Alwyn age"
Observation: 32 years
Thought: I need to calculate 32 raised to the 3 power.
Action: Calculator
Action Input: 32^3
Observation: Answer: 32768
Thought: I now know the final answer.
Final Answer: Taylor Swift’s boyfriend is Joe Alwyn and his current age raised to the 3 power is 32768.
分析這個(gè)輸出可以知道,它的思路很清晰。
它的動作包括:
- 讀題:Thought(理解題意)
- 執(zhí)行:Action(做什么)、Action Input(輸入是什么)、Observation(輸出是什么)
- 總結(jié):Final Answer(最終輸出)
每一個(gè)輸出之后緊跟著一個(gè)Thought,思考下一步做什么,如果發(fā)現(xiàn)任務(wù)全部完成就輸出最終答案。
5. Memory
如果想做一個(gè)聊天機(jī)器人,那么要求機(jī)器人有短暫的記憶,記住對話的歷史。
Langchain的ConversationChain就提供這樣一個(gè)功能。
默認(rèn)情況下,ConversationChain具有一種簡單類型的內(nèi)存,它會記住所有先前的輸入/輸出并將它們添加到傳遞的上下文中。
|
1
2
3
4
5
|
# ConversationChain用法
fromlangchainimportOpenAI, ConversationChain
llm=OpenAI(temperature=0)
conversation=ConversationChain(llm=llm, verbose=True)# (將verbose設(shè)置為True,以便我們可以看到提示)
conversation.predict(input="Hi there!")
|
輸出
> Entering new chain...
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:> Finished chain.
’ Hello! How are you today?
遇到的錯(cuò)誤
-
ImportError: cannot import name ’load_tools’ from ’langchain.agents’
我用的是python3.7,然后將python版本升級到了3.9就解決了。
馬上咨詢: 如果您有業(yè)務(wù)方面的問題或者需求,歡迎您咨詢!我們帶來的不僅僅是技術(shù),還有行業(yè)經(jīng)驗(yàn)積累。
QQ: 39764417/308460098 Phone: 13 9800 1 9844 / 135 6887 9550 聯(lián)系人:石先生/雷先生