一、引言
LLM非常擅长将输入转换成不同的格式,典型应用包括多语种文本翻译、拼写及语法纠正、语气调整、格式转换等。
本章节将介绍如何使用编程的方式,调用API接口来实现“文本转换”功能。
首先,我们需要OpenAI包,加载API密钥,定义getCompletion函数。
import openai
# 导入第三方库
import os
openai.api_key = "sk-..."
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"]
二、文本翻译
2.1 中文转西班牙语
prompt = f"""
将以下中文翻译成西班牙语: \
```您好,我想订购一个搅拌机。```
"""
response = get_completion(prompt)
print(response)
Hola, me gustaría ordenar una batidora.
2.2 识别语种
prompt = f"""
请告诉我以下文本是什么语种:
```Combien coûte le lampadaire?```
"""
response = get_completion(prompt)
print(response)
这段文本是法语。
2.3 多语种翻译
prompt = f"""
请将以下文本分别翻译成中文、英文、法语和西班牙语:
```I want to order a basketball.```
"""
response = get_completion(prompt)
print(response)
中文:我想订购一个篮球。
英文:I want to order a basketball.
法语:Je veux commander un ballon de basket.
西班牙语:Quiero pedir una pelota de baloncesto.
2.4 同时进行语气转换
prompt = f"""
请将以下文本翻译成中文,分别展示成正式与非正式两种语气:
```Would you like to order a pillow?```
"""
response = get_completion(prompt)
print(response)
正式语气:您是否需要订购一个枕头?
非正式语气:你想要订购一个枕头吗?
2.5 通用翻译器
随着全球化与跨境商务的发展,交流的用户可能来自各个不同的国家,使用不同的语言,因此我们需要一个通用翻译器,识别各个消息的语种,并翻译成目标用户的母语,从而实现更方便的跨国交流。
user_messages = [
"La performance du système est plus lente que d'habitude.", # System performance is slower than normal
"Mi monitor tiene píxeles que no se iluminan.", # My monitor has pixels that are not lighting
"Il mio mouse non funziona", # My mouse is not working
"Mój klawisz Ctrl jest zepsuty", # My keyboard has a broken control key
"我的屏幕在闪烁" # My screen is flashing
]
for issue in user_messages:
prompt = f"Tell me what language this is: ```{issue}```"
lang = get_completion(prompt)
print(f"Original message ({lang}): {issue}")
prompt = f"""
Translate the following text to English \
and Korean: ```{issue}```
"""
response = get_completion(prompt)
print(response, "\n")
Original message (The language is French.): La performance du système est plus lente que d’habitude.
The performance of the system is slower than usual.시스템의 성능이 평소보다 느립니다.
Original message (The language is Spanish.): Mi monitor tiene píxeles
que no se iluminan. English: “My monitor has pixels that do not light
up.”Korean: “내 모니터에는 밝아지지 않는 픽셀이 있습니다.”
Original message (The language is Italian.): Il mio mouse non funziona
English: “My mouse is not working.” Korean: “내 마우스가 작동하지 않습니다.”Original message (The language is Polish.): Mój klawisz Ctrl jest
zepsuty English: “My Ctrl key is broken” Korean: “내 Ctrl 키가 고장 났어요”Original message (The language is Chinese.): 我的屏幕在闪烁 English: My
screen is flickering. Korean: 내 화면이 깜박거립니다.
import time
for issue in user_messages:
time.sleep(20)
prompt = f"告诉我以下文本是什么语种,直接输出语种,如法语,无需输出标点符号: ```{issue}```"
lang = get_completion(prompt)
print(f"原始消息 ({lang}): {issue}\n")
prompt = f"""
将以下消息分别翻译成英文和中文,并写成
中文翻译:xxx
英文翻译:yyy
的格式:
```{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 do not light up.
=========================================
原始消息 (意大利语): Il mio mouse non funziona中文翻译:我的鼠标不工作
英文翻译:My mouse is not working=========================================
原始消息 (这段文本是波兰语。): Mój klawisz Ctrl jest zepsuty中文翻译:我的Ctrl键坏了
英文翻译:My Ctrl key is broken
=========================================
原始消息 (中文): 我的屏幕在闪烁中文翻译:我的屏幕在闪烁
英文翻译:My screen is flickering.=========================================
三、语气与写作风格调整
写作的语气往往会根据受众对象而有所调整。例如,对于工作邮件,我们常常需要使用正式语气与书面用词,而对同龄朋友的微信聊天,可能更多地会使用轻松、口语化的语气。
prompt = f"""
将以下文本翻译成商务信函的格式:
```小老弟,我小羊,上回你说咱部门要采购的显示器是多少寸来着?```
"""
response = get_completion(prompt)
print(response)
尊敬的先生/女士,
我是小羊,我希望能够向您确认一下我们部门需要采购的显示器尺寸是多少寸。上次我们交谈时,您提到了这个问题。
期待您的回复。
谢谢!
此致,
小羊
四、文件格式转换
ChatGPT非常擅长不同格式之间的转换,例如JSON到HTML、XML、Markdown等。在下述例子中,我们有一个包含餐厅员工姓名和电子邮件的列表的JSON,我们希望将其从JSON转换为HTML。
prompt = f"""
将以下Python字典从JSON转换为HTML表格,保留表格标题和列名:{data_json}
"""
response = get_completion(prompt)
print(response)
from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))
五、拼写及语法纠正
拼写及语法的检查与纠正是一个十分常见的需求,特别是使用非母语语言,例如,在论坛发帖时,或发表英文论文时,校对是一件十分重要的事情。
下述例子给定了一个句子列表,其中有些句子存在拼写或语法问题,有些则没有,我们循环遍历每个句子,要求模型校对文本,如果正确则输出“未发现错误”,如果错误则输出纠正后的文本。
text = [
"The girl with the black and white puppies have a ball.", # The girl has a ball.
"Yolanda has her notebook.", # ok
"Its going to be a long day. Does the car need it’s oil changed?", # Homonyms
"Their goes my freedom. There going to bring they’re suitcases.", # Homonyms
"Your going to need you’re notebook.", # Homonyms
"That medicine effects my ability to sleep. Have you heard of the butterfly affect?", # Homonyms
"This phrase is to cherck chatGPT for spelling abilitty" # spelling
]
for i in range(len(text)):
time.sleep(20)
prompt = f"""请校对并更正以下文本,注意纠正文本保持原始语种,无需输出原始文本。
如果您没有发现任何错误,请说“未发现错误”。
例如:
输入:I are happy.
输出:I am happy.
```{text[i]}```"""
response = get_completion(prompt)
print(i, response)
0 The girl with the black and white puppies has a ball.
1 Yolanda has her notebook.
2 It’s going to be a long day. Does the car need its oil changed?
3 Their goes my freedom. There going to bring their suitcases.
4 You’re going to need your notebook.
5 That medicine affects my ability to sleep. Have you heard of the butterfly effect?
6 This phrase is to check chatGPT for spelling ability.
以下是一个简单的语法纠错示例(译注:与 Grammarly 功能类似),输入文本为一段关于熊猫玩偶的评价,输出为纠正后的文本。本例使用的 Prompt 较为简单,你也可以进一步要求进行语调的更改。
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. It’s also a bit smaller than I expected for the price. I think there might be other options that are bigger for the same price. On the bright side, it arrived a day earlier than expected, so I got to play with it myself before giving it to my daughter.
引入 Redlines 包,详细显示并对比纠错过程:
# 如未安装redlines,需先安装
!pip3.8 install redlines
from redlines import Redlines
from IPython.display import display, Markdown
diff = Redlines(text,response)
display(Markdown(diff.output_markdown))
六、综合样例
下述例子展示了同一段评论,用一段prompt同时进行文本翻译+拼写纠正+风格调整+格式转换。
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"""
proofread and correct this review. Make it more compelling.
Ensure it follows APA style guide and targets an advanced reader.
Output in markdown format.
Text: ```{text}```
"""
# 校对注:APA style guide是APA Style Guide是一套用于心理学和相关领域的研究论文写作和格式化的规则。
# 它包括了文本的缩略版,旨在快速阅读,包括引用、解释和参考列表,
# 其详细内容可参考:https://apastyle.apa.org/about-apa-style
# 下一单元格内的汉化prompt内容由译者进行了本地化处理,仅供参考
response = get_completion(prompt)
display(Markdown(response))
prompt = f"""
针对以下三个反引号之间的英文评论文本,
首先进行拼写及语法纠错,
然后将其转化成中文,
再将其转化成优质淘宝评论的风格,从各种角度出发,分别说明产品的优点与缺点,并进行总结。
润色一下描述,使评论更具有吸引力。
输出结果格式为:
【优点】xxx
【缺点】xxx
【总结】xxx
注意,只需填写xxx部分,并分段输出。
将结果输出成Markdown格式。
```{text}```
"""
response = get_completion(prompt)
display(Markdown(response))