简介
最近随着chatgpt的衰亡,人工智能和大语言模型又再次进入了人们的视线,不同的是这一次像是来真的,各大公司都在拼命投入,心愿能在将来的AI赛道上占有一席之地。因为AI须要大规模的算力,尤其是对于大语言模型来说。大规模的算力就意味着须要大量金钱的投入。那么对于小公司或者集体来说是不是什么都做不了呢?
当然不是,尽管小公司或者集体不能开发底层的大语言模型,然而咱们能够在大语言模型之上进行利用开发,这应该就是咱们当初能做到的。
明天给大家介绍一个大语言模型的开发框架langchain,有了它,在AI的世界,你能够锦上添花。
什么是langchain
简略来说,langchain是一个基于大语言模型只上的开发框架,有了他,咱们就能够轻松在各种大模型之上进行理论利用的开发。
langchain的次要特点有两个,第一点就是组件化。langchain提供了各种封装好的组件供咱们应用,大大缩短了咱们开发的工夫。
第二点就是提供了工具链,能够组装各种组件,从而实现更加简单的性能。
langchain的装置
废话不多说,咱们来看下langchain是如何装置的。
AI时代大家肯定要学一下python,至于为什么要学习python呢?因为其余语言都不好使......
langchain实际上是python的一个开发包,所以能够通过pip或者conda两种形式来装置:
pip装置:
pip install langchain
conda装置:
conda install langchain -c conda-forge
默认状况下下面的装置形式是最简略的装置,还有很多和langchain集成的modules并没有装置进来,如果你心愿装置common LLM providers的依赖模块,那么能够通过上面的命令:
pip install langchain[llms]
如果你想装置所有的模块,那么能够应用上面的命令:
pip install langchain[all]
因为langchain是开源软件,所以你也能够通过源代码来装置,下载好源代码之后,通过上面的命令装置即可:
pip install -e .
langchain疾速应用
上面咱们以几个具体的例子来解说一下langchain如何应用的。
因为langchain只是一个大语言模型上的开发框架,它的所有的能力都是依赖于大语言模型的,所以在应用langchain之前,咱们须要一个大语言模型,最简略同时也是最弱小的大语言模型就是openai的chatgpt了。
接下来咱们就以接入openai为例子进行解说。
当然langchain也能够接入其余的大语言模型框架,前面的系列教程中咱们会具体解说。
要应用openai,必须先注册一个openai的账号,而后拿到openai的api key。
具体的注册流程这里就不讲了。大家能够自行参考网络上的各种教程。
有了api key之后,咱们须要配置一下环境变量:
export OPENAI_API_KEY="..."
而后装置openai的包:
pip install openai
接下来就能够欢快的应用openai提供的各种性能了。
当然,如果你不想在环境变量中配置openai的key,咱们也能够在OpenAI的构造函数中传入openai_api_key:
from langchain.llms import OpenAIllm = OpenAI(openai_api_key="...")
构建利用
有了下面的筹备工作,接下来咱们就能够开始应用langchain了。
当然,最最根底的一个利用就是跟大模型交互了,比方跟openai交互,咱们能够让openai给咱们写首诗:
>>> from langchain.llms import OpenAI>>> llm = OpenAI(temperature=0.9)>>> llm.predict("请以古龙的口气,写首对于春天诗")春天来了,万物复苏,终于迎来了一个新的时刻,草儿花儿抬起头,喜迎新绿与壮丽的色彩。山林里,小草发芽,河畔边,花儿香烈,这让咱们感到赏心悦目,这真是一个美妙的世界。春天来了,列位敌人,请喜迎这样一个新时刻,不要埋怨什么,享受春的和煦与快慰。
尽管写进去了,然而我感觉写的一般般吧。
然而这不重要,咱们晓得了如何通过langchain来调用openai的大模型,这个才是最重要的。
聊天模式
下面咱们调用LLM应用用的是"text in, text out"的模型。
尽管聊天模式也是基于LLM,然而他更进了一步,因为他保留了会话的高低问题,所以在对话上更加智能化。
在代码上,传入的就不是文本了,而是message对象。
在langchain中,目前反对上面几种音讯类型:AIMessage, HumanMessage, SystemMessage 和 ChatMessage。
在绝大多数状况下,咱们只须要用到AIMessage, HumanMessage, SystemMessage即可。
上面是应用的代码例子:
from langchain.chat_models import ChatOpenAIfrom langchain.schema import ( AIMessage, HumanMessage, SystemMessage)chat = ChatOpenAI(temperature=0)chat.predict_messages([HumanMessage(content="请以古龙的口气,写首对于春天诗")])
那么聊天模式和LLM模式有什么不一样呢?
大家能够看到,聊天模式调用的是predict_messages接口, 而LLM模式调用的是predict接口。
事实上聊天模式底层还是应用的是LLM,为了不便大家的应用,你也能够间接应用chat.predict办法来进行LLM形式的调用,如下所示:
chat.predict("请以古龙的口气,写首对于春天诗")
Prompt的模板
开发过LLM利用的人都晓得,在LLM中Prompt是十分重要的,一个好的Prompt间接决定了这个利用的品质。
然而Prompt必定须要联合用户的输出和咱们本人做的一些限定来联合应用。
这时候就须要用到Prompt的模板性能了。 咱们能够在零碎中设置好模板,用户只须要填充模板中的特定音讯即可。
在LLM模式中,能够应用PromptTemplates,这样来写:
from langchain.prompts import PromptTemplateprompt = PromptTemplate.from_template("请帮忙我详细描述一下这个物体,这个物体的名字是: {object}?")prompt.format(object="猫")
最初生成的后果如下:
请帮忙我详细描述一下这个物体,这个物体的名字是: 猫
如果是在chat models中,代码会简单一点点,然而逻辑实际上是一样的。 在chat models中,须要用到几种MessagePromptTemplate,比方:ChatPromptTemplate,SystemMessagePromptTemplate和HumanMessagePromptTemplate。
咱们具体来看下如何应用:
from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate,)template = "当初,你的角色是{your_role}, 请依据你的角色答复后续的问题."system_message_prompt = SystemMessagePromptTemplate.from_template(template)human_template = "{text}"human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])chat_prompt.format_messages(your_role="老师", text="世界上最远的中央是哪里?")
对应的输入如下:
[ SystemMessage(content="当初,你的角色是老师, 请依据你的角色答复后续的问题.", additional_kwargs={}), HumanMessage(content="世界上最远的中央是哪里?")]
十分完满。
Chains
langchain还有一个十分有用的性能就是Chains,他能够把多种不同的性能联合起来。
比方下面咱们用到了LLM,还用到了Prompt的模板,那么咱们能够用Chains把他们联合起来:
from langchain.chains import LLMChainchain = LLMChain(llm=llm, prompt=prompt)chain.run("猫")
当然,也能够联合chat应用:
from langchain import LLMChainfrom langchain.chat_models import ChatOpenAIfrom langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate,)chat = ChatOpenAI(temperature=0)template = "当初,你的角色是{your_role}, 请依据你的角色答复后续的问题."system_message_prompt = SystemMessagePromptTemplate.from_template(template)human_template = "{text}"human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])chain = LLMChain(llm=chat, prompt=chat_prompt)chain.run(your_role="老师", text="世界上最远的中央是哪里?")
Agents
什么是agents? 从字面意义上看,Agents就是代理。
事实上langchain中的Agents就是代理的意思。
比方咱们当初须要向openai询问昨天的天气,然而openai自身只是一个大模型,它并不知道实时的信息。然而通过agents就能够先进行一次判断,看看这个问题是交给大模型解决适合,还是交给搜索引擎来查问比拟适合。
这就是agents的作用。
agents利用LLM来判断须要怎么解决这个工作,并且以什么样的程序来解决这个工作。
然而应用agents是要有些条件的,首先你这个LLM模型必须反对agent,这样能力进行后续的工作。
其次是须要筛选适合的工具来进行你想要做的事件,比方:Google Search, Database lookup, Python REPL等等。
最初就是须要指定反对的agent的名字,这样LLM才晓得到底须要进行哪种action。
上面是一个应用SerpAPI联合openai来进行搜寻的例子:
from langchain.agents import AgentType, initialize_agent, load_toolsfrom langchain.llms import OpenAI# The language model we're going to use to control the agent.llm = OpenAI(temperature=0)# The tools we'll give the Agent access to. Note that the 'llm-math' tool uses an LLM, so we need to pass that in.tools = load_tools(["serpapi", "llm-math"], llm=llm)# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)# Let's test it out!agent.run("What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?")
agent比较复杂,性能也很弱小,后续咱们会具体解说。
Memory
最初要解说的langchain的一个性能就是Memory。
因为很多时候,咱们的利用应该是一个有状态的,也就是说利用须要晓得你之前做了什么,这样才能够给用户提供更好的服务。
然而之前咱们将的LLM或者chain都是无状态的。
所以langchain提供了一个Memory的性能,能够把之前的输入输出保存起来,不便后续的应用。
总结
有了langchain的各种工具,当初你就能够疾速开发一个属于你本人的LLM利用啦。