您现在的位置是:首页 >技术交流 >Openai+Coursera: ChatGPT Prompt Engineering(四)网站首页技术交流

Openai+Coursera: ChatGPT Prompt Engineering(四)

-派神- 2024-06-19 12:01:01
简介Openai+Coursera: ChatGPT Prompt Engineering(四)

想和大家分享一下最近学习的Coursera和openai联合打造ChatGPT Prompt Engineering在线课程.以下是我写的关于该课程的前两篇博客:

  1. ChatGPT Prompt Engineering(一)
  2. ChatGPT Prompt Engineering(二)
  3. ChatGPT Prompt Engineering(三)

今天我们来学习第三部分内容:转换(Transforming)。

转换(Transforming)

今天我们将探索如何使用大型语言模型进行文本转换任务,例如语言翻译、拼写和语法检查、语气调整和格式转换。

首先我们需要设置通过API来访问ChatGPT的主要代码:

import openai
 
openai.api_key ='YOUR_OPENAI_API_KEY'

 为防止出现Rate limits问题,我们需要安装一个为回退和重试提供函数装饰器的库 backoff

# pip install backoff
import backoff  

@backoff.on_exception(backoff.expo, openai.error.RateLimitError)
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)

 下面是让ChatGP识别文本是属于哪种语言:

prompt = f"""
Tell me which language this is: 
```Combien coûte le lampadaire?```
"""
response = get_completion(prompt)
print(response)

 将文本翻译成法语、西班牙语和英语:

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.",  # 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 Chinese: ```{issue}```
    """
    response = get_completion(prompt)
    print(response, "
")

 语调转换

ChatGPT可以根据不同的受众输出不同语调的内容。下面是一段英语俚语的对白,我们要求ChatGPT将其转换成商业信函语调。

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)

 格式转换

ChatGPT 可以在不同的格式之间进行转换。 提示语应描述输入和输出格式。下面我们要将一个来自于JSON格式的python字典转换为HTML格式:

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)

接下来我们要展示这个HTML页面:

from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))

 

 拼写检查/语法检查

这里我们有一些常见的语法和拼写问题的例子以及大型语言模型(LLM)的回应。

要向 LLM 发出您希望它校对您的文本的信号,您可以指示模型“校对”或“校对并更正”。下面的几段文本中存在语法错误或者拼写错误,我们要让ChatGPT来检查并纠错文本中的错误。

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)

下面我们尝试让ChatGPT来对中文的句子进行纠错,看看它是否能找出错误:

text = [ 
  "我们的公司是一家专业的电脑唯修,服务态度好,价格公道",  
  "他们在学校里学习了很多关于历史,数学和科学呢的知识。", 
  "我们去了一家新开的餐厅,才很好吃,服务员也很友好。", 
  "你得先做完作业,在出去玩!",  
  "那青山碧岭中随风起伏的阵阵林涛声,仿佛是大自然奏响的一枝交响乐.",  
  "七夕,只是牛郎与织女的节日,与俄们这些凡人无关。", 
  "我的人生有两件非常幸运的事情,一个是遇见了你,一个是将和你走过余生。"  
]
for t in text:
    prompt = f"""检查一下下面的本文是否存在错别字或者语法错误,如果有错误就重新改写句子。
    你在回复的时候必须输出2个句子,一句是原始文本,另一句是修改后的文本,如果没有发现任何错误,就说“没有找到错误”。

    请使用以下格式输出:
    原始文本:这里是原始文本
    修改后的文本:这里是修改后的文本
    
    原始文本:{t}"""
    response = get_completion(prompt)
    print(response)
    print()

 总体感觉ChatGPT对中文的错别字还是比较熟悉,对一些很明显的错别字纠错成功率可以达到100%,但是对于最后一句,我们运行了多次程序后发现有时候ChatGPT对最后一句的回复是:没有找到错误。大家可以尝试一下看看ChatGPT对中文错别字的识别率到底有多少。

下面我们给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"proofread and correct this review: ```{text}```"
response = get_completion(prompt)
print(response)

 由于短文内容比较长,我们使用一个校对工具来看看ChatGPT到底做了哪些修改:

from redlines import Redlines

diff = Redlines(text,response)
display(Markdown(diff.output_markdown))

 这里我们可以清晰的发现ChatGPT对原文做的修改。

下面我们让ChatGPT将上述原文修改为APA风格的文章(APA 风格是学术期刊文章和书籍等学术文件的写作风格和格式)

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实现多语言翻译功能、语言识别、错别字/语法检查、语调转换等功能。希望今天的内容能对大家有所帮助!

如果你觉得我的文章对你有所帮助,请帮我买杯咖啡!

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。