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 系統指令,用於指定模型的行爲
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章