Ingeniería de prompts iterativos y perfeccionamiento

Ingeniería de prompts con la API de OpenAI

Fouad Trad

Machine Learning Engineer

Ingeniería iterativa de prompts

  • Ningún prompt puede ser perfecto al principio
  • La ingeniería de prompts es un proceso iterativo en el que:
    • Crea un prompt
    • Introdúcelo en el modelo
    • Observa y analiza el resultado
    • Reitera para mejorar el prompt

Imagen que representa un ciclo. Un círculo azul con cuatro flechas que se conectan entre sí.

Ingeniería de prompts con la API de OpenAI

Perfeccionar prompts

Prompt inicial
prompt = "Generate an Excel sheet containing
five student names and their grades"

print(get_response(prompt))
I'm sorry, but as a text-based AI, I am 
unable to directly provide an Excel sheet. 
However, I can help you generate a sample
representation of the data you requested.
Prompt refinado
prompt = "Generate a table that I can copy 
to Excel, containing five student names and 
their grades"
print(get_response(prompt))
| Student Name | Grade |
|--------------|-------|
| Student 1    |  90   |
| Student 2    |  85   |
| Student 3    |  95   |
| Student 4    |  88   |
| Student 5    |  92   |
Ingeniería de prompts con la API de OpenAI

Ejemplo: análisis de una función de Python

code = '''
def calculate_rectangle_area(length, width):
    area = length * width
    return area
'''

Imagen que muestra un rectángulo y la fórmula para calcular su área a partir de su anchura y longitud. Área = longitud\*anchura.

Ingeniería de prompts con la API de OpenAI

Ejemplo: prompt inicial

prompt = f"""
  Analyze the code delimited by triple backticks with one sentence
  ```{code}```.
"""
print(get_response(prompt))
The code calculates the area of a rectangle based on its length and width.
Ingeniería de prompts con la API de OpenAI

Ejemplo: perfeccionamiento del prompt

Modificamos el prompt para obtener un lenguaje de programación.

prompt = f"""
  Analyze the code delimited by triple backticks and provide its programming 
  language with one sentence 
  ```{code}```.
"""
print(get_response(prompt))
The provided code is a function written in Python that calculates the area of a 
rectangle based on its length and width.
Ingeniería de prompts con la API de OpenAI

Ejemplo: refinamiento del prompt

Modificamos el prompt para obtener una salida estructurada

prompt = f"""
  For the function delimited by triple backticks, provide in a structured format
  the following:
  - description: one sentence short description
  - language: the programming language used
  - input: the inputs to the function
  - output: the output returned by the function
  ```{code}```.
"""
print(get_response(prompt))
Ingeniería de prompts con la API de OpenAI

Ejemplo: refinamiento del prompt

description: This function calculates the area of a rectangle.

language: Python

input:
 - length: The length of the rectangle.
 - width: The width of the rectangle.

output:
 - area: The calculated area of the rectangle, which is the product of the length 
 and width.
Ingeniería de prompts con la API de OpenAI

Refinamiento del prompt con pocos ejemplos

  • Clasificación de descripciones meteorológicas

Prompt inicial

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
Fresh snowfall with freezing temperatures. -> 
"""
print(get_response(prompt))
Snowy
Ingeniería de prompts con la API de OpenAI

Perfeccionamiento de prompts con pocos ejemplos

  • Clasificación de descripciones meteorológicas

Prompt inicial

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
The wind of change brought a refreshing breeze to the company's operations. -> 
"""
print(get_response(prompt))
Windy
Ingeniería de prompts con la API de OpenAI

Perfeccionamiento de prompts con pocos ejemplos

Prompt refinado

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
The political climate in the country was stormy -> Unknown
The wind of change brought a refreshing breeze to the company's operations. -> 
"""
print(get_response(prompt))
Unknown
Ingeniería de prompts con la API de OpenAI

Refinamiento de prompt para distintos tipos de prompt

  • Prompts few-shot: refina los ejemplos
  • Prompts de varios pasos: perfecciona los pasos orientativos
  • Prompts de cadena de pensamiento y autoconsistencia: perfecciona la descripción del problema

Imagen que muestra tres filas con interruptores y una mano ajustándolos para reflejar el refinamiento.

Ingeniería de prompts con la API de OpenAI

¡Practiquemos!

Ingeniería de prompts con la API de OpenAI

Preparing Video For Download...