Prompts de chain-of-thought y self-consistency

Ingeniería rápida con la API de OpenAI

Fouad Trad

Machine Learning Engineer

Chain-of-thought prompting

  • Hace que los LLM den pasos de razonamiento (pensamientos) antes de responder
  • Útil para tareas de razonamiento complejo
  • Ayuda a reducir errores del modelo

Imagen que muestra cómo un prompt chain-of-thought pide al modelo resolver un problema paso a paso, y la salida contiene el razonamiento de cada paso.

Ingeniería rápida con la API de OpenAI

Chain-of-thought prompting

Prompt estándar para resolver una tarea de razonamiento
prompt = """Q: You start with 15 books in your collection. At the bookstore, you 
purchase 8 new books. Then, you lend 3 to your friend and 2 to your cousin. Later, 
you visit another bookstore and buy 5 more books. How many books do you have now? 
A: The answer is"""

print(get_response(prompt))
25 books
Ingeniería rápida con la API de OpenAI

Chain-of-thought prompting

Chain-of-thought para resolver una tarea de razonamiento
prompt = """Q: You start with 15 books in your collection. At the bookstore, you 
purchase 8 new books. Then, you lend 3 to your friend and 2 to your cousin. Later, 
you visit another bookstore and buy 5 more books. How many books do you have now? 
A: Let's think step by step""" 
print(get_response(prompt))
Step 1: Start with the number of books in your collection: 15 books
Step 2: Purchase 8 new books at the bookstore: 15 + 8 = 23 books
Step 3: Lend 3 books to your friend: 23 - 3 = 20 books
Step 4: Lend 2 books to your cousin: 20 - 2 = 18 books
Step 5: Visit another bookstore and buy 5 more books: 18 + 5 = 23 books
Therefore, you have 23 books now.
Ingeniería rápida con la API de OpenAI

Chain-of-thought con few‑shots

example = """
Q: The odd numbers in this group add up to an even number:  9, 10, 13, 4, 2.
A: Adding all the odd numbers (9, 13) gives 22. The answer is True.
"""

question = """ Q: The odd numbers in this group add up to an even number: 15, 13, 82, 7. A: """
prompt = example + question print(get_response(prompt))
Adding all the odd numbers (15, 13, 7) gives 35. The answer is False.
Ingeniería rápida con la API de OpenAI

Chain-of-thought vs. prompts multi‑paso

Prompts multi‑paso

  • Incluyen pasos dentro del prompt

Diagrama que muestra que un prompt multi‑paso da al modelo una serie de pasos secuenciales a ejecutar.

Ingeniería rápida con la API de OpenAI

Chain-of-thought vs. prompts multi‑paso

Prompts multi‑paso

  • Incluyen pasos dentro del prompt

Diagrama que muestra que un prompt multi‑paso da al modelo una serie de pasos secuenciales a ejecutar.

Prompts chain-of-thought

  • Piden al modelo generar pasos intermedios

Imagen que muestra que en prompts chain-of-thought, los pasos los genera el modelo al producir la salida.

Ingeniería rápida con la API de OpenAI

Limitación del chain-of-thought

  • Un pensamiento fallido --> resultado fallido
  • Se introdujeron los prompts self-consistency

Diagrama de un prompt chain-of-thought destacando que un pensamiento con razonamiento defectuoso lleva a un mal resultado.

Ingeniería rápida con la API de OpenAI

Self-consistency prompting

  • Genera múltiples chain-of-thought repitiendo el prompt varias veces
  • Voto mayoritario para el resultado final

Imagen que muestra que un prompt self-consistency genera múltiples chain-of-thought, cada uno con salida, y el resultado final se obtiene por voto mayoritario.

Ingeniería rápida con la API de OpenAI

Self-consistency prompting

Puede hacerse definiendo múltiples prompts o un prompt que genere múltiples respuestas.

self_consistency_instruction = "Imagine three completely independent experts who 
reason differently are answering this question. The final answer is obtained by 
majority vote. The question is: "

problem_to_solve = "If there are 10 cars in the parking lot and 3 more cars arrive. Half the original number of cars leave. Then, half of the current number of cars arrive. How many cars are there in the parking?"
prompt = self_consistency_instruction + problem_to_solve print(get_response(prompt))
Ingeniería rápida con la API de OpenAI

Prompt self-consistency

Expert 1: Let's go step by step [...] Therefore, the total number of cars in the 
parking lot is 8 + 4 = 12.

Expert 2: First, let's calculate [...] Therefore, the total number of cars in the 
parking lot is now 5 + 2 = 7 cars.

Expert 3: Initially, there are 10 cars [...] Thus, the final answer is 8 + 4 = 12 
cars in the parking lot.

Based on the majority vote, the final answer is that there are 12 cars in the 
parking lot.
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...