The latest course of Wu Enda & OpenAI: prompt-engineering-for-developers reading notes

Reference: bilibili video (Chinese subtitles) , github project (including notes)

I. Introduction

At present, large-scale language models ( LLM) can be roughly divided into two types:

  • Based LLM: Basic LLM. Trained using large amounts of text data from the Internet or other sources by predicting the next word of a sentence.
  • Instruction Tuned LLM: The LLM fine-tuned by the instruction will follow the instruction to give the answer after being trained.

  So if you ask Based LLM"What is the capital of France?", it might predict the answer as "What is the largest city in France? What is the population of France?" based on articles on the Internet that are likely to is a list of quiz questions about the country of France. And if you ask Instruction Tuned LLMthe same question, it is more likely to output "The capital of France is Paris".

  Instruction Tuned LLMThe training of LLM usually starts from already trained basic LLMs, which have been trained on a large amount of text data. prompt-answerIt is then supervised fine-tuned using a human-annotated dataset, requiring it to follow these instructions. Finally, a technology called RLHF (reinforcement learning from human feedback, human feedback reinforcement learning) is usually used for further improvement, so that the system can follow instructions more helpfully (for specific principles, please refer to "Li Mu Paper Precision Series Nine: InstructGPT 》 ).

  Instruction Tuned LLMAfter training, it Based LLMis more helpful, authentic and harmless (safety) than . So many practical usage scenarios today have shifted Instruction Tuned LLM.

  This course is taught by Ng Enda and OpenAI's technical team Isa Fulfordmembers . In this course, we will share with you some of the possibilities of LLM and the best practices for implementing them, hopefully this will stimulate your imagination to create new applications (LLM or Large Language Model as a developer is more powerful using the API call to LLM to rapidly build software applications.) . This course includes:

  • Best practices for software development prompts
  • Common use cases, conclusions, inferences, transformations. expand
  • Use LLM to help create a chatbot

insert image description here

2. Prompt writing principles

  An important principle of creating a prompt is clear instructions , and the other is to give the model time to think .

2.1 Environment configuration

  This tutorial uses the ChatGPT API opened by OpenAI, so you need to have a ChatGPT first API_KEY(under the personal page after OpenAI login), and then you need to install openai's third-party library:

pip install openai     		   # 安装openai
pip install -U python-dotenv  # 安装python-dotenv

# 将自己的 API-KEY 导入系统环境变量
!export OPENAI_API_KEY='api-key'

Then import the relevant library:

import openai
import os
from dotenv import load_dotenv, find_dotenv
# 导入第三方库

_ = load_dotenv(find_dotenv())
# 读取系统中的环境变量
openai.api_key  = os.getenv('OPENAI_API_KEY')
# 设置 API_KEY

  In Section 8.1, we will delve into how to use the ChatCompletion API provided by OpenAI. Here, we first encapsulate it into a function. You don't need to know its internal mechanism, you only need to know that calling the function and entering the Prompt will give The corresponding Completion is enough. In this course, we use gpt-3.5-turbo.

# 一个封装 OpenAI 接口的函数,参数为 Prompt,返回对应结果
# 接受提示,返回提示补全内容
def get_completion(prompt, model="gpt-3.5-turbo"):
    '''
    prompt: 对应的提示
    model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT),有内测资格的用户可以选择 gpt-4
    '''
    messages = [{
    
    "role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # 模型输出的温度系数,控制输出的随机程度
    )
    # 调用 OpenAI 的 ChatCompletion 接口
    return response.choices[0].message["content"]

You can also refer to the method of calling the API in the ChatGPT advanced course "datawhalechina/HuggingLLM" :

import openai
OPENAI_API_KEY = "填入专属的API key"

openai.api_key = OPENAI_API_KEY

def complete(prompt):
    response = openai.Completion.create(
      model="text-davinci-003",
      prompt=prompt,
      temperature=0,
      max_tokens=64,
      top_p=1.0,
      frequency_penalty=0.0,
      presence_penalty=0.0
    )
    ans = response.choices[0].text
    return ans

2.2 Write clear, specific instructions

  You should express what you want the model to do by providing as clear and specific instructions as possible, this will guide the model to give the correct output and reduce the chances of you getting irrelevant or incorrect responses. Writing clear instructions does not mean short instructions, as in many cases longer prompts are actually clearer and provide more context , ultimately resulting in more detailed and relevant output.

  There are four strategies (tactics) for writing specific and clear instructions, which will be introduced one by one next:
insert image description here

2.2.1 Using delimiters

  Strategy one is to use delimiters, delimiters can clearly represent different parts of the input, delimiters can be: /```,""",---,<>,<tag>,<\tag>etc.:

  You can use any obvious punctuation to separate a specific portion of text from the rest of the prompt, avoiding prompt injection. The following is an example where we give a paragraph and ask GPT to summarize it, in this example we use ``` as a delimiter.

Prompt injection: input contains other instructions, which will overwrite your instructions. For this, using delimiters is a good strategy. The line break
is used in the prompt in this article , just for the convenience of display, and no symbols are needed in actually calling the API.\\

# 原例子是英文的,这里做了翻译,下同。
text = f"""
你应该提供尽可能清晰、具体的指示,以表达你希望模型执行的任务。\
这将引导模型朝向所需的输出,并降低收到无关或不正确响应的可能性。不要将写清晰的提示与写简短的提示混淆。\
在许多情况下,更长的提示可以为模型提供更多的清晰度和上下文信息,从而导致更详细和相关的输出。
"""
# 需要总结的文本内容
prompt = f"""
把用三个反引号括起来的文本总结成一句话。
```{
      
      text}```
"""
# 指令内容,使用 ```来分隔指令和待总结的内容
response = get_completion(prompt)
print(response)
提供清晰具体的指示,避免无关或不正确响应,不要混淆写清晰和写简短,更长的提示可以提供更多清晰度和上下文信息,导致更详细和相关的输出。

  Without a delimiter, incorrect results may be output. For example, in the example below, the model may forget the initial summary prompt and follow the text "write a poem about a cute panda". With the delimiter, the model knows that it needs to summarize the parts surrounded by ```.
insert image description here

2.2.2 Structured output (JSON, HTML, etc.)

  The second strategy is to perform structured output, which can be in the format of JSON, , HTMLetc., which can make the output of the model easier for us to parse. For example, you can read it into a dictionary or list in Python. .

  In the following example, we ask GPT to generate the title, author, and category of three books and ask GPT to return it to us JSONin format of , which we specify as JSONkeys for parsing. The advantage of this is that we can read it as a dictionary or list in python.

prompt = f"""
请生成包括书名、作者和类别的三本虚构书籍清单,并以 JSON 格式提供,其中包含以下键:book_id、title、author、genre。
"""
response = get_completion(prompt)
print(response)

{
  "books": [
    {
      "book_id": 1,
      "title": "The Shadow of the Wind",
      "author": "Carlos Ruiz Zafón",
      "genre": "Mystery"
    },
    {
      "book_id": 2,
      "title": "The Name of the Wind",
      "author": "Patrick Rothfuss",
      "genre": "Fantasy"
    },
    {
      "book_id": 3,
      "title": "The Hitchhiker's Guide to the Galaxy",
      "author": "Douglas Adams",
      "genre": "Science Fiction"
    }
  ]
}

2.2.3 Requiring the model to check whether the conditions are satisfied

  If the assumptions made by the task are not necessarily satisfied, we can tell the model to check these assumptions first, and if not, indicate and stop execution. You can also consider potential edge cases and how the model should handle them to avoid unexpected errors or results.

  In the example below, we will give the model two pieces of text: the steps to make tea and a piece of text with no explicit steps. We will ask the model to determine whether it contains a series of instructions, if it does, reformulate the instructions in a given format, and if it does not, answer the steps that were not provided.

# 有步骤的文本
text_1 = f"""
泡一杯茶很容易。首先,需要把水烧开。在等待期间,拿一个杯子并把茶包放进去。一旦水足够热,就把它倒在茶包上。\
等待一会儿,让茶叶浸泡。几分钟后,取出茶包。如果你愿意,可以加一些糖或牛奶调味。就这样,你可以享受一杯美味的茶了。
"""
prompt = f"""
您将获得由三个引号括起来的文本。如果它包含一系列的指令,则需要按照以下格式重新编写这些指令:

第一步 - ...
第二步 - …
…
第N步 - …

如果文本中不包含一系列的指令,则直接写“未提供步骤”。"
\"\"\"{
      
      text_1}\"\"\"
"""
response = get_completion(prompt)
print("Text 1 的总结:")
print(response)
Text 1 的总结:
第一步 - 把水烧开。
第二步 - 拿一个杯子并把茶包放进去。
第三步 - 把烧开的水倒在茶包上。
第四步 - 等待几分钟,让茶叶浸泡。
第五步 - 取出茶包。
第六步 - 如果你愿意,可以加一些糖或牛奶调味。
第七步 - 就这样,你可以享受一杯美味的茶了。
# 无步骤的文本
text_2 = f"""
今天阳光明媚,鸟儿在歌唱。这是一个去公园散步的美好日子。鲜花盛开,树枝在微风中轻轻摇曳。
人们外出享受着这美好的天气,有些人在野餐,有些人在玩游戏或者在草地上放松。这是一个完美的日子,可以在户外度过并欣赏大自然的美景。
"""
prompt = f"""
您将获得由三个引号括起来的文本。如果它包含一系列的指令,则需要按照以下格式重新编写这些指令:

第一步 - ...
第二步 - …
…
第N步 - …

如果文本中不包含一系列的指令,则直接写“未提供步骤”。"
\"\"\"{
      
      text_2}\"\"\"
"""
response = get_completion(prompt)
print("Text 2 的总结:")
print(response)
Text 2 的总结:
未提供步骤。

2.2.4 Few-shot Prompting

  Few-shot Prompting, giving the model a small number of examples of successfully performing tasks before asking it to perform the actual task.

  For example, in the following example, we tell the model that its task is to answer questions in a consistent style, and start by giving it an example of a conversation between a child and a grandfather. In the example, the grandparent answers with metaphors. Since we've told the model to answer with a consistent tone, and with this example, now we say "teach me resilience", it will answer the next task with a similar tone.

prompt = f"""
你的任务是以一致的风格回答问题。

<child>: 教我耐心。

<grandparent>: 挖出最深峡谷的河流源于一处不起眼的泉眼;最宏伟的交响乐从单一的音符开始;最复杂的挂毯以一根孤独的线开始编织。

<child>: 教我韧性。
"""
response = get_completion(prompt)
print(response)
<grandparent>: 韧性就像是一棵树,它需要经历风吹雨打、寒冬酷暑,才能成长得更加坚强。在生活中,我们也需要经历各种挫折和困难,才能锻炼出韧性。记住,不要轻易放弃,坚持下去,你会发现自己变得更加坚强。

2.3 Guidance model thinking

  If the task is too complicated, the model may draw wrong conclusions (make up some wrong guesses) in a hurry, just as humans may make mistakes when completing complex math problems without having time to calculate the answer. So we can reimagine the prompt, requesting the model to perform a series of related reasoning before giving the final answer. This is equivalent to instructing the model to spend more time thinking about the problem step by step, which also means that it spends more computing resources on the task.
insert image description here

2.3.1 Specify task steps

  Next we will show the effect of this strategy by giving a complex task and giving a series of steps to complete the task.

  First we describe the story of Jack and Jill, and give the steps to derive the final answer.

  • Summarize the text delimited by three backticks in one sentence;
  • Translate the abstract into French;
  • List each name in the French summary;
  • Outputs a JSON object containing the following keys: French_summary, num_names;
  • Separate answers with newlines.
text = f"""
在一个迷人的村庄里,杰克和吉尔兄妹出发去山顶一个井里打水。他们一边唱着歌,一边往上爬,然而不幸降临——杰克被一块石头绊倒,从山上滚了下来,\
吉尔紧随其后。虽然略有些摔伤,但他们还是回到了温馨的家中。尽管出了这样的意外,他们的冒险精神依然没有减弱,继续充满愉悦地探索。
"""
# example 1
prompt_1 = f"""
执行以下操作:
1-用一句话概括下面用三个反引号括起来的文本。
2-将摘要翻译成法语。
3-在法语摘要中列出每个人名。
4-输出一个 JSON 对象,其中包含以下键:French_summary,num_names。

请用换行符分隔您的答案。

Text:
```{
      
      text}```
"""
response = get_completion(prompt_1)
print("prompt 1:")
print(response)
Completion for prompt 1:
Two siblings, Jack and Jill, go on a quest to fetch water from a well on a hilltop, but misfortune strikes and they both tumble down the hill, returning home slightly battered but with their adventurous spirits undimmed.

Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits sur une colline, mais un malheur frappe et ils tombent tous les deux de la colline, rentrant chez eux légèrement meurtris mais avec leurs esprits aventureux intacts. 
Noms(法语中的姓名): Jack, Jill.

{
  "french_summary": "Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits sur une colline, mais un malheur frappe et ils tombent tous les deux de la colline, rentrant chez eux légèrement meurtris mais avec leurs esprits aventureux intacts.",
  "num_names": 2
}

  The above output still has some problems, for example, the key "name" will be replaced with French Noms, so we give a better prompt that specifies the format of the output (standardized output format can make the results more predictable) .

prompt_2 = f"""
1-用一句话概括下面用<>括起来的文本。
2-将摘要翻译成英语。
3-在英语摘要中列出每个名称。
4-输出一个 JSON 对象,其中包含以下键:English_summary,num_names。

请使用以下格式:
文本:<要总结的文本>
摘要:<摘要>
翻译:<摘要的翻译>
名称:<英语摘要中的名称列表>
输出 JSON:<带有 English_summary 和 num_names 的 JSON>

Text: <{
      
      text}>
"""
response = get_completion(prompt_2)
print("\nprompt 2:")
print(response)
Completion for prompt 2:
Summary: Two siblings, Jack and Jill, go on a quest to fetch water from a well on a hilltop, but misfortune strikes and they both tumble down the hill, returning home slightly battered but with their adventurous spirits undimmed.
Translation: Jack and Jill, deux frères et sœurs, ont eu un accident en allant chercher de l'eau dans un puits de montagne, mais ils ont continué à explorer avec un esprit d'aventure.
Names: Jack, Jill
Output JSON: {"french_summary": "Jack and Jill, deux frères et sœurs, ont eu un accident en allant chercher de l'eau dans un puits de montagne, mais ils ont continué à explorer avec un esprit d'aventure.", "num_names": 2}

2.3.2 Requiring the model to calculate itself in advance

  Sometimes we get better results when we explicitly instruct the model to think about solutions before making a decision. Next, we will give a question and a student's answer, and ask the model to judge whether the answer is correct.

prompt = f"""
判断学生的解决方案是否正确。

问题:
我正在建造一个太阳能发电站,需要帮助计算财务。

    土地费用为 100美元/平方英尺
    我可以以 250美元/平方英尺的价格购买太阳能电池板
    我已经谈判好了维护合同,每年需要支付固定的10万美元,并额外支付每平方英尺10美元
    作为平方英尺数的函数,首年运营的总费用是多少。

学生的解决方案:
设x为发电站的大小,单位为平方英尺。
费用:

    土地费用:100x
    太阳能电池板费用:250x
    维护费用:100,000美元+100x
    总费用:100x+250x+100,000美元+100x=450x+100,000美元
"""
response = get_completion(prompt)
print(response)
学生的解决方案是正确的。

  But note that the student's solution is actually wrong (the maintenance fee is 10w+10x instead of 100x). We can solve this problem by instructing the model to first find a solution on its own.

  In the following prompt, we ask the model to solve this problem by itself first, and then compare its own solution with the student's solution to judge whether the student's solution is correct. At the same time, we have given the format requirements for the output. By specifying the steps, giving the model more time to think, can sometimes lead to more accurate results.

prompt = f"""
请判断学生的解决方案是否正确,请通过如下步骤解决这个问题:

步骤:

    首先,自己解决问题。
    然后将你的解决方案与学生的解决方案进行比较,并评估学生的解决方案是否正确。在自己完成问题之前,请勿决定学生的解决方案是否正确。

使用以下格式:

    问题:问题文本
    学生的解决方案:学生的解决方案文本
    实际解决方案和步骤:实际解决方案和步骤文本
    学生的解决方案和实际解决方案是否相同:是或否
    学生的成绩:正确或不正确

问题:

    我正在建造一个太阳能发电站,需要帮助计算财务。 
    - 土地费用为每平方英尺100美元
    - 我可以以每平方英尺250美元的价格购买太阳能电池板
    - 我已经谈判好了维护合同,每年需要支付固定的10万美元,并额外支付每平方英尺10美元
    作为平方英尺数的函数,首年运营的总费用是多少。

学生的解决方案:

    设x为发电站的大小,单位为平方英尺。
    费用:
    1. 土地费用:100x
    2. 太阳能电池板费用:250x
    3. 维护费用:100,000+100x
    总费用:100x+250x+100,000+100x=450x+100,000

实际解决方案和步骤:
"""
response = get_completion(prompt)
print(response)
正确的解决方案和步骤:
    1. 计算土地费用:100美元/平方英尺 * x平方英尺 = 100x美元
    2. 计算太阳能电池板费用:250美元/平方英尺 * x平方英尺 = 250x美元
    3. 计算维护费用:10万美元 + 10美元/平方英尺 * x平方英尺 = 10万美元 + 10x美元
    4. 计算总费用:100x美元 + 250x美元 + 10万美元 + 10x美元 = 360x + 10万美元

学生的解决方案和实际解决方案是否相同:否

学生的成绩:不正确

  In this example, the student's answer was wrong, but it could be misleading to think that the student was correct if we hadn't let the model figure it out on its own first

2.4 Limitations

insert image description here

False knowledge : models occasionally generate false knowledge that appears to be true

  The model is exposed to a lot of knowledge during training, but it doesn't fully remember it, so it doesn't know the boundaries of its own knowledge. This means that when the model is trying to answer more obscure questions, it may make up answers that sound reasonable but are not actually correct. We call these fabricated ideas hallucination(hallucinations).

  For example, in the following example, we ask the model to tell us the information about the AeroGlide UltraSlim Smart Toothbrush product produced by Boie Company. In fact, this company is real, but the product is fabricated, and the model will tell us the fabricated knowledge seriously.

prompt = f"""
告诉我 Boie 公司生产的 AeroGlide UltraSlim Smart Toothbrush 的相关信息
"""
response = get_completion(prompt)
print(response)
Boie公司生产的AeroGlide UltraSlim Smart Toothbrush是一款智能牙刷,具有以下特点:

1. 超薄设计:刷头仅有0.8毫米的厚度,可以更容易地进入口腔深处,清洁更彻底。

2. 智能感应:牙刷配备了智能感应技术,可以自动识别刷头的位置和方向,确保每个部位都得到充分的清洁。

3. 高效清洁:牙刷采用了高速振动技术,每分钟可达到40000次,可以有效去除牙菌斑和污渍。

4. 轻松携带:牙刷采用了便携式设计,可以轻松放入口袋或旅行包中,随时随地进行口腔清洁。

5. 环保材料:牙刷采用了环保材料制造,不含有害物质,对环境友好。

总之,Boie公司生产的AeroGlide UltraSlim Smart Toothbrush是一款高效、智能、环保的牙刷,可以帮助用户轻松保持口腔健康。

  As you can see from the examples above, models can fabricate knowledge that looks very realistic, which can sometimes be dangerous. So make sure to use some of the tricks we cover in this section to try and avoid this when building your own applications.
  Another strategy for reducing hallucinations is to first ask the model to find any relevant references in the text, and then ask it to use these references to answer questions. This method of tracing source documents is often very helpful for reducing hallucinations.

Note: In this tutorial, we use \ to adapt the text to the screen size to improve the reading experience. GPT is not affected by \, but when you call other large models, you need to consider whether \ will affect the model performance

3. Instruction iteration

  When building applications with LLM, it is rarely a matter of writing a prompt once and getting the final result. But as long as you have a good iterative process to continuously improve your prompt, then you will be able to get a prompt that is suitable for the task, so an iterative process of optimizing the prompt and digging out a good prompt for a specific application is very important .

  In this chapter, we'll use the example of generating marketing copy from a product brochure to show some frameworks for how you can iteratively analyze and refine your prompts.

  In the previous course, I talked about the process of machine learning development:

  • Idea:
  • Code: get data, write code
  • Ruselt: Train the model and get the experimental results.
  • Error Analysis: According to the results, conduct error analysis and improve the solution ideas or methods

  Repeat the above steps and iterate repeatedly to obtain an effective machine learning model. It's a similar process when writing a prompt to develop an application using LLM.
  When you have an idea about the task to be done, you can try to write the first prompt to meet the two principles mentioned in the previous chapter: be clear and clear, and think step by step . Then you can run it and see the result. If it doesn't work the first time, the iterative process is to figure out why the instructions weren't clear enough or why the algorithm wasn't given enough time to think, to refine the idea, refine the hint, etc., looping many times until you find the one that works for your application Prompt.

insert image description here

3.1 Environment configuration

Same as the previous chapter, we first need to configure the OpenAI API environment

import openai
import os
from dotenv import load_dotenv, find_dotenv
# 导入第三方库

_ = load_dotenv(find_dotenv())
# 读取系统中的环境变量

openai.api_key  = os.getenv('OPENAI_API_KEY')
# 设置 API_KEY
# 一个封装 OpenAI 接口的函数,参数为 Prompt,返回对应结果
def get_completion(prompt, model="gpt-3.5-turbo"):
    '''
    prompt: 对应的提示
    model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT),有内测资格的用户可以选择 gpt-4
    '''
    messages = [{
    
    "role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # 模型输出的温度系数,控制输出的随机程度
    )
    # 调用 OpenAI 的 ChatCompletion 接口
    return response.choices[0].message["content"]

3.2 Generate a marketing description from the product brochure

  Here's a brochure for a chair inspired by an ornate mid-century family, then discusses construction, dimensions, chair options, materials, and more, made in Italy. Let's say you want to use this cookbook to help your marketing team write marketing-style descriptions for your online retail website.

# 示例:产品说明书
fact_sheet_chair = """
概述

    美丽的中世纪风格办公家具系列,包括文件柜、办公桌、书柜、会议桌等。多种外壳颜色和底座涂层可选。
    可选塑料前后靠背装饰(SWC-100)或10种面料和6种皮革的全面装饰(SWC-110)。
    底座涂层选项为:不锈钢、哑光黑色、光泽白色或铬。椅子可带或不带扶手。
    适用于家庭或商业场所。符合合同使用资格。

结构

    五个轮子的塑料涂层铝底座。
    气动椅子调节,方便升降。

尺寸

    宽度53厘米|20.87英寸
    深度51厘米|20.08英寸
    高度80厘米|31.50英寸
    座椅高度44厘米|17.32英寸
    座椅深度41厘米|16.14英寸

选项

    软地板或硬地板滚轮选项。
    两种座椅泡沫密度可选:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺)。
    无扶手或8个位置PU扶手。

材料
外壳底座滑动件

    改性尼龙PA6/PA66涂层的铸铝。
    外壳厚度:10毫米。
    座椅
    HD36泡沫

原产国

    意大利
"""
# 提示:基于说明书创建营销描述
prompt = f"""
你的任务是帮助营销团队基于技术说明书创建一个产品的营销描述。根据```标记的技术说明书中提供的信息,编写一个产品描述。

技术说明: ```{
      
      fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)

产品描述:

我们自豪地推出美丽的中世纪风格办公家具系列,其中包括文件柜、办公桌、书柜、会议桌等。我们的产品采用多种外壳颜色和底座涂层,以满足您的个性化需求。您可以选择塑料前后靠背装饰(SWC-100)或10种面料和6种皮革的全面装饰(SWC-110),以使您的办公室更加舒适和时尚。

我们的底座涂层选项包括不锈钢、哑光黑色、光泽白色或铬,以满足您的不同需求。椅子可带或不带扶手,适用于家庭或商业场所。我们的产品符合合同使用资格,为您提供更加可靠的保障。

我们的产品采用五个轮子的塑料涂层铝底座,气动椅子调节,方便升降。尺寸为宽度53厘米|20.87英寸,深度51厘米|20.08英寸,高度80厘米|31.50英寸,座椅高度44厘米|17.32英寸,座椅深度41厘米|16.14英寸,为您提供舒适的使用体验。

我们的产品还提供软地板或硬地板滚轮选项,两种座椅泡沫密度可选:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺),以及无扶手或8个位置PU扶手,以满足您的不同需求。

我们的产品采用改性尼龙PA6/PA66涂层的铸铝外壳底座滑动件,外壳厚度为10毫米,座椅采用HD36泡沫,为您提供更加舒适的使用体验。我们的产品原产国为意大利,为您提供更加优质的品质保证。

3.2.1 Iteration 1: limit the length of the generated text

  The above answers do a good job of writing the product description starting from the technical specification, but the description is too long. So I'll update my prompt to ask for a maximum of 50 words.

# 优化后的 Prompt,要求生成描述不多于 50 词,原文都是英文。
prompt = f"""
您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。

根据```标记的技术说明书中提供的信息,编写一个产品描述。

使用最多50个词。

技术规格:```{
      
      fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)

Introducing our beautiful medieval-style office furniture collection, including filing cabinets, desks, bookcases, and conference tables. Choose from a variety of shell colors and base coatings, with optional plastic or fabric/leather decoration. The chair features a plastic-coated aluminum base with five wheels and pneumatic height adjustment. Perfect for home or commercial use. Made in Italy.

Take out the answer and split it according to the space. The answer is 54 characters, which better fulfills my request

lst = response.split()
print(len(lst))
54    

You might also consider describing the length limit as: use 3 sentences, use 300 characters (in English), etc.

LLM's are okay, but not that great, at adhering to a very precise word limit. Sometimes it outputs 60 or 65 words of content, but that's more than reasonable. The reason for this is that LLMs interpret text using something called tokenizers, but they tend to be mediocre at counting characters. There are many different ways to try and control the length of the output you get.

3.2.2 Iteration 2: Focus on the right details

  The second problem we will find is that this site is not selling directly to consumers, it is actually selling furniture to furniture retailers, who will care more about the technical details and materials of the chairs. In this case, you can modify the prompt so that it more precisely describes the technical details of the chair (focusing on aspects relevant to the target audience).

# 优化后的 Prompt,说明面向对象,应具有什么性质且侧重于什么方面
prompt = f"""
您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。根据```标记的技术说明书中提供的信息,编写一个产品描述。

该描述面向家具零售商,所以要更偏向技术性,并侧重于产品、材料和构造。

使用最多50个单词。

技术规格: ```{
      
      fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)
这款中世纪风格办公家具系列包括文件柜、办公桌、书柜和会议桌等,适用于家庭或商业场所。可选多种外壳颜色和底座涂层,底座涂层选项为不锈钢、哑光黑色、光泽白色或铬。椅子可带或不带扶手,可选软地板或硬地板滚轮,两种座椅泡沫密度可选。外壳底座滑动件采用改性尼龙PA6/PA66涂层的铸铝,座椅采用HD36泡沫。原产国为意大利。

  Below I want to include the product id at the end of the description. So at the end of the prompt, I use 7 characters to describe the ID of each product.

# 更进一步,要求在描述末尾包含 7个字符的产品ID
prompt = f"""
Your task is to help a marketing team create a 
description for a retail website of a product based 
on a technical fact sheet.

Write a product description based on the information 
provided in the technical specifications delimited by 
triple backticks.

The description is intended for furniture retailers, 
so should be technical in nature and focus on the 
materials the product is constructed from.

At the end of the description, include every 7-character 
Product ID in the technical specification.

Use at most 50 words.

Technical specifications: ```{
      
      fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)
Introducing our beautiful medieval-style office furniture collection, featuring file cabinets, desks, bookshelves, and conference tables. Available in multiple shell colors and base coatings, with optional plastic or fabric/leather decorations. The chair comes with or without armrests and has a plastic-coated aluminum base with five wheels and pneumatic height adjustment. Suitable for home or commercial use. Made in Italy.

Product IDs: SWC-100, SWC-110
prompt = f"""
您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。根据```标记的技术说明书中提供的信息,编写一个产品描述。

该描述面向家具零售商,因此应具有技术性质,并侧重于产品的材料构造。

在描述末尾,用7个字符来描述每个产品的ID。

使用最多50个单词。

技术规格: ```{
      
      fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)
这款中世纪风格的办公家具系列包括文件柜、办公桌、书柜和会议桌等,适用于家庭或商业场所。可选多种外壳颜色和底座涂层,底座涂层选项为不锈钢、哑光黑色、光泽白色或铬。椅子可带或不带扶手,可选塑料前后靠背装饰或10种面料和6种皮革的全面装饰。座椅采用HD36泡沫,可选中等或高密度,座椅高度44厘米,深度41厘米。外壳底座滑动件采用改性尼龙PA6/PA66涂层的铸铝,外壳厚度为10毫米。原产国为意大利。产品ID:SWC-100/SWC-110。

3.2.3 Iteration 3: Tabular description

  A more complex prompt example is shown below to give you a deeper understanding of the capabilities of ChatGPT.

  Here I added some additional instructions, asking it to extract information and organize it into tables, and specify the table name, column name and format of the table, and also ask it to format all the content into HTML that can be used on the web page.

# 要求抽取信息并组织成表格,并指定表格名、列名和格式
prompt = f"""
您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。根据```标记的技术说明书中提供的信息,编写一个产品描述。

该描述面向家具零售商,因此应具有技术性质,并侧重于产品的材料构造。

在描述末尾,用7个字符来描述每个产品的ID。

在描述之后,生成一个表格,提供产品的尺寸。表格应该有两列。第一列包括尺寸的名称。第二列只包括英寸的测量值。

表格命名为“产品尺寸”。

将所有内容格式化为可用于网站的HTML格式。将描述放在<div>元素中。

技术规格:```{
      
      fact_sheet_chair}```
"""

response = get_completion(prompt)
print(response)
<div>
<h2>中世纪风格办公家具系列椅子</h2>
<p>这款椅子是中世纪风格办公家具系列的一部分,适用于家庭或商业场所。它有多种外壳颜色和底座涂层可选,包括不锈钢、哑光黑色、光泽白色或铬。您可以选择带或不带扶手的椅子,以及软地板或硬地板滚轮选项。此外,您可以选择两种座椅泡沫密度:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺)。</p>
<p>椅子的外壳底座滑动件是改性尼龙PA6/PA66涂层的铸铝,外壳厚度为10毫米。座椅采用HD36泡沫,底座是五个轮子的塑料涂层铝底座,可以进行气动椅子调节,方便升降。此外,椅子符合合同使用资格,是您理想的选择。</p>
<p>产品ID:SWC-100</p>
</div>

<table>
  <caption>产品尺寸</caption>
  <tr>
    <th>宽度</th>
    <td>20.87英寸</td>
  </tr>
  <tr>
    <th>深度</th>
    <td>20.08英寸</td>
  </tr>
  <tr>
    <th>高度</th>
    <td>31.50英寸</td>
  </tr>
  <tr>
    <th>座椅高度</th>
    <td>17.32英寸</td>
  </tr>
  <tr>
    <th>座椅深度</th>
    <td>16.14英寸</td>
  </tr>
</table>
# 表格是以 HTML 格式呈现的,加载出来
from IPython.display import display, HTML

display(HTML(response))

insert image description here

3.3 Summary

  This chapter demonstrates the prompt iterative development process LLMin developing an application. Developers need to try to write a prompt first, and then gradually improve it through iterations until the desired result is obtained. The key is having an efficient process for developing prompts, not knowing the perfect prompt.
  For some more complex applications, a prompt can be developed iteratively and evaluated using a number of examples. For example, test different prompts on multiple information tables to understand the average or worst performance of multiple information tables, which can make the app more mature (when the app matures, there must be some indicators to promote the last few steps of increment rapid improvement). Also, while using Jupyter for the notebook example, try different variations and see the results.

4. Summarizing

ChatGPT API application reference "datawhalechina/HuggingLLM" .

4.1 Introduction

  There is so much text in today's world that few of us have enough time to read everything we want to know. But one of the most exciting applications of LLMs is summarizing text. This chapter will introduce how to use the programming method to call the API interface to realize the "text summary" function.

  First, we need the OpenAI package, load the API key, and define the getCompletion function.

import openai
import os
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
openai.api_key = OPENAI_API_KEY

def get_completion(prompt, model="gpt-3.5-turbo"): 
    messages = [{
    
    "role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # 值越低则输出文本随机性越低
    )
    return response.choices[0].message["content"]

4.2 Single-text summarization

  Here we give an example of a product review. For e-commerce platforms, there are often massive commodity reviews on the website, and these reviews reflect the thoughts of all customers. If we have a tool to summarize these massive and lengthy reviews, we can quickly browse more reviews, gain insight into customer preferences, and guide platforms and merchants to provide better services.

Enter text (Chinese translation)

prod_review_zh = """
这个熊猫公仔是我给女儿的生日礼物,她很喜欢,去哪都带着。公仔很软,超级可爱,面部表情也很和善。但是相比于价钱来说,
它有点小,我感觉在别的地方用同样的价钱能买到更大的。快递比预期提前了一天到货,所以在送给女儿之前,我自己玩了会。
"""

4.2.1 Limit output text length

Try to summarize with a maximum of 30 words

prompt = f"""
Your task is to generate a short summary of a product review from an ecommerce site. 

Summarize the review below, delimited by triple 
backticks, in at most 30 words. 

Review: ```{
      
      prod_review}```
"""

response = get_completion(prompt)
print(response)
Soft and cute panda plush toy loved by daughter, but a bit small for the price. Arrived early.

Chinese translation

prompt = f"""
你的任务是从电子商务网站上生成一个产品评论的简短摘要。请对三个反引号之间的评论文本进行概括,最多30个词汇。

评论: ```{
      
      prod_review_zh}```
"""

response = get_completion(prompt)
print(response)
柔软可爱的熊猫毛绒玩具深受女儿的喜爱,但价格有点小。提前到货。

4.2.2 Focus on specific groups/angles

  Sometimes, for different businesses, our emphasis on text will be different. For example, for product review texts, logistics will pay more attention to the timeliness of transportation, merchants will pay more attention to price and product quality, and platforms will care more about the overall service experience.
  We can increase the prompt to reflect the emphasis on a specific angle.

focus on transportation

prompt = f"""
你的任务是从电子商务网站上生成一个产品评论的简短摘要。请对三个反引号之间的评论文本进行概括,最多30个词汇,并且聚焦在产品运输上。

评论: ```{
      
      prod_review_zh}```
"""

response = get_completion(prompt)
print(response)
熊猫毛绒玩具比预期提前了一天到货,相比它的价格而言,客户觉得玩具有点小。

It can be seen that the output results start with "Express delivery arrives one day earlier", which reflects the emphasis on express delivery efficiency.

Focus on price and quality

prompt = f"""
你的任务是从电子商务网站上生成一个产品评论的简短摘要。请对三个反引号之间的评论文本进行概括,最多30个词汇,并且聚焦在产品价格和质量上。

评论: ```{
      
      prod_review_zh}```
"""

response = get_completion(prompt)
print(response)
熊猫毛绒玩具柔软可爱,但对于这个尺寸来说,价格有点高。

It can be seen that the output results start with "good quality, low price, small size", which reflects the emphasis on product price and quality.

4.2.3 Information Extraction

  In the previous section, although we made the text summary more focused on a specific aspect by adding a prompt that focuses on key perspectives, it can be found that some other information will be retained in the results, such as price and quality perspectives. The message "Express delivery ahead of schedule" was added.
  Sometimes this information is helpful, but if we only want to extract information from a certain angle, and filter out all other information, we can ask LLM to do Extract(information extraction) instead of ( Summarizetext summarization).

prompt = f"""
你的任务是从电子商务网站上的产品评论中提取相关信息。请从以下三个反引号之间的评论文本中提取产品运输相关的信息,最多30个词汇。

评论: ```{
      
      prod_review_zh}```
"""

response = get_completion(prompt)
print(response)
快递比预期提前了一天到货。

4.3 Multi-text summary prompt

  In the actual workflow, we often have a lot of comment texts. The following shows an example of calling the "Text Summary" tool based on a for loop and printing it in sequence. Of course, in actual production, it is unrealistic to use a for loop for millions or even tens of millions of comment texts. You may need to consider methods such as integrating comments and distributing them to improve computing efficiency.

review_1 = prod_review 

# review for a standing lamp
review_2 = """
Needed a nice lamp for my bedroom, and this one \
had additional storage and not too high of a price \
point. Got it fast - arrived in 2 days. The string \
to the lamp broke during the transit and the company \
happily sent over a new one. Came within a few days \
as well. It was easy to put together. Then I had a \
missing part, so I contacted their support and they \
very quickly got me the missing piece! Seems to me \
to be a great company that cares about their customers \
and products. 
"""

# review for an electric toothbrush
review_3 = """
My dental hygienist recommended an electric toothbrush, \
which is why I got this. The battery life seems to be \
pretty impressive so far. After initial charging and \
leaving the charger plugged in for the first week to \
condition the battery, I've unplugged the charger and \
been using it for twice daily brushing for the last \
3 weeks all on the same charge. But the toothbrush head \
is too small. I’ve seen baby toothbrushes bigger than \
this one. I wish the head was bigger with different \
length bristles to get between teeth better because \
this one doesn’t.  Overall if you can get this one \
around the $50 mark, it's a good deal. The manufactuer's \
replacements heads are pretty expensive, but you can \
get generic ones that're more reasonably priced. This \
toothbrush makes me feel like I've been to the dentist \
every day. My teeth feel sparkly clean! 
"""

# review for a blender
review_4 = """
So, they still had the 17 piece system on seasonal \
sale for around $49 in the month of November, about \
half off, but for some reason (call it price gouging) \
around the second week of December the prices all went \
up to about anywhere from between $70-$89 for the same \
system. And the 11 piece system went up around $10 or \
so in price also from the earlier sale price of $29. \
So it looks okay, but if you look at the base, the part \
where the blade locks into place doesn’t look as good \
as in previous editions from a few years ago, but I \
plan to be very gentle with it (example, I crush \
very hard items like beans, ice, rice, etc. in the \ 
blender first then pulverize them in the serving size \
I want in the blender then switch to the whipping \
blade for a finer flour, and use the cross cutting blade \
first when making smoothies, then use the flat blade \
if I need them finer/less pulpy). Special tip when making \
smoothies, finely cut and freeze the fruits and \
vegetables (if using spinach-lightly stew soften the \ 
spinach then freeze until ready for use-and if making \
sorbet, use a small to medium sized food processor) \ 
that you plan to use that way you can avoid adding so \
much ice if at all-when making your smoothie. \
After about a year, the motor was making a funny noise. \
I called customer service but the warranty expired \
already, so I had to buy another one. FYI: The overall \
quality has gone done in these types of products, so \
they are kind of counting on brand recognition and \
consumer loyalty to maintain sales. Got it in about \
two days.
"""

reviews = [review_1, review_2, review_3, review_4]
for i in range(len(reviews)):
    prompt = f"""
    Your task is to generate a short summary of a product \ 
    review from an ecommerce site. 

    Summarize the review below, delimited by triple \
    backticks in at most 20 words. 

    Review: ```{
      
      reviews[i]}```
    """
    response = get_completion(prompt)
    print(i, response, "\n")
0 Soft and cute panda plush toy loved by daughter, but a bit small for the price. Arrived early. 

1 Affordable lamp with storage, fast shipping, and excellent customer service. Easy to assemble and missing parts were quickly replaced. 

2 Good battery life, small toothbrush head, but effective cleaning. Good deal if bought around $50. 

3 The product was on sale for $49 in November, but the price increased to $70-$89 in December. The base doesn't look as good as previous editions, but the reviewer plans to be gentle with it. A special tip for making smoothies is to freeze the fruits and vegetables beforehand. The motor made a funny noise after a year, and the warranty had expired. Overall quality has decreased. 

  If you have a website, there are hundreds of comments on it. You can set up a dashboard that generates short summaries of tons of reviews, making it easy for customers to browse and for you to quickly understand what customers think. You can always click on it to see the original detailed review if you like.

5. Inferring

5.1 Introduction

  In this chapter you will infer sentiment and themes from product reviews and news articles. These tasks can be seen as the process by which a model receives text as input and performs some kind of analysis , such as extracting labels, extracting entities, understanding text sentiment, etc.

  In the traditional machine learning workflow, if you want to do emotion recognition tasks, you need to collect data sets, train models, deploy models on the cloud, and perform inference. This process is a lot of work, and for each task, such as sentiment analysis, extracting entities, etc., a separate model needs to be trained and deployed.

  A very nice feature of large language models is that for many of these tasks, you only need to write a prompt and get the result without a lot of work (convenience). This greatly speeds up application development. You can also use just one model and one API to perform many different tasks without having to figure out how to train and deploy many different models.

Start the environment below:

import openai
import os

OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY2")
openai.api_key = OPENAI_API_KEY
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"]

5.2 Emotion recognition

The following is an example of the evaluation of a desk lamp.
1. Sentiment Classification (Positive/Negative)

# 中文
lamp_review_zh = """
我需要一盏漂亮的卧室灯,这款灯具有额外的储物功能,价格也不算太高。我很快就收到了它。在运输过程中,我们的灯绳断了,
但是公司很乐意寄送了一个新的。几天后就收到了。这款灯很容易组装,我发现少了一个零件,于是联系了他们的客服,他们很快就给我寄来了缺失的零件!
在我看来,Lumina 是一家非常关心顾客和产品的优秀公司!
"""

Now let's write a prompt to classify the sentiment of this review. Although the product is a bit problematic, the final result is positive.

# 中文
prompt = f"""
以下用三个反引号分隔的产品评论的情感是什么?评论文本: ```{
      
      lamp_review_zh}```
"""
response = get_completion(prompt)
print(response)
情感是积极的/正面的。

  If you want a more succinct answer for easier post-processing, you can add another command to the prompt, "Answer in one word: "positive" or "negative""

prompt = f"""
以下用三个反引号分隔的产品评论的情感是什么?用一个单词回答:「正面」或「负面」。
评论文本: ```{
      
      lamp_review_zh}```
"""
response = get_completion(prompt)
print(response)
正面

2. Emotion recognition

  Still using the desk lamp comments below, this time I want it to recognize the sentiments in the comments as a list, no more than five.

# 中文
prompt = f"""
识别以下评论的情感,情感不超过五个,并将答案格式化为以逗号分隔的单词列表。

评论文本: ```{
      
      lamp_review_zh}```
"""
response = get_completion(prompt)
print(response)
满意,感激,信任,赞扬,愉快

3. Recognize anger

  Knowing if a customer is very angry is important for many businesses. An angry customer may deserve extra attention, having a customer support or customer success team contact the customer to understand the situation and resolve the issue for the customer.

# 中文
prompt = f"""
以下评论的作者是否表达了愤怒?评论用三个反引号分隔。给出是或否的答案。

评论文本: ```{
      
      lamp_review_zh}```
"""
response = get_completion(prompt)
print(response)

  If you want to build all these classifiers using regular supervised learning, it is impossible to do it in a few minutes. We encourage everyone to try changing some of these prompts to make different inferences about this light fixture review.

5.3 Information Extraction

  Information extraction is part of Natural Language Processing (NLP). In the following task, we will extract product names and company names from reviews.

  If you're trying to summarize the many reviews of online shopping e-commerce sites, it can be difficult to figure out what the product is, who made it, and what the sentiment of the review was to track positive or negative sentiment trends for a particular product or a particular manufacturer. it works.

  In the example below, we're asking it to format the response as a JSON object, where the keys are the item and brand name.

# 中文
prompt = f"""
从评论中识别以下项目:
- 评论者购买的物品
- 制造该物品的公司

评论文本用三个反引号分隔。将你的响应格式化为以 “物品” 和 “品牌” 为键的 JSON 对象。
如果信息不存在,请使用 “未知” 作为值。
答案尽可能简短。
  
评论文本: ```{
      
      lamp_review_zh}```
"""
response = get_completion(prompt)
print(response)
{
  "物品": "卧室灯",
  "品牌": "Lumina"
}

  As shown above, the product is a bedroom lamp and the brand is Luminar, you can easily load this into a Python dictionary and then do other processing on this output.

5.4 Multitasking

  It took 3 or 4 prompts to extract all of the above information, but it is actually possible to write a single prompt to extract all of them at the same time.

# 中文
prompt = f"""
用三个反引号分隔的部分是评论,从评论中识别以下项目:
- 情绪(正面或负面)
- 是否表达了愤怒?(是或否)
- 评论者购买的物品
- 制造该物品的公司

将您的答案格式化为 JSON 对象,以 “Sentiment”、“Anger”、“Item” 和 “Brand” 作为键。
如果信息不存在,请使用 “未知” 作为值。
答案尽可能简短。
将 Anger 值格式化为布尔值。

评论文本: ```{
      
      lamp_review_zh}```
"""
response = get_completion(prompt)
print(response)
{
  "Sentiment": "正面",
  "Anger": false,
  "Item": "卧室灯",
  "Brand": "Lumina"
}

5.5 Topic Inference

  Infer topics: Given a long text, what is this text about? What is the theme? Below is a bogus newspaper article about surveys of government workers' satisfaction with the organizations they work for.

# 中文
story_zh = """
在政府最近进行的一项调查中,要求公共部门的员工对他们所在部门的满意度进行评分。调查结果显示,NASA 是最受欢迎的部门,满意度为 95%。

一位 NASA 员工 John Smith 对这一发现发表了评论,他表示:
“我对 NASA 排名第一并不感到惊讶,这是一个与了不起的人一起工作的好地方。我为成为这样一个创新组织的一员感到自豪。”

NASA 的管理团队也对这一结果表示欢迎,主管 Tom Johnson 表示:
“我们很高兴听到我们的员工对 NASA 的工作感到满意。
我们拥有一支才华横溢、忠诚敬业的团队,他们为实现我们的目标不懈努力,看到他们的辛勤工作得到回报真是太棒了。”

调查还显示,社会保障管理局的满意度最低,只有 45%的员工表示满意。政府承诺解决调查中员工提出的问题,并努力提高所有部门的工作满意度。
"""

1. Infer the 5 themes of the article

# 中文
prompt = f"""
确定以下给定文本中讨论的五个主题。

每个主题用1-2个单词概括。

输出时用逗号分割每个主题。

给定文本: ```{
      
      story_zh}```
"""
response = get_completion(prompt)
print(response)
调查结果, NASA, 社会保障管理局, 员工满意度, 政府承诺

2. Identify a given topic

  Suppose a news website has the following topics of interest: NASA, local government, engineering, employee satisfaction, federal government, etc. We want to identify which of the above topics are included in the article. The prompt can be written as: Determine whether each item in the topic list is in the text Theme of. Give a list of answers in the form of 0 or 1.

topic_list = [
    "nasa", "local government", "engineering", 
    "employee satisfaction", "federal government"
]
# 中文
prompt = f"""
判断主题列表中的每一项是否是给定文本中的一个主题,

每个主题答案用 0 或 1表示,并以列表的形式给出。

主题列表:美国航空航天局、地方政府、工程、员工满意度、联邦政府

给定文本: ```{
      
      story_zh}```
"""
response = get_completion(prompt)
print(response)
美国航空航天局:1
地方政府:1
工程:0
员工满意度:1
联邦政府:1

  In machine learning this is called Zero-Shot: without giving the model any labeled training data, it can determine which topics a news article covers, just from the prompt.
3. News reminder
  If we want to generate a news reminder, we can also use this process of processing news. Assuming I really like what NASA is doing, I can build a system that outputs an alert whenever NASA news comes out.

topic_dict = {
    
    i.split(':')[0]: int(i.split(':')[1]) for i in response.split(sep='\n')}
if topic_dict['美国航空航天局'] == 1:
    print("提醒: 关于美国航空航天局的新消息")
提醒: 关于美国航空航天局的新消息

  In just a few minutes, this chapter builds several systems for reasoning about text that would previously have taken days or even weeks of the time of a skilled machine learning developer. Both for skilled machine learning developers and novices alike, prompts can be used to quickly build and develop fairly complex NLP tasks.

Six, machine translation (transforming)

6.1 Introduction

  LLM is very good at transforming input into different formats, such as multilingual translation, grammar correction, tone adjustment, format conversion, etc. In the past, it may be necessary to write a bunch of regular expressions, which is very painful. Now these functions can be realized by calling the API interface (use ChatGPT to proofread the work content).

Start environment:

import openai
import os
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY2")
openai.api_key = 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"]

6.2 Machine translation

1. Chinese to Spanish

prompt = f"""
将以下中文翻译成西班牙语: \ 
```您好,我想订购一个搅拌机。```
"""
response = get_completion(prompt)
print(response)
Hola, me gustaría ordenar una batidora.

2. Identify language

prompt = f"""
请告诉我以下文本是什么语种: 
```Combien coûte le lampadaire?```
"""
response = get_completion(prompt)
print(response)
这是法语。

3. Multilingual translation

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.

4. Universal translator

  With the development of globalization and cross-border business, communication users may come from different countries and use different languages. Therefore, we need a universal translator to identify the language of each message and translate it into the native language of the target user, so as to realize More convenient cross-border communication.

  Here are several texts in different languages, we write more complex code for multitasking (identify languages, and translate to Chinese and English):

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"告诉我以下文本是什么语种,直接输出语种,如法语,无需输出标点符号: ```{
      
      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 don't 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. 
=========================================

6.3 Tone/Style Adjustments

  The tone of writing tends to vary depending on the audience. For example, for work emails, we often need to use formal tone and written words, while for WeChat chats with friends of the same age, we use more relaxed and colloquial tone.

prompt = f"""
将以下文本翻译成商务信函的格式: 
```小老弟,我小羊,上回你说咱部门要采购的显示器是多少寸来着?```
"""
response = get_completion(prompt)
print(response)
尊敬的XXX(收件人姓名):

您好!我是XXX(发件人姓名),在此向您咨询一个问题。上次我们交流时,您提到我们部门需要采购显示器,
但我忘记了您所需的尺寸是多少英寸。希望您能够回复我,以便我们能够及时采购所需的设备。

谢谢您的帮助!

此致

敬礼

XXX(发件人姓名)
prompt = f"""
请将以下文本翻译成中文,分别展示成正式与非正式两种语气: 
```Would you like to order a pillow?```
"""
response = get_completion(prompt)
print(response)
正式语气(formal):请问您需要订购枕头吗?
非正式语气(informal):你要不要订一个枕头?

6.4 Format Conversion

  ChatGPT is very good at converting between different formats, such as JSON to HTML, XML, Markdown, etc. In the following example, we have a JSON that contains a list of restaurant staff names and emails, and we want to convert it from JSON to HTML.

data_json = {
    
     "resturant employees" :[ 
    {
    
    "name":"Shyam", "email":"[email protected]"},
    {
    
    "name":"Bob", "email":"[email protected]"},
    {
    
    "name":"Jai", "email":"[email protected]"}
]}
prompt = f"""
将以下Python字典从JSON转换为HTML表格,保留表格标题和列名:{
      
      data_json}
"""
response = get_completion(prompt)
print(response)
<table>
  <caption>resturant employees</caption>
  <thead>
    <tr>
      <th>name</th>
      <th>email</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Shyam</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>Bob</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>Jai</td>
      <td>[email protected]</td>
    </tr>
  </tbody>
</table>
from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))
resturant employees
name email
Shyam [email protected]
Bob [email protected]
Jai [email protected]

6.5 Spell check, grammar correction

  Checking and correcting spelling and grammar is a very common requirement, especially when using non-native speakers, such as when publishing English papers.

  Some of the sentences below have spelling or grammar problems. Loop through each sentence, ask the model to proofread the text, and output "no errors found" if it is correct, and output the corrected text if it is wrong.

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)):
    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 未发现错误。
