langchain入门及两种模型的使用

一、简介

 1、OpenAi、chatgpt

Openai就是开发chatgpt系列AI产品的公司。

chatgpt是一款AI产品,chatgpt plus也是一款AI产品,后者可以看做是前者的会员版/付费版。

chatgpt-3.5、chatgpt-4这俩简单说都是AI技术模型,后者可以看做是前者的升级版,后者比前者先进很多。当然在这之前还有早更早期的版本。

目前chatgpt这款产品是基于chatgpt-3.5模型的,chatgpt plus则是可以自由选择和切换基于chatgpt-3.5模型或者chatgpt-4模型。

chatgpt是免费的,chatgpt plus是收费的,20美元一个月。

api、api key,IT行业常用语,IT开发人员才会用到,普通非专业人士基本用不到。chatgpt和chatgpt plus都提供api服务,想用就需要申请对应的api key,用的时候都是需要额外付费的。

openai官网:https://openai.com

chatgpt官方链接(在openai官网里可以找到):https://chat.openai.com

openai中文文档:https://www.openaidoc.com.cn/ 

目前国内有的终端设备可以访问openai官网,有的不能。chatgpt官方链接全都不能访问。想进去浏览一下就得找梯子了。

想玩官方的chatgpt或者chatgpt plus,一是要有梯子才能访问,二是注册需要国外手机号,另外chatgpt plus还涉及到充值需要国外银行卡,总体而言对于小白来说还是有一些麻烦的。

2、大语言模型

大语言模型(LLM)是指使用大量文本数据训练的深度学习模型,可以生成自然语言文本或理解语言文本的含义。大语言模型可以处理多种自然语言任务,如文本分类、问答、对话等,是通向人工智能的一条重要途径。

简单理解:

3、目前大型语言模型(LLM)实例

  • GPT3/3.5/4(MoE),openAI公司
  • LLaMA (Meta数据泄漏,开源社区狂欢)
  • chatGLM (中文语料),清华大学开源

 我们可以在自己的本地运行一个开源的LLM模型,但是性能好点的LLM模型需要庞大的GPU资源,家里一般电脑无法支撑,当然你如果有一个a100的显卡那可以

既然我们无法本地搭建,那就可找第三方已经搭建好的模型,有提供好的API供我们使用,比如

  • 百度的 文心ERNIE
  • 阿里 的通义千问
  • Openai
  • Replicate

这些公司都有API,通过这些API调用他们自己搭建好的模型

这里使用国外openai的API调用模型,所以我们首先需要安装他们的 SDK:

pip install openai -i https://mirrors.aliyun.com/pypi/simple

4、Langchain 简介

LangChain是一个基于语言模型开发应用程序的框架

 官网

https://www.langchain.com/

中文官网

https://www.langchain.com.cn/

python langchain

https://python.langchain.com.cn/docs/get_started/introduction

Langchain 旨在帮助开发这些类型应用程序,比如:

  • 基于文档数据的问答
  • 聊天机器人
  • 代理

python langchain中有六大核心模块,分别是

  1. 模型输入输出 ( Model I/O ):与语言模型进行接口

    对于模型,LangChain中可以使用不同类型的模型

    LLMs

    大型语言模型(LLMs)是将文本字符串作为输入,并返回文本字符串作为输出。

    聊天模型

    聊天模型将聊天消息列表作为输入,并返回聊天消息。

  2. 检索:与特定于应用程序的数据进行接口
  3. 管道:构建调用序列
  4. 代理:让管道根据高级指令选择使用哪些工具
  5. 内存:在管道运行期间保持应用程序状态
  6. 回调:记录和流式传输任何管道的中间步骤

5、Langchain与OpenAI

Langchain 作为一个开源框架,提供与OpenAI等语言模型的接口,简化了在应用程序中集成和利用语言模型能力的过程。

二、开发环境

1、本地环境

不做过多介绍,使用pycharm本地python环境即可(可以配合Jupyter )

 1、永久设置pip源为国内源

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple

2、安装langchain 如果不想永久设置国内源,可以指定临时源

pip install langchain -i https://mirrors.aliyun.com/pypi/simple

2、在线环境

Google Colab 可以在云端运行 Python 代码。Google Colab(全称Google Colaboratory)是一个由Google提供的云端开发环境,用于数据分析、机器学习和深度学习任务。它基于Jupyter Notebook,提供了一个免费的、云端的Python编程环境,用户可以直接在浏览器中编写和执行Python代码。

网址:https://colab.research.google.com/

三、模型

1、模型介绍

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

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

Langchain的支持众多模型供应商,包括OpenAI、ChatGLM、HuggingFace等。本文中,我们将以OpenAI为例,后续内容中提到的模型默认为OpenAI提供的模型。

Langchain的封装,比如,对OpenAI模型的封装,实际上是指的是对OpenAI API的封装。

1.1、LLM

LLM 是一种基于统计的机器学习模型,用于对文本数据进行建模和生成。LLM学习和捕捉文本数据中的语言模式、语法规则和语义关系,以生成连贯并合乎语言规则的文本。

在Langchain的环境中,LLM特指文本补全模型(text completion model)。

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

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

1.2、Chat Models

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

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

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

2、Langchain与OpenAI模型

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

ENDPOINT MODEL 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函数。

 3、模型示列代码

Langchain中使用LLM和聊天模型

与LLM的交互

与LLM的交互,我们需要使用 langchain.llms 模块中的 OpenAI 类

当然也可以使用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.

代码补充说明:

1、如果你不想设置环境变量,你可以在初始化OpenAI LLM类时直接通过openai_api_key命名参数传递密钥:

from langchain.llms import OpenAI

llm = OpenAI(openai_api_key="...")

2、predict方法专门用于根据输入文本生成对应文本,还可以直接使用如下(但是不推荐)

text = "What is AI?"
print(llm(text))

与聊天模型的交互

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

当然也可以使用其他chat_models 中的类

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__

Copy

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

langchain.schema.messages.AIMessage

Copy

接下来我们使用 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)

Copy

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

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

Copy

3个消息类

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

Langchain类 OpenAI角色 作用
AIMessage assistant 模型回答的消息
HumanMessage user 用户向模型的请求或提问
SystemMessage system 系统指令,用于指定模型的行为

猜你喜欢

转载自blog.csdn.net/qq_34491508/article/details/134811485