提示工程简介

使用 OpenAI API 的 Prompt Engineering

Fouad Trad

Machine Learning Engineer

什么是提示工程?

为大语言模型设计提示或指令以获得期望的响应

一位工程师介绍提示工程术语的图片。

使用 OpenAI API 的 Prompt Engineering

提示工程就像制订食谱

显示一位厨师烹饪的图片。

使用 OpenAI API 的 Prompt Engineering

为什么要做提示工程?

  一张图示,展示高质量提示会带来高质量回答

使用 OpenAI API 的 Prompt Engineering

为什么要做提示工程?

  一张图示,展示答案质量受输入提示质量影响:高质量提示带来高质量答案,低质量提示带来低质量答案。

使用 OpenAI API 的 Prompt Engineering

回顾:OpenAI API

  • 允许与 OpenAI 模型交互
  • 本课程已配置完成
  • 可访问 Chat Completions 端点

chatgpt_logo_white.png

使用 OpenAI API 的 Prompt Engineering

回顾:消息角色

每条消息都有 种角色之一

显示用户角色图标的图片。

使用 OpenAI API 的 Prompt Engineering

回顾:消息角色

每条消息都有 种角色之一

显示两个角色:用户和助手 的图标。

使用 OpenAI API 的 Prompt Engineering

回顾:消息角色

每条消息都有 种角色之一

分别显示三种角色:用户、系统、助手 的图标。

使用 OpenAI API 的 Prompt Engineering

回顾:消息角色

每条消息都有 种角色之一

分别显示三种角色,并在系统与助手间有传递系统消息的箭头的图标。

  • System message: 指导模型行为
使用 OpenAI API 的 Prompt Engineering

回顾:消息角色

每条消息都有 种角色之一

分别显示三种角色,并在系统与助手间传系统消息、用户与助手间传提示 的箭头的图标。

  • System message: 指导模型行为
  • User message: 来自用户的提示
使用 OpenAI API 的 Prompt Engineering

回顾:消息角色

每条消息都有 种角色之一

分别显示三种角色,并在系统与助手、用户与助手、助手与用户之间传递的箭头的图标。

  • System message: 指导模型行为
  • User message: 来自用户的提示
  • Assistant message: 对用户提示的响应
使用 OpenAI API 的 Prompt Engineering

回顾:控制参数

显示温度计图标,取值 0 到 2,0 为无随机性,2 为最高随机性。

  • temperature 控制答案的随机性
使用 OpenAI API 的 Prompt Engineering

回顾:控制参数

显示温度计图标(0 到 2)与表示 max_tokens 的滑块图标;较低值产生更短响应。

  • temperature 控制答案的随机性
  • max_tokens 控制响应长度
使用 OpenAI API 的 Prompt Engineering

回顾:与 OpenAI API 通信

prompt = "What is prompt engineering?"

client = OpenAI(api_key="api_key")
response = client.chat.completions.create(
model = "gpt-4o-mini",
messages = [{"role": "user", "content": prompt}],
temperature = 0 )
print(response.choices[0].message.content)
Prompt engineering refers to the process of designing and refining prompts or 
instructions given to a language model like ChatGPT in order to elicit desired 
responses or behaviors. It involves formulating specific guidelines or hints to 
guide the model's output towards a desired outcome.
使用 OpenAI API 的 Prompt Engineering

创建 get_response() 函数

def get_response(prompt):

response = client.chat.completions.create( model = "gpt-4o-mini", messages = [{"role": "user", "content": prompt}], temperature = 0 )
return response.choices[0].message.content

用法

response = get_response("What is prompt engineering?")
print(response)
Prompt engineering refers to the process of designing and refining prompts or instructions given to a language model 
like ChatGPT in order to elicit desired responses or behaviors. It involves formulating specific guidelines or hints 
to guide the model's output towards a desired outcome.
使用 OpenAI API 的 Prompt Engineering

改进提示

prompt = "What is prompt engineering? Explain it in terms that can be understood 
by a 5-year-old"
response = get_response(prompt)
print(response)
Imagine you have a very smart friend who can understand and answer lots of 
questions. But sometimes, they might not understand exactly what you want or give 
the wrong answer. So, prompt engineering is like giving your friend really clear 
instructions or hints to help them give you the best answer possible.
使用 OpenAI API 的 Prompt Engineering

Passons à la pratique !

使用 OpenAI API 的 Prompt Engineering

Preparing Video For Download...