AI prompt course -- first day

AI prompt course -- first day

AI prompt course notes

1 Two types of LLMs

  • Base LLMs
  • Instruct tuned LLMs (i.e., ChatGPT, DeepSeek, et al.)

2 Environment variables

Use .env file to provide parameters for Jupyter or Python. An example for a .env file can be

1
2
OPENAI_API_KEY='sk-***'
OPENAI_BASE_URL='https://api.siliconflow.cn/v1' # Here we use SiliconFlow's free models

Save this file in your Python or Jupyter source code directory. Then, you can load these parameters by the following code:

1
2
3
4
5
6
import os
from openai import OpenAI # version 1.78.0

# load .env to environment variables
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

We use the Python library provided by OpenAI, openai, to chat with models.

1
2
3
4
5
6
7
8
9
client = OpenAI()
def get_completion(prompt, model="Qwen/Qwen2.5-7B-Instruct", temperature=0.6):
messages = [{"role": "user", "content": prompt}]
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
)
return response.choices[0].message.content

Here, we generate a new object named client, and define a function named get_completion. The main part of this function is the request from client, which needs two key parameters: model and messages. The parameter temperature is optional and its function is adjust the creativity of LLM. The higher the value, the more creative the response. Generally, when we employ LLMs as translators, the value is typically set below 0.4. We set the default model as Qwen2.5-7B-Instruct, the newest open-source model provided by a Chinese company, Alibaba. The variable messages is a single-element list with a dictionary. In the dictionary, the key “role” can be “user” or “system”, while the key “content” is the prompt or instruct, respectively. Now, we don’t set any instruct for this model, and directly get the response from the one-time request.

3 Two principles in prompting

Principle 1: Write clear and specific instructions

1) Use delimiters to clearly indicate distinct parts of the input