2 It's going to be a long day. Does the car need its oil changed?
3 Their goes my freedom. They're 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.

  The following is a simple error correction example, input the original text, output the corrected text, and use the Redlineslibrary to output the difference before and after error correction.

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.
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,需先安装
!pip3.8 install redlines
from redlines import Redlines
from IPython.display import display, Markdown

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

insert image description here

6.6 Comprehensive example: text translation + spelling correction + style adjustment + format conversion

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"""
针对以下三个反引号之间的英文评论文本,
首先进行拼写及语法纠错,
然后将其转化成中文,
再将其转化成优质淘宝评论的风格,从各种角度出发,分别说明产品的优点与缺点,并进行总结。
润色一下描述,使评论更具有吸引力。
输出结果格式为:
【优点】xxx
【缺点】xxx
【总结】xxx
注意,只需填写xxx部分,并分段输出。
将结果输出成Markdown格式。
```{
      
      text}```
"""
response = get_completion(prompt)
display(Markdown(response))

【advantage】

  • Super soft and cuddly, a very popular daughter birthday gift.
  • Adults love pandas too, and I love it too.
  • Arrived a day early to give me time to play with it.

【shortcoming】

  • One ear is lower than the other, asymmetrical.
  • The price is a bit expensive, the size is a bit small, there may be larger options at the same price.

