Інженерія підказок для розробки чатботів

Інженерія підказок з OpenAI API

Fouad Trad

Machine Learning Engineer

Навіщо чатботам інженерія підказок

  • Важко передбачити запитання користувачів
  • Складно гарантувати ефективні відповіді
  • Інженерія підказок керує поведінкою чатбота

Зображення чатбота на мобільному телефоні.

Інженерія підказок з OpenAI API

Інженерія підказок чатбота з OpenAI API

Зображення трьох ролей повідомлення та їх взаємодії.

  • Кожне повідомлення має визначену роль
Інженерія підказок з OpenAI API

Інженерія підказок чатбота з OpenAI API

Зображення трьох ролей повідомлення та їх взаємодії, з акцентом на взаємодії користувача й асистента.

  • Кожне повідомлення має визначену роль
  • Фокус був на підказках користувача
Інженерія підказок з OpenAI API

Інженерія підказок чатбота з OpenAI API

Зображення трьох ролей повідомлення та їх взаємодії, з акцентом на взаємодії системи й асистента.

  • Кожне повідомлення має визначену роль
  • Фокус був на підказках користувача
  • Системні підказки скеровують поведінку чатбота
Інженерія підказок з OpenAI API

Кінцева точка chat completions для розробки чатботів

  • Надсилає серію повідомлень до моделі як список
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

Зміна 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

Системне повідомлення: визначте призначення

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

Системне повідомлення: настанови для відповіді

  • Задайте аудиторію, тон, довжину, структуру
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

Системне повідомлення: настанови для відповіді

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

Системне повідомлення: керування поведінкою

  • Умовні підказки для відповіді на запитання
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
Інженерія підказок з OpenAI API

Системне повідомлення: керування поведінкою

  • Умовні підказки для відповіді на запитання
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

Системне повідомлення: керування поведінкою

  • Умовні підказки для відповіді на запитання
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

Давайте потренуємось!

Інженерія підказок з OpenAI API

Preparing Video For Download...