Engenharia de prompts para criar chatbots

Engenharia rápida com a API OpenAI

Fouad Trad

Machine Learning Engineer

A necessidade de engenharia de prompts para chatbots

  • Difícil prever as perguntas do usuário
  • Desafiador garantir boas respostas
  • Engenharia de prompts guia o comportamento do chatbot

Imagem mostrando um chatbot em um celular.

Engenharia rápida com a API OpenAI

Engenharia de prompts de chatbot com a API da OpenAI

Imagem mostrando os três papéis de uma mensagem e a comunicação entre eles.

  • Cada mensagem tem um papel definido
Engenharia rápida com a API OpenAI

Engenharia de prompts de chatbot com a API da OpenAI

Imagem mostrando os três papéis de uma mensagem e a comunicação entre eles, com destaque para a interação entre usuário e assistente.

  • Cada mensagem tem um papel definido
  • O foco tem sido nos prompts do usuário
Engenharia rápida com a API OpenAI

Engenharia de prompts de chatbot com a API da OpenAI

Imagem mostrando os três papéis de uma mensagem e a comunicação entre eles, com destaque para a interação entre o sistema e o assistente.

  • Cada mensagem tem um papel definido
  • O foco tem sido nos prompts do usuário
  • Prompts do sistema guiam o comportamento do chatbot
Engenharia rápida com a API OpenAI

Endpoint de chat completions para criar chatbots

  • Envia uma lista de mensagens para o modelo
response = client.chat.completions.create(
  model="gpt-3.5-turbo",
  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.
Engenharia rápida com a API OpenAI

Alterando get_response() para chatbot

  • Enviando um único prompt
def get_response(prompt):
    messages = [
      {"role": "user", "content": prompt}
    ]
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message.content

prompt = "<PROMPT>" print(get_response(prompt))
  • Enviando dois prompts
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-3.5-turbo", 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))
Engenharia rápida com a API OpenAI

Mensagem do sistema: defina o propósito

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?
  • Permite que o chatbot dê suporte preciso ao domínio
  • Sem propósito definido, as respostas podem ser irrelevantes
Engenharia rápida com a API OpenAI

Mensagem do sistema: diretrizes de resposta

  • Especifique público, tom, tamanho, estrutura
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))
Engenharia rápida com a API OpenAI

Mensagem do sistema: diretrizes de resposta

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 [...]
Engenharia rápida com a API OpenAI

Mensagem do sistema: orientação de comportamento

  • Prompts condicionais para responder perguntas
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
Engenharia rápida com a API OpenAI

Mensagem do sistema: orientação de comportamento

  • Prompts condicionais para responder perguntas
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.

"""
Engenharia rápida com a API OpenAI

Mensagem do sistema: orientação de comportamento

  • Prompts condicionais para responder perguntas
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.
Engenharia rápida com a API OpenAI

Vamos praticar!

Engenharia rápida com a API OpenAI

Preparing Video For Download...