GPT之路(八) LangChain - Models入门

环境:Python 3.11.4, LangChain 0.0.270, Jupyter 

Models模型简介

官方地址:LangChian - Models

Langchain所封装的模型分为两类:

  • 大语言模型 (LLM)
  • 聊天模型 (Chat Models)

Langchain的支持众多模型供应商,包括OpenAI、ChatGLM、ModelScope、HuggingFace等。后面的示例我将以OpenAI为例,后续内容中提到的模型默认为OpenAI提供的模型。Langchain的封装,比如,对OpenAI模型的封装,实际上是指的是对OpenAI API的封装。

LLM

LLM 是一种基于统计的机器学习模型,用于对文本数据进行建模和生成。LLM学习和捕捉文本数据中的语言模式、语法规则和语义关系,以生成连贯并合乎语言规则的文本。在Langchain的环境中,LLM特指文本补全模型(text completion model)。

注,文本补全模型是一种基于语言模型的机器学习模型,根据上下文的语境和语言规律,自动推断出最有可能的下一个文本补全。

输入输出
一条文本内容 一条文本内容

聊天模型 (Chat Models)

聊天模型是语言模型的一种变体。聊天模型使用语言模型,并提供基于"聊天消息"的接口。

输入输出
一组聊天消息 一条聊天消息

聊天消息 除了消息内容文本,还会包含一些其他参数数据。这在后续的内容中会看到。

Langchain与OpenAI模型

参考OpenAI Model endpoint compatibility 文档,gpt模型都归为了聊天模型,而davinci, curie, babbage, ada模型都归为了文本补全模型。

ENDPOINTMODEL NAME
/v1/chat/completions gpt-4, gpt-4-0613, gpt-4-32k, gpt-4-32k-0613, gpt-3.5-turbo, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k, gpt-3.5-turbo-16k-0613
/v1/completions (Legacy) text-davinci-003, text-davinci-002, text-davinci-001, text-curie-001, text-babbage-001, text-ada-001, davinci, curie, babbage, ada

Langchain提供接口集成不同的模型。为了便于切换模型,Langchain将不同模型抽象为相同的接口 BaseLanguageModel,并提供 predict 和 predict_messages 函数来调用模型。

当使用LLM时推荐使用predict函数,当使用聊天模型时推荐使用predict_messages函数。

与LLM的交互

与LLM的交互,我们需要使用 langchain.llms 模块中的 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)

你应该能看到类似如下输出:

AI (Artificial Intelligence) is a branch of computer science that deals with creating intelligent machines that can think, reason, learn, and problem solve. AI systems are designed to mimic human behavior and can be used to automate tasks or provide insights into data. AI can be used in a variety of fields, such as healthcare, finance, robotics, and more.

与聊天模型的交互

与聊天模型的交互,我们需要使用 langchain.chat_models 模块中的 ChatOpenAI 类。

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)
response = chat.predict_messages([ 
  HumanMessage(content="What is AI?")
])
print(response)

你应该能看到类似如下输出:

content='AI, or Artificial Intelligence, refers to the simulation of human intelligence processes by machines, especially computer systems. These processes include learning, reasoning, problem-solving, perception, and language understanding. AI technology has the capability to drastically change and improve the way we work, live, and interact.' additional_kwargs={} example=False

通过以下代码我们查看一下 response 变量的类型:

response.__class__

可以看到,它是一个 AIMessage 类型的对象。

langchain.schema.messages.AIMessage

接下来我们使用 SystemMessage 指令来指定模型的行为。如下代码指定模型对AI一无所知,在回答AI相关问题时,回答“I don't know”。

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)

你应该能看到类似如下输出:

content="I don't know." additional_kwargs={} example=False

3个消息类

Langchain框架提供了三个消息类,分别是 AIMessageHumanMessage 和 SystemMessage。它们对应了OpenAI聊天模型API支持的不同角色 assistantuser 和 system。请参考 OpenAI API文档 - Chat - Role

Langchain类OpenAI角色作用
AIMessage assistant 模型回答的消息
HumanMessage user 用户向模型的请求或提问
SystemMessage system 系统指令,用于指定模型的行为
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章