---
# System prepended metadata

title: 1/2 ChatGPT Prompt Engineering for Developers

---

---
#該區塊是此page style設定，更動時注意!
slideOptions:
slideNumber: true #顯示頁碼
# type: slide #投影片模式
tags: LLM,LangChain,TAIA,HIT #設定標籤
disqus: hackmd #允許讀者在文件下方留下評論

---

[TOC] 

---

# **ChatGPT Prompt Engineering for Developers**
學習課程: [ChatGPT Prompt Engineering for Developers](https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/)
學習參考的文章: 
1. [第四組張紘嘉的LLM讀書會第二堂課預習筆記](https://hackmd.io/@wQJtnj7sQEagWAEtO8foqw/hongallm_previe)
2. Installation and configuration(Windows)
    - [Jupyter Notebook 完整介紹、安裝及使用說明](https://medium.com/ai-for-k12/jupyter-notebook-%E5%AE%8C%E6%95%B4%E4%BB%8B%E7%B4%B9-%E5%AE%89%E8%A3%9D%E5%8F%8A%E4%BD%BF%E7%94%A8%E8%AA%AA%E6%98%8E-846b5432f044) 
    - [OpenAI ChatGPT 帳號如何註冊？如何取得API金鑰？](https://www.cool3c.com/article/193336)
3. [OpenAI 《Best practices for prompt engineering with OpenAI API》](https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api)
4. [All code for "ChatGPT Prompt Engineering for Developers"](https://github.com/jojoee/chatgpt-prompt-engineering-for-developers) 
5. [ihower's ChatGPT Prompt Engineering for Developers](https://ihower.tw/notes/%E6%8A%80%E8%A1%93%E7%AD%86%E8%A8%98-AI/OpenAI/ChatGPT+Prompt+Engineering+for+Developers)
6. [1小時初學者教學-製作你的AI app](https://www.youtube.com/watch?v=8GsLbNdbV6c&t=3727s)
[](https://realpython.com/practical-prompt-engineering/#describe-your-request-in-numbered-steps)              
[](https://ec26ubh65w.feishu.cn/docx/PuULdQP3wojyZYxn157cnsDXnqe)

     
## 如何設置 OPEN AI Libraryand OPEN API KEY
### OPEN AI Library
#### Check Open AI version, helper function 的寫法會不同
![openai_library_greater_than_1](https://hackmd.io/_uploads/r16hdeaPT.png)
  - 如果是1.0.0 以上
  helper function:
  ```
  client = openai.OpenAI()

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0
    )
    return response.choices[0].message.content
  ```
  - 如果是1.0.0 以下(例如0.27.0)
  ```
  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"]
  ```
  ### OPEN API KEY
  #### 方法1
  openai.api_key ="sk-xxxxxxxxsxxx"
  #### 方法2
  在Window 環境變數裡, 設定OPENAI_API_KEY(:warning:如果已經開了Jupyter, 請重開.)
  ![open_ai_set_in_windows_environment](https://hackmd.io/_uploads/rk3mjx6vp.png)
  ```
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

openai.api_key  = os.getenv('OPENAI_API_KEY')
print(openai.api_key)
  ```
  #### 方法3 載入環境文件
  環境文件 = .env 文件(請放在jupyter同一層)
  ![open_ai_key_in_env](https://hackmd.io/_uploads/H13T0gpv6.png)
```
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

openai.api_key  = os.getenv('OPENAI_API_KEY')
print(openai.api_key)
```

## Guidelines for Prompting
### Prompting Principles
- Principle 1: Write clear and specific instructions
    - Tactic 1: Use delimiters to clearly indicate distinct parts of the input(請用分隔符號來明確界定輸入的不同部分也可以避免 Prompt injection: 指用戶輸入一些 prompt 讓你對模型發出你不想做的事情)
        - Delimiters can be anything like: ```, """, < >, <tag> </tag>
        - ![Delimiters ](https://hackmd.io/_uploads/r1co4-aDT.png)

    - Tactic 2: Ask for a structured output (輸出的格式)
        - JSON, HTML
        - ![JSON](https://hackmd.io/_uploads/r1czSWaPa.png)

    - Tactic 3: Ask the model to check whether conditions are satisfied(指定滿足條件)
        - 例子1: 食譜的做法步驟
            - ![conditions](https://hackmd.io/_uploads/ByaYD-6v6.png)

        - 例子2: 只是一段話的描述
            - ![no_coditions](https://hackmd.io/_uploads/SJjydb6P6.png)
    - Tactic 4: "Few-shot" prompting(給範例)
        - ![few-shot](https://hackmd.io/_uploads/BJpHibTDa.png)



- Principle 2: Give the model time to “think”
    - Tactic 1: Specify the steps required to complete a task¶
        - 1. 使用指定的步驟去處理一個任務.
            - 方法1:
            *Input*:
            ![Specify the steps required to complete a task¶](https://hackmd.io/_uploads/rJhYnHawT.png)
            *Output*:
            ![Specify the steps_output](https://hackmd.io/_uploads/r1R5hBav6.png)
            
            問題: 一開始在Summary的內容, 只會出現 "The text is about siblings Jack and Jill who go on a quest to fetch water from a hilltop well",我就直接去ChatGPT去執行. 然後, 我就加入一段文字(如下), 在重新執行. 就解決了. 但還在學習更好的解決方法
            ![Screenshot 2023-12-30 155952](https://hackmd.io/_uploads/B1WKAHpDT.png)
            - 方法2:
            Ask for output in a specified format
            使用以上的方法, 可確保輸出格式更為規範化
            ![Ask for output in a specified format](https://hackmd.io/_uploads/H1nKlUTD6.png)

    - Tactic 2: Instruct the model to work out its own solution before rushing to a conclusion
        - 我們要如何知道推論結果是否是直接根據結論去判斷對錯. 有那些理解錯誤.
        請模型自己列出思考全部過程, 是一個好的方法
        例子: 下列的直接給出結果,得到不正確的回答 
        ![directly_prompt](https://hackmd.io/_uploads/HJWOQ8aDp.png)
        我們修改指令讓模列思考全部過程, 得到正確的回答
        ![fix_directly_prompt](https://hackmd.io/_uploads/HkzDHITDT.png)
        ![fix_directly_promp_outputt](https://hackmd.io/_uploads/HkBpNIava.png)
- Model Limitations: Hallucinations(幻覺)
    舉例: 我用三星的A53去當成APPLE的產品
    ![hallucinate](https://hackmd.io/_uploads/H1d9PUaDp.png)
    註解: LLM 幻覺 影片
         https://www.youtube.com/watch?v=JvLiEdTKKgk
         https://www.youtube.com/watch?v=Z7DNizgA3zU
         https://www.youtube.com/watch?v=EerVxweAKlI
         (仍然在學習中)
## Iterative Prompt Development(如何一步步地檢查和改進你的指令)
在這一章, 要不斷根據結果調整指令，直到達到最終的目標，這個過程就是迭代式的提示開發
### Version0
fact sheet chair 的文字
::::info
:::spoiler fact_sheet_chair = """
OVERVIEW
- Part of a beautiful family of mid-century inspired office furniture, 
including filing cabinets, desks, bookcases, meeting tables, and more.
- 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 options.
- 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”
- DEPTH 51 CM | 20.08”
- HEIGHT 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 lb/ft3) or high (2.8 lb/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 文字
::::info
:::spoiler 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)
::::

Version 0 Output: 這個輸出的文字太長了.
![iterative_version0_output](https://hackmd.io/_uploads/SJG3pI6wT.png)

### Version1(限制文字輸出的長度或幾句話或幾段絡)

prompt 文字修改限制50字
::::info
:::spoiler 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)
::::
Version 1 Output
![iterative_version1_output](https://hackmd.io/_uploads/HygaAUaDT.png)
>> 可以使用len(response.split()) 去確認多少字. 大約在50字上下.

### Version2 (輸出的結果, 關注在不是我們想要的細節)
### Verson2-1
要寫我們需要的細節
The description is intended for furniture retailers, 
so should be technical in nature and focus on the 
materials the product is constructed from.
(這段描述是給家具零售商用的，因此它應該包含專業術語，並且著重介紹產品的材質構成)
prompt 文字
::::info
:::spoiler 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)

::::
Version 2-1 Output
![iterative_version2_output](https://hackmd.io/_uploads/HJg71Ktaw6.png)
我就使用ChatGPT去比對Version 1的Output, 得到以下的結果
![iterative_version2_compare](https://hackmd.io/_uploads/SJvOtKTva.png)

### Version2-2
At the end of the description, include every 7-character Product ID in the technical specification.(
希望模型可以在描述的最後，能加上技術規範中規定的 7 個字元產品ID。)

prompt 文字
::::info
:::spoiler 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)
::::
Version 2-2 Output
![iterative_version2-2_output](https://hackmd.io/_uploads/ryI69KawT.png)

### Version3
Description needs a table of dimensions
```
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.
(要求模型將輸出資訊並將其整理成表格,例如HTML '<div>格式。)
```
prompt 文字
::::info
:::spoiler 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)
::::
Version 3 Output
    ![iterative_version3_output](https://hackmd.io/_uploads/SJLDhFTvp.png)

加載Python函式庫(IPython)來顯示HTML內容 
```
    from IPython.display import display, HTML
    display(HTML(response))
```
輸出畫面如下:
![iterative_version3_output_to_html](https://hackmd.io/_uploads/BkaxAF6w6.png)

相關文章: 
    1. [Iterative Prompt Development](https://medium.com/@aanshsavla2453/iterative-prompt-development-14252e0597f5)
    2. [Prompt Engineering 提示工程總結 – 用戶篇](https://www.samsonhoi.com/1032/prompt-engineering-for-users)(相關: Iterative Prompt Development 迭代提示開發)
    
## Summarizing(摘要)
這一章, 主要學習如何讓模型更好地幫我們摘要文本內容，將重點放在特定的主題上。

### 摘要1

prod_review 內容
::::info
:::spoiler 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字
```
Summarize the review below, delimited by triple 
backticks, in at most 30 words.   
```
prompt 文字
::::info
:::spoiler 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)
::::
摘要1 Output
![summary_version1_output](https://hackmd.io/_uploads/rk4uPc6w6.png)
### 摘要2
Summarize with a focus on shipping and delivery. 例子:
```
Summarize the review below, delimited by triple 
backticks, in at most 30 words, and focusing on any aspects \
that mention shipping and delivery of the product. 
```
prompt 文字
::::info
:::spoiler prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
Shipping deparmtment. 

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

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

response = get_completion(prompt)
print(response)
::::
摘要2 Output
![summary_version2_output](https://hackmd.io/_uploads/ryj-O5pwa.png)

### 摘要3
Summarize with a focus on price and value. 例子:
```
Summarize the review below, delimited by triple 
backticks, in at most 30 words, and focusing on any aspects \
that are relevant to the price and perceived value.
```
prompt 文字
::::info
:::spoiler prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
pricing deparmtment, responsible for determining the \
price of the product. 

Summarize the review below, delimited by triple 
backticks, in at most 30 words, and focusing on any aspects \
that are relevant to the price and perceived value. 

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

response = get_completion(prompt)
print(response)
::::
摘要3 Output
![summary_version3_output](https://hackmd.io/_uploads/HkJMK56P6.png)

### Try "extract" instead of "summarize"
“摘要”和“提取”看似相同，但它們可能導致不同的輸出。當你要求模型進行“摘要”時，它可能會改寫或濃縮信息。然而，“提取”通常意味著直接從文本中拉出關鍵信息而不重新措辭。因此，如果你想要直接從文本中獲得關鍵信息的片段，可以考慮在你的指令中使用“提取”, 例子:
```
From the review below, delimited by triple quotes \
extract the information relevant to shipping and \ 
delivery. Limit to 30 words. 
```

prompt 文字
::::info
:::spoiler prompt = f"""
Your task is to extract relevant information from \ 
a product review from an ecommerce site to give \
feedback to the Shipping department. 

From the review below, delimited by triple quotes \
extract the information relevant to shipping and \ 
delivery. Limit to 30 words. 

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

response = get_completion(prompt)
print(response)
::::
Extract Output
The relevant information for the Shipping department is that the product arrived a day earlier than expected.

將上面的summarize and extract 輸出, 使用ChatGPT指出兩者的不同?
![summary_version2_output_summarize_and_extract](https://hackmd.io/_uploads/SyI6n96PT.png)

### Summarize multiple product reviews
Multiple product views
::::info
:::spoiler 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 來摘要出多個輸出, 然後每個輪出限制20字
```
for i in range(len(reviews)) :
    prompt = f"""
    Your task is to generate a short summary of a product \
    review from an ecommeric website.
    
    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 Panda plush toy is loved by daughter, soft and cute, but small for the price. Arrived early. 

1 Great lamp with storage, fast delivery, excellent customer service, and easy assembly. Highly recommended. 

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 product, and the need to buy a replacement. 

相關文章:
1. [Summarising using Prompt Engineering](https://medium.com/@aanshsavla2453/summarising-using-prompt-engineering-a-topic-from-prompt-engineering-course-by-deeplearning-ai-3e350f809a1)
2. [60+ ChatGPT Prompts for Summarizing & Summaries](https://aihabit.net/chatgpt-prompts-for-summaries/)
3. [Advanced Techniques in Text Summarization: Leveraging Generative AI and Prompt Engineering](https://bluetickconsultants.medium.com/advanced-techniques-in-text-summarization-leveraging-generative-ai-and-prompt-engineering-fba23f507435)
4. [Mastering ChatGPT: Effective Summarization with LLMs](https://towardsdatascience.com/chatgpt-summarization-llms-chatgpt3-chatgpt4-artificial-intelligence-16cf0e3625ce)

## Inferring(推理)
推論是指模型使用文本作為輸入，並執行某種分析的任務，包含
- 提取標籤、提取名稱
- 情感分析
- ...
這一章, 你將從產品評論和新聞文章中推斷情感和主題.
### 案例1: 一個檯燈的產品評價, 針對產品品價分析

Product review text
::::info
:::spoiler lamp_review = """
Needed a nice lamp for my bedroom, and this one had \
additional storage and not too high of a price point. \
Got it fast.  The string to our 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.  I had a missing part, so I contacted their \
support and they very quickly got me the missing piece! \
Lumina seems to me to be a great company that cares \
about their customers and products!!
"""
::::

#### 推理1-1(判斷正向或負向的情)

Sentiment (positive/negative)¶
::::info
:::spoiler prompt = f"""
What is the sentiment of the following product review, 
which is delimited with triple backticks?

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)
::::

Inferring Version 1-1 Output
The sentiment of the product review is **positive**.

#### 推理1-2 (調整直接回答正向/負向)
直接要求只給一個字(Postive/Negative)
::::info
:::spoiler prompt = f"""
What is the sentiment of the following product review, 
which is delimited with triple backticks?

Give your answer as a single word, either "positive" \
or "negative".

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)
::::
Inferring Version 1-2 Output
positive

#### 推理2 Identify types of emotions
包含那些情感
::::info
:::spoiler prompt = f"""
Identify a list of emotions that the writer of the \
following review is expressing. Include no more than \
five items in the list. Format your answer as a list of \
lower-case words separated by commas.

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)
::::
Inferring Version2 Output
satisfied, pleased, grateful, impressed, happy

#### 使用emotions and emoji
相關資料: [emoji](https://github.com/ikatyang/emoji-cheat-sheet)
::::info
:::spoiler prompt = f"""
Identify a list of emotions that the writer of the following review is expressing. Represent each emotion with an appropriate emoji, but also keep the emotion word in your list. Include no more than five emotions in the list. Format your answer as a list of lower-case words, each followed by its corresponding emoji.

Review text: ```{lamp_review}```
"""
response = get_completion(prompt)
print(response)
::::
 Output
![infer_version3_output_emoji](https://hackmd.io/_uploads/ryi8Wr0w6.png)

#### Inferring Version3 Identify anger (識別是否有憤怒的情緒)
::::info
:::spoiler prompt = f"""
Is the writer of the following review expressing anger?\
The review is delimited with triple backticks. \
Give your answer as either yes or no.

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)
::::
Inferring Version3 Output
No

#### Inferring Version4 Extract product and company name from customer reviews (提取特定的內容)
從評論內容提取出"評論者購買的產品"和"製造商"的資訊, 以JSON格式輸出
::::info
:::spoiler prompt = f"""
Identify the following items from the review text: 
- Item purchased by reviewer
- Company that made the item

The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Item" and "Brand" as the keys. 
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
  
Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)
::::
Inferring Version4 Output
```
{
  "Item": "lamp",
  "Brand": "Lumina"
}
```

#### Inferring Version5 多任務並行
結合Version1-Version4的prompt, 使用JSON格式輸出，以"產品"、"品牌"、"憤怒"、"情緒"作為key，其中憤怒部分用布林值表示。回答儘量簡短為主, 如果資料沒有發現, 就用"unknown"來顯示
::::info
:::spoiler prompt = f"""
Identify the following items from the review text: 
- Sentiment (positive or negative)
- Is the reviewer expressing anger? (true or false)
- Item purchased by reviewer
- Company that made the item

The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Sentiment", "Anger", "Item" and "Brand" as the keys.
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
Format the Anger value as a boolean.

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)
::::
Inferring Version5 Output
```
{
  "Sentiment": "positive",
  "Angry": false,
  "Item": "lamp",
  "Brand": "Lumina"
}
```

### 案例2: 針對報紙文章內容分析

Story(關於政府工人對他們的工作機構的感受的報紙文章。)

::::info
:::spoiler story = """
In a recent survey conducted by the government, 
public sector employees were asked to rate their level 
of satisfaction with the department they work at. 
The results revealed that NASA was the most popular 
department with a satisfaction rating of 95%.

One NASA employee, John Smith, commented on the findings, 
stating, "I'm not surprised that NASA came out on top. 
It's a great place to work with amazing people and 
incredible opportunities. I'm proud to be a part of 
such an innovative organization."

The results were also welcomed by NASA's management team, 
with Director Tom Johnson stating, "We are thrilled to 
hear that our employees are satisfied with their work at NASA. 
We have a talented and dedicated team who work tirelessly 
to achieve our goals, and it's fantastic to see that their 
hard work is paying off."

The survey also revealed that the 
Social Security Administration had the lowest satisfaction 
rating, with only 45% of employees indicating they were 
satisfied with their job. The government has pledged to 
address the concerns raised by employees in the survey and 
work towards improving job satisfaction across all departments.
"""

::::
#### Inferring 提取文章主題
#### Inferring 提取文章主題 Infer 5 topics
找出五個這篇文章有關的主題, 使用用逗號將結果分隔
::::info
:::spoiler prompt = f"""
Determine five topics that are being discussed in the \
following text, which is delimited by triple backticks.

Make each item one or two words long. 

Format your response as a list of items separated by commas.

Text sample: '''{story}'''
"""
response = get_completion(prompt)
print(response)
::::
Version1-1 Output
survey, government, department, NASA, job satisfaction
#### Version1-2 修改格式
```
response.split(sep= ',')
```
Version1-2 Output
```
['survey',
 ' satisfaction rating',
 ' NASA',
 ' Social Security Administration',
 ' job satisfaction']
#### Version 2 

```
#### Version2 Make a news alert for certain topics
判斷包含特定的主題
#### 步驟一, 先把五個主題(NASA、地方政府、工程、員工滿意度、聯邦政府),存了列表裡.
```
topic_list = [
    "nasa", "local government", "engineering", 
    "employee satisfaction", "federal government"
]
```
#### 步驟二, 判斷這篇文章是否和以上五個主題有關.針對每個主題，用0或1來表示是否有包含, 0:沒有, 1:有

::::info
:::spoiler prompt = f"""
Determine whether each item in the following list of \
topics is a topic in the text below, which
is delimited with triple backticks.

Give your answer as list with 0 or 1 for each topic.\

List of topics: {", ".join(topic_list)}

Text sample: '''{story}'''
"""
response = get_completion(prompt)
print(response)
::::
#### 輸出
[1, 0, 0, 1, 1]
#### **問題**~~~~
有時侯輸出不是符合你要的格式, 哪上列的輸出格式, 會導致步驟三有錯誤產生
, 因此將以下的句子做了修改:
```
Give your answer as list with 0 or 1 for each topic.\
```
改成
```
Format your response as a list of items and with 0 or 1 for each topic.
```
**輸出**
nasa: 1
local government: 0
engineering: 0
employee satisfaction: 1
federal government: 1

#### 步驟三, 在判斷是否有NASA的文章
```
topic_dict = {i.split(': ')[0]: int(i.split(': ')[1]) for i in response.split(sep='\n')}
if topic_dict['nasa'] == 1:
    print("ALERT: New NASA story!")
```
輸出 (因為有NASA)
ALERT: New NASA story!

相關文章:
1. [Inferring using Prompt Engineering](https://medium.com/@aanshsavla2453/inferring-using-prompt-engineering-a-topic-from-prompt-engineering-course-by-deep-learning-ai-33f4eda5984b)

## Transforming(轉換)
這一章將探討如何使用大型語言模型來執行文本轉換任務，如語言翻譯、拼寫和語法檢查、語氣調整和格式轉換。ChatGPT 接受多種語言的訓練，使得它具有翻譯的能力。以下是一些如何利用這項能力的示例。


### Translation (翻譯與語言辨識)
1. 將內容從英文翻譯成西班牙文及繁體中文
::::info
:::spoiler prompt = f"""
Translate the following English text to Spanish: \ 
```Hi, I would like to order a blender```
"""
response = get_completion(prompt)
print(response)
::::
Output:
Hola, me gustaría ordenar una licuadora.

::::info
:::spoiler prompt = f"""
Translate the following English text to Tranditional Chinese: \ 
```Hi, I would like to order a blender```
"""
response = get_completion(prompt)
print(response)
::::
Output:
嗨，我想訂購一台攪拌機。

2. 判斷這是什麼語言
::::info
:::spoiler prompt = f"""
Translate the following  text to Tranditional Chinese and Japanese
and English pirate: \
```I want to order a basketball```
"""
response = get_completion(prompt)
print(response)
::::
Output:
This language is French.
3. 將內容翻譯成法文,西班牙和英文
::::info
:::spoiler prompt = f"""
Translate the following  text to French and Spanish
and English pirate: \
```I want to order a basketball```
"""
response = get_completion(prompt)
print(response)
::::
Output:
French: ```Je veux commander un ballon de basket```
Spanish: ```Quiero ordenar una pelota de baloncesto```
English: ```I want to order a basketball```

4. 通用的翻譯器
::::info
:::spoiler 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
] 
::::
::::info
:::spoiler for issue in user_messages:
    prompt = f"Tell me what language this is: ```{issue}```"
    lang = get_completion(prompt)
    print(f"Original message ({lang}): {issue}")

    prompt = f"""
    Translate the following  text to English \
    and Korean: ```{issue}```
    """
    response = get_completion(prompt)
    print(response, "\n")
::::
Output:
先判斷那種語言, 在翻譯成英文和日文
::::info
:::spoiler Original message(The language is French.):La performance du système est plus lente que d'habitude.
English: The system performance is slower than usual.
Japanese: システムのパフォーマンスが通常よりも遅いです。 

Original message(The language is Spanish.):Mi monitor tiene píxeles que no se iluminan.
English translation: "My monitor has pixels that do not light up."

Japanese translation: 「私のモニターには点灯しないピクセルがあります。」(Watashi no monitā ni wa tentō shinai pikuseru ga arimasu.) 

Original message(The language is Italian.):Il mio mouse non funziona
English: "My mouse is not working."
Japanese: 「私のマウスが動作しません。」(Watashi no mausu ga dōsa shimasen.) 

Original message(The language is Polish.):Mój klawisz Ctrl jest zepsuty
English: My Ctrl key is broken
Japanese: 私のCtrlキーは壊れています (Watashi no Ctrl kī wa kowareteimasu) 

Original message(The language is Chinese.):我的屏幕在闪烁
English: My screen is flickering.
Japanese: 私の画面が点滅しています。 (Watashi no gamen ga tenmetsu shiteimasu.) 
::::
### Tone Transformation and Format Conversion(語氣與格式轉換)
 
5. 將內容轉換成正式和非正式的形式
::::info
:::spoiler prompt = f"""
Translate the following text to Spanish in both the \
formal and informal forms: 
'Would you like to order a pillow?'
"""
response = get_completion(prompt)
print(response)
::::
Output:
Formal: ¿Le gustaría ordenar una almohada?
Informal: ¿Te gustaría ordenar una almohada?

6. 將內容轉換成正式的商業書信風格
::::info
:::spoiler prompt = f"""
Translate the following from slang to a business letter: 
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
response = get_completion(prompt)
print(response)
::::
Output:
::::info
:::spoiler Dear Sir/Madam,

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

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

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

Sincerely,
Joe
::::
7. 將這個Python字典從JSON轉換成HTML格式
::::info
:::spoiler data_json = { "resturant employees" :[ 
    {"name":"Shyam", "email":"shyamjaiswal@gmail.com"},
    {"name":"Bob", "email":"bob32@gmail.com"},
    {"name":"Jai", "email":"jai87@gmail.com"}
]}

prompt = f"""
Translate the following python dictionary from JSON to an HTML \
table with column headers and title: {data_json}
"""
response = get_completion(prompt)
print(response)
::::

Output:
::::info
:::spoiler <!DOCTYPE html>
<html>
<head>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
</head>
<body>

<h2>Restaurant Employees</h2>

<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
  </tr>
  <tr>
    <td>Shyam</td>
    <td>shyamjaiswal@gmail.com</td>
  </tr>
  <tr>
    <td>Bob</td>
    <td>bob32@gmail.com</td>
  </tr>
  <tr>
    <td>Jai</td>
    <td>jai87@gmail.com</td>
  </tr>
</table>

</body>
</html>
::::
使用Markdown格式輸出
```
from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))
```
Output:
![Transforming-markdown](https://hackmd.io/_uploads/r1DOyn0wa.png)

### Spellcheck/Grammar check(拼字/文法檢查)
8. 校對並更正內容
::::info
:::spoiler text = [ 
  "The girl with the black and white puppies have a ball.",  # The girl has a ball.
  "Yolanda has her notebook.", # ok
  "Its going to be a long day. Does the car need it’s oil changed?",  # Homonyms
  "Their goes my freedom. There going to bring they’re suitcases.",  # Homonyms
  "Your going to need you’re notebook.",  # Homonyms
  "That medicine effects my ability to sleep. Have you heard of the butterfly affect?", # Homonyms
  "This phrase is to cherck chatGPT for speling abilitty"  # spelling
]

for t in text:
    prompt = f"""Proofread and correct the following text
    and rewrite the corrected version. If you don't find
    and errors, just say "No errors found". Don't use 
    any punctuation around the text:
    ```{t}```"""
    response = get_completion(prompt)
    print(response)
::::
Output:
::::info
:::spoiler The girl with the black and white puppies has a ball.
No errors found.
No errors found.
There goes my freedom. They're going to bring their suitcases.
You're going to need your notebook.
That medicine affects my ability to sleep. Have you heard of the butterfly effect?
This phrase is to check chatGPT for spelling ability.
::::
9. "proofread and correct this review" 這個指令意味著要求模型檢查和糾正該文本中的任何語法、拼寫錯誤或不規則之處。這包括但不限於加入連接詞；它還可能涉及修改句子結構、更正拼寫錯誤、改善語法和澄清含糊不清的表達. 主要就是提高文本的整體清晰度和正確性。

::::info
:::spoiler text = f"""
Got this for my daughter for her birthday cuz she keeps taking \
mine from my room.  Yes, adults also like pandas too.  She takes \
it everywhere with her, and it's super soft and cute.  One of the \
ears is a bit lower than the other, and I don't think that was \
designed to be asymmetrical. It's a bit small for what I paid for it \
though. I think there might be other options that are bigger for \
the same price.  It arrived a day earlier than expected, so I got \
to play with it myself before I gave it to my daughter.
"""
prompt = f"proofread and correct this review: ```{text}```"
response = get_completion(prompt)
print(response)
::::

Output:
![Transforming-proofread](https://hackmd.io/_uploads/SyVZznADa.png)
以下輸出校對並更正內容, 使用以下的語法
```
from redlines import Redlines

diff = Redlines(text,response)
display(Markdown(diff.output_markdown))
```
Output:
![Transforming-proofread_and_correct](https://hackmd.io/_uploads/Hyyy7hCD6.png)

10. 確保這個內容符合APA格式，使用Markdown格式輸出
我們可以找到原始評論和模型輸出之間的差異.因此, 我們將使用這個Python ReadLines 去獲得始評論和模型輸出之間的差異. 然後顯示出來, 所以可以從輸出看到始評論和模型輸出之間的差異以及已經更正的內容.

相關文件: [APA](https://zh.wikipedia.org/zh-tw/APA%E6%A0%BC%E5%BC%8F)
::::info
:::spoiler prompt = f"""
proofread and correct this review. Make it more compelling. 
Ensure it follows APA style guide and targets an advanced reader. 
Output in markdown format.
Text: ```{text}```
"""
response = get_completion(prompt)
display(Markdown(response))
::::
Output:
::::info
:::spoiler Review of a Panda Plush Toy

I purchased this adorable panda plush toy as a birthday gift for my daughter, who has a penchant for taking my belongings from my room. Contrary to popular belief, adults can also appreciate the charm of pandas. This cuddly companion has quickly become her constant companion, accompanying her wherever she goes. Its irresistibly soft and cute appearance is simply delightful.

However, I did notice a minor flaw in the design. One of the ears is slightly lower than the other, which appears to be unintentional asymmetry. While this does not detract significantly from the overall appeal, it is worth mentioning for the sake of accuracy.

In terms of size, I must admit that I expected a slightly larger plush toy considering its price. It would be prudent to explore alternative options that offer a more substantial size for the same cost. Nevertheless, the quality and craftsmanship of this panda plush toy are commendable.

To my surprise, the package arrived a day earlier than anticipated. This unexpected early delivery allowed me to indulge in some playtime with the panda plush toy before presenting it to my daughter. The joy it brought me during those moments only heightened my excitement to see her reaction.

In conclusion, despite its minor imperfections and relatively smaller size, this panda plush toy has captured the hearts of both my daughter and myself. Its undeniable charm, coupled with its exceptional softness, make it a delightful addition to any collection. For those seeking a lovable companion, this panda plush toy is a worthwhile investment.
::::
相關文章: 
1. [Transformation using Prompt Engineering](https://medium.com/@aanshsavla2453/transformation-using-prompt-engineering-a-topic-of-prompt-engineering-course-from-deeplearning-ai-8831089dfccc) 
2. [掌握生成式 AI 企業導入流程提示工程 Prompt Flow 與 Semantic Kernel](https://www.youtube.com/watch?v=mfmkJD8awyE)(主要是看Semantic Kernel:44:20)

## Expand(擴寫)
擴屏是將短的文本內容, 比如一組說明或一系列的主題, 並讓LLM生成更長的文本內容. 例如: 作為腦力激蕩的夥伴. 但如果有人濫用, 可能就會生成大量垃圾邸件. 所以, 在使用LLM時, 請記得以負責任的方式使用. 
關於 temparture. 透過調整模型的temperature(溫度)參數可以調整模型輸出的多樣性(0-2之間)
- 降低溫度(最低到0)：輸出的結果更加確定. 有時侯會直出現相同的輸出.
- 提高溫度(最高到2)：輸出更加多樣，更具有創造力, 但有時侯創造出跟文本完全沒有相關的內容, 就答非所問
### Customize the automated reply to a customer email, given the sentiment from the lesson on "inferring". (客制化的自動回覆顧客郵件)

::::info
:::spoiler 客戶的電子郵件內容+客戶內容的情緒

sentiment = "negative"

review = f"""
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.
"""
::::
 你是一個客戶服務的AI助理, 主要任務是發送一個關於客戶的電子郵件的回覆, 條件就是如果情緒是正向或中立的，請感謝他們的評論. 但是如果是情緒是負向的，道歉並建議他們聯繫客戶服務
::::info
:::spoiler prompt =f"""
You are a customer service AI assistant.
Your task is to send an email reply to a valued customer.
Given then customer email delimited by ```, \
Generate a reply to thank the customer for their review.
If the sentiment is positive or neutral, thank them for \
their review.
If the sentiment is negative, apologize and suggest that \
they can reach out the customer service.
Make sure to use specific details from the review.
Write in a concise and professional tone.
Sing the mail as `AI customer agent`.
Customer review: ```{review}```
Review sentiment: {sentiment}
"""
response = get_completion(prompt)
print(response)
::::
Ouput:(負面)
::::info
:::spoiler Dear Valued Customer,

Thank you for taking the time to share your review with us. We appreciate your feedback and apologize for any inconvenience you may have experienced.

We are sorry to hear about the price increase you noticed in December. We understand how frustrating it can be when prices change unexpectedly. We strive to provide fair and competitive pricing for our customers, and we apologize if this was not reflected in your recent purchase.

Regarding the quality of the base and the motor noise issue you encountered, we apologize for any disappointment caused. We always aim to deliver products of the highest quality, and we regret that this was not your experience. We appreciate your feedback and will take it into consideration as we continue to improve our products.

If you have any further concerns or questions, we encourage you to reach out to our customer service team. They will be more than happy to assist you and address any issues you may have.

Once again, thank you for your review. We value your feedback and will use it to enhance our products and services. We hope to have the opportunity to serve you better in the future.

Best regards,

AI customer agent
::::
### Remind the model to use details from the customer's email(確保你有使用評論中提到的具體細節, 以及以簡潔專業的預期書寫, 在最後以AI的身份署名郵件)
::::info
:::spoiler prompt = f"""
You are a customer service AI assistant.
Your task is to send an email reply to a valued customer.
Given the customer email delimited by ```, \
Generate a reply to thank the customer for their review.
If the sentiment is positive or neutral, thank them for \
their review.
If the sentiment is negative, apologize and suggest that \
they can reach out to customer service. 
Make sure to use specific details from the review.
Write in a concise and professional tone.
Sign the email as `AI customer agent`.
Customer review: ```{review}```
Review sentiment: {sentiment}
"""
response = get_completion(prompt, temperature=0.7)
print(response)
::::
Output:
::::info
:::spoiler Dear valued customer,

Thank you for taking the time to share your feedback with us. We truly appreciate your support and loyalty to our brand.

We apologize for any inconvenience you experienced regarding the pricing of our 17 piece system. We understand your frustration and want to assure you that we strive to provide fair and consistent pricing to all our customers. If you have any further concerns or questions about our pricing policy, please do not hesitate to reach out to our customer service team.

We also appreciate your feedback regarding the quality of the blade locking mechanism. We apologize for any disappointment caused. We continuously strive to improve our products and your feedback is invaluable in this process.

We are sorry to hear about the issue with the motor and the expiration of the warranty. We understand how frustrating this can be. If you ever encounter any difficulties in the future, please feel free to contact our customer service team. They will be more than happy to assist you.

Once again, we thank you for your review and for choosing our products. We value your feedback and hope to have the opportunity to serve you better in the future.

Best regards,

AI customer agent
::::

(注意, 如果openai version >= 1.0.0, 需要修改以下的程式, 否則會有ERROR顯示)
```
response = get_completion(prompt, temperature=0.7)
```
改成
```
response = get_completion(prompt)
```
在修改
```
def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0.7, # 原先設置為0
    )
    return response.choices[0].message.content
```
## Chatbot
這一章, 您將探索如何利用聊天格式與專為特定任務或行為量身定制的聊天機器人進行深入對話

你也可以很簡單的用LLM來建立你自己的聊天機器人，例如
- 扮演AI客服
- 扮演餐廳AI接單員

創建一個基於 GPT-3.5-turbo API 的聊天機器人,在這種設定中，確實可以定義不同的角色以及他們的互動方式。以下是您提到的三個角色的詳細說明：
* system：這部分用於指定聊天機器人（ChatBot）的個性設定、行為規範或其他操作參數。這些設定將影響聊天機器人的整體表現和回覆方式。
* user：這裡指的是使用者或對話者的輸入。在設計聊天機器人時，這可以是真實的用戶互動，也可以是預設的、用於測試的用戶回覆示例。
* assistant：這代表聊天機器人本身的回覆。在設計過程中，您可以設定預期的機器人回覆或示例，以便於檢測和調整機器人的行為和回覆模式。

::::info
:::spoiler messages的3個角色
messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},
{'role':'user', 'content':'Hi, my name is Isa'},
{'role':'assistant', 'content': "Hi Isa! It's nice to meet you. \
Is there anything I can help you with today?"},
{'role':'user', 'content':'Yes, you can remind me, What is my name?'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)
::::
針對messages 的保存方式
而且在使用像 GPT-3.5 這樣的 API 進行多輪對話時，要使 ChatBot 保有對話歷史的記憶，確實需要將先前的對話歷史（包括用戶和機器人的所有對話）再次提交給模型。這樣做的確會受到 token 的限制。

每次請求給模型的文本（包括先前的對話和新的用戶輸入）都會被轉換成 tokens，這是模型處理語言的基本單位。大型語言模型如 GPT-3 或 GPT-3.5 有一定的 token 限制，這意味著每次請求中可包含的字符數量是有上限的。超過這個限制，模型就無法處理整個文本。
如果沒有之前的輸入
::::info
:::spoiler messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},    
{'role':'user', 'content':'Hi, my name is Isa'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)
::::
Output:
Hello Isa! It's nice to meet you. How can I assist you today?

在輸入,
::::info
:::spoiler messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},    
{'role':'user', 'content':'Yes,  can you remind me, What is my name?'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)
::::
Output:

I'm sorry, but as a chatbot, I don't have access to personal information like your name. So, I don't know what your name is. Is there anything else I can help you with?
(因為沒有之前的messages, 所以chatbot不知道USER的姓名是Isa)

如果有包含歷史資料,
::::info
:::spoiler messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},
{'role':'user', 'content':'Hi, my name is Isa'},
{'role':'assistant', 'content': "Hi Isa! It's nice to meet you. \
Is there anything I can help you with today?"},
{'role':'user', 'content':'Yes, you can remind me, What is my name?'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)
::::
Output:
Your name is Isa!(chatbot就會回答Isa)

### OrderBot 的例子
您可以自動化收集用戶提示和助手回應來構建一個訂單機器人（OrderBot）。這個OrderBot可以用於披薩餐廳接收訂單。
::::info
:::spoiler 收集messages的函數,用於處理用戶輸入和生成機器人的回應.
它從用戶那裡收集輸入（prompt），並將其添加到對話歷史（context）中。
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)
::::

::::info
:::spoiler 在 context 變數中，有一個以 'system' 角色定義的字典，這個字典定義了 OrderBot 的行為和個性。這包括該如何迎接顧客、收集訂單、詢問是自取還是外送等指示，以及確保收集到完整的訂單信息，包括如果是外送的話還需索取地址，最後收取付款。
import panel as pn  # GUI
pn.extension()

panels = [] # collect display 

context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza restaurant. \
You first greet the customer, then collects the order, \
and then asks if it's a pickup or delivery. \
You wait to collect the entire order, then summarize it and check for a final \
time if the customer wants to add anything else. \
If it's a delivery, you ask for an address. \
Finally you collect the payment.\
Make sure to clarify all options, extras and sizes to uniquely \
identify the item from the menu.\
You respond in a short, very conversational friendly style. \
::::

::::info
:::spoiler Pizza 菜單內容
The menu includes \
pepperoni pizza  12.95, 10.00, 7.00 \
cheese pizza   10.95, 9.25, 6.50 \
eggplant pizza   11.95, 9.75, 6.75 \
fries 4.50, 3.50 \
greek salad 7.25 \
Toppings: \
extra cheese 2.00, \
mushrooms 1.50 \
sausage 3.00 \
canadian bacon 3.50 \
AI sauce 1.50 \
peppers 1.00 \
Drinks: \
coke 3.00, 2.00, 1.00 \
sprite 3.00, 2.00, 1.00 \
bottled water 5.00 \
"""} ]  # accumulate messages
::::

介面元件:
::::info
:::spoiler import panel as pn  # GUI
pn.extension()

panels = [] # collect display 

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),
)

dashboard
::::

下列是收集完一份食物訂單後，生成一個 JSON 格式的摘要，其中包含每項食品的價格細節 (包含複製對話歷史, 添加新的系統提示, 最後，打印出生成的回應，這應該是一個符合上述要求的 JSON 格式的訂單摘要。)
::::info
:::spoiler messages = context.copy()
messages.append(
{
'role':'system', 'content':'create a json summary of the previous food order. Itemize the price for each item \
 The fields should be 1) pizza, include size 2)list of toppings 3)list of drinks, include size 4) list of sides include size \
 5)Delivery method include price 6) total price'
},)
response = get_completion_from_messages(messages, temperature=0)
print(response)
::::
Output:
```
{
  "pizza": {
    "type": "eggplant",
    "size": "medium"
  },
  "toppings": [
    "mushrooms"
  ],
  "drinks": [],
  "sides": [
    {
      "type": "fries",
      "size": "regular"
    }
  ],
  "deliveryMethod": {
    "method": "pickup",
    "price": 0
  },
  "totalPrice": 16.25
}
```
## 總結
在這個課程裡, 學到了...:
1. 寫提示詞(Prompt)的兩個關鍵原則
- 寫清楚具體的說明
    - 給模型足夠的思考時間
2. 提示詞的開發是一個持續迭代的過程
3. 學習了一些常見的應用
   - 推論,翻譯、格式轉換,擴寫 

針對, OrderBot有一些討論
::::info
:::spoiler 1. 是否可以分享給其他人一起使用呢?
是的，您可以開發一個具有特定用途的機器人並與線上用戶分享，這與在R語言中開發Shiny應用程序有些類似。以下是幾種方法：

使用Web框架：您可以利用像Flask或Django這樣的Python Web框架來創建一個Web應用程序，並將您的聊天機器人作為其中的一部分。這種方法允許您創建一個完整的Web界面，用戶可以通過瀏覽器與您的機器人進行互動。

構建API：您可以將您的機器人作為一個後端服務，並為其提供一個API接口。這樣，其他開發者可以通過API與您的機器人進行交互，並將其整合到他們自己的應用程序中。

部署到雲服務：您可以使用像Heroku、AWS或Google Cloud這樣的雲服務來部署您的應用程序。這些平台提供了容易使用的工具和服務來幫助您管理應用程序的部署和擴展。

使用聊天平台：如果您的目標是創建一個可以在即時通訊平台上運行的機器人（如Telegram、Slack或Discord），您可以使用這些平台的API來整合您的機器人。

提供文檔和使用說明：無論您選擇哪種方法，都應該提供清晰的文檔和使用說明，以幫助用戶了解如何與您的機器人互動。

記住，開發並分享一個聊天機器人需要考慮到用戶隱私和數據安全等因素。确保您的應用程序符合相關的隱私法規和最佳實踐。

::::
::::info
:::spoiler 2. 想要將最終Order data存入資料庫, 但問題是我們如何知道這張Order已經確認了?
在「ChatGPT提示工程」課程中建立的披薩餐廳訂單機器人案例中，確實需要一種方法來確認訂單，然後才將數據以JSON格式提取出來以儲存至數據庫或進行其他處理。關於訂單確認的觸發問題，有幾種策略可以考慮：

使用特定的結束語句：在對話設計中，可以設置一個特定的結束語句，如「您的訂單已確認。」當檢測到這個特定語句時，就可以觸發提取數據的操作。

建立多階段對話流程：設計一個多階段的對話流程，例如在最後階段有一個明確的確認步驟，比如問用戶是否確定訂單。只有在接收到肯定回答後，才觸發數據提取。

設置關鍵詞加上上下文檢查：除了檢查關鍵詞「確認」之外，還可以檢查該詞出現的上下文。例如，您可以檢查「確認」一詞是否出現在對話的最後幾句中，或是否與其他特定詞匯（如「訂單」、「完成」等）一起出現。

用戶驅動的確認：允許用戶通過明確的命令或選擇來確認訂單，例如回答「是的，請確認我的訂單」。這樣可以減少因誤解對話內容而錯誤觸發的風險。

後端處理：在後端系統中處理確認邏輯，而不完全依賴ChatGPT的回答。例如，可以在後端跟踪用戶的訂單選項，並在特定階段（如用戶回答特定問題後）觸發確認和數據提取。

綜上所述，最佳策略可能會結合這些方法，以確保在正確的時刻以合適的格式提取訂單數據。设计这样的系统时，考虑到准确性和用户体验是非常重要的。
::::
根據上面的答案,例如:建立多階段對話流程 ChatGPT 和其他基於大型語言模型的聊天機器人在處理有序任務和複雜的指令時可能會遇到一些問題:
1. 順序問題：ChatGPT有時可能無法準確維持指令的順序。這是因為這些模型是基於學習到的大量文本數據來生成回答的，而不是按照傳統程式化邏輯運行。因此，它可能無法完全理解或記住一系列復雜的步驟。

2. 處理多階段對話的挑戰：在一個多階段的對話過程中，模型可能在不正確的時候提供JSON格式的回應。例如，在對話的中間階段就提供JSON回應，而不是在整個訂單過程完成後。

3. 上下文理解的限制：雖然ChatGPT能夠處理和回應復雜的對話，但它對上下文的理解仍有限制。在長對話或包含多個步驟和指令的情況下，這些限制變得更為明顯。
::::

