【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.pswp.cn/news/696964.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/696964.shtml
英文地址,請注明出處:http://en.pswp.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…