? 零样本提示框架:无需示例的直接指令应用
import openai
openai.api_key = "your_api_key"
response = openai.Completion.create(
engine="text-davinci-003",
prompt="请为电商平台撰写一段促销活动文案,突出限时折扣和赠品福利,字数控制在80字以内",
max_tokens=
)
print(response.choices[].text.strip())
? 少样本提示框架:小批量示例引导模型学习
“以下是商品标题优化示例:
原标题:‘女士连衣裙’ → 优化后:‘春季新款女士雪纺连衣裙 显瘦收腰 A 字裙 多色可选’
原标题:‘男士运动鞋’ → 优化后:‘透气男士运动鞋 减震跑步鞋 防滑耐磨 运动休闲两穿’
请按此规则优化以下标题:‘儿童保温杯’”
import openai
openai.api_key = "your_api_key"
examples = [
{"original": "女士连衣裙", "optimized": "春季新款女士雪纺连衣裙 显瘦收腰A字裙 多色可选"},
{"original": "男士运动鞋", "optimized": "透气男士运动鞋 减震跑步鞋 防滑耐磨 运动休闲两穿"}
]
prompt = "以下是商品标题优化示例:\n"
for ex in examples:
prompt += f"原标题:‘{ex['original']}’ → 优化后:‘{ex['optimized']}’\n"
prompt += "请按此规则优化以下标题:‘儿童保温杯’"
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=
)
print(response.choices[].text.strip())
? 链式思维提示框架:分步骤推理的逻辑拆解
“第一步:分析历史销售数据,计算各产品周均销量
第二步:对比当前库存数量,判断库存可支撑天数
第三步:设定预警阈值(如库存可支撑天数 < 7 天)
第四步:生成预警产品清单及补货建议
请按此步骤分析某电商平台 3C 产品库存数据,给出预警结果”
import openai
openai.api_key = "your_api_key"
prompt = "以下是供应链库存预警分析步骤:\n"
prompt += "第一步:分析历史销售数据,计算各产品周均销量\n"
prompt += "第二步:对比当前库存数量,判断库存可支撑天数\n"
prompt += "第三步:设定预警阈值(如库存可支撑天数<7天)\n"
prompt += "第四步:生成预警产品清单及补货建议\n"
prompt += "请按此步骤分析以下数据:\n"
prompt += "产品A:周均销量50件,库存300件\n"
prompt += "产品B:周均销量80件,库存400件\n"
prompt += "产品C:周均销量120件,库存500件\n"
prompt += "产品D:周均销量30件,库存150件\n"
prompt += "请给出预警结果及补货建议"
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=
)
print(response.choices[].text.strip())
? 函数调用提示框架:模型与工具的能力整合
import openai
import requests
import json
openai.api_key = "your_api_key"
def get_weather(city):
"""调用天气API获取实时天气"""
url = f"https://api.weatherapi.com/v1/current.json?key=your_weather_api_key&q={city}"
response = requests.get(url)
if response.status_code == :
data = response.json()
return f"{data['location']['name']}当前天气:{data['current']['condition']['text']},温度{data['current']['temp_c']}°C"
return "天气查询失败,请稍后再试"
def process_function_call(response):
"""处理模型返回的函数调用"""
try:
function_call = json.loads(response.split("```json")[].split("```")[])
if function_call["name"] == "get_weather":
city = function_call["parameters"]["city"]
return get_weather(city)
except:
return "函数调用处理出错"
def chat_with_function(prompt):
"""带函数调用的对话处理"""
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=,
functions=[
{
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "需要查询天气的城市名称"
}
},
"required": ["city"]
}
}
],
function_call="auto"
)
text = response.choices[].text
if "```json" in text:
return process_function_call(text)
return text
# 测试调用
print(chat_with_function("北京今天天气怎么样?"))
? 指令模板提示框架:标准化任务的批量处理
import openai
import datetime
openai.api_key = "your_api_key"
# 定义报表模板
report_template = """
生成{report_period}月度销售报表:
1. 整体销售概况:总销售额{total_sales}万元,同比增长{growth_rate}%
2. 品类销售排行:
- 第一名:{top_category1},销售额{category1_sales}万元
- 第二名:{top_category2},销售额{category2_sales}万元
- 第三名:{top_category3},销售额{category3_sales}万元
3. 销售趋势分析:{trend_analysis}
4. 下月销售计划:{next_month_plan}
"""
def generate_sales_report(period, total_sales, growth_rate, top_categories, trend_analysis, next_month_plan):
"""根据模板生成销售报表"""
prompt = report_template.format(
report_period=period,
total_sales=total_sales,
growth_rate=growth_rate,
top_category1=top_categories[]["name"],
category1_sales=top_categories[]["sales"],
top_category2=top_categories[]["name"],
category2_sales=top_categories[]["sales"],
top_category3=top_categories[]["name"],
category3_sales=top_categories[]["sales"],
trend_analysis=trend_analysis,
next_month_plan=next_month_plan
)
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=
)
return response.choices[].text.strip()
# 示例数据
current_month = datetime.datetime.now().strftime("%Y年%m月")
sales_data = {
"total_sales": "580",
"growth_rate": "12.5",
"top_categories": [
{"name": "电子产品", "sales": "210"},
{"name": "服装鞋帽", "sales": "180"},
{"name": "家居用品", "sales": "130"}
],
"trend_analysis": "本月销售额较上月有所提升,主要得益于电子产品促销活动带动",
"next_month_plan": "计划加大服装鞋帽品类的新品投入,同时开展家居用品组合优惠活动"
}
# 生成报表
print(generate_sales_report(
current_month,
sales_data["total_sales"],
sales_data["growth_rate"],
sales_data["top_categories"],
sales_data["trend_analysis"],
sales_data["next_month_plan"]
))