项目:使用Prompt稳定输出结构化结果
日期:5.15-5.20
一、Prompt基础框架
Instruction(必须): 指令,即你希望模型执行的具体任务。
Context(选填): 背景信息,或者说是上下文信息,这可以引导模型做出更好的反应。
Input Data(选填): 输入数据,告知模型需要处理的数据。
Output Indicator(选填): 输出指示器,告知模型我们要输出的类型或格式。
推理:Instruction + Context + Input Data
信息提取:Instruction + Context + Input Data + Output Indicator
二、CRISPE Prompt框架
CRISPE 分别代表以下含义:
CR: Capacity and Role(能力与角色)。你希望 文心一言 扮演怎样的角色。
I: Insight(洞察力),背景信息和上下文(坦率说来我觉得用 Context 更好)。
S: Statement(指令),你希望 文心一言 做什么。
P: Personality(个性),你希望 文心一言 以什么风格或方式回答你。
E: Experiment(尝试),要求 文心一言 为你提供多个答案。
三、其他
Zero-Shot Prompts
Zero-Shot Prompting 是一种自然语言处理技术,可以让计算机模型根据提示或指令进行任务处理。文心一言就用到这个技术。
Zero-Shot Chain of Thought
这个技巧使用起来非常简单,只需要在问题的结尾里放一句 Let‘s think step by step (让我们一步步地思考),模型输出的答案会更加准确。
Few-Shot
我们给模型一些示例,从而让模型返回更符合我们需求的答案。这个技巧是使用了一个叫 Few-Shot 的方法。
Self-Consistency
自洽是对 Chain of Thought 的一个补充,它能让模型生成多个思维链,然后取最多数答案的作为最终结果。
PAL Models
程序辅助语言模型,一种使用 LLMs 读取自然语言问题并生成程序作为中间推理步骤的方法,被称为程序辅助语言模型(PAL)。它与思维链提示不同,因为它不是使用自由形式文本来获得解决方案,而是将解决步骤卸载到类似 Python 解释器的编程运行时中。
json生成 让ERNIE Bot稳定输出结构化结果
json是全球使用最广泛的数据交换格式之一,支持我们的所有需求。 使用提示工程中语法规则强制模型仅输出 JSON格式。
jsonschema学习链接:JSON Schema - Creating your first schema
示例代码:
import erniebot
erniebot.api_type = 'aistudio'
erniebot.access_token = {}
messages=[{'role': 'user', 'content': '''“这个水果是苹果,id记录为123456, 价格是5人民币每500克”, 请根据我的描述生成一段json结果,json数据参考如下的jsonschema的描述。
```{ "title": "Product",
"description": "一个商品的目录",
"type": "object",
"properties": {"productId": { "description": "商品唯一识别码", "type": "integer"},
"productName": {"description": "商品名称","type": "string"},
"price": {"description": "商品的价格","type": "number","exclusiveMinimum": 0}
},
"required": [ "productId", "productName", "price" ]
}```'''}]
response= erniebot.ChatCompletion.create(model='ernie-3.5', messages=messages)
first_response = response.get_result()
print(first_response)
输出结果:
{
"title": "Product",
"description": "一个商品的目录",
"type": "object",
"properties": {
"productId": {
"description": "商品唯一识别码",
"type": "integer"
},
"productName": {
"description": "商品名称",
"type": "string"
},
"price": {
"description": "商品的价格",
"type": "number",
"exclusiveMinimum": 0
}
},
"required": [ "productId", "productName", "price" ]
}