Ingeniería de prompts para desarrollar chatbots

Ingeniería rápida con la API de OpenAI

Fouad Trad

Machine Learning Engineer

La necesidad de ingeniería de prompts para chatbots

  • Difícil predecir las preguntas del usuario
  • Complica garantizar respuestas efectivas
  • La ingeniería de prompts guía el comportamiento del chatbot

Imagen que muestra un chatbot en un teléfono móvil.

Ingeniería rápida con la API de OpenAI

Ingeniería de prompts para chatbots con la API de OpenAI

Imagen que muestra los tres roles de un mensaje y su comunicación.

  • Cada mensaje tiene un rol asignado
Ingeniería rápida con la API de OpenAI

Ingeniería de prompts para chatbots con la API de OpenAI

Imagen que muestra los tres roles de un mensaje y su comunicación, destacando la interacción entre usuario y asistente.

  • Cada mensaje tiene un rol asignado
  • Nos hemos centrado en los prompts del usuario
Ingeniería rápida con la API de OpenAI

Ingeniería de prompts para chatbots con la API de OpenAI

Imagen que muestra los tres roles de un mensaje y su comunicación, destacando la interacción entre sistema y asistente.

  • Cada mensaje tiene un rol asignado
  • Nos hemos centrado en los prompts del usuario
  • Los prompts del sistema guían el comportamiento del chatbot
Ingeniería rápida con la API de OpenAI

Endpoint de chat completions para desarrollar chatbots

  • Envía una serie de mensajes al modelo como lista
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.
Ingeniería rápida con la API de OpenAI

Cambiar get_response() para un chatbot

  • Enviar un solo 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))
  • Enviar dos 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))
Ingeniería rápida con la API de OpenAI

Mensaje del sistema: define el 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 el chatbot dé ayuda precisa del dominio
  • Si no defines el propósito, puede dar respuestas fuera de contexto
Ingeniería rápida con la API de OpenAI

Mensaje del sistema: pautas de respuesta

  • Especifica audiencia, tono, longitud, estructura
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))
Ingeniería rápida con la API de OpenAI

Mensaje del sistema: pautas de respuesta

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 [...]
Ingeniería rápida con la API de OpenAI

Mensaje del sistema: guiar el comportamiento

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


"""
Ingeniería rápida con la API de OpenAI

Mensaje del sistema: guiar el comportamiento

  • Prompts condicionales para responder preguntas
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.

"""
Ingeniería rápida con la API de OpenAI

Mensaje del sistema: guiar el comportamiento

  • Prompts condicionales para responder preguntas
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.
Ingeniería rápida con la API de OpenAI

¡Vamos a practicar!

Ingeniería rápida con la API de OpenAI

Preparing Video For Download...