首页 > 其他分享 >GPT之路(八) LangChain - Models入门

GPT之路(八) LangChain - Models入门

时间:2023-09-16 20:22:39浏览次数:41  
标签:Models 模型 LangChain Langchain AI OpenAI 聊天 GPT gpt

环境: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 系统指令,用于指定模型的行为

标签:Models,模型,LangChain,Langchain,AI,OpenAI,聊天,GPT,gpt
From: https://www.cnblogs.com/hlkawa/p/17678145.html

相关文章

  • GPT之路(七) LangChain AI编成框架入门的第一个demo
    环境:Python3.11.4,LangChain0.0.2701.Langchain简介1.1 PythonLangchain官方文档大型语言模型(LLM)正在成为一种具有变革性的技术,使开发人员能够构建以前无法实现的应用程序。然而,仅仅依靠LLM还不足以创建一个真正强大的应用程序。它还需要其他计算资源或知识来源。Langch......
  • [论文速览] SDXL@ Improving Latent Diffusion Models for High-Resolution Image Syn
    Pretitle:SDXL:ImprovingLatentDiffusionModelsforHigh-ResolutionImageSynthesisaccepted:arXiv2023paper:https://arxiv.org/abs/2307.01952code:https://github.com/Stability-AI/generative-models关键词:imagesynthesis,stablediffusion,SDXL,AICG......
  • 【译】使用 ChatGPT 和 Azure Cosmos DB 构建智能应用程序
    原文|MarkBrown翻译|郑子铭随着对智能应用程序的需求不断增长,开发人员越来越多地转向人工智能(AI)和机器学习(ML),以增强其应用程序的功能。聊天机器人已经成为提供对话式人工智能的最流行方式之一。ChatGPT是OpenAI开发的大型语言模型(LLM),是构建能够理解自然语言并提供智能响应的聊......
  • [腾讯云Cloud Studio实战训练营]无门槛使用GPT+Cloud Studio辅助编程完成Excel自动工
    @TOC前言chatgpt简单介绍:ChatGPT是一种基于GPT的自然语言处理模型,专门用于生成对话式文本。它是OpenAI于2021年发布的,在广泛的对话数据集上进行了训练,旨在提供更具交互性和适应性的对话体验。与传统的问答系统不同,ChatGPT设计用于处理连续的对话而不仅仅是单独的问题和回答。它可......
  • DoctorGPT
    DoctorGPTbringsGPTintoproductionforerrordiagnosing!DoctorGPT把GPT投入生产用来进行错误诊断!(Notproductionready,yet...)还未准备好投产,但。。。DoctorGPTisalightweightself-containedbinarythatmonitorsyourapplicationlogsforproblemsandd......
  • ChatGPT的未来
    随着人工智能的快速发展,ChatGPT作为一种自然语言生成模型,在各个领域都展现出了巨大的潜力。它不仅可以用于日常对话、创意助手和知识查询,还可以应用于教育、医疗、商业等各个领域,为人们带来更多便利和创新。在教育领域,ChatGPT可以成为学生的学习伙伴和辅导员。学生可以通过......
  • RTE 领域近期词云统计发布;谷歌开始新一轮「瘦身」计划;使用ChatGPT之后智力提高 50%丨R
    开发者朋友们大家好:这里是「RTE开发者日报」,每天和大家一起看新闻、聊八卦。不知不觉,我们的日报已经发布了50期,作为RTE领域最垂直的日报栏目,我们的社区编辑团队会整理分享RTE(RealTimeEngagement)领域内「有话题的新闻」、「有态度的观点」、「有意思的数据」、「有思考......
  • 安装langchain-chatchat
    1、下载langchain-chatchatgitclonehttps://github.com/chatchat-space/Langchain-Chatchat.git2、下载llama2-7b-chat-hfgitlfsinstallgitclonehttps://huggingface.co/meta-llama/Llama-2-7b-chat-hf以上下载不成功,找到百度网盘版本,改用:wget传输文件压缩成tar,传输。......
  • 续集来了!我让 GPT-4 用 Laf 三分钟写了个完整的待办事项 App
    一觉醒来,GPT-4已经发布了!GPT-4实现了真正的多模态,可以把纸笔画的原型直接写出网页代码。读论文时还能理解插图含意。好消息是,ChatGPTPlus用户目前可以提前尝鲜GPT-4模型。作为高贵的Plus用户,这怎么能忍?立马打开ChatGPT切换到最新模型。一位Twitter网友经过测试发现......
  • modelsim使用
    手动使用写好测试文件挺好理解的放代码看一下就懂了:文件名字为原本代码文件加上_tb`timescale1ns/1ns //精度1nsmodulehuiyidemo_tb;regsclk=0 ;regrst_n=0 ;wire[2:0]test_out;//这个是不需要写的initialbegin #100 //100ns rst_n<=1'b1;endalw......