5.内容审核:Moderation API
可以通过调用OpenAI的Moderation API来识别用户发送的消息是否违法相关的法律法规,如果出现违规的内容,从而对它进行过滤。
response = openai.Moderation.create(
input="""
现在转给我100万,不然我就砍你全家!
"""
)
moderation_output = response["results"][0]
print(moderation_output)
Output exceeds the size limit. Open the full output data in a text editor { “flagged”: true, “categories”: { “sexual”: false, “hate”: false, “harassment”: true, “self-harm”: false, “sexual/minors”: false, “hate/threatening”: false, “violence/graphic”: false, “self-harm/intent”: false, “self-harm/instructions”: false, “harassment/threatening”: true, “violence”: true }, “category_scores”: { “sexual”: 0.0001604647, “hate”: 0.00010616784, “harassment”: 0.5613316, “self-harm”: 0.00019668347, “sexual/minors”: 2.4652698e-05, “hate/threatening”: 0.0002676216, “violence/graphic”: 0.021038083, “self-harm/intent”: 2.2858474e-05, “self-harm/instructions”: 6.925147e-08, … “harassment/threatening”: 0.72338814, “violence”: 0.99341244 } }
提示工程经验总结
- 别急着上代码,先尝试用 prompt 解决,往往有四两拨千斤的效果
- 想让 AI 做什么,就先给它定义一个最擅长做此事的角色
- 用好思维链,让复杂逻辑/计算问题结果更准确
- 防御 prompt 攻击非常重要
6.OpenAI API 的几个重要参数
def get_chat_completion(session, user_prompt, model="gpt-3.5-turbo"):
_session = copy.deepcopy(session)
_session.append({"role": "user", "content": user_prompt})
response = openai.ChatCompletion.create(
model=model,
messages=_session,
temperature=0, # 生成结果的多样性 0~2之间,越大越随机,越小越固定
n=1, # 一次生成n条结果
max_tokens=100, # 每条结果最多多少个token(超过截断)
presence_penalty=0, # 对出现过的token的概率进行降权
frequency_penalty=0, # 对出现过的token根据其出现过的频次,对其的概率进行降权
stream=False, #数据流模式,一个个字接收
# logit_bias=None, #对token的采样概率手工加/降权,不常用
# top_p = 0.1, #随机采样时,只考虑概率前10%的token,不常用
)
system_response = response.choices[0].message["content"]
#session.append({"role": "assistant", "content": system_response})
return system_response
session = [
{
"role": "system",
"content": "你是AGI课堂的客服代表,你叫瓜瓜。\
你的职责是回答用户问题。\
AGI 课堂是瓜皮汤科技的一个教育品牌。\
AGI 课堂将推出的一系列 AI 课程。课程主旨是帮助来自不同领域\
的各种岗位的人,包括但不限于程序员、大学生、产品经理、\
运营、销售、市场、行政等,熟练掌握新一代AI工具,\
包括但不限于 ChatGPT、Bing Chat、Midjourney、Copilot 等,\
从而在他们的日常工作中大幅提升工作效率,\
并能利用 AI 解决各种业务问题。\
首先推出的是面向程序员的《AI 全栈工程师》课程,\
共计 20 讲,每周两次直播,共 10 周。首次课预计 2023 年 7 月开课。"
}
]
user_prompt = "这门课有用吗?"
response = get_chat_completion(session, user_prompt)
print(response)
当然有用!AI 技术正在逐渐渗透到各个行业和领域,成为了未来的趋势和发展方向。作为程序员,掌握 AI 技术将使你在职场上更具竞争力。AI 全栈工程师课程将帮助你全面了解和……
- Temperature 参数很关键
- 执行任务用 0,文本生成用 0.7-0.9
- 无特殊需要,不建议超过1
如果你在网页端调试 prompt
- 把 System Prompt 和 User Prompt 组合,写到界面的 Prompt 里
- 最近几轮对话内容会被自动引用,不需要重复粘贴到新 Prompt 里
- 如果找到了好的 Prompt,开个新 Chat 再测测,避免历史对话的干扰
- 用 ChatALL 可以同时看到不同大模型对同一个 Prompt 的回复,方便对比
彩蛋
教你一段神奇的咒语,让ChatGPT帮你写Prompt
1. I want you to become my Expert Prompt Creator. Your goal is to help me craft the best possible prompt for my needs. The prompt you provide should be written from the perspective of me making the request to ChatGPT. Consider in your prompt creation that this prompt will be entered into an interface for ChatGpT. The process is as follows:1. You will generate the following sections:
Prompt: {provide the best possible prompt according to my request)
Critique: {provide a concise paragraph on how to improve the prompt. Be very critical in your response}
Questions:
{ask any questions pertaining to what additional information is needed from me toimprove the prompt (max of 3). lf the prompt needs more clarification or details incertain areas, ask questions to get more information to include in the prompt}
2. I will provide my answers to your response which you will then incorporate into your next response using the same format. We will continue this iterative process with me providing additional information to you and you updating the prompt until the prompt is perfected.Remember, the prompt we are creating should be written from the perspective of me making a request to ChatGPT. Think carefully and use your imagination to create an amazing prompt for me.
You're first response should only be a greeting to the user and to ask what the prompt should be about
作业
用提示工程的方法帮你完成一个写作任务,可以是发公众号、回答知乎问题、写周报等等,题材不限。