LangChain学习笔记(一)
文中大部分都能够在官方文档中找到,示例为自己的实践,有条件能够直接参阅官方文档。
1. 什么是LangChain
LangChain is a framework for developing applications powered by language models. We believe that the most powerful and differentiated applications will not only call out to a language model via an api, but will also:
- Be data-aware: connect a language model to other sources of data
- Be agentic: Allow a language model to interact with its environment
LangChain是一个用于开发由言语模型驱动的运用程序结构。咱们相信,最强大和有差异化的运用程序不只会经过API调用言语模型,还会具有以下特点:
- 数据感知:将言语模型衔接到其他数据源。
- 智能署理:允许言语模型与其环境进行交互。
随着chatgpt的火爆,大型言语模型(LLMs)正在成为一种具有革新性的技术,环绕大言语模型的辅佐开发结构LangChain也面世了,它不只能够使咱们更方便的调用大言语模型的api,也能够使其与其他的资源(数据源/shell/api)进行交互,充分发挥大言语模型的才干。
模块
LangChain有六个模块:
- Model I/O(IO模型):供给与大言语模型进行交互的接口
- Data Connection(数据衔接):供给特定于运用数据的接口
- Chains:通用解释为对组件的调用序列,能够将多个调用链接起来
- Memory(回忆模块): 用于保留一个chain中运用程序的状态
- Agents(署理模块):供给其他东西的署理,使得chain能够决议并运用某些东西,比方履行shell命令,调用google api进行搜索等
- Callbacks(回调模块):用于日志记录、监控、流式传输等
2. 构建运用
2.1 装置
pip install langchain #装置langchain
pip intstall openai #装置openai
- 设置
OPENAI_API_KEY
环境变量
2.2 经过大言语模型获取猜测
对大言语模型根底的调用
from langchain.llms import OpenAI
# temperature: 0.0-2.0, 越大越随机;model:默许运用text-davinci-003
llm = OpenAI(temperature=0.9, client="llms", model="text-davinci-003")
print(llm.predict("罗列三种生果"))
#也能够写作
print(llm("罗列三种生果"))
输出:
苹果、梨、橘子
2.3 对对话模型的调用
谈天模型是大言语模型的变体。与大言语模型不同的是,大言语模型以文本进行交互,谈天模型能够运用AIMessage
,HumanMessage
,SystemMessage
进行交互,别离对应openai中的system
,user
,assistant
人物。
from langchain.cache import Base
from langchain.chat_models import ChatOpenAI
from langchain.schema import (HumanMessage, SystemMessage)
# model 默许运用gpt-3.5-turbo
chat = ChatOpenAI(temperature=0.9, client="chat")
result = chat.predict_messages(
[
SystemMessage(content="以下是AI帮手与人类之间的友好对话。"),
HumanMessage(content="把这句话翻译为英语:我爱你。"),
])
print(result)
输出:
content='The translation of this sentence to English is: "I love you."' additional_kwargs={} example=False
当然也能够直接以文本进行交互
print(chat.predict("把这句话翻译为英语:我爱你。"))
输出:
I love you.
2.4 Prompt templates(提示模板)
便于根据不同的上下文来生成不同的prompt,运用程序调用起来更灵活
from langchain.prompts import (ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate)
system_template = "你是一个翻译帮手,协助人们把{源言语}翻译为{目标言语}"
system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)
human_template = "{文本}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
format = chat_prompt.format_messages(源言语="汉语", 目标言语="英语", 文本="我爱你")
print(format)
输出:
[SystemMessage(content='你是一个翻译帮手,协助人们把汉语翻译为英语', additional_kwargs={}), HumanMessage(content='我爱你', additional_kwargs={}, example=False)]
用于后续调用
chat = ChatOpenAI(temperature=0.9, client="chat")
print(chat.predict_messages(format))
输出:
content='I love you.' additional_kwargs={} example=False
2.5 Chains
咱们也能够运用chain来组装model和prompt
# 在2.4的根底上添加
from langchain import LLMChain
chain = LLMChain(llm=chat, prompt=chat_prompt)
result = chain.run(源言语="汉语", 目标言语="英语", 文本="我爱你")
print(result)
输出:
I love you.
2.6 Agents
运用署理来调用东西以完成更复杂的任务。运用署理分为三步:
- 挑选言语模型:LLM/Chat Model
- 挑选东西:例如,Google Search,Shell, ChatGPT Plugins等
- 构建署理
比方咱们能够运用SerpAPI经过谷歌搜索某些东西并进行简单的数学计算:
- 装置包:
pip install google-search-results
- 设置环境变量:
SERPAPI_API_KEY
from langchain.agents import AgentType, initialize_agent, load_tools
from langchain.llms import OpenAI
llm = OpenAI(temperature=0, client="agent", model="text-davinci-003")
# serpapi: 能够用来调用google的api,llm-math:进行数学计算
tools = load_tools(["serpapi", "llm-math"], llm)
# AgentType.ZERO_SHOT_REACT_DESCRIPTION:直译便是零样本_反应_描绘,仅经过描绘来确定运用哪个东西
# verbose=True: 显示详细描绘(能够理解为AI的心路历程)
agent = initialize_agent(tools, llm, AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
print(agent.run("北京昨日最高气温是多少摄氏度?然后算一下这个数字的平方根"))
输出:
> Entering new chain...
I need to find the temperature first, then calculate the square root
Action: Search
Action Input: "Beijing yesterday highest temperature"
Observation: Beijing Temperature Yesterday. Maximum temperature yesterday: 102 F (at 5:00 pm) Minimum temperature yesterday: 72 F (at 5:00 am) Average temperature ...
Thought: I need to convert the temperature to Celsius
Action: Calculator
Action Input: 102 Fahrenheit to Celsius
Observation: Answer: 38.888888888888886
Thought: I now need to calculate the square root
Action: Calculator
Action Input: 38.888888888888886^(1/2)
Observation: Answer: 6.236095644623235
Thought: I now know the final answer
Final Answer: The highest temperature in Beijing yesterday was 38.888888888888886C and the square root of this number is 6.236095644623235.
> Finished chain.
The highest temperature in Beijing yesterday was 38.888888888888886C and the square root of this number is 6.236095644623235.
功用很强大,但实践体验下来不是特别夸姣,一是不稳定,常常需要查询很多次才干得到成果,二是描绘稍微含糊或者复杂一点,得到的成果或许便是错误的,或者无法带入到下一步,直接报错 摆烂。
serpapi注册用户每月有100次免费的谷歌调用,经不起折腾,不过咱们能够来测验下免费的duckduckgo search (鸭鸭狗)
- 装置
pip install duckduckgo-search
from langchain.llms import OpenAI
from langchain.agents import initialize_agent, AgentType, load_tools
llm = OpenAI(temperature=0, client="agent", model="text-davinci-003")
tools = load_tools(["ddg-search", "llm-math"], llm)
agent = initialize_agent(tools, llm, AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
print(agent.run("北京昨日最高气温是多少摄氏度?然后算一下这个数字的平方根"))
输出:
> Entering new chain...
I need to find the highest temperature in Beijing yesterday and then calculate the square root of that number
Action: duckduckgo_search
Action Input: "Beijing highest temperature yesterday"
Observation: By Reuters BEIJING, June 22 — The temperature in Beijing breached 106 degrees Fahrenheit on Thursday and shattered the record for the hottest day in June as heatwaves that had seared nort
hern... On Friday, Beijing baked in temperatures as high as 40.3C, after sizzling at 41.1C on Thursday, the second-hottest day recorded by the Chinese capital in modern times. Beijing's all-time high of ... The city experienced its all-time recorded high of 41.9 C (107 F) on July 24, 1999. Chinese meteorologists say the current heat wave was caused by warm air masses associated with high-pressure ridges in the atmosphere,
compounded by thin cloud covers and long daylight hours around the summer solstice. Last week, Beijing recorded its highest temperature for mid-June, with weather officials warning the public to stay indoors as the mercury hit 39.4C. With Agence France-Presse and Reuters... Beijing recorded a high temperature of 41.1 C yesterday (Jun 23) as the capital city continues to experience an ongoing heatwave, according to a report from Beijing News (新京报). This isn't the first time the city has seen the heat cranked up to over 41 C, but the first time it's happened in June.
Thought: I now have the highest temperature in Beijing yesterday
Action: Calculator
Action Input: 41.1
Observation: Answer: 41.1
Thought: I now know the final answer
Final Answer: The highest temperature in Beijing yesterday was 41.1C and its square root is 6.4.
一次查询便得到了成果,效果好像更好,不过日期错了,并不是昨日
也能够调用本地shell:
from langchain.llms import OpenAI
from langchain.agents import initialize_agent, AgentType, load_tools
llm = OpenAI(temperature=0, client="shell", model="text-davinci-003")
tools = load_tools(["terminal", "llm-math"], llm)
self_ask_with_search = initialize_agent(
tools, llm, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
self_ask_with_search.run("get the memory info by GB")
2.7 Memory
保护程序状态,拥有上下文回忆
from langchain.prompts import (ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder)
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template(
"以下是AI帮手与人类之间的友好对话。"
),
MessagesPlaceholder(variable_name="history"),
HumanMessagePromptTemplate.from_template("{input}")
])
llm = ChatOpenAI(temperature=0, client="memory")
memory = ConversationBufferMemory(return_messages=True)
conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm)
while True:
user = input("用户: ")
answer = conversation.predict(input = user)
print("AI帮手:", answer)
咱们与AI进行交互:
用户: 你好
AI帮手: 你好!有什么我能够协助你的吗?
接下来验证的确存在回忆:
用户: 我说的是上一句话是什么
AI帮手: 你说的上一句话是"你好"。
3. 最终
LangChain的大体功用如上,与LLMs一样,现在也在蓬勃发展阶段,由于大言语模型的不稳定性,现在运用起来还有很多不尽如人意的当地,可是咱们也能够看到其潜力和愿景。个人觉得,agents部分是最吸引人的当地,将LLMs的才干由文本延伸到程序之外,极大地提升了想象空间。 更多内容能够检查官方文档。
附:参阅链接&文档
- 官方文档