Delimiters can be anything like: ```, “””, <tag></tag>, :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.
<input>{text}</input>
"""
content = get_completion(prompt=prompt, temperature=0.6)
print(content)
Output
To guide a model effectively, provide clear and specific instructions, often in a longer format, to reduce irrelevant or incorrect responses and ensure more detailed and relevant outputs.

2) Ask for a structural output like JSON and HTML

Requiring the model provide a specific format can help your next step.

1
2
3
4
5
6
7
8
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)
Output
```json
[
    {
        "book_id": "001",
        "title": "Whispers of the Wraith",
        "author": "Eleanor Blackwood",
        "genre": "Fantasy"
    },
    {
        "book_id": "002",
        "title": "The Last Starlight Sonata",
        "author": "Liam Fletcher",
        "genre": "Science Fiction"
    },
    {
        "book_id": "003",
        "title": "Murder at the Mosaic Market",
        "author": "Sophie Hart",
        "genre": "Mystery"
    }
]
```

3) Ask the model to check whether the conditions are satisfied

Maybe some of the input is not satisfy your requirement, and you can ignore them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Sample 1
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 - ...
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)
Output
Completion for Text 1:
Step 1 - Get some water boiling.
Step 2 - Grab a cup and put a tea bag in it.
Step 3 - Once the water is hot enough, pour it over the tea bag.
Step 4 - Let it sit for a bit so the tea can steep.
Step 5 - After a few minutes, take out the tea bag.
Step 6 - If you like, add some sugar or milk to taste.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Sample 2
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 - ...
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)
Output
Completion for Text 2:
No steps provided.

4) “Few-shot” prompting

Giving some samples can help models learn the output styles.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)
Output
<grandparent>: Imagine a little sapling in a garden. It faces many challenges—dry spells, strong winds, even the shadow of a taller tree. Yet, it doesn't give up. Instead, it learns to bend and stretch, growing stronger with each obstacle. Just like that sapling, resilience is about not letting difficulties defeat you. It's about finding the strength to keep going, even when things get tough. Remember, every great journey starts with a single step, and every great achievement is built upon countless small efforts.

Principle 2: Give the model time to “think”

1) Specify the steps required to complete a task

Avoid models giving wrong answers too hastily.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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: <{text}>
"""
response = get_completion(prompt_2)
print("\nCompletion for prompt 2:")
print(response)
Output
Completion for prompt 2:
Summary: Jack and Jill, siblings, embark on a quest to fetch water but have a mishap on the hill, yet their spirits remain adventurous.
Translation: Jack et Jill, frères et sœurs, entreprennent une quête pour ramener de l'eau mais ont un malheur sur la colline, mais leurs esprits restent aventuriers.
Names: Jack, Jill
Output JSON: 
{
  "french_summary": "Jack et Jill, frères et sœurs, entreprennent une quête pour ramener de l'eau mais ont un malheur sur la colline, mais leurs esprits restent aventuriers.",
  "num_names": 2
}

2) Instruct the model to work out its own solution before running to a conclusion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 if the student's solution is correct until
you have done 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 for $250 / square foot
- I negotiated a contract for maintenance that will cost \
me a flat $100k per year, and an additional $10 / 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
```
Actual solution:
"""
response = get_completion(prompt)
print(response)
Output
```
Let x be the size of the installation in square feet.
 
1. Land cost: $100 per square foot, so the cost for x square feet is 100x.
2. Solar panel cost: $250 per square foot, so the cost for x square feet is 250x.
3. Maintenance cost: $100,000 per year plus $10 per square foot, so the cost for x square feet is 100,000 + 10x.
 
The total cost for the first year of operations is the sum of these three costs:
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
```
 
The student's solution is incorrect because they incorrectly added the maintenance cost term. The maintenance cost should be $100,000 per year plus $10 per square foot, which simplifies to 10x, not 100x. Therefore, the correct total cost function is \(360x + 100,000\).

4 Model limitations: Hallucinations

Boie is a real company, the product name is not real.

1
2
3
4
5
prompt = f"""
Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
"""
response = get_completion(prompt)
print(response)
Output
The AeroGlide UltraSlim Smart Toothbrush by Boie is a modern, compact, and sleek electric toothbrush designed to provide an efficient and comfortable brushing experience. Here are some key features and details about this product:
 
### Features:
1. **Slim Design**: The toothbrush is ultra-slim and lightweight, making it easy to handle and store. Its compact size is particularly beneficial for those who have limited space or find traditional toothbrushes cumbersome.
 
2. **Advanced Cleaning Technology**: Equipped with advanced cleaning technology, the AeroGlide UltraSlim aims to deliver a thorough and efficient clean. It often features multiple brush heads with different bristle strengths and shapes to cater to various dental needs.
 
3. **Smart Features**: Many models of the AeroGlide UltraSlim come with smart features such as:
   - **Pressure Sensors**: These sensors monitor the pressure applied during brushing and alert users if they are brushing too hard.
   - **Timer Function**: The toothbrush typically has a built-in timer to ensure users brush for the recommended two minutes.
   - **Brushing Modes**: Users can choose from different brushing modes, such as sensitive, whitening, and gum care, to target specific dental needs.
   - **App Integration**: Some models can connect to a mobile app, which provides feedback on brushing habits and allows for tracking progress over time.
 
4. **Ease of Use**: The brush is designed to be user-friendly, with intuitive controls and a straightforward interface. It often comes with a charging base and a travel case for easy storage and transport.
 
5. **Durability**: Boie emphasizes the durability of their products, ensuring that the brush and its components are built to last and withstand regular use.
 
### User Experience:
The AeroGlide UltraSlim is designed to provide a comfortable and effective brushing experience. Its slim design and smart features make it a popular choice among those who prioritize convenience and modern technology in their oral hygiene routine.
 
### Conclusion:
The Boie AeroGlide UltraSlim Smart Toothbrush is a contemporary and practical choice for individuals seeking a high-quality, efficient, and smart toothbrush. Its combination of advanced technology, user-friendly design, and smart features makes it a commendable option for maintaining good oral health in a modern lifestyle.

Reference

作者

CodeFox

发布于

2025-05-10

更新于

2025-05-18

许可协议

评论