提示工程(3):总结(summarizing)能力

总结(Summarizing)能力

总结能力是我最喜欢的模型能力。很多的文章是基本上没有时间去阅读的,利用模型的总结能力,可以快速的了解文章的大概。
接下来,我们用代码的方式来试验模型总结的能力。

import openai
openai.api_key="your openai 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"]

下面是一个产品的评论。

prod_review = """
Got this panda plush toy for my daughter's birthday,
who loves it and takes it everywhere. 
It's soft and super cute, and its face has a friendly look.
It's a bit small for what I paid 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 her.
"""

我们来做个30词的总结。如下:

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 a child, but considered small for the price. Arrived early.

上面关于产品评论的30 word左右的总结。

如果想把这个评论总结给到发货部分(shipping department),我们可以改一个提示(short summary of a product to the shipping department, 并且,focusing on any aspectes that mention shipping and delivery)。

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

Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects that metion shipping and delivery of product.

Review: '''{prod_review}'''

"""
response = get_completion(prompt)
print(response)
The customer received the panda plush toy a day earlier than expected, allowing them to play with it before giving it as a gift.

我们看上述的总结,“发货时间早于期望时间”,十分简明的

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

Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects that metion pricing and preceived value.

Review: '''{prod_review}'''

"""
response = get_completion(prompt)
print(response)

“too small fo the price paid”.

上面是我们利用总结(summarising)的能力。

现在我们来讲讲提出/摘取(extract)的能力。
我们只是要提出或摘取客户评价中我们关心的语句,应该怎么做呢?请看下面的例子(extract the info. relevant to shipping and delivery…)。

prompt = f"""
Your task is to extract relevant information of a product review from an ecommerce site to give feedback to the Shipping department.

Summarize the review below, delimited by triple backticks, extract the information relevant to shipping and delivery of product. Limit to 30 words.

Review: '''{prod_review}'''

"""
response = get_completion(prompt)
print(response)
The product arrived a day earlier than expected.

假如我们有很多评论,希望做一个快速的总结和浏览,以了解其大概。我们可以见下面的示例。

下面是三个不同产品的评论。

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 manufacturers’ \
replacements heads are pretty expensive, but you can \
get generic ones those are 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 around49 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 of29. \
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. PYI: 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]

我们快速生成每个评估的总结,每段总结在20字左右。

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 tripe backticks in at most 20 words.

    Review: '''{reviews[i]}'''

    """

    response = get_completion(prompt)
    print (i, response, "\n")
0 "Panda plush toy loved by daughter, soft and cute, but small for the price." 

1 ```Nice lamp with storage, fast delivery, excellent customer service. Great company that cares about customers and products.``` 

2 The reviewer recommends the electric toothbrush for its impressive battery life, but criticizes the small toothbrush head. 

3 The reviewer mentions a price increase, a slight flaw in the base, and a motor issue after a year. 

上面讲解的就是关于模型总结(summarizing)的能力。下次我们讲推断。

提示工程(2):提示迭代开发

一个好的提示,不是一蹴而就写出来的。需要不断的迭代,才能写得得到预期结果的提示。这个迭代的过程我们称为迭代的提示开发(Iterative Prompt Development,见下图)。

提示迭代开发(Iterative Prompt Development)

首先,我们有个很好的idea;其次,用代码和数据去实现它(清晰且明确的指令);然后,根据实验的结果或错误(为何不是想要的结果),不断调优和改进,直到最终达到满意的效果。

我们来试验一下,给出一段产品描述(一把意大利生产的椅子),对prompt进行调优,让模型给出满意的结果。

import openai
openai.api_key="your openai key starting with sk-"

下面是一段方法,辅助执行LLM交互。
输入:提示、采用的gpt模型(默认为gpt-3.5-turbo)
输出:gpt返回的文字

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"]

以下是某个意大利生产椅子的产品介绍和规格说明书。

fact_sheet_chair = """
OVERVIEW
- Part of a beautiful family of mid-century inspired office including filing cabinets, desks, bookcases, meeting tables,
- Several options of shell color and base finishes.
- Available with plastic back and front upholstery (SWC-100) or full upholstery (SWC-110) in 10 fabric and 6 leather optic
- Base finish options are: stainless steel, matte black, gloss white, or chrome.
- Chair is available with or without armrests.
- Suitable for home or business settings.
- Qualified for contract use.

