2.內容

2.2 零樣本Prompt

比如：

`prompt：對給定文本執行情感分析，並將其分類爲中性、負面或正面。輸入：最近的旅行很棒輸出：正面`

2.3 思維鏈Prompt

2.3.1 標準Prompt

```問題 1： Sam有 3 檯筆記本。她又購買了 4 包筆記本，每包 5 個筆記本。莎拉現在有多少本筆記本？

2.3.2 鏈式Prompt

```問題 1： Sam有 3 檯筆記本。她又購買了 4 包筆記本，每包 5 個筆記本。莎拉現在有多少本筆記本？

3+(4×5)=3+20=23

30−20+6=16

2.4 One-Shot 和 Few Shot 的Prompt

```One-Shot Prompt:

```使用以下三個片段，構建一個故事：

1.在一個未來的城市，人們通過植入式芯片實現了智能增強。
2.突然間，一種未知的數字病毒傳播到城市的所有芯片，引發了混亂。
3.一位年輕的黑客天才被選中，試圖解決這場數字危機，但她需要與一臺舊式人工智能建立聯繫，尋求幫助。

```這是太棒了！（負面）

3.代碼實現

3.1 Zero Shot Prompt

```from openai import OpenAI
import re
import os
import pandas as pd
client = OpenAI()```

```def sentiment_analysis_example(text):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": 'Perform sentiment analysis on the given text: "{text}". Classify it as either Positive, Negative, or Neutral sentiment. Provide the result in JSON format with the following keys: sentiment.'
},
{
"role": "user",
"content": f'Analyze the sentiment of the text "{text}" and categorize it as Positive, Negative, or Neutral. Return the result in JSON format with the key "sentiment".'
}
],
max_tokens=150,
n=1,
stop=None,
temperature=0
)

result = response.choices[0].message.content
try:
start_index = result.find('{')
end_index = result.rfind('}')

if start_index != -1 and end_index != -1:
result = result[start_index:end_index + 1]
except:
pass
print(result)

example_text = "This is fantastic!"
sentiment_analysis_example(example_text)```

```{
"sentiment": "Positive"
}```

3.2 CoT Prompt

```from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "Determine the cost"
},
{
"role": "user",
"content": "Sam has 3 notebooks. She purchases 4 more packs of notebooks, with 5 notebooks in each pack. How many notebooks does Sarah have now?"
},
{
"role": "assistant",
"content": "Sam initial notebooks: 3 notebooks\nNotebooks from the new packs: 4 packs * 5 notebooks/pack = 20 notebooks\nNow, add these two quantities:\n3 notebooks (initial) + 20 notebooks (new packs) = 23 notebooks"
},
{
"role": "user",
"content": "The basket has 30 apples. If 20 apples are used for lunch and an additional 6 are bought from the market, how many apples are there in the basket?"
}
],
temperature=0,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
result = response.choices[0].message.content
print(result)```

```Apples in the basket: 30 apples
Apples used for lunch: 20 apples
Apples bought from the market: 6 apples
Now, subtract the apples used and add the apples bought:
30 apples - 20 apples + 6 apples = 16 apples```