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 | OPENAI_API_KEY='sk-***' |
Save this file in your Python or Jupyter source code directory. Then, you can load these parameters by the following code:
1 | import os |
We use the Python library provided by OpenAI, openai, to chat with models.
1 | client = OpenAI() |
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 | text = f""" |
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 | prompt = f""" |
```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 | # Sample 1 |
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 | # Sample 2 |
Completion for Text 2: No steps provided.
4) “Few-shot” prompting
Giving some samples can help models learn the output styles.
1 | prompt = f""" |
<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 | prompt_2 = f""" |
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 | prompt = f""" |
``` 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 | prompt = f""" |
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
AI prompt course -- first day
https://codefoxs.github.io/2025/05/10/15-AI-prompt-beginner/