引入外部上下文

使用 OpenAI API 的 Prompt Engineering

Fouad Trad

Machine Learning Engineer

为何需要外部上下文

  • 预训练语言模型只识别其训练过的信息
  • 需要补充更多上下文
  • 提高准确性与效果

代表手机上聊天机器人的图标。

使用 OpenAI API 的 Prompt Engineering

LLM 的信息缺口

  • 知识截止
system_prompt = "Act as a financial expert that knows about the latest trends."

user_prompt = "What are the top financial trends in 2023?"

print(get_response(system_prompt, user_prompt))
I apologize for any inconvenience, but as of my last knowledge update in 
September 2021, I don't have information about financial trends in 2023.
使用 OpenAI API 的 Prompt Engineering

LLM 的信息缺口

  • 请求非公开信息
system_prompt = "Act as a study buddy that helps me with my studies to succeed 
in exams."

user_prompt = "What is the name of my favorite instructor?" print(get_response(system_prompt, user_prompt))
I don't have personal information about you, including the name of your 
favorite instructor.
使用 OpenAI API 的 Prompt Engineering

如何提供额外信息?

  • 提供过往对话样例
  • 使用系统提示词

显示两个消息对话框以体现对话特性的图标。

使用 OpenAI API 的 Prompt Engineering

示例对话

  • 引导模型回答特定问题
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are a customer service chatbot that responds to user queries in a gentle way"},

{"role": "user", "content": "What services do you offer?"},
{"role": "assistant", "content": "We provide services for web application development, mobile app development, and custom software solutions."},
{"role": "user", "content": "How many services do you have?"}])
print(response.choices[0].message.content)
We have 3 services including web application development, mobile app development, and custom software solutions.
  • 缺点:可能需要大量示例
使用 OpenAI API 的 Prompt Engineering

System prompt

  • 包含客服回答所需的上下文
services = "ABC Tech Solutions, a leading IT company, offers a range of services: 
application development, mobile app development, and custom software solutions."

system_prompt = f"""You are a customer service chatbot that responds to user queries in a gentle way. Some information about our services are delimited by triple backticks. ```{services}```"""
user_prompt = "How many services do you offer?" print(get_response(system_prompt, user_prompt))
We have 3 services including web application development, mobile app development, 
and custom software solutions.
使用 OpenAI API 的 Prompt Engineering

最后提示

  • 之前的方法适用于小型上下文
  • 更大的上下文需要更高级的技术

代表警报的图标。

使用 OpenAI API 的 Prompt Engineering

Passons à la pratique !

使用 OpenAI API 的 Prompt Engineering

Preparing Video For Download...