100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 大模型从入门到应用——LangChain:模型(Models)-[聊天模型(Chat Models):使用

大模型从入门到应用——LangChain:模型(Models)-[聊天模型(Chat Models):使用

时间:2020-02-09 16:23:28

相关推荐

大模型从入门到应用——LangChain:模型(Models)-[聊天模型(Chat Models):使用

分类目录:《大模型从入门到应用》总目录

LangChain系列文章:

基础知识快速入门 安装与环境配置链(Chains)、代理(Agent:)和记忆(Memory)快速开发聊天模型 模型(Models) 基础知识大型语言模型(LLMs) 基础知识LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(Human Input LLM)缓存LLM的调用结果加载与保存LLM类、流式传输LLM与Chat Model响应和跟踪tokens使用情况 聊天模型(Chat Models) 基础知识使用少量示例和响应流式传输 文本嵌入模型 Aleph Alpha、Amazon Bedrock、Azure OpenAI、Cohere等Embaas、Fake Embeddings、Google Vertex AI PaLM等 提示(Prompts) 基础知识提示模板 基础知识连接到特征存储创建自定义提示模板和含有Few-Shot示例的提示模板部分填充的提示模板和提示合成序列化提示信息 示例选择器(Example Selectors)输出解析器(Output Parsers) 记忆(Memory) 基础知识记忆的类型 会话缓存记忆、会话缓存窗口记忆和实体记忆对话知识图谱记忆、对话摘要记忆和会话摘要缓冲记忆对话令牌缓冲存储器和基于向量存储的记忆 将记忆添加到LangChain组件中自定义对话记忆与自定义记忆类聊天消息记录记忆的存储与应用 索引(Indexes) 基础知识文档加载器(Document Loaders)文本分割器(Text Splitters)向量存储器(Vectorstores)检索器(Retrievers) 链(Chains) 基础知识通用功能 自定义Chain和Chain的异步APILLMChain和RouterChainSequentialChain和TransformationChain链的保存(序列化)与加载(反序列化) 链与索引 文档分析和基于文档的聊天问答的基础知识图问答(Graph QA)和带来源的问答(Q&A with Sources)检索式问答文本摘要(Summarization)、HyDE和向量数据库的文本生成 代理(Agents) 基础知识代理类型自定义代理(Custom Agent)自定义MRKL代理带有ChatModel的LLM聊天自定义代理和自定义多操作代理(Custom MultiAction Agent)工具 基础知识自定义工具(Custom Tools)多输入工具和工具输入模式人工确认工具验证和Tools作为OpenAI函数 工具包(Toolkit)代理执行器(Agent Executor) 结合使用Agent和VectorStore使用Agents的异步API和创建ChatGPT克隆处理解析错误、访问中间步骤和限制最大迭代次数为代理程序设置超时时间和限制最大迭代次数和为代理程序和其工具添加共享内存 计划与执行 回调函数(Callbacks)

使用少量示例

本部分的内容介绍了如何在聊天模型(Chat Models)中使用少量示例。关于如何最好地进行少量示例提示尚未形成明确的共识。因此,我们尚未固定任何关于此的抽象概念,而是使用现有的抽象概念。

交替的人工智能/人类消息

进行少量示例提示的第一种方式是使用交替的人工智能/人类消息。以下是一个示例:

from langchain.chat_models import ChatOpenAIfrom langchain import PromptTemplate, LLMChainfrom langchain.prompts.chat import (ChatPromptTemplate,SystemMessagePromptTemplate,AIMessagePromptTemplate,HumanMessagePromptTemplate,)from langchain.schema import (AIMessage,HumanMessage,SystemMessage)chat = ChatOpenAI(temperature=0)template="You are a helpful assistant that translates english to pirate."system_message_prompt = SystemMessagePromptTemplate.from_template(template)example_human = HumanMessagePromptTemplate.from_template("Hi")example_ai = AIMessagePromptTemplate.from_template("Argh me mateys")human_template="{text}"human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, example_human, example_ai, human_message_prompt])chain = LLMChain(llm=chat, prompt=chat_prompt)# 从格式化的消息中获取聊天完成结果chain.run("I love programming.")

输出:

"I be lovin' programmin', me hearty!"

系统消息

OpenAI提供了一个可选的name参数,我们也建议与系统消息一起使用以进行少量示例提示。以下是如何使用此功能的示例:

template="You are a helpful assistant that translates english to pirate."system_message_prompt = SystemMessagePromptTemplate.from_template(template)example_human = SystemMessagePromptTemplate.from_template("Hi", additional_kwargs={"name": "example_user"})example_ai = SystemMessagePromptTemplate.from_template("Argh me mateys", additional_kwargs={"name": "example_assistant"})human_template="{text}"human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, example_human, example_ai, human_message_prompt])chain = LLMChain(llm=chat, prompt=chat_prompt)# 从格式化的消息中获取聊天完成结果chain.run("I love programming.")

输出:

"I be lovin' programmin', me hearty!"

响应流式传输

本部分介绍了如何在聊天模型中使用流式传输:

from langchain.chat_models import ChatOpenAIfrom langchain.schema import (HumanMessage,)from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandlerchat = ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)resp = chat([HumanMessage(content="Write me a song about sparkling water.")])

输出:

Verse 1:Bubbles rising to the topA refreshing drink that never stopsClear and crisp, it's pure delightA taste that's sure to exciteChorus:Sparkling water, oh so fineA drink that's always on my mindWith every sip, I feel aliveSparkling water, you're my vibeVerse 2:No sugar, no calories, just pure blissA drink that's hard to resistIt's the perfect way to quench my thirstA drink that always comes firstChorus:Sparkling water, oh so fineA drink that's always on my mindWith every sip, I feel aliveSparkling water, you're my vibeBridge:From the mountains to the seaSparkling water, you're the keyTo a healthy life, a happy soulA drink that makes me feel wholeChorus:Sparkling water, oh so fineA drink that's always on my mindWith every sip, I feel aliveSparkling water, you're my vibeOutro:Sparkling water, you're the oneA drink that's always so much funI'll never let you go, my friendSparkling

参考文献:

[1] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:/

[2] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:/

大模型从入门到应用——LangChain:模型(Models)-[聊天模型(Chat Models):使用少量示例和响应流式传输]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。