设置
翻译
通用翻译器
语调变换
格式转换
拼写检查/语法检查。
转换
在本笔记中,我们将探索如何使用大型语言模型进行文本转换任务,例如语言翻译、拼写和语法检查、语气调整和格式转换。
设置
import openai
import osfrom dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env fileopenai.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"""
Translate the following English text to Spanish: ```Hi, I would like to order a blender```
"""
response = get_completion(prompt)
print(response)
告诉我这是什么语言:
prompt = f"""
Tell me which language this is:```Combien coûte le lampadaire?```
"""
response = get_completion(prompt)
print(response)
将以下文本翻译成法语、西班牙语和英语pirate:
prompt = f"""
Translate the following text to French and Spanish and English pirate: ```I want to order a basketball```
"""
response = get_completion(prompt)
print(response)
将以下文本以正式和非正式形式翻译成西班牙语:
prompt = f"""
Translate the following text to Spanish in both the formal and informal forms:
'Would you like to order a pillow?'
"""
response = get_completion(prompt)
print(response)
通用翻译器
想象一下,您在一家大型跨国电子商务公司负责 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", # 我的键盘有一个损坏的控制键"我的屏幕在闪烁" # 我的屏幕在闪烁
] 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")
语调变换
写作可以根据目标受众而有所不同。ChatGPT 可以产生不同的语调。
prompt = f"""
将以下俚语(slang)翻译成商业信函:
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
response = get_completion(prompt)
print(response)
chatgpt 输出:
格式转换
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)
# python程序 显示HTML格式的响应
from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))
chatgpt输出:
name | |
---|---|
Shyam | shyamjaiswal@gmail.com |
Bob | bob32@gmail.com |
Jai | jai87@gmail.com |
拼写检查/语法检查
这里有一些常见的语法和拼写问题的例子以及LLM's (大语言模型)的回应。
要向 LLM 发出 您希望它校对的您的文本的信号,您可以指示模型“校对”(proofread)或“校对并更正”(proofread and correct)。
#“那个带着黑白小狗的女孩玩得很开心。”,
# "Yolanda 有她的笔记本。",
# “这将是漫长的一天。汽车需要换油吗?”,
# “他们带走了我的自由。他们打算带上他们的行李箱。”,
# “你需要你的笔记本。”,
# “那药影响我的睡眠能力。你听说过蝴蝶效应吗?”,
# “这句话是为了检查 chatGPT 的拼写能力”
text = [ "The girl with the black and white puppies have 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 speling abilitty" # spelling拼写
]
for t in text:prompt = f"""校对并更正以下文本并重写更正后的版本。如果您没有找到错误,只需说“未发现错误”。不要在文本周围使用任何标点符号:```{t}```"""response = get_completion(prompt)print(response)
chatgpt 输入:
校对并更正以下逗号分隔的文本。如果您没有找到错误,只需说明第几条文本未发现错误。不要在文本周围使用任何标点符号: "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"chatgpt 输出:
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)
chatgpt 输入:
校对并更正此评论:```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.```
chatgpt 输出:
from redlines import Redlines
#将两个文本进行比较,并以Markdown格式显示差异
diff = Redlines(text,response)
display(Markdown(diff.output_markdown))
#校对并更正此评论。使其更具吸引力。确保它遵循 APA 风格指南并面向高级读者。以 markdown 格式输出。
#文本:
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}```
"""
response = get_completion(prompt)
display(Markdown(response))
chatgpt 输出:
将校对和更正的评论谷歌翻译成中文
那个带着黑白小狗的女孩正在玩一个舞会。
女孩和她的黑白小狗快乐玩耍的可爱景象给观察者带来了欢乐。
约兰达很珍惜她的笔记本。
约兰达 (Yolanda) 是一个很有条理和创造力的人,她紧握着她珍贵的笔记本,珍视着里面的内容。
这将是富有成效的一天。汽车需要更换机油吗?
随着一天的展开,会有一种使命感和期待感。是时候对汽车的机油进行必要的保养了吗?
我的自由就这样消失了。他们提着手提箱。
唉,这一刻已经到来,我的自由也随之而去。他们准备提着行李箱,象征着出发。
您将需要可靠的笔记本。
随身携带可信赖的笔记本是必不可少的。它的存在可确保您的工作做好准备并提高效率。
那药影响我的睡眠质量。你了解蝴蝶效应吗?
我服用的药物对我获得安宁睡眠的能力有显著影响。顺便问一下,你有没有遇到过蝴蝶效应的概念?
该短语旨在评估 ChatGPT 的拼写能力。
这个特定短语的目的是评估 ChatGPT 在准确拼写方面的熟练程度。
The End