CONSTRUCTION
- 5-wheel plastic coated aluminum base.
- Pneumatic chair adjust for easy raise/lower action.

DIMENSIONS
- WIDTH 53 CM | 20.87"
- DEPTI 51 CM | 20.08*
- HEIGAT 80 CM 31.50"
- SEAT HEIGHT 44 CM | 17.32"
- SEAT DEPTH 41 CM | 16.14"

OPTIONS
- Soft or hard-floor caster options.
- Two choices of seat foam densities: medium (1.8 1b/ft3) or high (2.8 1b/ft3)

- Armless or 8 position PU armrests

MATERIALS
SHELL BASE GLIDER
- Cast Aluminum with modified nylon PA6/PA66 coating.
- Shell thickness: 10 mm.
SEAT
- HD36 foam

COUNTRY OF ORIGIN
- Italy
"""

基于上述内容,让模型为市场部写一段产品介绍。

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.

Technical specifications: '''{fact_sheet_chair}'''
"""

response = get_completion(prompt)
print(response)
Introducing our stunning mid-century inspired office chair, a perfect addition to any home or business setting. This chair is part of a beautiful family of office furniture, including filing cabinets, desks, bookcases, and meeting tables, allowing you to create a cohesive and stylish workspace.

One of the standout features of this chair is the variety of options available. You can choose from several shell colors and base finishes, allowing you to customize the chair to suit your personal style. The chair is available with either plastic back and front upholstery or full upholstery in a range of 10 fabric and 6 leather optic options. The base finish options include stainless steel, matte black, gloss white, or chrome. Additionally, you have the choice of having the chair with or without armrests.

Constructed with durability and comfort in mind, this chair features a 5-wheel plastic coated aluminum base, ensuring stability and ease of movement. The pneumatic chair adjust allows for easy raise and lower action, providing you with the perfect seating position.

