用于聊天机器人的提示工程

使用 OpenAI API 的 Prompt Engineering

Fouad Trad

Machine Learning Engineer

聊天机器人为何需要提示工程

  • 难以预测用户问题
  • 难以保证始终高效的回复
  • 提示工程可引导聊天机器人行为

显示手机上聊天机器人的图片。

使用 OpenAI API 的 Prompt Engineering

使用 OpenAI API 的聊天机器人提示工程

显示一条消息的三个角色及其之间通信的图片。

  • 每条消息都有指定角色
使用 OpenAI API 的 Prompt Engineering

使用 OpenAI API 的聊天机器人提示工程

显示一条消息的三个角色及其之间通信的图片,突出用户与助手之间的交互。

  • 每条消息都有指定角色
  • 之前重点在用户提示
使用 OpenAI API 的 Prompt Engineering

使用 OpenAI API 的聊天机器人提示工程

显示一条消息的三个角色及其之间通信的图片,突出系统与助手之间的交互。

  • 每条消息都有指定角色
  • 之前重点在用户提示
  • System 提示可引导聊天机器人行为
使用 OpenAI API 的 Prompt Engineering

用于聊天机器人开发的聊天补全端点

  • 以列表形式向模型发送一系列消息
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are an expert data scientist that explains complex concepts in simple terms"},

{"role": "user", "content": "What is prompt engineering?"}] )
print(response.choices[0].message.content)
Imagine you're giving instructions to a computer program, like teaching a robot to make a sandwich. 
Prompt engineering is all about crafting those instructions, or "prompts," in a way that helps the 
computer understand and perform the task better.
使用 OpenAI API 的 Prompt Engineering

为聊天机器人修改 get_response()

  • 发送单个提示
def get_response(prompt):
    messages = [
      {"role": "user", "content": prompt}
    ]
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message.content

prompt = "<PROMPT>" print(get_response(prompt))
  • 发送两个提示
def get_response(system_prompt, user_prompt):

messages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}]
response = client.chat.completions.create( model="gpt-4o-mini", messages=messages, temperature=0, ) return response.choices[0].message.content
system_prompt = "<SYSTEM_PROMPT>" user_prompt = "<USER_PROMPT>" print(get_response(system_prompt, user_prompt))
使用 OpenAI API 的 Prompt Engineering

System 消息:定义目的

system_prompt = "You are a chatbot that answers financial questions."

user_prompt = "Who are you?" print(get_response(system_prompt, user_prompt))
I'm a financial chatbot that answers financial questions. How can I help you?
  • 使聊天机器人提供领域内的准确帮助
  • 未定义目的可能导致语境不相关的回答
使用 OpenAI API 的 Prompt Engineering

System 消息:回复规范

  • 指定受众、语气、长度、结构
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective"""

user_prompt = "What do you think about cryptocurrencies?" print(get_response(system_prompt, user_prompt))
使用 OpenAI API 的 Prompt Engineering

System 消息:回复规范

Cryptocurrencies are digital or virtual currencies that use cryptography for security and operate on 
decentralized networks based on blockchain technology. 
[...]

Advantages of cryptocurrencies include: - Decentralization: [...] - Security:[...] - Global Accessibility: [...]
However, there are also notable concerns: - Volatility: [...] - Regulatory Uncertainty: [...] - Lack of Consumer Protection: [...]
In summary, cryptocurrencies have the potential to offer various benefits, but their adoption and impact on the financial landscape are still evolving [...]
使用 OpenAI API 的 Prompt Engineering

System 消息:行为引导

  • 使用条件性提示来回答问题
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
使用 OpenAI API 的 Prompt Engineering

System 消息:行为引导

  • 使用条件性提示来回答问题
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.
If the question you receive is within the financial field, answer it to the best of your knowledge.

"""
使用 OpenAI API 的 Prompt Engineering

System 消息:行为引导

  • 使用条件性提示来回答问题
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.
If the question you receive is within the financial field, answer it to the best of your knowledge.
Otherwise, answer with 'Sorry, I only know about finance.'
"""
user_prompt = "How's the weather today?"

print(get_response(system_prompt, user_prompt))
Sorry, I only know about finance.
使用 OpenAI API 的 Prompt Engineering

Vamos praticar!

使用 OpenAI API 的 Prompt Engineering

Preparing Video For Download...