平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“Langchain集成管理prompt功能”来说,它看着像小点,放到项目里常会牵出环境、配置、兼容性和维护成本。下面按实际采用顺序,把思路、关键写法和容易踩坑的地方讲清楚,便于大家直接对照操作。
结合项目来看,经过了chatGPT,大家都知道了prompt-based learning,也明白了prompt在生成式模型的重要性。假设问答任务要用prompt A, 摘要生成任务要用prompt B,那么如何管理不同的prompt呢?
Langchain主要的功能就是集成管理prompt。
安装
pip install langchain
实际处理时,采用langchain需采用一个大语言模型。这个模型能够用openai的gpt-turbo-3.5,也能够用Hugging face hub里面的大模型。
从实现思路看,用这些大模型就需调用他们的api,所以就要去这些网站生成相应的token。
落到代码里,LangChain提供了许多模块,能够用来构建语言模型应用程序。这些模块能够组合在一起新建更复杂的应用程序,也能够单独用来轻松的应用程序。
LangChain主要有以下模块
# 导入LLM包装器。
from langchain.llms import OpenAI
# 初始化包装器,temperature越高结果越随机
llm = OpenAI(temperature=0.9)
# 进行调用
text = "What would be a good company name for a company that makes colorful socks?"
print(llm(text))
结合项目来看,一般来说我们不会直接把输入给模型,而是将输入和一些别的句子连在一起,形成prompts之后给模型。
从实现思路看,比如之前根据产品取名的用例,在实际服务中我们可能只想输入"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}?",
)
那么,对于模型来说,真正的输入就是
print(prompt.format(product="colorful socks"))
What is a good name for a company that makes colorful socks?
结合项目来看,很容易想到,我们的模型有很多,prompts也有很多,那么需把他们组装起来,这就是Chains做的事情。
结合项目来看,一个Chain包含一个Template和一个模型。比如LLMChain,就包含一个PromptTemplate和一个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}?",
)
从实现思路看,我们能够新建一个LLMChain,随后将llm和prompt给chain。
from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=prompt)
然后能够运行这个chain
chain.run("colorful socks")
Socktastic!'
关于Agents,需理解以下的概念:
实际处理时,这里有一个例子。假设想知道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"
# 导入llm模型
llm = OpenAI(temperature=0)
# 导入一些tools,这里倒入serpapi和llm-math
# SerpApi是一个付费提供搜索结果API的第三方服务提供商。它允许用户通过简单的API调用访问各种搜索引擎的搜索结果,包括Google、Bing、Yahoo、Yandex等。
# llm-math是langchain里面的能做数学计算的模块
tools = load_tools(["serpapi", "llm-math"], llm=llm)
# 初始化tools,models 和使用的agent
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
# 输出结果
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.
分析这个输出能够知道,它的思路很清晰。
它的动作包括:
从实现思路看,每一个输出之后紧跟着一个Thought,思考下一步做什么,如果发现任务全部完成就输出最后答案。
若想做一个聊天机器人,那么要求机器人有短暂的记忆,记住对话的历史。
在这个场景下,Langchain的ConversationChain就提供这样一个功能。
结合项目来看,默认情况下,ConversationChain具有一种轻松类型的内存,它会记住所有先前的输入/输出同时将它们添加到传递的上下下文。
# ConversationChain用法
from langchain import OpenAI, ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True) # (将verbose设置为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?
参考
(链接已移除)
从实现思路看,以上就是Langchain集成管理prompt功能详解的详细内容,更多关于Langchain集成管理prompt的资料请关注脚本之家其它相关文章!