The dimensions of this chair are as follows: width 53 cm (20.87"), depth 51 cm (20.08"), height 80 cm (31.50"), seat height 44 cm (17.32"), and seat depth 41 cm (16.14"). These dimensions make it suitable for a variety of body types and ensure a comfortable sitting experience.

We offer additional options to enhance your chair's functionality. You can choose between soft or hard-floor caster options, depending on your flooring type. Additionally, you have the choice of two seat foam densities: medium (1.8 1b/ft3) or high (2.8 1b/ft3). This allows you to select the level of support that best suits your needs.

The materials used in the construction of this chair are of the highest quality. The shell base glider is made of cast aluminum with a modified nylon PA6/PA66 coating, ensuring durability and longevity. The shell thickness is 10 mm, providing a sturdy and reliable structure. The seat is made of HD36 foam, offering exceptional comfort for extended periods of sitting.

Rest assured that this chair is of Italian origin, known for its craftsmanship and attention to detail. It is also qualified for contract use, making it suitable for commercial settings.

Upgrade your workspace with our stylish and functional mid-century inspired office chair. Experience the perfect blend of comfort, durability, and style.

写得不错,上面的内容结合了产品介绍和规格说明书的内容,就是内容好象多了些,我们尝试减少一些。加上”use at most 50 words”,以下是运行的效果。

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.

Use at most 50 words.

Technical specifications: '''{fact_sheet_chair}'''
"""

response = get_completion(prompt)
print(response)
Introducing our mid-century inspired office chair, perfect for both home and business settings. With various color and base options, this chair offers style and versatility. Choose between plastic or full upholstery, and select from a range of finishes. The chair features a 5-wheel base and pneumatic chair adjustment for easy use. Made in Italy.

篇幅大大缩减了。上面有多少字呢?

len(response.split(" "))
54

54个words。虽然不太精准(50),但也大大的减少了篇幅。我们试试280个字符,看是否会准确一些。加prompt里加上Use at most 280 characters。

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.

Use at most 280 characters.

Technical specifications: '''{fact_sheet_chair}'''
"""

response = get_completion(prompt)
print(response)
Introducing our mid-century inspired office chair! With various color and base options, this chair is perfect for any home or business setting. It features a 5-wheel base and pneumatic chair adjust for easy use. Made with high-quality materials from Italy.
len(response)
256

不错,毕竟没有超过280个字符。

接下来我们换一种风格。我们需要更多关于产品技术和材料方面的说明。因此,我们在prompt里加了之方面的要求:The description is intended for furniture retailers, so should be technical in nature and focus on the materials the product is constructed from.

我们来试下效果如何?

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.

Use at most 50 words.

Technical specifications: '''{fact_sheet_chair}'''
"""

response = get_completion(prompt)
print(response)
Introducing our mid-century inspired office chair, perfect for both home and business settings. Constructed with a durable cast aluminum shell and a 5-wheel plastic coated aluminum base, this chair offers easy raise/lower action with its pneumatic chair adjust. Choose from a variety of shell colors and base finishes, and enjoy the option of plastic or full upholstery. With its stylish design and high-quality materials, this chair is a must-have for any modern workspace. Made in Italy.

如果我们希望在简单介绍后面,有一个产品ID和技术参数表,可以这样试下:At the end of the description, include every 7-character Product ID in the technical specification。

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 mid-century inspired office chair, perfect for both home and business settings. With a variety of shell colors and base finishes to choose from, this chair offers style and versatility. The 5-wheel plastic coated aluminum base and pneumatic chair adjustment ensure easy mobility and comfort. Made with high-quality materials, including a cast aluminum shell and HD36 foam seat, this chair is built to last. Available with or without armrests and suitable for any floor type. Product ID: SWC-100, SWC-110.

来个更复杂的prompt。我们希望得到一个参数表可能直接在网站展示(即以HTML的方式输出):第一列是参数维度;第二列是参数值。

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.

After the description, include a table that gives the product's dimensions. The table should have two columns. In the first column include the name of the dimension. In the second column include the measurements in inches only. Give the table the title 'Product Dimensions'.

Format everything as HTML that can be used in a website.
Place the description in a <div> element.

Technical specifications: '''{fact_sheet_chair}'''
"""

response = get_completion(prompt)
print(response)
<div>
  <h2>Product Description</h2>
  <p>
    Introducing our mid-century inspired office chair, a perfect addition to any home or business setting. This chair is part of a beautiful family of office furniture that includes filing cabinets, desks, bookcases, and meeting tables. With several options for shell color and base finishes, you can customize this chair to fit your unique style. Choose between plastic back and front upholstery or full upholstery in a variety of fabric and leather optic options. The base finish options include stainless steel, matte black, gloss white, or chrome. You can also choose to have the chair with or without armrests.
  </p>
  <p>
    The construction of this chair is top-notch. It features a 5-wheel plastic coated aluminum base, ensuring stability and durability. The pneumatic chair adjust allows for easy raise and lower action, providing maximum comfort and convenience.
  </p>
  <p>
    The dimensions of this chair are as follows:
  </p>
  <table>
    <caption>Product Dimensions</caption>
    <tr>
      <th>Width</th>
      <td>53 cm | 20.87"</td>
    </tr>
    <tr>
      <th>Depth</th>
      <td>51 cm | 20.08"</td>
    </tr>
    <tr>
      <th>Height</th>
      <td>80 cm | 31.50"</td>
    </tr>
    <tr>
      <th>Seat Height</th>
      <td>44 cm | 17.32"</td>
    </tr>
    <tr>
      <th>Seat Depth</th>
      <td>41 cm | 16.14"</td>
    </tr>
  </table>
  <p>
    Additional options for this chair include soft or hard-floor caster options and two choices of seat foam densities: medium (1.8 1b/ft3) or high (2.8 1b/ft3). You can also choose between armless or 8 position PU armrests, providing even more customization possibilities.
  </p>
  <p>
    The materials used in the construction of this chair are of the highest quality. The shell and base glider are made of cast aluminum with a modified nylon PA6/PA66 coating, ensuring durability and longevity. The seat is made of HD36 foam, providing exceptional comfort.
  </p>
  <p>
    This chair is proudly made in Italy, a country known for its craftsmanship and attention to detail.
  </p>
  <p>
    Product IDs: SWC-100, SWC-110
  </p>
</div>

让我们来显示一下这段HTML。

from IPython.display import display, HTML
display(HTML(response))

Product Description

Introducing our mid-century inspired office chair, a perfect addition to any home or business setting. This chair is part of a beautiful family of office furniture that includes filing cabinets, desks, bookcases, and meeting tables. With several options for shell color and base finishes, you can customize this chair to fit your unique style. Choose between plastic back and front upholstery or full upholstery in a variety of fabric and leather optic options. The base finish options include stainless steel, matte black, gloss white, or chrome. You can also choose to have the chair with or without armrests.

The construction of this chair is top-notch. It features a 5-wheel plastic coated aluminum base, ensuring stability and durability. The pneumatic chair adjust allows for easy raise and lower action, providing maximum comfort and convenience.

The dimensions of this chair are as follows:

Product Dimensions
Width 53 cm | 20.87″
Depth 51 cm | 20.08″
Height 80 cm | 31.50″
Seat Height 44 cm | 17.32″
Seat Depth 41 cm | 16.14″

Additional options for this chair include soft or hard-floor caster options and two choices of seat foam densities: medium (1.8 1b/ft3) or high (2.8 1b/ft3). You can also choose between armless or 8 position PU armrests, providing even more customization possibilities.

The materials used in the construction of this chair are of the highest quality. The shell and base glider are made of cast aluminum with a modified nylon PA6/PA66 coating, ensuring durability and longevity. The seat is made of HD36 foam, providing exceptional comfort.

This chair is proudly made in Italy, a country known for its craftsmanship and attention to detail.

Product IDs: SWC-100, SWC-110

一个关于产品的介绍网页就生成了。

上面几个例子,我们能看到通过不断的实验和迭代prompt,模型能逐步给出满意的结果。每次迭代,我们都在不断尝试、分析模型给出的结果、明晰指令&给模型思考的时间、以及给模型学习一些示例等等(见下图)。

提示工程(1):原则和示例

ChatGPT提示工程(Prompt Engineering)

最近正在学习吴恩达的AI课程。结合open AI chatGPT能力,理解和记录相关的知识。本文主要讲述提示工程(prompt engineering),及如何利用和验证大模型的总结、摘要、转换、扩展等能力。最后,会写一个为客户提供点餐服务的对方机器人。整体系列文章会包括以下内容:
1. 原则和示例
– 如何写明确具体的指令
– 给模型时间“思考”
2. 提示迭代开发(Iterative Prompt Development)
3. LLM主要能力示例
– 总结(Summarizing)
– 摘要(Inferring)
– 转换(Transforming)
– 扩展(Expanding)
4. 创新聊天机器人(chatbot)

第一部分:提示工程(Prompt Engineering)的原则和示例

1. LLM的分类

我们通常把大语言模型(LLM)分为2类,一类是基础的LLM;一类是指令调优过的LLM(见下图)。
– 基础的LLM基于文本训练数据,预测下个单词;
– 指令调优化LLM会遵循指令,提供更有价值、准确的预测。其实也是指令高估的目的。
调优的方法是基于人类反馈的强化学习(RLHF,Reinforcement Learning withHuman Feedback)。我们这一系列讲的提示工程(prompt engineering)m会讲解调优的最佳实践。

设想LLM是一位刚刚入职的应届毕业生,需要他去处理一些事务。我们需要十分清晰和具体的指令,给他们一些以前任务完成的样板,甚至一些任务的背景资料等等,他们才能较好的完成,给出我们期待的结果。接下来,我们来讲述如何编写清晰和具体的指令。

2. 提示的原则一:清晰和具体的指令

方法1: 利用分隔符

  • 双引号 “””
  • 单引号 ”’
  • 扛 – – –
  • 尖括号
  • XML 标签

我们来试下分隔符的用法。

import openai
openai.api_key="用你的api key,以ski-开头"

get_completion函数,用于辅助执行LLM交互。
其中输入:提示、采用的gpt模型(默认为gpt-3.5-turbo);
其中输出:gpt返回的文字。

def get_completion (prompt, model="gpt-3.5-turbo"):  # 使用 gpt 3.5 turbo模型
    messages = [{"role": "user", "content": prompt}] 
    response = openai.ChatCompletion.create(  # 使用openai的ChatCompletion函数
        model=model, 
        messages=messages, 
        temperature=0 # 预测的随机程序
    ) 
    return response.choices[0].message["content"]

下例我们合适分隔符:

# 用'''做为分隔符,清晰告知 LLM 所引用的文字

text = f"""
You should express what you want a model to do by \
providing instructions that are as clear and \
specific as you can possibly make them. \
This will guide the model towards the desired output, \
and reduce the chances of receiving irrelevant \
or incorrect responses. Don't confuse writing a \
clear prompt with writing a short prompt. \
In many cases, longer prompts provide more clarity \
and context for the model, which can lead to \
more detailed and relevant outputs.
"""

prompt = f"""
Summarize the text delimited by triple backticks \
into a single sentence.
'''{text}'''
"""

response = get_completion(prompt)
print(response)
To guide a model towards the desired output and reduce irrelevant or incorrect responses, it is important to provide clear and specific instructions, which may require longer prompts for more clarity and context.

这个例子也许看不出分隔符有什么明显的作用。
试想一下,当你的提示prompt与所有引用的文字刚好有冲突的要求(例如:prompt要求对某个主题做总结,而所引用文字提到对某个主题要展开),这时LLM理解与我们预期可能就不一样的。分隔符在这类场景会特别有用。

方法2: 结构化输出

如:HTML、JSON

prompt = f"""
Generate a list of three made-up book titles along \ 
with their authors and genres.
Provide them in JSON format with the following keys: 
book_id, title, author, genre.
"""

response = get_completion(prompt)
print(response)
{
  "books": [
    {
      "book_id": 1,
      "title": "The Enigma of Elysium",
      "author": "Evelyn Sinclair",
      "genre": "Mystery"
    },
    {
      "book_id": 2,
      "title": "Whispers in the Wind",
      "author": "Nathaniel Blackwood",
      "genre": "Fantasy"
    },
    {
      "book_id": 3,
      "title": "Echoes of the Past",
      "author": "Amelia Rivers",
      "genre": "Romance"
    }
  ]
}

方法3: 检查执行某任务的条件或假设是否满足

我们让LLM将制作一杯茶的步骤,从一段文字里整理出来。请见下面示例:

text_1 = f"""
Making a cup of tea is easy! First, you need to get some \
water boiling. While that's happening, \
grab a cup and put a tea bag in it. Once the water is \
hot enough, just pour it over the tea bag. \
Let it sit for a bit so the tea can steep. After a \
few minutes, take out the tea bag. If you \
like, you can add some sugar or milk to taste. \
And that's it! You've got yourself a delicious \
cup of tea to enjoy.
"""

prompt =f"""
You will be provided with text delimited by triple quotes.\
If it contains a sequence of instructions, \
re-write those instructions in the following format:
Step 1 - ...x
Step 2 - ...
...
Step N - ...

If the text does not contain a sequence of instructions, \
then simply write \"No steps provided.\"

\"\"\"\{text_1}\"\"\"
"""

response = get_completion(prompt)
print("Completion for Text 1:")
print(response)
Completion for Text 1:
Step 1 - Get some water boiling.
Step 2 - Grab a cup and put a tea bag in it.
Step 3 - Pour the hot water over the tea bag.
Step 4 - Let the tea steep for a few minutes.
Step 5 - Take out the tea bag.
Step 6 - Add sugar or milk to taste.
Step 7 - Enjoy your cup of tea.

从上面的例子,chatGPT已经把步骤整理出来了。
我们再用一个例子,里面不包括制作的步骤,看看我们会得到什么?请见下面例子。

text_2 = f"""
The sun is shining brightly today, and the birds are \ 
singing. It's a beautiful day to go for a \ 
walk in the park. The flowers are blooming, and the \
trees are swaying gently in the breeze. People \ 
are out and about, enjoying the lovely weather. \ 
Some are having picnics, while others are playing \ 
games or simply relaxing on the grass. It's a \
perfect day to spend time outdoors and appreciate the \
beauty of nature.
"""

prompt =f"""
You will be provided with text delimited by triple quotes.\
If it contains a sequence of instructions, \
re-write those instructions in the following format:
Step 1 - ...x
Step 2 - ...
...
Step N - ...

If the text does not contain a sequence of instructions, \
then simply write \"No steps provided.\"

\"\"\"\{text_2}\"\"\"
"""

response = get_completion(prompt)
print("Completion for Text 2:")
print(response)
Completion for Text 2:
No steps provided.

方法4: Few-shot prompting

在模型执行任务之前,先给一些任务完成的示例

下面的例子先给了模型一串老人与小孩的对话,让模型以这种方式和风格继续这种对话。

prompt = f"""
Your task is to answer in a consistent style.

<child>: Teach me about patience.
«grandparent›: The river that carves the deepest \ 
valley flows from a modest spring: the \
grandest symphony originates from a single note; \
the most intricate tapestry begins with a solitary thread.

<child>: Teach me about resilience.
"""

response = get_completion(prompt)
print(response)
«grandparent›: Resilience is the unwavering strength that emerges from facing adversity. It is the ability to bounce back, to rise above challenges, and to persevere even when the odds seem insurmountable. Like a mighty oak tree that withstands the fiercest storms, resilience is the foundation of inner fortitude and growth.

3. 提示的原则二:给模型时间思考

方法1: 明确完成任务的步骤


step 1: ...
step 2: ...
...
step N: ...

以下的例子,明确了完成任务的步骤。

text = f"""
In a charming village, siblings Jack and Jill set out on \
a quest to fetch water from a hilltop \
well. As they climbed, singing joyfully, misfortune \
struck-Jack tripped on a stone and tumbled \
down the hill, with Jill following suit. \
Though slightly battered, the pair returned home to \
comforting embraces. Despite the mishap, \
their adventurous spirits remained undimmed, and they \ 
continued exploring with delight.
"""

# example 1
prompt_1 = f"""
Perform the following actions:
1 - Summarize the following text delimited by triple \
backticks with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a JSON object that contains the following \
keys: french_summary, num_names.

Separate your answers with line breaks.

Text:
'''{text}'''
"""

response = get_completion(prompt_1)
print("Completion for promt 1:")
print(response)
Completion for promt 1:
1 - Jack and Jill, siblings, go on a quest to fetch water from a well on a hill, but they both fall down the hill after Jack trips on a stone, yet they return home and continue exploring with joy.

2 - Jack et Jill, frère et sœur, partent en quête d'eau d'un puits situé au sommet d'une colline, mais ils tombent tous les deux après que Jack trébuche sur une pierre, cependant ils rentrent chez eux et continuent à explorer avec joie.

3 - Jack, Jill.

4 - {
  "french_summary": "Jack et Jill, frère et sœur, partent en quête d'eau d'un puits situé au sommet d'une colline, mais ils tombent tous les deux après que Jack trébuche sur une pierre, cependant ils rentrent chez eux et continuent à explorer avec joie.",
  "num_names": 2
}
# example 2, asking for output in a specified format
prompt_2 = f"""
Your task is to perform the following actions:
1 - Summarize the following text delimited by <> with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the 
    following keys: french_summary, num_names.

Use the following format:
Text: <text to summarize>
Summary: <summary>
Translation: <summary translation>
Names: <list of names in Italian summary>
Output JSON: <json with summary and num_names>
Text to summarize: <{text}>
"""

response=get_completion(prompt_2)
print("\nCompletion for prompt 2:")
print(response)
Completion for prompt 2:
Summary: Jack and Jill, siblings from a charming village, go on a quest to fetch water from a hilltop well but encounter misfortune along the way. 
Translation: Jack et Jill, frère et sœur d'un charmant village, partent en quête d'eau d'un puits au sommet d'une colline mais rencontrent des malheurs en chemin.
Names: Jack, Jill
Output JSON: {"french_summary": "Jack et Jill, frère et sœur d'un charmant village, partent en quête d'eau d'un puits au sommet d'une colline mais rencontrent des malheurs en chemin.", "num_names": 2}

方法2: 指示模型在做出结论之前推理出自己的解决方案

我们让模型来解一道应用题。先给出题目,并给出学生的解答。让模型判断一下学生的解答是否正确。

prompt = f"""
Determine if the student's solution is correct or not.

Question:
I'm building a solar power installation and I need \
help working out the financials.

- Land costs 100 / square foot
- I can buy solar panels for250 / square foot
- I negotiated a contract for maintenance that will cost \
me a flat 100k per year, and an additional10 / square \
foot

what is the total cost for the first year of operations 
as a function of the number of square feet.

Student's Solution:
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
"""

response = get_completion(prompt)
print(response)
The student's solution is correct. The total cost for the first year of operations is indeed 450x + 100,000, where x is the size of the installation in square feet.

模型认为学生的解答是正确的。实际上学生解错了。

现在我们用这个方法来纠正模型的这个错误呢?下面的提示是这样写的:
第一步:模型自己来解答这道题
第二步:与学生的解答相比较,评估学生解答是否正确
并且,我们明确了输出格式,如:问题、解答、步骤等等

prompt = f"""
Your task is to determine if the student's solution is correct or not.
To solve the problem do the following:
- First, work out your own solution to the problem.
- Then compare your solution to the student's solution and evaluate if the student's solution is correct or not.
Don't decide the student's solution is correct until you have dork the problem yourself.

Use the following format:
Question:
'''
question here
'''
Student's solution:
'''
student's solution here
'''
Actual solution:
'''
steps to work out the solution and your solution here
'''
Is the student's solution the same as actual solution just calculated:
'''
yes or no
'''
Student grade:
'''
correct or incorrect
'''

Question:
I'm building a solar power installation and I need \
help working out the financials.

- Land costs 100 / square foot
- I can buy solar panels for250 / square foot
- I negotiated a contract for maintenance that will cost
me a flat 100k per year, and an additional10 / square foot

what is the total cost for the first year of operations 
as a function of the number of square feet.

Student's Solution:
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
"""

response = get_completion(prompt)
print(response)
Actual Solution:
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100 * x =100x
2. Solar panel cost: 250 * x =250x
3. Maintenance cost: 100,000 +10 * x = 100,000 +10x
Total cost: 100x +250x + 100,000 +10x = 360x +100,000

Is the student's solution the same as actual solution just calculated:
No

Student grade:
Incorrect

通过这些指令,模型发现了学习解答是错误的。

如何搭建open AI运行环境?

为了能够试验openAI的一些功能,搭建一个Jupyter Notebook的环境是很好的选择。下面将指导你完成搭建环境的过程,并引入相关的库,以便可以开始试验openAI。

步骤1:安装Jupyter Notebook

Jupyter Notebook是一个交互式的Web应用程序,允许创建和共享包含实时代码、方程式、可视化和叙述性文本的文档。
你可以通过以下命令使用pip安装Jupyter Notebook:pip install jupyter

步骤2:创建一个新的Jupyter Notebook

  • 在终端输入以下命令来启动Jupyter Notebook:jupyter notebook
  • 这将在你的默认Web浏览器中打开一个新的Jupyter Notebook窗口。

步骤3:安装所需的库

在Jupyter Notebook中,你可以使用以下命令来安装需要的库:!pip install openai
– openAI的Python库,用于访问openAI的功能。

步骤4:开始试验openAI

在安装完所需的库之后,你可以开始在Notebook中试验openAI的功能。你可以查阅openAI的官方文档来了解如何使用其API。以下是一个简单的示例代:

import openai  

# 设置API密钥  
openai.api_key = 'YOUR_API_KEY'  

# 生成文本
response = openai.Completion.create(engine='text-davinci-003', prompt='Hello, world!', max_tokens=100)  
print(response.choices[0].text)


### 1. Python版本问题

在运行`!pip install openai`出现以下错误。

```shell
× Building wheel for aiohttp (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [98 lines of output]
× Building wheel for yarl (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [53 lines of output]

将python降级到3.11.0,即可解决。具体方法如下:
a. 下载和安装

b. 安装openai
python3.11 -m pip install openai

1. Python版本问题

在运行!pip install openai出现以下错误。

× Building wheel for aiohttp (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [98 lines of output]
× Building wheel for yarl (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [53 lines of output]

将python降级到3.11.0,即可解决。具体方法如下:
a. 下载和安装

b. 安装openai
python3.11 -m pip install openai

Troubleshooting

2. Jupyter notebook找不到Python 3.11版本怎么办?

a. 检查jupyter可用到的kernels
jupyter kernelspec list

Available kernels:
  python3.11    /Users/leying/Library/Jupyter/kernels/python3.11
  python3       /Library/Frameworks/Python.framework/Versions/3.11/share/jupyter/kernels/python3

正常情况下,应该显示上述3.11的kernel。如果找不到3.11,则运行这个命令:python3.11 -m ipykernel install --user --name=python3.11,出现以下提示即配置kernel成功了。

Installed kernelspec python3.11 in /Users/leying/Library/Jupyter/kernels/python3.11

这时,重新启动Jupyter Notebook,即可以选择3.11 kernel了。

3.OpenAI > 1.0版本的升级

OpenAI在升级1.0版本之后,会出现AttributeError: module ‘openai’ has no attribute ‘ChatCompletion’错误。

由于ChatCompletion这个方法已经没有了。要用completions替代。具体的代码如下:

# old
# import openai
# openai.api_key="your key"

# new
from openai import OpenAI
client = OpenAI(
    api_key = "your key"
)

# old
# def get_completion (prompt, model="gpt-3.5-turbo"):  # 使用 gpt 3.5 turbo模型
#    messages = [{"role": "user", "content": prompt}] 
#    response = openai.ChatCompletion.create(  # 使用openai的ChatCompletion函数
#        model=model, 
#        messages=messages, 
#        temperature=0 # 预测的随机程序
#    ) 
#    return response.choices[0].message["content"]

# new
def get_completion (prompt, model="gpt-3.5-turbo"):  # 使用 gpt 3.5 turbo模型
    messages = [{"role": "user", "content": prompt}] 
    response = client.chat.completions.create (  # 使用openai的ChatCompletion函数
        model = model,
        messages=messages
    )
    return response.choices[0].message.content

如何搭建WordPress个人博客

一直想有自己的一个博客,记录自己学习、工作和生活方面的笔记和感悟。趁这段空闲时间搭建了一下,比自己想像中要简单很多。以后要多静下心来,记录下点点滴滴。

下面讲一下WordPres搭建方法和步骤:

一、方法

在腾讯云上购买一台WordProcess服务器,关联上域名即可。

二、步骤

1. 购买域名

为了省事,我直接通过腾讯云注册域名。https://dnspod.cloud.tencent.com

2. 购买WordPress服务器

a. 选择最便宜的服务器。因为只是为了个人博客使用,最低配置即可。如真有访问量大或需要更大的存储,可以随时升级.
b. 如为了节省备案的时间,可以选择境外的服务器.
c. 选择WordPress应用模板。原来我还试过选择“宝塔Linux面板”,再在宝塔内安装WordPress。后来感觉这样更麻烦,且效果也没有分别。所以,直接选择WordPress应用模板.

3. 访问自己的博客

完成上个步骤之后,我们会有一个公网的IP地址。直接在浏览器输入该IP地址,即可访问自己的博客了.
公网IP在哪里找?在控制台 –> 轻量应用服务器 –> 选择你购买的服务器 –> 网络与域名.

在网络与域名卡片里,会有1个公网IP和内网IP。公网IP是可以让大众访问到的IP地址,也是随后域名要关联的IP地址.

4. 域名绑定

在上述”网络与域名”里,点击“管理域名”。当在腾许购买了域名和轻量服务器,向导会直接引导配置,基本上周点“下一步”就可以完成了.

5. 配置WordPress

a. 找到WordPress管理员密码在控制台找到应用管理,在应用内软件信息卡片里。找到下面截图内容,点击“登录”.

b. 自动进行客户端。复制粘贴刚的命令,即可出现管理员的用户名和密码.

c. WordPress后台登录
登录的网址为:公网ip地址/admin 或 域名/admin.

三、完成

至此,WordPress即配置成功。接下来,我们可以挑选WordPress主题,以找到自巴喜欢的网站或者个人博客风格。另外,我们也可以建立用户,用于更新个人博客.
如有遗漏或错误之处,欢迎大家指正.