上一篇咱们写了LangChain的hello world,这一篇咱们来看LangChain的Model模块。
前言
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
chat = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
response = chat([ HumanMessage(content="Hello Langchain!") ])
print(response)
上文的Hello 比如中, 展现了LangChain
包括chat_models
谈天模型。别外,LangChain
还封装了大言语模型。咱们能够这样了解:LangChain
为AI应用框架,封装大言语模型肯定是中心功能,之所以还单独供给一个chat_model,是因为AI谈天机器人是首要的AI应用形式,为了便利,单独拎了谈天模型出来。
LLM 大模型
LangChain
支撑的模型有OpenAI、ChatGLM、Llama、Hugging Face。其间,Hugging Face是现在最成功的大模型社区,咱们能够便利的在这里挑选相应的大模型完成各种NLP使命。最近,Google推出的Gemini,号称比OpenAI 4.0还优秀,等待中….
Completion
OpenAI为代表的AIGC大模型,LangChain在与之共舞时,首要以文本补全为主。LangChain
将Prompt
喂给大模型,生成文本补全。咱们常用的文本补全模型有text-davinci-003
等
谈天模型
谈天模型是言语模型的一种变体。其谈天音讯的接口会涉及一些参数操作,比如谈天回忆、人物等。咱们常用的谈天模型有gpt-4
、gpt-3.5-turbo
接口
LangChain
供给接口集成不同的模型,并笼统了BaseLanguageModel
对接这些模型,能够运用predict
、predict_messages
调用模型。
- predict demo
!pip install langchain==0.0.235 openai
from langchain.llms import OpenAI
import os os.environ['OPENAI_API_KEY'] = '您的有用OpenAI API Key'
llm = OpenAI(model_name="text-davinci-003") response = llm.predict("What is AI?") print(response)
LangChain这次调用OpenAI的过程,首先由langchain.llms 大模型中引进OpenAI。接着实例化OpenAI,并挑选model_name=”text-davinci-003″,最后调用实例上的predict办法,让它生成以下答复。
AI (Artificial Intelligence) is the ability of a computer or machine to think and learn, and to act independently of humans. It includes the use of algorithms, data analysis, and robotics to carry out complex tasks that would normally require human intelligence. AI can be used to automate mundane tasks, improve decision-making processes, and create new products and services.
- predict_messages demo
from langchain.chat_models import ChatOpenAI from langchain.schema import AIMessage, HumanMessage, SystemMessage
import os
os.environ['OPENAI_API_KEY'] = '您的有用OpenAI API Key'
chat = ChatOpenAI(temperature=0,model_name='gpt-3.5-turbo')
response = chat.predict_messages([ HumanMessage(content="What is AI?") ])
print(response)
这个比如和上篇的比如差不多,区别在于chat.predict_messages,而上篇直接chat。下面是谈天内容的回来。
content='AI, or Artificial Intelligence, refers to the simulation of human intelligence in machines that are programmed to think and learn like humans. It involves the development of computer systems capable of performing tasks that typically require human intelligence, such as visual perception, speech recognition, decision-making, problem-solving, and language translation. AI can be categorized into two types: Narrow AI, which is designed for specific tasks, and General AI, which possesses the ability to understand, learn, and apply knowledge across various domains.' additional_kwargs={} example=False
response.__class__
langchain.schema.messages.AIMessage
response = chat.predict_messages([ SystemMessage(content="You are a chatbot that knows nothing about AI. When you are asked about AI, you must say 'I don't know'"),
HumanMessage(content="What is deep learning?") ])
print(response)
这个比如咱们运用SystemMessage
指定了谈天机器人的人物,假定他不懂AI, 再运用HumanMessage
问了相关AI的问题,得到的答案是NO!
总结
-
LangChain
供给了谈天和文本补全两类模型,即Completion和Chat。 - 不管是补全式还是谈天式大模型,都有predict 的概念 即
推理
。 - LangChain在谈天模型中供给了三个音讯类,分别是
AIMessage
、HumanMessage
、SystemMessage
- 下次花时间探究下
LangChain
调用Hugging Face 上其它模型的比如。