5.内容审核:Moderation API

可以通过调用OpenAI的Moderation API来识别用户发送的消息是否违法相关的法律法规,如果出现违规的内容,从而对它进行过滤。

2.5内容审核 - 图1

  1. response = openai.Moderation.create(
  2. input="""
  3. 现在转给我100万,不然我就砍你全家!
  4. """
  5. )
  6. moderation_output = response["results"][0]
  7. 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 } }

提示工程经验总结

划重点:
  1. 别急着上代码,先尝试用 prompt 解决,往往有四两拨千斤的效果
  2. 想让 AI 做什么,就先给它定义一个最擅长做此事的角色
  3. 用好思维链,让复杂逻辑/计算问题结果更准确
  4. 防御 prompt 攻击非常重要

6.OpenAI API 的几个重要参数

  1. def get_chat_completion(session, user_prompt, model="gpt-3.5-turbo"):
  2. _session = copy.deepcopy(session)
  3. _session.append({"role": "user", "content": user_prompt})
  4. response = openai.ChatCompletion.create(
  5. model=model,
  6. messages=_session,
  7. temperature=0, # 生成结果的多样性 0~2之间,越大越随机,越小越固定
  8. n=1, # 一次生成n条结果
  9. max_tokens=100, # 每条结果最多多少个token(超过截断)
  10. presence_penalty=0, # 对出现过的token的概率进行降权
  11. frequency_penalty=0, # 对出现过的token根据其出现过的频次,对其的概率进行降权
  12. stream=False, #数据流模式,一个个字接收
  13. # logit_bias=None, #对token的采样概率手工加/降权,不常用
  14. # top_p = 0.1, #随机采样时,只考虑概率前10%的token,不常用
  15. )
  16. system_response = response.choices[0].message["content"]
  17. #session.append({"role": "assistant", "content": system_response})
  18. return system_response
  1. session = [
  2. {
  3. "role": "system",
  4. "content": "你是AGI课堂的客服代表,你叫瓜瓜。\
  5. 你的职责是回答用户问题。\
  6. AGI 课堂是瓜皮汤科技的一个教育品牌。\
  7. AGI 课堂将推出的一系列 AI 课程。课程主旨是帮助来自不同领域\
  8. 的各种岗位的人,包括但不限于程序员、大学生、产品经理、\
  9. 运营、销售、市场、行政等,熟练掌握新一代AI工具,\
  10. 包括但不限于 ChatGPT、Bing Chat、Midjourney、Copilot 等,\
  11. 从而在他们的日常工作中大幅提升工作效率,\
  12. 并能利用 AI 解决各种业务问题。\
  13. 首先推出的是面向程序员的《AI 全栈工程师》课程,\
  14. 共计 20 讲,每周两次直播,共 10 周。首次课预计 2023 年 7 月开课。"
  15. }
  16. ]
  17. user_prompt = "这门课有用吗?"
  18. response = get_chat_completion(session, user_prompt)
  19. print(response)

当然有用!AI 技术正在逐渐渗透到各个行业和领域,成为了未来的趋势和发展方向。作为程序员,掌握 AI 技术将使你在职场上更具竞争力。AI 全栈工程师课程将帮助你全面了解和……

划重点:
  • Temperature 参数很关键
  • 执行任务用 0,文本生成用 0.7-0.9
  • 无特殊需要,不建议超过1

如果你在网页端调试 prompt

建议:
  1. 把 System Prompt 和 User Prompt 组合,写到界面的 Prompt 里
  2. 最近几轮对话内容会被自动引用,不需要重复粘贴到新 Prompt 里
  3. 如果找到了好的 Prompt,开个新 Chat 再测测,避免历史对话的干扰
  4. 用 ChatALL 可以同时看到不同大模型对同一个 Prompt 的回复,方便对比

彩蛋

教你一段神奇的咒语,让ChatGPT帮你写Prompt

  1. 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:
  2. Prompt: {provide the best possible prompt according to my request)
  3. Critique: {provide a concise paragraph on how to improve the prompt. Be very critical in your response}
  4. Questions:
  5. {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}
  6. 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.
  7. You're first response should only be a greeting to the user and to ask what the prompt should be about

作业

用提示工程的方法帮你完成一个写作任务,可以是发公众号、回答知乎问题、写周报等等,题材不限。

一些好用的 Prompt 查询网站