【OpenAI官方课程】第五课:ChatGPT文本转换Transforming

欢迎来到ChatGPT 开发人员提示工程课程(ChatGPT Prompt Engineering for Developers)!本课程将教您如何通过OpenAI API有效地利用大型语言模型(LLM)来创建强大的应用程序。

本课程由OpenAI 的Isa Fulford和 DeepLearning.AI 的Andrew Ng主讲,深入了解 LLM 的运作方式,提供即时工程的最佳实践,并演示 LLM API 在各种应用程序中的使用。

在本课程笔记本中,我们将探讨如何使用大型语言模型进行文本转换任务,例如语言翻译、拼写和语法检查、语气调整以及格式转换。

设置

import openai
import osfrom dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # 读取本地的.env文件openai.api_key  = os.getenv('OPENAI_API_KEY')
def get_completion(prompt, model="gpt-3.5-turbo", temperature=0): messages = [{"role": "user", "content": prompt}]response = openai.ChatCompletion.create(model=model,messages=messages,temperature=temperature, )return response.choices[0].message["content"]

翻译

ChatGPT是通过许多语言的来源进行训练的。这使得该模型具有翻译能力。以下是如何使用这种能力的一些示例。

prompt = f"""
将以下英文文本翻译成西班牙文:\ 
```Hi, I would like to order a blender```
"""
response = get_completion(prompt)
print(response)

Hola, me gustaría ordenar una licuadora.

prompt = f"""
告诉我这是哪种语言: 
```Combien coûte le lampadaire?```
"""
response = get_completion(prompt)
print(response)

这是法语。

prompt = f"""
将以下文本翻译成法语、西班牙语和英语: \
```I want to order a basketball```
"""
response = get_completion(prompt)
print(response)

法语: Je veux commander un ballon de basket
西班牙语: Quiero pedir una pelota de baloncesto
英语: I want to order a basketball

prompt = f"""
将以下文本翻译成西班牙语的正式和非正式形式: 
'Would you like to order a pillow?'
"""
response = get_completion(prompt)
print(response)

正式: ¿Le gustaría ordenar una almohada?
非正式: ¿Te gustaría ordenar una almohada?

通用翻译器

假设你负责一个大型跨国电子商务公司的IT部门。用户用各自的母语给你发消息,反映他们遇到的IT问题。你的员工来自世界各地,只会说他们的母语。你需要一个通用翻译器!

user_messages = ["La performance du système est plus lente que d'habitude.",  # 系统性能比平时慢。"Mi monitor tiene píxeles que no se iluminan.",              # 我的显示器有像素点不亮。"Il mio mouse non funziona",                                 # 我的鼠标不工作"Mój klawisz Ctrl jest zepsuty",                             # 我的Ctrl键坏了"我的屏幕在闪烁"                                               # 我的屏幕在闪烁
] 
for issue in user_messages:prompt = f"告诉我这是哪种语言: ```{issue}```"lang = get_completion(prompt)print(f"原始消息 ({lang}): {issue}")prompt = f"""将以下文本翻译成英文和韩文: ```{issue}```"""response = get_completion(prompt)print(response, "\n")

原始消息 (这是法语.): La performance du système est plus lente que d’habitude.
英文: The system performance is slower than usual.
韩文: 시스템 성능이 평소보다 느립니다.
原始消息 (这是西班牙语.): Mi monitor tiene píxeles que no se iluminan.
英文: My monitor has pixels that don’t light up.
韩文: 내 모니터에는 불이 켜지지 않는 픽셀이 있습니다.
原始消息 (这是意大利语.): Il mio mouse non funziona
英文: My mouse is not working.
韩文: 내 마우스가 작동하지 않습니다.
原始消息 (这是波兰语.): Mój klawisz Ctrl jest zepsuty
英文: My Ctrl key is broken.
韩文: 제 Ctrl 키가 고장 났어요.
原始消息 (这是中文(简体).): 我的屏幕在闪烁
英文: My screen is flickering.
韩文: 내 화면이 깜빡입니다.

文本语气转换

写作可以根据预期受众而变化。ChatGPT可以产生不同的语气。