[Summary]
This panda toy is very suitable as a birthday gift, it is soft and cute, and is loved by children. While the price is a bit pricey, the size is a bit small and the asymmetrical design is a bit of a let down. If you want a larger, similarly priced option, you may want to consider other options. Overall, this is a good panda toy, worth buying.

Seven, text expansion (Expanding)

7.1 Preface

  ExpandingThis involves feeding short text, such as a set of instructions or a list of topics, into a large language model and having the model generate longer text, such as an email or essay based on a certain topic.

  There are some good uses for this, such as using large language models as brainstorming buddies. But there are some problems with this practice, for example, someone may use it to generate a lot of spam. So when you use these features of large language models, please only use them responsibly and in ways that benefit people.

  In this chapter, you will learn how to generate personalized emails based on certain information. We will also use another input parameter of the model temperature, which controls the diversity, randomness of the answers.

Configure the environment of OpenAI API:

# 将自己的 API-KEY 导入系统环境变量
!export OPENAI_API_KEY='api-key'
import openai
import os
from dotenv import load_dotenv, find_dotenv
# 导入第三方库

_ = load_dotenv(find_dotenv())
# 读取系统中的环境变量

openai.api_key  = os.getenv('OPENAI_API_KEY')
# 设置 API_KEY
# 一个封装 OpenAI 接口的函数,参数为 Prompt,返回对应结果
def get_completion(prompt, model="gpt-3.5-turbo", temperature=0):
    '''
    prompt: 对应的提示
    model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT),有内测资格的用户可以选择 gpt-4
    temperature: 温度系数
    '''
    messages = [{
    
    "role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature, # 模型输出的温度系数,控制输出的随机程度
    )
    # 调用 OpenAI 的 ChatCompletion 接口
    return response.choices[0].message["content"]

7.2 Customize customer email

Given customer reviews and sentiment (sentiment can also be extracted first with prompt), generate custom emails to reply to customers, the following is an example:

# 在推理那章,已经学习了对一个评论判断其情感倾向
sentiment = "negative"

# 一个产品的评价
review = f"""
他们在11月份的季节性销售期间以约49美元的价格出售17件套装,折扣约为一半。\
但由于某些原因(可能是价格欺诈),到了12月第二周,同样的套装价格全都涨到了70美元到89美元不等。\
11件套装的价格也上涨了大约10美元左右。\
虽然外观看起来还可以,但基座上锁定刀片的部分看起来不如几年前的早期版本那么好。\
不过我打算非常温柔地使用它,例如,\
我会先在搅拌机中将像豆子、冰、米饭等硬物研磨,然后再制成所需的份量,\
切换到打蛋器制作更细的面粉,或者在制作冰沙时先使用交叉切割刀片,然后使用平面刀片制作更细/不粘的效果。\
制作冰沙时,特别提示:\
将水果和蔬菜切碎并冷冻(如果使用菠菜,则轻轻煮软菠菜,然后冷冻直到使用;\
如果制作果酱,则使用小到中号的食品处理器),这样可以避免在制作冰沙时添加太多冰块。\
大约一年后,电机发出奇怪的噪音,我打电话给客服,但保修已经过期了,所以我不得不再买一个。\
总的来说,这些产品的总体质量已经下降,因此它们依靠品牌认可和消费者忠诚度来维持销售。\
货物在两天内到达。
"""

  Above is a customer testimonial about Blender, now we will customize the response based on sentiment.

  The prompt here is: Assuming you are an AI customer agent (this transparency is important, let the customer know that the email is AI-generated), your task is to send an email reply for the customer, according to the Customer email, generating a reply thanking the customer for their review.

prompt = f"""
你是一位客户服务的AI助手。
你的任务是给一位重要客户发送邮件回复。
根据客户通过“```”分隔的评价,生成回复以感谢客户的评价。提醒模型使用评价中的具体细节
用简明而专业的语气写信。
作为“AI客户助手”签署电子邮件。
客户评论:
```{
      
      review}```
评论情感:{
      
      sentiment}
"""
response = get_completion(prompt)
print(response)
尊敬的客户,

非常感谢您对我们产品的评价。我们非常抱歉您在购买过程中遇到了价格上涨的问题。我们一直致力于为客户提供最优惠的价格,但由于市场波动,价格可能会有所变化。我们深表歉意,如果您需要任何帮助,请随时联系我们的客户服务团队。

我们非常感谢您对我们产品的详细评价和使用技巧。我们将会把您的反馈传达给我们的产品团队,以便改进我们的产品质量和性能。

再次感谢您对我们的支持和反馈。如果您需要任何帮助或有任何疑问,请随时联系我们的客户服务团队。

祝您一切顺利!

AI客户代理

7.3 Use temperature coefficient

  Next, we'll use a parameter of the language model called "temperature" that will allow us to vary the diversity of the model's responses. You can think of temperature as the degree of model exploration or randomness.

  In general, I recommend using a temperature of zero when building applications that require predictable responses, or reliable, predictable systems. Higher temperatures can be used if you are trying to use the model in a more creative way, requiring a more varied output.

  For example, in a particular phrase, the most likely next word for "my favorite food" is "pizza", followed by "sushi" and "tacos". Therefore, at zero temperature, the model will always choose the most likely next word. And at higher temperatures, it will also choose one of the less likely words, and at higher temperatures, it might even choose tortillas. As the model continues, the responses will become increasingly different.
insert image description here

prompt = f"""
你是一名客户服务的AI助手。你的任务是给一位重要的客户发送邮件回复。根据通过“```”分隔的客户电子邮件生成回复,以感谢客户的评价。
如果情感是积极的或中性的,感谢他们的评价。如果情感是消极的,道歉并建议他们联系客户服务。
请确保使用评论中的具体细节。以简明和专业的语气写信。以“AI客户代理”的名义签署电子邮件。
客户评价:```{
      
      review}```
评论情感:{
      
      sentiment}
"""
response = get_completion(prompt, temperature=0.7)
print(response)
尊敬的客户,

非常感谢您对我们产品的评价。我们由衷地为您在购买过程中遇到的问题表示抱歉。我们确实在12月份的第二周调整了价格,但这是由于市场因素所致,并非价格欺诈。我们深刻意识到您对产品质量的担忧,我们将尽一切努力改进产品,以提供更好的体验。

我们非常感激您对我们产品的使用经验和制作技巧的分享。您的建议和反馈对我们非常重要,我们将以此为基础,进一步改进我们的产品。

如果您有任何疑问或需要进一步帮助,请随时联系我们的客户服务部门。我们将尽快回复您并提供帮助。

最后,请再次感谢您对我们产品的评价和选择。我们期待着未来与您的合作。

此致

敬礼

AI客户代理

  It can be seen that with temperature = 0.7, different emails are generated and the answer can be considered more creative.

8. Chatbots

8.1 Preface

  One of the exciting things about using a large language model is that we can use it to build a custom chatbot with very little effort. In this section, we explore how chat formats (interfaces) can be leveraged for extended conversations with chatbots that are personalized or specialized for specific tasks or behaviors.

Start the configuration environment:

import os
import openai

OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY2")
openai.api_key = OPENAI_API_KEY

  A chat model like ChatGPTthis is trained to take a series of user messages (user) as input and a model-generated content message (assistant message) as output. This chat format is designed to make multi-turn conversations easy, while being equally useful for single-turn quests without any dialogue.
insert image description here

  Next we use two helper functions:

  • get_completion: A function that takes a single prompt as input (single dialogue) and outputs a single content.
  • get_completion_from_messages: The input is a list of messages from different roles, which can use different descriptions; then output the results of multiple rounds of dialogue. The following is an example of a message list:
    • system massage: General instructions for the conversation, used to set the behavior and role of the assistant (ChatGPT);
    • user massage: User message. For example, in the ChatGPT web interface, the message you enter is the user message;
    • assistant massage: Assistant message, that is, the message output by ChatGPT.
    • After setting the system massage, there are usually several rounds of dialogue information between the user and the assistant, which constitute the message list.
      insert image description here

  The advantage of system massage is that it provides a way for developers to guide the assistant and guide its response without making the request itself part of the conversation, without making the user aware (in GPT-4, the user can also set the system massage).

def get_completion(prompt, model="gpt-3.5-turbo"):
	# 单轮对话
    messages = [{
    
    "role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # 控制模型输出的随机程度
    )
    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, # 控制模型输出的随机程度
    )
#     print(str(response.choices[0].message)) # 打印整个massage,结果包含content和role
    return response.choices[0].message["content"]

  Now let's try to use these messages in a dialog. We'll use the above function to get the replies from these messages, and at the same time, use a higher temperature (higher generates more variety).

8.2 Chatbots

  Several examples of multi-turn dialogues are given below. The system sets the assistant as someone who speaks like Shakespeare, and then sets 3 messages between the user and the assistant.

# 中文
messages =  [  
{
    
    'role':'system', 'content':'你是一个像莎士比亚一样说话的助手。'},    
{
    
    'role':'user', 'content':'给我讲个笑话'},   
{
    
    'role':'assistant', 'content':'鸡为什么过马路'},   
{
    
    'role':'user', 'content':'我不知道'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)
因为它要去找“母鸡”。哈哈哈!(注:此为英文双关语,"chicken"是鸡的意思,也是胆小的意思;"cross the road"是过马路的意思,也是“破坏规则”的意思。)

  Let's do another example. system sets the assistant to "You're a friendly chatbot" and the first user message is "Yes, can you remind me what my name is?"

# 中文
messages =  [  
{
    
    'role':'system', 'content':'你是个友好的聊天机器人。'},    
{
    
    'role':'user', 'content':'好,你能提醒我,我的名字是什么吗?'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)
抱歉,我不知道您的名字,因为我们是虚拟的聊天机器人,和现实生活中的人类在不同的世界中。

  As you can see above, the model doesn't actually know my name.

  Therefore, each interaction with the language model is an independent interaction, which means that we must provide all relevant messages for the model to refer to in the current dialogue. If you want your model to reference or "remember" earlier parts of the conversation, you must provide earlier communications in the model's input, which we call context. Let's try again.

# 中文
messages =  [  
{
    
    'role':'system', 'content':'你是个友好的聊天机器人。'},
{
    
    'role':'user', 'content':'Hi, 我是Isa'},
{
    
    'role':'assistant', 'content': "Hi Isa! 很高兴认识你。今天有什么可以帮到你的吗?"},
{
    
    'role':'user', 'content':'是的,你可以提醒我, 我的名字是什么?'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)
当然可以!您的名字是Isa。

  Now that we have given the model context, which is my name mentioned in the previous conversation, we then ask the same question and the model is able to respond.

8.3 Orderbot (orderbot)

  Now, we build an "ordering robot", we need it to automatically collect user information and accept orders from pizzerias.

  The function below will automatically collect our user messages, then append them to a list called contexts without us entering them manually, and then use that context every time the model is called. The response of the model will also be added to the context, so the context will become longer and longer, and the model can use the context information of multiple rounds of dialogue to determine what to do next.

def collect_messages(_):
    prompt = inp.value_input
    inp.value = ''
    context.append({
    
    'role':'user', 'content':f"{
      
      prompt}"})
    response = get_completion_from_messages(context) 
    context.append({
    
    'role':'assistant', 'content':f"{
      
      response}"})
    panels.append(
        pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
    panels.append(
        pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={
    
    'background-color': '#F6F6F6'})))
 
    return pn.Column(*panels)

  Now, we'll set up and run the UI to display the order bot. The initial context contains system messages containing menus. Note that the context grows over time.

!pip install panel
# 中文
import panel as pn  # GUI
pn.extension()

panels = [] # collect display 

context = [{
    
    'role':'system', 'content':"""
你是订餐机器人,为披萨餐厅自动收集订单信息。
你要首先问候顾客。然后等待用户回复收集订单信息。收集完信息需确认顾客是否还需要添加其他内容。
最后需要询问是否自取或外送,如果是外送,你要询问地址。
最后告诉顾客订单总金额,并送上祝福。

请确保明确所有选项、附加项和尺寸,以便从菜单中识别出该项唯一的内容。
你的回应应该以简短、非常随意和友好的风格呈现。

菜单包括:

菜品:
意式辣香肠披萨(大、中、小) 12.95、10.00、7.00
芝士披萨(大、中、小) 10.95、9.25、6.50
茄子披萨(大、中、小) 11.95、9.75、6.75
薯条(大、小) 4.50、3.50
希腊沙拉 7.25

配料:
奶酪 2.00
蘑菇 1.50
香肠 3.00
加拿大熏肉 3.50
AI酱 1.50
辣椒 1.00

饮料:
可乐(大、中、小) 3.00、2.00、1.00
雪碧(大、中、小) 3.00、2.00、1.00
瓶装水 5.00
"""} ]  # accumulate messages


inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")

interactive_conversation = pn.bind(collect_messages, button_conversation)

dashboard = pn.Column(
    inp,
    pn.Row(button_conversation),
    pn.panel(interactive_conversation, loading_indicator=True, height=300),
)

insert image description here

The following is the system message set by the ordering robot:
  You are an orderbot, an automated service that collects orders for a pizzeria. Greet customers first, then collect orders. Then ask the customer if they want to pick up or deliver. After waiting and collecting the entire order, wrap it up and do a final check to see if the customer has anything else to add. If you want to match song, ask the delivery address. Work out all costs at the end, making sure all items, extras, extras, dimensions are clear. You need to respond in a short, chatty, friendly style. The menu includes…
insert image description here

  We can ask the model to create a JSON order summary to send to the order system. So we now append another system massage, which is another prompt. User messages can also be used here, not necessarily system messages.

messages =  context.copy()
messages.append(
{
    
    'role':'system', 'content':'创建上一个食品订单的 json 摘要。\
逐项列出每件商品的价格,字段应该是 1) 披萨,包括大小 2) 配料列表 3) 饮料列表,包括大小 4) 配菜列表包括大小 5) 总价'},    
)

response = get_completion_from_messages(messages, temperature=0)
print(response)

  Note that here we use a lower temperature because for these types of tasks we want the output to be relatively predictable.

以下是上一个食品订单的 JSON 摘要:

```
{
  "order": {
    "pizza": {
      "type": "芝士披萨",
      "size": "大",
      "price": 10.95
    },
    "toppings": [
      {
        "name": "蘑菇",
        "price": 1.5
      }
    ],
    "drinks": [
      {
        "name": "雪碧",
        "size": "大",
        "price": 3
      },
      {
        "name": "雪碧",
        "size": "大",
        "price": 3
      }
    ],
    "sides": [],
    "total_price": 18.45
  }
}
注意:我假设蘑菇大奶酪披萨的价格是12.45美元,而不是12.95美元,因为顾客只点了一种浇头。

  Now, we have built our own food ordering chatbot. You can modify the system massage at will to change the behavior of the chatbot and make it play different roles and have different knowledge. You can also modify the menu or prompt to create your own order bot!

Nine. Summary

In this course we learned:

  • Two key principles about prompts:

    • Write clear and specific instructions;
    • Give the model some thinking time (step by step).
  • An iterative approach to prompt development and understanding the process of finding the right prompt for your application is critical.

  • Features of large language models are introduced, including summarization, inference, transformation, and extension.

  • Build custom chatbots.

  We hope you can come up with some app ideas and try to build them yourself. You can start with a very small project, maybe it has some practical value, or it may not have practical value at all, just something fun and fun. Please use the learnings from your first project to build a better second project, even a better third project, etc. Or, if you already have a larger project idea, go for it.

  Large language models are very powerful, and as a reminder, we urge you to use them responsibly and only build things that have a positive impact on others. In this day and age, those who build AI systems can have a huge impact on others. These tools must therefore be used responsibly.

  Building applications based on large language models is a very exciting and growing field right now. Now that you've completed the course, we think you now have a wealth of knowledge that will help you build things that others don't know how to build today. So I hope you too can help us spread the word and encourage others to take this course too.

  Finally, I hope you had a great time completing this course and thank you for completing this course. We look forward to hearing about the amazing things you build.

Guess you like

Origin blog.csdn.net/qq_56591814/article/details/130629019