当前位置: 首页 > news >正文

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

分类目录:《自然语言处理从入门到应用》总目录


使用少量示例

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

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

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

from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain
from 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 ChatOpenAI
from langchain.schema import (HumanMessage,
)
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
chat = ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)
resp = chat([HumanMessage(content="Write me a song about sparkling water.")])

输出:

Verse 1:
Bubbles rising to the top
A refreshing drink that never stops
Clear and crisp, it's pure delight
A taste that's sure to exciteChorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibeVerse 2:
No sugar, no calories, just pure bliss
A drink that's hard to resist
It's the perfect way to quench my thirst
A drink that always comes firstChorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibeBridge:
From the mountains to the sea
Sparkling water, you're the key
To a healthy life, a happy soul
A drink that makes me feel wholeChorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibeOutro:
Sparkling water, you're the one
A drink that's always so much fun
I'll never let you go, my friend
Sparkling

参考文献:
[1] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/
[2] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/

http://www.lryc.cn/news/101486.html

相关文章:

  • Java在线OJ项目(三)、前后端交互API模块
  • 项目——负载均衡在线OJ
  • idea连接远程服务器上传war包文件
  • 使用PyGWalker可视化分析表格型数据
  • Visual C++中的虚函数和纯虚函数(以外观设计模式为例)
  • 电子元器件选型与实战应用—01 电阻选型
  • javascript 模板引擎
  • 【数据结构】带头+双向+循环链表(DList)(增、删、查、改)详解
  • 接口自动化测试平台
  • 【物联网】微信小程序接入阿里云物联网平台
  • PKG内容查看工具:Suspicious Package for Mac安装教程
  • 第16节:R语言医学分析实例:肺切除手术的Apriori关联规则分析
  • ChatGPT+MidJourney 3分钟生成你的动画故事
  • 在CSDN学Golang云原生(Kubernetes Pod调度)
  • Rust vs Go:常用语法对比(七)
  • 【HarmonyOS】API6使用storage实现轻量级数据存储
  • Python Flask构建微信小程序订餐系统 (十二)
  • C++——模板的作用2:特例化
  • Python Web开发技巧VII
  • LaTex4【下载模板、引入文献】
  • 【VSCode部署模型】导出TensorFlow2.X训练好的模型信息
  • windows环境下,安装elasticsearch
  • Elasticsearch入门笔记(一)
  • 记一次安装nvm切换node.js版本实例详解
  • 生态共建丨YashanDB与构力科技完成兼容互认证
  • React从入门到实战-react脚手架,消息订阅与发布
  • 从零构建深度学习推理框架-1 简介和Tensor
  • 使用WGCLOUD监测安卓(Android)设备的运行状态
  • C++笔记之迭代器失效问题处理
  • Tomcat的startup.bat文件出现闪退问题