prompt = f"""
将以下俚语转换成商务信函: 
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
response = get_completion(prompt)
print(response)

尊敬的先生/女士,

我写信是想向您介绍一款我认为可能对您有兴趣的落地灯。请查看附件中的规格以供审阅。

谢谢您的时间和考虑。

真诚地,

Joe

格式转换

ChatGPT可以在不同格式之间进行转换。提示应描述输入和输出格式。

data_json = { "resturant employees" :[ {"name":"Shyam", "email":"shyamjaiswal@gmail.com"},{"name":"Bob", "email":"bob32@gmail.com"},{"name":"Jai", "email":"jai87@gmail.com"}
]}
prompt = f"""
将以下python字典从JSON转换为带有列标题和标题的HTML表格: {data_json}
"""
response = get_completion(prompt)
print(response)

Output:

<table><caption>Restaurant Employees</caption><thead><tr><th>Name</th><th>Email</th></tr></thead><tbody><tr><td>Shyam</td><td>shyamjaiswal@gmail.com</td></tr><tr><td>Bob</td><td>bob32@gmail.com</td></tr><tr><td>Jai</td><td>jai87@gmail.com</td></tr></tbody>
</table>
from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))
Restaurant Employees
NameEmail
Shyamshyamjaiswal@gmail.com
Bobbob32@gmail.com
Jaijai87@gmail.com

拼写检查/语法检查。

以下是常见语法和拼写问题以及LLM的响应的一些示例。

要向LLM发出信号,指示它对您的文本进行校对,您可以指示模型“校对”或“校对和纠正”。

text = [ "The girl with the black and white puppies have a ball.",  # 这个女孩有一个球。"Yolanda has her notebook.", # 没问题"Its going to be a long day. Does the car need it’s oil changed?",  # 同音异形词"Their goes my freedom. There going to bring they’re suitcases.",  # 同音异形词"Your going to need you’re notebook.",  # 同音异形词"That medicine effects my ability to sleep. Have you heard of the butterfly affect?", # 同音异形词"This phrase is to cherck chatGPT for speling abilitty"  # 拼写
]
for t in text:prompt = f"""校对并纠正以下文本并重写已更正的版本。如果您找不到错误,只需说“未发现错误”。在文本周围不要使用任何标点符号:```{t}```"""response = get_completion(prompt)print(response)

The girl with the black and white puppies has a ball.
No errors found.
It’s going to be a long day. Does the car need its oil changed?
Their goes my freedom. There going to bring they’re suitcases.
Corrected version:
There goes my freedom. They’re going to bring their suitcases.
You’re going to need your notebook.
That medicine affects my ability to sleep. Have you heard of the butterfly effect?
This phrase is to check ChatGPT for spelling ability.

text = f"""
Got this for my daughter for her birthday cuz she keeps taking \
mine from my room.  Yes, adults also like pandas too.  She takes \
it everywhere with her, and it's super soft and cute.  One of the \
ears is a bit lower than the other, and I don't think that was \
designed to be asymmetrical. It's a bit small for what I paid for it \
though. I think there might be other options that are bigger for \
the same price.  It arrived a day earlier than expected, so I got \
to play with it myself before I gave it to my daughter.
"""
prompt = f"校对并纠正此评论:```{text}```"
response = get_completion(prompt)
print(response)

I got this for my daughter’s birthday because she keeps taking mine from my room. Yes, adults also like pandas too. She takes it everywhere with her, and it’s super soft and cute. However, one of the ears is a bit lower than the other, and I don’t think that was designed to be asymmetrical. Additionally, it’s a bit small for what I paid for it. I think there might be other options that are bigger for the same price. On the positive side, it arrived a day earlier than expected, so I got to play with it myself before I gave it to my daughter.

prompt = f"""
校对并纠正此评论。使其更具吸引力。 
确保它遵循APA风格指南并针对高级读者。 
以markdown格式输出。
Text: ```{text}```
"""
response = get_completion(prompt)
display(Markdown(response))

Title: A Soft and Cute Panda Plush Toy for All Ages
Introduction: As a parent, finding the perfect gift for your child’s birthday can be a daunting task. However, I stumbled upon a soft and cute panda plush toy that not only made my daughter happy but also brought joy to me as an adult. In this review, I will share my experience with this product and provide an honest assessment of its features.
Product Description: The panda plush toy is made of high-quality materials that make it super soft and cuddly. Its cute design is perfect for children and adults alike, making it a versatile gift option. The toy is small enough to carry around, making it an ideal companion for your child on their adventures.
Pros: The panda plush toy is incredibly soft and cute, making it an excellent gift for children and adults. Its small size makes it easy to carry around, and its design is perfect for snuggling. The toy arrived a day earlier than expected, which was a pleasant surprise.
Cons: One of the ears is a bit lower than the other, which makes the toy asymmetrical. Additionally, the toy is a bit small for its price, and there might be other options that are bigger for the same price.
Conclusion: Overall, the panda plush toy is an excellent gift option for children and adults who love cute and cuddly toys. Despite its small size and asymmetrical design, the toy’s softness and cuteness make up for its shortcomings. I highly recommend this product to anyone looking for a versatile and adorable gift option.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/696964.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

缓存篇—缓存雪崩

什么是缓存雪崩 通常我们为了保证缓存中的数据与数据库中的数据一致性&#xff0c;会给 Redis 里的数据设置过期时间&#xff0c;当缓存数据过期后&#xff0c;用户访问的数据如果不在缓存里&#xff0c;业务系统需要重新生成缓存&#xff0c;因此就会访问数据库&#xff0c;并…

QEMU源码全解析 —— virtio(22)

接前一篇文章&#xff1a;QEMU源码全解析 —— virtio&#xff08;21&#xff09; 前几回讲解了virtio驱动的加载。本回开始讲解virtio驱动的初始化。 在讲解virtio驱动的初始化之前&#xff0c;先要介绍virtio配置的函数集合变量virtio_pci_config_ops。实际上前文书也有提到…

c# HttpCookie操作,建立cookie工具类

HttpCookie 是一个在.NET Framework中用于管理和操作HTTP Cookie的类。它提供了一种方便的方式来创建、设置、读取和删除Cookie。 Cookie是一种在客户端和服务器之间传递数据的机制&#xff0c;用于跟踪用户的会话状态和存储用户相关的信息。它通常由服务器发送给客户端&#…

万字干货-京东零售数据资产能力升级与实践

开篇 京东自营和商家自运营模式&#xff0c;以及伴随的多种运营视角、多种组合计算、多种销售属性等数据维度&#xff0c;相较于行业同等量级&#xff0c;数据处理的难度与复杂度都显著增加。如何从海量的数据模型与数据指标中提升检索数据的效率&#xff0c;降低数据存算的成…

parallels配置centos虚拟环境

parallels Desktop M1/M2芯片Parallels Desktop 19虚拟机安装使用教程&#xff08;超详细&#xff09;-CSDN博客 下镜像记得找和mac芯片匹配的 安装就选第一个centos7不要选第二个 安装有问题就选回退重启 parallel desktop 18/19安装centos7.2009教程_parallels desktop 19…

echarts多y轴样式重叠问题

1、主要属性设置 yAxis: [{//y轴1nameTextStyle: {align: "right",padding: 0}},{//y轴2nameTextStyle: {align: "left",padding: 0}},{//y轴3axisLabel: {margin: 50},nameTextStyle: {align: "left",padding: [0, 0, 0, 50]},axisPointer: {l…

Python Web开发记录 Day2:CSS

名人说&#xff1a;莫道桑榆晚&#xff0c;为霞尚满天。——刘禹锡&#xff08;刘梦得&#xff0c;诗豪&#xff09; 创作者&#xff1a;Code_流苏(CSDN)&#xff08;一个喜欢古诗词和编程的Coder&#x1f60a;&#xff09; 目录 二、CSS1、CSS-初始入门①快速了解②CSS应用方式…

【C语言】sizeof()函数

前言 sizeof函数用于获取数据类型或变量在内存中所占的字节数。 sizeof函数返回的是编译时确定的值&#xff0c;不会计算动态分配的内存大小。 sizeof函数可以用于多种类型的数据&#xff0c;包括数组、指针、结构体、枚举等。 1.数组 int arr[5];printf("%zu ", siz…

文件上传与下载

文件上传与下载 1. 文件上传 为了能上传文件&#xff0c;必须将表单的 method 设置为 POST&#xff0c;并将 enctype 设置为 multipart/form-data 。 有两种实现文件上传的方式&#xff1a; 底层使用 Apache Commons FileUpload 包 底层使用 Servlet 3.1 内置的文件上传功能…

如何计算文件哈希值(MD5值)

生成文件hash值的用途 哈希值&#xff0c;即HASH值&#xff0c;是通过对文件内容进行加密运算得到的一组二进制值&#xff0c;主要用途是用于文件校验或签名。正是因为这样的特点&#xff0c;它常常用来判断两个文件是否相同。 比如&#xff0c;从网络上下载某个文件&#xff0…

MySQL主从同步

MySQL主从同步&#xff08;复制&#xff09;是一种数据复制技术&#xff0c;用于将数据从一个MySQL数据库&#xff08;称为“主”&#xff09;复制到另一个或多个MySQL数据库&#xff08;称为“从”&#xff09;。这个过程通常用于负载均衡、数据备份、灾难恢复和其他类似场景。…

C++ Primer Plus 笔记(持续更新)

编译器的正解 数据&#xff0b;算法程序 赋值从右向左进行 cin&#xff0c;cout的本质也是对象 类和对象的解释

centerOS docker搭建flowable,流程引擎

1、准备一个mysql数据库&#xff0c;库名为flowable 2、mysql驱动下载&#xff0c;下载地址为&#xff1a; https://mvnrepository.com/artifact/mysql/mysql-connector-java此处使用的是8.0.22版本的驱动&#xff0c;且数据库必须使用版本8&#xff0c;否则第二次启动报错 3、…

OpenAI文生视频大模型Sora概述

Sora&#xff0c;美国人工智能研究公司OpenAI发布的人工智能文生视频大模型&#xff08;但OpenAI并未单纯将其视为视频模型&#xff0c;而是作为“世界模拟器” &#xff09;&#xff0c;于2024年2月15日&#xff08;美国当地时间&#xff09;正式对外发布。 Sora可以根据用户…

samber/lo 库的使用方法:type

samber/lo 库的使用方法&#xff1a;type samber/lo 是一个 Go 语言库&#xff0c;提供了一些常用的集合操作函数&#xff0c;如 Filter、Map 和 FilterMap。 这个库函数太多&#xff0c;因此我决定按照功能分别介绍&#xff0c;本文介绍的是 samber/lo 库中type相关的函数。汇…

Redis中的AOF重写到底是怎么一回事

首先我们知道AOF和RDB都是Redis持久化的方法。RDB是Redis DB&#xff0c;一种二进制数据格式&#xff0c;这样就是相当于全量保存数据快照了。AOF则是保存命令&#xff0c;然后恢复的时候重放命令。 AOF随着时间推移&#xff0c;会越来越大&#xff0c;因为不断往里追加命令。…

哪些行业适合做小程序?零售电商、餐饮娱乐、旅游酒店、教育生活、医疗保健、金融社交、体育健身、房产汽车、企管等,你的行业在其中么?

引言 在当今数字化时代&#xff0c;小程序成为了各行各业快速发展的数字工具之一。它的轻便、灵活的特性使得小程序在多个行业中找到了广泛的应用。本文将探讨哪些行业适合开发小程序&#xff0c;并介绍各行业中小程序的具体应用。 一、零售和电商 在当今数字化的商业环境中&…

C++ RAII

RAII定义 RAII&#xff08;Resource Acquisition Is Initialization&#xff09;是C编程中的一种重要的资源管理技术。它的核心思想是&#xff1a;资源的获取应该在对象的构造阶段进行&#xff0c;而资源的释放则应该在对象的析构阶段进行。通过利用C对象的生命周期和析构函数…

C#之WPF学习之路(2)

目录 控件的父类 DispatcherObject类 DependencyObject类 DependencyObject 类的关键成员和方法 Visual类 Visual 类的主要成员和方法 UIElement类 UIElement 类的主要成员和功能 FrameworkElement类 FrameworkElement 类的主要成员和功能 控件的父类 在 WPF (Windo…

谷粒商城篇章9 ---- P248-P261/P292-P294 ---- 消息队列【分布式高级篇六】

目录 1 消息队列(Message Queue)简介 1.1 概述 1.2 消息服务中两个重要概念 1.3 消息队列主要有两种形式的目的地 1.4 JMS和AMQP对比 1.5 应用场景 1.6 Spring支持 1.7 SpringBoot自动配置 1.7 市面上的MQ产品 2 RabbitMQ 2.1 RabbitMQ简介 2.1.1 RabbitMQ简介 2…