# 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

# 5.结束语

posted @ 2024-01-28 11:21  哥不是小萝莉  阅读(627)  评论(0编辑  收藏  举报