prompt = f"""
分别将以下句子翻译为英语、法语、西班牙语、中文:
```I want to order a basketball```
"""
response = get_completion(prompt)
print(response)
输出:
代码语言:javascript复制
英语:I want to order a basketball.
法语:Je veux commander un ballon de basket.
西班牙语:Quiero pedir una pelota de baloncesto.
中文:我想订购一个篮球。
代码语言:javascript复制
prompt = f"""
使用正式和非正式的口气翻译下面的句子为中文:
'Would you like to order a pillow?'
"""
response = get_completion(prompt)
print(response)
输出:
代码语言:javascript复制
正式口气:您是否需要订购一个枕头?
非正式口气:你要不要订一个枕头?
2. 通用翻译器
代码语言:javascript复制
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"""
翻译以下文本为中文和俄文: ```{issue}```
"""
response = get_completion(prompt)
print(response, "n")
输出:
代码语言:javascript复制
Original message (This is French.): La performance du système est plus lente que d'habitude.
中文:系统性能比平常慢。
俄文:Системная производительность медленнее, чем обычно.
Original message (This is Spanish.): Mi monitor tiene píxeles que no se iluminan.
我的显示器有不亮的像素。
Мой монитор имеет пиксели, которые не светятся.
Original message (This is Italian.): Il mio mouse non funziona
我的鼠标不工作。
Моя мышь не работает.
Original message (This is Polish.): Mój klawisz Ctrl jest zepsuty
我的Ctrl键坏了
Моя клавиша Ctrl сломана
Original message (This is Chinese (Simplified).): 我的屏幕在闪烁
中文:我的屏幕在闪烁。
俄文:Мой экран мигает.
3. 风格改写
代码语言:javascript复制
prompt = f"""
Translate the following from slang to a business letter:
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
response = get_completion(prompt)
print(response)
输出:
代码语言:javascript复制
Dear Sir/Madam,
I am writing to bring to your attention a standing lamp that I believe may be of interest to you. Please find attached the specifications for your review.
Thank you for your time and consideration.
Sincerely,
Joe
代码语言:javascript复制
prompt = f"""
使用一首中国的诗词来表达下面的句子:
'The little girl propped up a small boat and
stole the white lotus back.
Without understanding the hidden traces,
the duckweed drove along'
"""
response = get_completion(prompt)
print(response)
输出:
代码语言:javascript复制
小姑娘撑起小船,偷了白莲回。
不解其中蕴含,萍水随波逐。
4. 格式转化
代码语言:javascript复制
data_json = { "resturant employees" :[
{"name":"Shyam", "email":"shyamjaiswal@gmail.com"},
{"name":"Bob", "email":"bob32@gmail.com"},
{"name":"Jai", "email":"jai87@gmail.com"}
]}
prompt = f"""
Translate the following python dictionary from JSON to an HTML
table with column headers and title: {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 speling abilitty" # spelling
]
for t in text:
prompt = f"""Proofread and correct the following text
and rewrite the corrected version. If you don't find
and errors, just say "No errors found". Don't use
any punctuation around the text:
```{t}```"""
response = get_completion(prompt)
print(response)
输出:
代码语言:javascript复制
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.
代码语言:javascript复制
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: ```{text}```"
response = get_completion(prompt)
print(response)
输出:
代码语言:javascript复制
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.
代码语言:javascript复制
from redlines import Redlines
diff = Redlines(text,response)
display(Markdown(diff.output_markdown))
Title: A Soft and Cute Panda Plush Toy for All Ages
I purchased this panda plush toy for my daughter's birthday because she kept taking mine from my room.
As an adult, I also appreciate the cuteness of pandas.
My daughter takes it everywhere with her, and it's incredibly soft and adorable.
However, I did notice that one of the ears is slightly lower than the other, which I don't think was intended to be asymmetrical.
While I love the quality of the toy, I do think it's a bit small for the price I paid.
I believe there are other options available that are larger for the same price.
Nonetheless, the toy arrived a day earlier than expected, which gave me the opportunity to play with it myself before giving it to my daughter.
Overall, I highly recommend this panda plush toy for anyone who loves soft and cute stuffed animals.
It's perfect for children and adults alike.
6. 扩写
生成客户服务电子邮件,邮件是根据每个客户的评论量身定制的
代码语言:javascript复制
# given the sentiment from the lesson on "inferring",
# and the original customer message, customize the email
sentiment = "负面"
# review for a blender
review = f"""
所以,他们仍然有季节性的17件套
11月份的售价约为49美元
半价,但出于某种原因(称之为哄抬价格)
大约在12月的第二周,价格都上涨了
最高可达70美元至89美元不等
系统11件套系统的价格上涨了约10美元或
因此,价格也与之前29美元的售价不同
所以它看起来不错,但如果你看看底座
刀片锁定位置看起来不太好
就像几年前的版本一样,但我
计划对它非常温和(例如,我喜欢
非常坚硬的物品,如豆类、冰块、大米等
先用搅拌机搅拌,然后把它们按份量粉碎
我要加入搅拌机,然后换成搅打
切出更细的面粉,并使用横切刀片
制作冰沙时先用平刀片
如果我需要更细/更少浆的话)。制作时的特殊提示
冰沙,切细并冷冻水果
蔬菜(如果用菠菜稍微炖一下,会软化
菠菜然后冷冻,直到可以使用,如果制作的话
冰糕,使用中小型食品加工机)
你计划以这种方式使用,你可以避免添加
如果在做奶昔的时候有很多冰的话
大约一年后,发动机发出了奇怪的声音
我打电话给客服,但保修期已过
已经了,所以我不得不再买一个。仅供参考:总体
这些类型的产品质量都很差,所以
他们有点指望品牌知名度和
消费者忠诚度以维持销售额。大约收到了两天。
"""
prompt = f"""
你是一名客服人工智能助理。
您的任务是向有价值的客户发送电子邮件回复。
给定由```分隔的客户电子邮件生成回复以感谢客户的评价。
如果情绪是积极的或中立的,感谢他们他们的评论。
如果情绪消极,道歉并建议他们可以联系客户服务。
确保使用评论中的具体细节。用简洁而专业的语气写作。
在电子邮件上签名为“AI客户代理”。
顾客评价: ```{review}```
评价情绪: {sentiment}
"""
response = get_completion(prompt)
print(response)
temperature = 0, 输出内容 比较稳定
否则, temperature 越大,内容越多样化
7. 聊天
代码语言:javascript复制
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
# this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
# this is the degree of randomness of the model's output
)
# print(str(response.choices[0].message))
return response.choices[0].message["content"]