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

Langchain集成管理prompt功能詳解

 更新時(shí)間:2023年03月29日 09:59:35   作者:AudreyXu  
這篇文章主要為大家介紹了Langchain集成管理prompt功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

LangChain是什么 如何使用

經(jīng)過了chatGPT,大家都知道了prompt-based learning,也明白了prompt在生成式模型的重要性。假設(shè)問答任務(wù)要用prompt A, 摘要生成任務(wù)要用prompt B,那么如何管理不同的prompt呢?
Langchain主要的功能就是集成管理prompt。

安裝

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ú)用于簡(jiǎn)單的應(yīng)用程序。

LangChain主要有以下模塊

1. LLM:從語言模型中輸出預(yù)測(cè)結(jié)果

  • 例子:基于公司產(chǎn)品生成公司名稱
# 導(dǎo)入LLM包裝器。
from langchain.llms import OpenAI
# 初始化包裝器,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

一般來說我們不會(huì)直接把輸入給模型,而是將輸入和一些別的句子連在一起,形成prompts之后給模型。
例如之前根據(jù)產(chǎn)品取名的用例,在實(shí)際服務(wù)中我們可能只想輸入"socks",那么"What would be a good company name for a company that makes"就是我們的template。

from langchain.prompts import PromptTemplate
prompt = PromptTemplate(
    input_variables=["product"],
    template="What is a good name for a company that makes {product}?",
)

那么,對(duì)于模型來說,真正的輸入就是

print(prompt.format(product="colorful socks"))
What is a good name for a company that makes colorful socks?

3. Chains:將LLMs和prompts結(jié)合起來

很容易想到,我們的模型有很多,prompts也有很多,那么需要把他們組裝起來,這就是Chains做的事情。
一個(gè)Chain包含一個(gè)Template和一個(gè)模型。例如LLMChain,就包含一個(gè)PromptTemplate和一個(gè)LLM。
這樣我們的例子就可以

from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
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。

from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=prompt)

然后可以運(yùn)行這個(gè)chain

chain.run("colorful socks")
Socktastic!'

4. Agents:基于用戶輸入動(dòng)態(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次方。

from langchain.agents import laod_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
import os
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ù)提供商。它允許用戶通過簡(jiǎn)單的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è)輸出可以知道,它的思路很清晰。
它的動(dòng)作包括:

  • 讀題:Thought(理解題意)
  • 執(zhí)行:Action(做什么)、Action Input(輸入是什么)、Observation(輸出是什么)
  • 總結(jié):Final Answer(最終輸出)

每一個(gè)輸出之后緊跟著一個(gè)Thought,思考下一步做什么,如果發(fā)現(xiàn)任務(wù)全部完成就輸出最終答案。

5. Memory

如果想做一個(gè)聊天機(jī)器人,那么要求機(jī)器人有短暫的記憶,記住對(duì)話的歷史。
Langchain的ConversationChain就提供這樣一個(gè)功能。

默認(rèn)情況下,ConversationChain具有一種簡(jiǎn)單類型的內(nèi)存,它會(huì)記住所有先前的輸入/輸出并將它們添加到傳遞的上下文中。

# ConversationChain用法
from langchain import OpenAI, 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版本升級(jí)到了3.9就解決了。

參考

https://langchain.readthedocs.io/en/latest/getting_started/getting_started.html

以上就是Langchain集成管理prompt功能詳解的詳細(xì)內(nèi)容,更多關(guān)于Langchain集成管理prompt的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論