当前位置:   article > 正文

02__models_chatopenai类是什么

chatopenai类是什么

LangChain提供两种封装的模型接口

1.大规模语言模型(LLM):输入文本字符串,返回文本字符串

2.聊天模型:基于一个语言模型,输入聊天消息列表,返回聊天消息

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

LangChain的封装,如对OpenAI模型的封装,实际上是指对OpenAI API的封装

LLM

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

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

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

聊天模型 (Chat Models)

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

LangChain与OpenAI模型 

参考OpenAI官方文档https://platform.openai.com/docs/models/model-endpoint-compatibility

gpt模型都归为了聊天模型,而davinci, curie, babbage, ada模型都归为了文本补全模型。

LangChain框架支持不同模型,为了方便切换模型,提供通用接口BaseLanguageModel,并提供predict和predict_messages函数调用。

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

LLM交互

需要使用langchain.llms模块中的OpenAI类

  1. # 从langchain的llms中导入OpenAI类
  2. from langchain.llms import OpenAI
  3. # 设置环境变量OPEN_AI_KEY
  4. import os
  5. os.environ['OPENAI_API_KEY'] = '您的私有OPENAI_API_KEY'
  6. # OpenAI类实例化llm对象,模型选择text-davinci-003
  7. llm = OpenAI(model_name = 'text-davinci-003')
  8. # llm对象调用predict方法和模型交互,传入字符“What Is AI”
  9. response = llm.predict("What Is AI")
  10. print(response)

您应该能看到如下输出:

AI (Artificial Intelligence) is a type of computer technology that enables a machine to simulate human intelligence and behavior. AI systems can be used to automate tasks, learn from data, and make decisions. Examples of AI include natural language processing (NLP), machine learning, computer vision, robotics, and expert systems.

聊天模型的交互

需要使用langchain.chat_models模块的ChatOpenAI类

  1. # 从langchain.chat_models中导入ChatOpenAI类
  2. from langchain.chat_models import ChatOpenAI
  3. from langchain.schema import AIMessage,HumanMessage,SystemMessage
  4. # 设置环境变量OPEN_AI_KEY
  5. import os
  6. os.environ['OPENAI_API_KEY'] = '您的私有OPENAI_API_KEY'
  7. # ChatOpenAI类实例化chat对象,temperature参数设为0,模型默认为gpt-3.5
  8. chat = ChatOpenAI(temperature = 0)
  9. # chat对象调用predict_messages方法和模型交互,传入字符“What Is AI”
  10. response = chat.predict_messages([HumanMessage(content = "What Is AI")])
  11. print(response)

您应该能看到如下输出:

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 would typically require human intelligence, such as 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. AI has applications in various fields, including healthcare, finance, transportation, and entertainment, and is continuously evolving and advancing.' additional_kwargs={} example=False

接下来我们使用SystemMessage指令,指定模型行为。

如下代码指定模型对AI一无所知,当提问到AI相关知识,会回答“I don’t know ”

  1. # 从langchain.chat_models中导入ChatOpenAI类
  2. from langchain.chat_models import ChatOpenAI
  3. from langchain.schema import AIMessage,HumanMessage,SystemMessage
  4. # 设置环境变量OPEN_AI_KEY
  5. import os
  6. os.environ['OPENAI_API_KEY'] = '您的私有OPENAI_API_KEY'
  7. # ChatOpenAI类实例化chat对象,temperature参数设为0,模型默认为gpt-3.5
  8. chat = ChatOpenAI(temperature = 0)
  9. # chat对象调用predict_messages方法和模型交互,传入字符“What Is AI”
  10. 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 AI")])
  11. print(response)
content="I don't know." additional_kwargs={} example=False

三个消息类

langchain框架提供三个消息类,是AIMessage、HumanMessage、SystemMessage,分别对应OpenAI聊天模型API支持的三种角色assitant、user、system。请参考 ​​​​​​OpenAI API文档

总结:介绍了LLM模型与聊天模型,以及两者的区别。使用langchain框架实现了与OpenAI LLM和聊天模型的对话。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/380164?site
推荐阅读
相关标签
  

闽ICP备14008679号