ChatGPT Prompting开发实战(十)

一、关于文本内容翻译/语法检查/格式转换等相关任务的prompt开发

使用LLM可以完成以下关于文本内容的任务:

-识别文本内容使用的语言,并且根据需要翻译为另外的语言

-输入一段文本,让模型来检查是否有拼写或者语法方面的错误

-对文本内容的格式进行转换

接下来会给出具体示例,通过调用模型“gpt-3.5-turbo”来演示并解析如何针对上述需求来编写相应的prompts。

二、结合案例演示解析如何使用prompt进行文本翻译

首先给出一段英文文本,需要翻译为西班牙语:

prompt示例如下:

prompt = f"""

Translate the following English text to Spanish: \

```Hi, I would like to order a blender```

"""

response = get_completion(prompt)

print(response)

打印输出结果如下:

Hola, me gustaría ordenar una licuadora.

接下来修改prompt,要求模型分析所给的文本使用什么语言。

prompt示例如下:

prompt = f"""

Tell me which language this is:

```Combien coûte le lampadaire?```

"""

response = get_completion(prompt)

print(response)

打印输出结果如下:

This language is French.

接下来的需求是让模型把英文文本同时翻译成法语和西班牙语。

prompt示例如下:

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)

打印输出结果如下:

French: ```Je veux commander un ballon de basket```

Spanish: ```Quiero ordenar una pelota de baloncesto```

English: ```I want to order a basketball```

下面修改prompt,要求模型翻译文本时考虑使用正式用语和非正式用语。

prompt示例如下:

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)    

打印输出结果如下:

Formal: ¿Le gustaría ordenar una almohada?

Informal: ¿Te gustaría ordenar una almohada?

接下来设计一个通用的语言翻译工具,可以把各种语言都翻译为英语。下面给出一组使用各种语言来描述的文本信息:

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

]

prompt示例如下:

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: 내 화면이 깜박거립니다.

三、结合案例演示解析如何使用prompt进行文本语气转换和格式转换

下面的prompt用来把一段俚语转换为商务语气的文本。

prompt示例如下:

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)

打印输出结果如下:

Dear Sir/Madam,

I hope this letter finds you well. My name is Joe, and I am writing to bring your attention to a specification document regarding a standing lamp.

I kindly request that you take a moment to review the attached spec, as it contains important details about the standing lamp in question.

Thank you for your time and consideration. I look forward to hearing from you soon.

Sincerely,

Joe

接下来给出JSON格式的数据:

data_json = { "resturant employees" :[

    {"name":"Shyam", "email":"[email protected]"},

    {"name":"Bob", "email":"[email protected]"},

    {"name":"Jai", "email":"[email protected]"}

]}

要求模型把这些数据用HTML的格式来显示。

prompt示例如下:

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)

打印输出结果如下:

Restaurant Employees

 

   

   

 

 

   

   

 

 

   

   

 

 

   

   

 

Name Email
Shyam [email protected]
Bob [email protected]
Jai [email protected]

四、结合案例演示解析如何使用prompt进行文本拼写及语法错误检查

通过编写prompt指导模型对输入的文本进行拼写及语法错误检查,并输出结果。

首先给出一组需要检查的文本信息:

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

]

prompt示例如下:

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)

       打印输出结果如下:

       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?

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.

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: ```{text}```"

response = get_completion(prompt)

print(response)

打印输出结果如下:

Got this for my daughter for her 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 believe 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.

你可能感兴趣的:(AI,prompt,chatgpt,自然语言处理,大模型开发,人工智能)