SlideShare a Scribd company logo
: Programming, not Prompting Language Models
Shangyin Tan
It’s never been easier to prototype
impressive AI demos.
Turning monolithic LMs into reliable AI
systems remains challenging.
3
Turning monolithic LMs into reliable AI
systems remains challenging.
4
The DSPy paradigm - dspy.ai
Let’s program—not prompt—LMs.
Connect declarative modules into a computation graph, and compile it into a chain of optimized
prompts (or LM finetunes) automatically. How?
1. Hand-written Prompts Signatures: question -> answer long_document -> summary
2. Prompting Techniques and Chains Modules: dspy.ChainOfThought dspy.ReAct
3. Manual Prompt Engineering
a. Optimizers (given a metric you want to maximize)
b. Assertions (similar to assertions in programming language)
Let’s get concrete: Question Answering with HotPotQA
Question: How many storeys are in the castle David Gregory inherited?
Passages: (1) St. Gregory Hotel is a 9-floor boutique hotel… (2) Kinnairdy Castle
is tower house with five storeys…
Answer: Kinnairdy Castle has five storeys.
Let’s build three programs for this task. Program 1.
CoT = dspy.ChainOfThought("question -> answer")
How many storeys are in the castle
David Gregory inherited?
Answer: Castle Gregory has three storeys.
CoT
CoT
Question
Chain of Thought (Reasoning); Answer
Let’s build another program for this task. Program 2.
class RAG(dspy.Module):
def __init__(self, num_passages=3):
self.retrieve = dspy.Retrieve(k=num_passages)
self.generate_answer = dspy.ChainOfThought("context, question -> answer")
def forward(self, question):
passages = self.retrieve(question).passages
return self.generate_answer(context=passages, question=question)
Retriever
<finds relevant passages>
Answer
LM
Question
Retriever
St. Gregory Hotel is a 9-floor
boutique hotel…
St. Gregory Hotel has nine storeys.
LM
How many storeys are in the castle
David Gregory inherited?
Let’s build a solid Program 3 for this task.
class MultiHop(dspy.Module):
def __init__(self, passages_per_hop=3):
self.generate_query = dspy.ChainOfThought("context, question -> query")
self.retrieve = dspy.Retrieve(k=passages_per_hop)
self.generate_answer = dspy.ChainOfThought("context, question -> answer")
def forward(self, question):
context = []
for _ in range(2):
query = self.generate_query(context=context, question=question).query
context += self.retrieve(query).passages
return self.generate_answer(context=context, question=question)
LM
What castle did David
Gregory inherit?
Retriever
David Gregory… inherited the
Kinnairdy Castle in 1664.
LM
How many storeys are
in Kinnairdy Castle?
Retriever
Kinnairdy Castle is tower
house with five storeys…
LM
Kinnairdy Castle has five storeys.
How many storeys are in the castle
David Gregory inherited?
What does the DSPy Compiler do?
A Few
Examples
(labeled and/or unlabeled)
Improved
DSPy
Program
DSPy Program
DSPy Compiler
(a specific optimizer)
improved_dspy_program = optimizer.compile(dspy_program, few_examples)
class MultiHop(dspy.Module):
def __init__(self, passages_per_hop=3):
self.generate_query = dspy.ChainOfThought("context, question -> query")
self.retrieve = dspy.Retrieve(k=passages_per_hop)
self.generate_answer = dspy.ChainOfThought("context, question -> answer")
def forward(self, question):
context = []
for _ in range(2):
query = self.generate_query(context, question).query
context += self.retrieve(query).passages
return self.generate_answer(context, question)
Let’s compile our multi-hop program.
Basket of optimizers (and growing!)
fewshot_program = dspy.LabeledFewShot(k=8).compile(program, trainset=trainset)
teleprompter = dspy.BootstrapFewShotWithRandomSearch(metric=gsm8k_accuracy)
bootstrapped_program = teleprompter.compile(program, trainset=trainset)
ensemble = dspy.Ensemble(reduce_fn=dspy.majority).compile(bootstrapped_program.programs[:7])
bootstrappedx2_program = teleprompter.compile(program, teacher=bootstrapped_program, trainset=trainset)
GSM8K - grade school math dataset
20 birds migrate on a seasonal basis from one lake to another, searching
for food. If they fly from lake Jim to lake Disney in one season, which is 50
miles apart, then the next season they fly from lake Disney to lake London,
60 miles apart, calculate the combined distance all of the birds have
traveled in the two seasons.
Answer: 20 * (50 + 60) = 2200 miles
class SimpleMathSolver(dspy.Module):
def __init__(self):
self.prog = dspy.ChainOfThought("question -> answer")
def forward(self, question):
return self.prog(question=question)
simple_math_solver = SimpleMathSolver()
First DSPy program
Given the fields `question`, produce the fields `answer`.
---
Follow the following format.
Question: ${question}
Reasoning: Let's think step by step in order to ${produce the answer}. We ...
Answer: ${answer}
---
Question: 20 birds migrate on a seasonal basis from one lake to another, searching for food. If they fly
from lake Jim to lake Disney in one season, which is 50 miles apart, then the next season they fly from
lake Disney to lake London, 60 miles apart, calculate the combined distance all of the birds have traveled
in the two seasons.
Reasoning: Let's think step by step in order to produce the answer. We know that the
birds fly 50 miles from lake Jim to lake Disney and then 60 miles from lake Disney to
lake London. To find the combined distance, we simply add the two distances
together.
Answer: The combined distance all of the birds have traveled in the two seasons is
50 miles + 60 miles = 110 miles.
Two problems:
1. Reasoning does not include all the information needed
2. Answer in complicated sentences, not a single number
LMs are not following what we expect.
And, there's no way to even specify the constraints except manual
prompt tuning.
Goal: Enable DSPy programmers to define constraints on LM behavior.
Can we make a robust, extensible programming construct?
Including Assertions in DSPy
dspy.Assert - DSPy must either pass the assertion or raise an Exception
dspy.Suggest - DSPy should try to pass the assertion, but permit the code to continue otherwise
dspy.Suggest(constraint: bool, instruction_message: str)
Introducing DSPy Assertions — to guide LM behavior in DSPy programs.
DSPy Assertions
Assertion-driven backtracking
Self-refine LMs by providing feedback and past
erroneous response
Example selection and bootstrapping
Provide better demonstrations for in-context learning
def extract_number(question):
"""extract numbers from a question"""
def has_numbers(rationale, numbers):
"""whether rationale has all the numbers, if not, return the missing number"""
class SimpleMathSolverWithSuggest(dspy.Module):
def __init__(self):
self.prog = dspy.ChainOfThought("question -> answer")
def forward(self, question):
pred = self.prog(question=question)
rationale_has_numbers, missing_number = has_numbers(pred.rationale, extract_number(question))
dspy.Suggest(rationale_has_numbers, f"Your Reasoning should contain {missing_number}.")
dspy.Suggest(len(pred.answer) < 10, "Your Answer should be a number.")
return pred
simple_math_solver_suggest = SimpleMathSolverWithSuggest().activate_assertions()
DSPy program with Assertions
Assertion-driven backtracking
Self-refine LMs by providing feedback and past
erroneous response
Given the fields `question`, produce the fields `answer`.
---
Follow the following format.
Question: ${question}
Reasoning: Let's think step by step in order to ${produce the answer}. We ...
Answer: ${answer}
---
Question: 20 birds migrate on a seasonal basis from one lake to another, searching for food. If they fly
from lake Jim to lake Disney in one season, which is 50 miles apart, then the next season they fly from
lake Disney to lake London, 60 miles apart, calculate the combined distance all of the birds have traveled
in the two seasons.
Reasoning: Let's think step by step in order to produce the answer. We know that the
birds fly 50 miles from lake Jim to lake Disney and then 60 miles from lake Disney to
lake London. To find the combined distance, we simply add the two distances
together.
Answer: The combined distance all of the birds have traveled in the two seasons is
50 miles + 60 miles = 110 miles.
def extract_number(question):
"""extract numbers from a question"""
def has_numbers(rationale, numbers):
"""whether rationale has all the numbers, if not, return the missing number"""
class SimpleMathSolverWithSuggest(dspy.Module):
def __init__(self):
self.prog = dspy.ChainOfThought("question -> answer")
def forward(self, question):
pred = self.prog(question=question)
rationale_has_numbers, missing_number = has_numbers(pred.rationale, extract_number(question))
dspy.Suggest(rationale_has_numbers, f"Your Reasoning should contain {missing_number}.")
dspy.Suggest(len(pred.answer) < 10, "Your Answer should be a number.")
return pred
simple_math_solver_suggest = SimpleMathSolverWithSuggest().activate_assertions()
Given the fields `question`, produce the fields `answer`.
---
Follow the following format.
Question: ${question}
Previous Reasoning: past Reasoning with errors
Instructions: Some instructions you must satisfy
Reasoning: Let's think step by step in order to ${produce the answer}. We ...
Answer: ${answer}
---
Question: 20 birds migrate on a seasonal basis from one lake to another, searching for food. If they fly from lake Jim
to lake Disney in one season, which is 50 miles apart, then the next season they fly from lake Disney to lake London,
60 miles apart, calculate the combined distance all of the birds have traveled in the two seasons.
Previous Reasoning: produce the answer. We know that the birds fly 50 miles from lake Jim to lake Disney and then
60 miles from lake Disney to lake London. To find the combined distance, we simply add the two distances together.
Instructions: Your Reasoning should contain 20.
Reasoning: Let's think step by step in order to calculate the combined distance all of the birds have
traveled in the two seasons. In the first season, 20 birds fly 50 miles from lake Jim to lake Disney,
which totals to 20 birds * 50 miles = 1000 miles. In the second season, they fly 60 miles from lake
Disney to lake London, which totals to 20 birds * 60 miles = 1200 miles. Therefore, the combined
distance all of the birds have traveled in the two seasons is 1000 miles + 1200 miles = 2200 miles.
Answer: The combined distance all of the birds have traveled in the two seasons is 2200 miles.
def extract_number(question):
"""extract numbers from a question"""
def has_numbers(rationale, numbers):
"""whether rationale has all the numbers, if not, return the missing number"""
class SimpleMathSolverWithSuggest(dspy.Module):
def __init__(self):
self.prog = dspy.ChainOfThought("question -> answer")
def forward(self, question):
pred = self.prog(question=question)
rationale_has_numbers, missing_number = has_numbers(pred.rationale, extract_number(question))
dspy.Suggest(rationale_has_numbers, f"Your Reasoning should contain {missing_number}.")
dspy.Suggest(len(pred.answer) < 10, "Your Answer should be a number.")
return pred
simple_math_solver_suggest = SimpleMathSolverWithSuggest().activate_assertions()
Given the fields `question`, produce the fields `answer`.
---
Follow the following format.
Question: ${question}
Previous Reasoning: past Reasoning with errors
Instructions: Some instructions you must satisfy
Reasoning: Let's think step by step in order to ${produce the answer}. We ...
Answer: ${answer}
---
Question: 20 birds migrate on a seasonal basis from one lake to another, searching for food. If they fly from lake Jim
to lake Disney in one season, which is 50 miles apart, then the next season they fly from lake Disney to lake London,
60 miles apart, calculate the combined distance all of the birds have traveled in the two seasons.
Previous Answer: The combined distance all of the birds have traveled in the two seasons is 2200 miles.
Instructions: Your Answer should be a number.
Reasoning: Let's think step by step in order to produce the answer. We know that in the first season,
20 birds fly 50 miles from lake Jim to lake Disney, totaling 20 birds * 50 miles = 1000 miles. In the
second season, they fly 60 miles from lake Disney to lake London, totaling 20 birds * 60 miles =
1200 miles. Therefore, the combined distance all of the birds have traveled in the two seasons is
1000 miles + 1200 miles = 2200 miles.
Answer: 2200 miles
optimizer = BootstrapFewShotWithRandomSearch(
gsm8k_metric, max_bootstrapped_demos=3, max_labeled_demos=6, num_candidate_programs=6)
compiled_prog = optimizer.compile(student=simple_math_solver)
compiled_prog_suggest = optimizer.compile(student=simple_math_solver_suggest)
Example selection and bootstrapping
Provide better demonstrations for in-context learning
1. Demonstrations need to pass DSPy assertions, too.
2. Demonstrations can also contain traces with errors and fixes.
3. Optimizer is "assertion-aware" - it also calculates how many assertion failure in
addition to "metric"
Results
Simple CoT CoT w Assertions Compiled CoT Compiled w Assertion
61.7 74.3 83.7 84.0
*Compiled w Assertion does not outperform Compiled on this task. Our
observations are: the harder assertions are, the better they are at
improving in-context learning demonstrations.
How could we use LM Assertions here?
HumanEval - code generation
class NaiveCodeGenerator(dspy.Module):
def __init__(self):
self.prog = dspy.ChainOfThought("prompt -> code")
def forward(self, prompt):
pred = self.prog(prompt=prompt)
return pred
First DSPy program
🤖Code
Generator
Prompt
🤖Test
Generator
suggest(check_tests(code, test), f"The
generated code failed the test {test}")
Code
Code Generation Pipeline w. Suggestions
Test
Code
🤖Code
Generator
Prompt
Code
Code Generation Pipeline
Code
UPDATED PROMPT WITH FEEDBACK
Prompt: . . .
Past Code: <previous attempt w/ errors> . . .
Instruction: The generated code failed the test
<test>, please fix it. . .
class NaiveCodeGenerator(dspy.Module):
def __init__(self):
self.prog = dspy.ChainOfThought("prompt -> code")
self.generate_test = dspy.ChainOfThought("prompt -> test")
def forward(self, prompt):
pred = self.prog(prompt=prompt)
tests = self.generate_test(prompt=prompt)
result, test, error = check_tests(pred, tests)
dspy.Suggest( result == "passed",
f"The generated code failed the test {test}, please fix {error}.",
backtrack_module = self.prog )
return pred
DSPy program with Assertions
Results
Naive Program Generator Program Generator w Assertions
70.7 75.6
*Issues with self-consistency:
1. Both tests and code went wrong in the same way
2. Generated tests are wrong, thus not giving useful feedback
Notebook: bit.ly/dspy-gsm8k
bit.ly/dspy-humaneval
bit.ly/dspy-intro
Twitter: @Shangyint
Multi-Hop Question Answering with HotPotQA with Suggestions
class MultiHop(dspy.Module):
def __init__(self, passages_per_hop=3):
self.generate_query = dspy.ChainOfThought("context, question -> query")
self.retrieve = dspy.Retrieve(k=passages_per_hop)
self.generate_answer = dspy.ChainOfThought("context, question -> answer")
def forward(self, question):
context = []
queries = [question]
for _ in range(2):
query = self.generate_query(context=context, question=question).query
dspy.Suggest(len(query) < 100,
"Query should be less than 100 characters")
dspy.Suggest(is_query_distinct(query, queries),
f"Query should be distinct from {queries}")
queries += query
context += self.retrieve(query).passages
return self.generate_answer(context=context, question=question)
The queries should
be different from
previous ones
The query should be
concise
Multi-Hop Question Answering with HotPotQA with Suggestions
class MultiHop(dspy.Module):
def __init__(self, passages_per_hop=3):
self.generate_query = dspy.ChainOfThought("context, question -> query")
self.retrieve = dspy.Retrieve(k=passages_per_hop)
self.generate_answer = dspy.ChainOfThought("context, question -> answer")
def forward(self, question):
context = []
queries = [question]
for _ in range(2):
query = self.generate_query(context=context, question=question).query
dspy.Suggest(len(query) < 100,
"Query should be less than 100 characters")
dspy.Suggest(is_query_distinct(query, queries),
f"Query should be distinct from {queries}")
queries += query
context += self.retrieve(query).passages
return self.generate_answer(context=context, question=question)
Context: …
Question: …
Past_Query: {previous attempt w/ errors}
Instructions: Query should be distinct from …
backtrack and regenerate query with new prompt
update prompt with feedback
Fail ❌
Context
🤖Query
Generator
Retriever
Question
🤖Answer
Generator
Answer
Query
Context + Previous Queries
✓ suggest(len(query) < 100, "Query should be less than 100 chars")
✗ suggest(is_query_distinct(query, prev_queries), f"Query should be
distinct from {prev_queries}")
UPDATED PROMPT WITH FEEDBACK
Context: . . .
Question: . . .
Past Query: <previous attempt w/ errors> . . .
Instruction: Query should be distinct from . . .
Multi-hop QA Pipeline

More Related Content

Similar to Programming Foundation Models with DSPy - Meetup Slides

Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
Koen Handekyn
 
Programming in as3 the basics
Programming in as3 the basicsProgramming in as3 the basics
Programming in as3 the basics
Joseph Burchett
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
Programming Homework Help
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
NagaLakshmi_N
 
Banishing Loops with Functional Programming in PHP
Banishing Loops with Functional Programming in PHPBanishing Loops with Functional Programming in PHP
Banishing Loops with Functional Programming in PHP
David Hayes
 
java 8 Hands on Workshop
java 8 Hands on Workshopjava 8 Hands on Workshop
java 8 Hands on Workshop
Jeanne Boyarsky
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
Typesafe
 
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
Tekblink Jeeten
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
Neelkanth Sachdeva
 
cluster(python)
cluster(python)cluster(python)
cluster(python)
Noriyuki Kojima
 
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Live Exam Helper
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
Scott Leberknight
 
Most Asked Coding Questions .pdf
Most Asked Coding Questions .pdfMost Asked Coding Questions .pdf
Most Asked Coding Questions .pdf
krishna415649
 
CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1
johnnygoodman
 
Ruby meetup evolution of bof search
Ruby meetup   evolution of bof search Ruby meetup   evolution of bof search
Ruby meetup evolution of bof search
Miha Mencin
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
Vijaykota11
 
"S"OLID Principles - Single responsibility principle
"S"OLID Principles - Single responsibility principle"S"OLID Principles - Single responsibility principle
"S"OLID Principles - Single responsibility principle
Nishant Upadhyay
 
Annotaed slides for dynamic programming algorithm
Annotaed slides for dynamic programming algorithmAnnotaed slides for dynamic programming algorithm
Annotaed slides for dynamic programming algorithm
johnathangamal27
 

Similar to Programming Foundation Models with DSPy - Meetup Slides (20)

Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
 
Programming in as3 the basics
Programming in as3 the basicsProgramming in as3 the basics
Programming in as3 the basics
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
Banishing Loops with Functional Programming in PHP
Banishing Loops with Functional Programming in PHPBanishing Loops with Functional Programming in PHP
Banishing Loops with Functional Programming in PHP
 
java 8 Hands on Workshop
java 8 Hands on Workshopjava 8 Hands on Workshop
java 8 Hands on Workshop
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
cluster(python)
cluster(python)cluster(python)
cluster(python)
 
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Most Asked Coding Questions .pdf
Most Asked Coding Questions .pdfMost Asked Coding Questions .pdf
Most Asked Coding Questions .pdf
 
CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1
 
Ruby meetup evolution of bof search
Ruby meetup   evolution of bof search Ruby meetup   evolution of bof search
Ruby meetup evolution of bof search
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
"S"OLID Principles - Single responsibility principle
"S"OLID Principles - Single responsibility principle"S"OLID Principles - Single responsibility principle
"S"OLID Principles - Single responsibility principle
 
Annotaed slides for dynamic programming algorithm
Annotaed slides for dynamic programming algorithmAnnotaed slides for dynamic programming algorithm
Annotaed slides for dynamic programming algorithm
 

More from Zilliz

How CXAI Toolkit uses RAG for Intelligent Q&A
How CXAI Toolkit uses RAG for Intelligent Q&AHow CXAI Toolkit uses RAG for Intelligent Q&A
How CXAI Toolkit uses RAG for Intelligent Q&A
Zilliz
 
Multimodal Embeddings (continued) - South Bay Meetup Slides
Multimodal Embeddings (continued) - South Bay Meetup SlidesMultimodal Embeddings (continued) - South Bay Meetup Slides
Multimodal Embeddings (continued) - South Bay Meetup Slides
Zilliz
 
Ensuring Secure and Permission-Aware RAG Deployments
Ensuring Secure and Permission-Aware RAG DeploymentsEnsuring Secure and Permission-Aware RAG Deployments
Ensuring Secure and Permission-Aware RAG Deployments
Zilliz
 
Retrieval Augmented Generation Evaluation with Ragas
Retrieval Augmented Generation Evaluation with RagasRetrieval Augmented Generation Evaluation with Ragas
Retrieval Augmented Generation Evaluation with Ragas
Zilliz
 
Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+
Zilliz
 
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Zilliz
 
It's your unstructured data: How to get your GenAI app to production (and spe...
It's your unstructured data: How to get your GenAI app to production (and spe...It's your unstructured data: How to get your GenAI app to production (and spe...
It's your unstructured data: How to get your GenAI app to production (and spe...
Zilliz
 
The History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal EmbeddingsThe History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal Embeddings
Zilliz
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
Zilliz
 
How Vector Databases are Revolutionizing Unstructured Data Search in AI Appli...
How Vector Databases are Revolutionizing Unstructured Data Search in AI Appli...How Vector Databases are Revolutionizing Unstructured Data Search in AI Appli...
How Vector Databases are Revolutionizing Unstructured Data Search in AI Appli...
Zilliz
 
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and OllamaTirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Zilliz
 
ASIMOV: Enterprise RAG at Dialog Axiata PLC
ASIMOV: Enterprise RAG at Dialog Axiata PLCASIMOV: Enterprise RAG at Dialog Axiata PLC
ASIMOV: Enterprise RAG at Dialog Axiata PLC
Zilliz
 
Metadata Lakes for Next-Gen AI/ML - Datastrato
Metadata Lakes for Next-Gen AI/ML - DatastratoMetadata Lakes for Next-Gen AI/ML - Datastrato
Metadata Lakes for Next-Gen AI/ML - Datastrato
Zilliz
 
Multimodal Retrieval Augmented Generation (RAG) with Milvus
Multimodal Retrieval Augmented Generation (RAG) with MilvusMultimodal Retrieval Augmented Generation (RAG) with Milvus
Multimodal Retrieval Augmented Generation (RAG) with Milvus
Zilliz
 
Building an Agentic RAG locally with Ollama and Milvus
Building an Agentic RAG locally with Ollama and MilvusBuilding an Agentic RAG locally with Ollama and Milvus
Building an Agentic RAG locally with Ollama and Milvus
Zilliz
 
Specializing Small Language Models With Less Data
Specializing Small Language Models With Less DataSpecializing Small Language Models With Less Data
Specializing Small Language Models With Less Data
Zilliz
 
Occiglot - Open Language Models by and for Europe
Occiglot - Open Language Models by and for EuropeOcciglot - Open Language Models by and for Europe
Occiglot - Open Language Models by and for Europe
Zilliz
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 

More from Zilliz (20)

How CXAI Toolkit uses RAG for Intelligent Q&A
How CXAI Toolkit uses RAG for Intelligent Q&AHow CXAI Toolkit uses RAG for Intelligent Q&A
How CXAI Toolkit uses RAG for Intelligent Q&A
 
Multimodal Embeddings (continued) - South Bay Meetup Slides
Multimodal Embeddings (continued) - South Bay Meetup SlidesMultimodal Embeddings (continued) - South Bay Meetup Slides
Multimodal Embeddings (continued) - South Bay Meetup Slides
 
Ensuring Secure and Permission-Aware RAG Deployments
Ensuring Secure and Permission-Aware RAG DeploymentsEnsuring Secure and Permission-Aware RAG Deployments
Ensuring Secure and Permission-Aware RAG Deployments
 
Retrieval Augmented Generation Evaluation with Ragas
Retrieval Augmented Generation Evaluation with RagasRetrieval Augmented Generation Evaluation with Ragas
Retrieval Augmented Generation Evaluation with Ragas
 
Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+Scaling Vector Search: How Milvus Handles Billions+
Scaling Vector Search: How Milvus Handles Billions+
 
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
Garbage In, Garbage Out: Why poor data curation is killing your AI models (an...
 
It's your unstructured data: How to get your GenAI app to production (and spe...
It's your unstructured data: How to get your GenAI app to production (and spe...It's your unstructured data: How to get your GenAI app to production (and spe...
It's your unstructured data: How to get your GenAI app to production (and spe...
 
The History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal EmbeddingsThe History of Embeddings & Multimodal Embeddings
The History of Embeddings & Multimodal Embeddings
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
 
How Vector Databases are Revolutionizing Unstructured Data Search in AI Appli...
How Vector Databases are Revolutionizing Unstructured Data Search in AI Appli...How Vector Databases are Revolutionizing Unstructured Data Search in AI Appli...
How Vector Databases are Revolutionizing Unstructured Data Search in AI Appli...
 
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and OllamaTirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
Tirana Tech Meetup - Agentic RAG with Milvus, Llama3 and Ollama
 
ASIMOV: Enterprise RAG at Dialog Axiata PLC
ASIMOV: Enterprise RAG at Dialog Axiata PLCASIMOV: Enterprise RAG at Dialog Axiata PLC
ASIMOV: Enterprise RAG at Dialog Axiata PLC
 
Metadata Lakes for Next-Gen AI/ML - Datastrato
Metadata Lakes for Next-Gen AI/ML - DatastratoMetadata Lakes for Next-Gen AI/ML - Datastrato
Metadata Lakes for Next-Gen AI/ML - Datastrato
 
Multimodal Retrieval Augmented Generation (RAG) with Milvus
Multimodal Retrieval Augmented Generation (RAG) with MilvusMultimodal Retrieval Augmented Generation (RAG) with Milvus
Multimodal Retrieval Augmented Generation (RAG) with Milvus
 
Building an Agentic RAG locally with Ollama and Milvus
Building an Agentic RAG locally with Ollama and MilvusBuilding an Agentic RAG locally with Ollama and Milvus
Building an Agentic RAG locally with Ollama and Milvus
 
Specializing Small Language Models With Less Data
Specializing Small Language Models With Less DataSpecializing Small Language Models With Less Data
Specializing Small Language Models With Less Data
 
Occiglot - Open Language Models by and for Europe
Occiglot - Open Language Models by and for EuropeOcciglot - Open Language Models by and for Europe
Occiglot - Open Language Models by and for Europe
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 

Recently uploaded

FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptxFIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Alliance
 
UiPath Community Day Amsterdam: Code, Collaborate, Connect
UiPath Community Day Amsterdam: Code, Collaborate, ConnectUiPath Community Day Amsterdam: Code, Collaborate, Connect
UiPath Community Day Amsterdam: Code, Collaborate, Connect
UiPathCommunity
 
How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...
DianaGray10
 
Redefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI CapabilitiesRedefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI Capabilities
Priyanka Aash
 
Perth MuleSoft Meetup July 2024
Perth MuleSoft Meetup July 2024Perth MuleSoft Meetup July 2024
Perth MuleSoft Meetup July 2024
Michael Price
 
Self-Healing Test Automation Framework - Healenium
Self-Healing Test Automation Framework - HealeniumSelf-Healing Test Automation Framework - Healenium
Self-Healing Test Automation Framework - Healenium
Knoldus Inc.
 
Demystifying Neural Networks And Building Cybersecurity Applications
Demystifying Neural Networks And Building Cybersecurity ApplicationsDemystifying Neural Networks And Building Cybersecurity Applications
Demystifying Neural Networks And Building Cybersecurity Applications
Priyanka Aash
 
What's New in Teams Calling, Meetings, Devices June 2024
What's New in Teams Calling, Meetings, Devices June 2024What's New in Teams Calling, Meetings, Devices June 2024
What's New in Teams Calling, Meetings, Devices June 2024
Stephanie Beckett
 
Indian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for StartupsIndian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for Startups
AMol NAik
 
Keynote : Presentation on SASE Technology
Keynote : Presentation on SASE TechnologyKeynote : Presentation on SASE Technology
Keynote : Presentation on SASE Technology
Priyanka Aash
 
Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17
Bhajan Mehta
 
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptxFIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Alliance
 
Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024
siddu769252
 
FIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptxFIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Alliance
 
Keynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive SecurityKeynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive Security
Priyanka Aash
 
FIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptxFIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptx
FIDO Alliance
 
Discovery Series - Zero to Hero - Task Mining Session 1
Discovery Series - Zero to Hero - Task Mining Session 1Discovery Series - Zero to Hero - Task Mining Session 1
Discovery Series - Zero to Hero - Task Mining Session 1
DianaGray10
 
Choosing the Best Outlook OST to PST Converter: Key Features and Considerations
Choosing the Best Outlook OST to PST Converter: Key Features and ConsiderationsChoosing the Best Outlook OST to PST Converter: Key Features and Considerations
Choosing the Best Outlook OST to PST Converter: Key Features and Considerations
webbyacad software
 
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptxFIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Alliance
 
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
Fwdays
 

Recently uploaded (20)

FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptxFIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptx
 
UiPath Community Day Amsterdam: Code, Collaborate, Connect
UiPath Community Day Amsterdam: Code, Collaborate, ConnectUiPath Community Day Amsterdam: Code, Collaborate, Connect
UiPath Community Day Amsterdam: Code, Collaborate, Connect
 
How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...How UiPath Discovery Suite supports identification of Agentic Process Automat...
How UiPath Discovery Suite supports identification of Agentic Process Automat...
 
Redefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI CapabilitiesRedefining Cybersecurity with AI Capabilities
Redefining Cybersecurity with AI Capabilities
 
Perth MuleSoft Meetup July 2024
Perth MuleSoft Meetup July 2024Perth MuleSoft Meetup July 2024
Perth MuleSoft Meetup July 2024
 
Self-Healing Test Automation Framework - Healenium
Self-Healing Test Automation Framework - HealeniumSelf-Healing Test Automation Framework - Healenium
Self-Healing Test Automation Framework - Healenium
 
Demystifying Neural Networks And Building Cybersecurity Applications
Demystifying Neural Networks And Building Cybersecurity ApplicationsDemystifying Neural Networks And Building Cybersecurity Applications
Demystifying Neural Networks And Building Cybersecurity Applications
 
What's New in Teams Calling, Meetings, Devices June 2024
What's New in Teams Calling, Meetings, Devices June 2024What's New in Teams Calling, Meetings, Devices June 2024
What's New in Teams Calling, Meetings, Devices June 2024
 
Indian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for StartupsIndian Privacy law & Infosec for Startups
Indian Privacy law & Infosec for Startups
 
Keynote : Presentation on SASE Technology
Keynote : Presentation on SASE TechnologyKeynote : Presentation on SASE Technology
Keynote : Presentation on SASE Technology
 
Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17
 
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptxFIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
 
Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024
 
FIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptxFIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptx
 
Keynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive SecurityKeynote : AI & Future Of Offensive Security
Keynote : AI & Future Of Offensive Security
 
FIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptxFIDO Munich Seminar Introduction to FIDO.pptx
FIDO Munich Seminar Introduction to FIDO.pptx
 
Discovery Series - Zero to Hero - Task Mining Session 1
Discovery Series - Zero to Hero - Task Mining Session 1Discovery Series - Zero to Hero - Task Mining Session 1
Discovery Series - Zero to Hero - Task Mining Session 1
 
Choosing the Best Outlook OST to PST Converter: Key Features and Considerations
Choosing the Best Outlook OST to PST Converter: Key Features and ConsiderationsChoosing the Best Outlook OST to PST Converter: Key Features and Considerations
Choosing the Best Outlook OST to PST Converter: Key Features and Considerations
 
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptxFIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
 
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
"Hands-on development experience using wasm Blazor", Furdak Vladyslav.pptx
 

Programming Foundation Models with DSPy - Meetup Slides

  • 1. : Programming, not Prompting Language Models Shangyin Tan
  • 2. It’s never been easier to prototype impressive AI demos.
  • 3. Turning monolithic LMs into reliable AI systems remains challenging. 3
  • 4. Turning monolithic LMs into reliable AI systems remains challenging. 4
  • 5. The DSPy paradigm - dspy.ai Let’s program—not prompt—LMs. Connect declarative modules into a computation graph, and compile it into a chain of optimized prompts (or LM finetunes) automatically. How? 1. Hand-written Prompts Signatures: question -> answer long_document -> summary 2. Prompting Techniques and Chains Modules: dspy.ChainOfThought dspy.ReAct 3. Manual Prompt Engineering a. Optimizers (given a metric you want to maximize) b. Assertions (similar to assertions in programming language)
  • 6. Let’s get concrete: Question Answering with HotPotQA Question: How many storeys are in the castle David Gregory inherited? Passages: (1) St. Gregory Hotel is a 9-floor boutique hotel… (2) Kinnairdy Castle is tower house with five storeys… Answer: Kinnairdy Castle has five storeys.
  • 7. Let’s build three programs for this task. Program 1. CoT = dspy.ChainOfThought("question -> answer") How many storeys are in the castle David Gregory inherited? Answer: Castle Gregory has three storeys. CoT CoT Question Chain of Thought (Reasoning); Answer
  • 8. Let’s build another program for this task. Program 2. class RAG(dspy.Module): def __init__(self, num_passages=3): self.retrieve = dspy.Retrieve(k=num_passages) self.generate_answer = dspy.ChainOfThought("context, question -> answer") def forward(self, question): passages = self.retrieve(question).passages return self.generate_answer(context=passages, question=question) Retriever <finds relevant passages> Answer LM Question Retriever St. Gregory Hotel is a 9-floor boutique hotel… St. Gregory Hotel has nine storeys. LM How many storeys are in the castle David Gregory inherited?
  • 9. Let’s build a solid Program 3 for this task. class MultiHop(dspy.Module): def __init__(self, passages_per_hop=3): self.generate_query = dspy.ChainOfThought("context, question -> query") self.retrieve = dspy.Retrieve(k=passages_per_hop) self.generate_answer = dspy.ChainOfThought("context, question -> answer") def forward(self, question): context = [] for _ in range(2): query = self.generate_query(context=context, question=question).query context += self.retrieve(query).passages return self.generate_answer(context=context, question=question) LM What castle did David Gregory inherit? Retriever David Gregory… inherited the Kinnairdy Castle in 1664. LM How many storeys are in Kinnairdy Castle? Retriever Kinnairdy Castle is tower house with five storeys… LM Kinnairdy Castle has five storeys. How many storeys are in the castle David Gregory inherited?
  • 10. What does the DSPy Compiler do? A Few Examples (labeled and/or unlabeled) Improved DSPy Program DSPy Program DSPy Compiler (a specific optimizer) improved_dspy_program = optimizer.compile(dspy_program, few_examples)
  • 11. class MultiHop(dspy.Module): def __init__(self, passages_per_hop=3): self.generate_query = dspy.ChainOfThought("context, question -> query") self.retrieve = dspy.Retrieve(k=passages_per_hop) self.generate_answer = dspy.ChainOfThought("context, question -> answer") def forward(self, question): context = [] for _ in range(2): query = self.generate_query(context, question).query context += self.retrieve(query).passages return self.generate_answer(context, question) Let’s compile our multi-hop program.
  • 12. Basket of optimizers (and growing!) fewshot_program = dspy.LabeledFewShot(k=8).compile(program, trainset=trainset) teleprompter = dspy.BootstrapFewShotWithRandomSearch(metric=gsm8k_accuracy) bootstrapped_program = teleprompter.compile(program, trainset=trainset) ensemble = dspy.Ensemble(reduce_fn=dspy.majority).compile(bootstrapped_program.programs[:7]) bootstrappedx2_program = teleprompter.compile(program, teacher=bootstrapped_program, trainset=trainset)
  • 13. GSM8K - grade school math dataset 20 birds migrate on a seasonal basis from one lake to another, searching for food. If they fly from lake Jim to lake Disney in one season, which is 50 miles apart, then the next season they fly from lake Disney to lake London, 60 miles apart, calculate the combined distance all of the birds have traveled in the two seasons. Answer: 20 * (50 + 60) = 2200 miles
  • 14. class SimpleMathSolver(dspy.Module): def __init__(self): self.prog = dspy.ChainOfThought("question -> answer") def forward(self, question): return self.prog(question=question) simple_math_solver = SimpleMathSolver() First DSPy program
  • 15. Given the fields `question`, produce the fields `answer`. --- Follow the following format. Question: ${question} Reasoning: Let's think step by step in order to ${produce the answer}. We ... Answer: ${answer} --- Question: 20 birds migrate on a seasonal basis from one lake to another, searching for food. If they fly from lake Jim to lake Disney in one season, which is 50 miles apart, then the next season they fly from lake Disney to lake London, 60 miles apart, calculate the combined distance all of the birds have traveled in the two seasons. Reasoning: Let's think step by step in order to produce the answer. We know that the birds fly 50 miles from lake Jim to lake Disney and then 60 miles from lake Disney to lake London. To find the combined distance, we simply add the two distances together. Answer: The combined distance all of the birds have traveled in the two seasons is 50 miles + 60 miles = 110 miles.
  • 16. Two problems: 1. Reasoning does not include all the information needed 2. Answer in complicated sentences, not a single number LMs are not following what we expect. And, there's no way to even specify the constraints except manual prompt tuning.
  • 17. Goal: Enable DSPy programmers to define constraints on LM behavior. Can we make a robust, extensible programming construct?
  • 18. Including Assertions in DSPy dspy.Assert - DSPy must either pass the assertion or raise an Exception dspy.Suggest - DSPy should try to pass the assertion, but permit the code to continue otherwise dspy.Suggest(constraint: bool, instruction_message: str)
  • 19. Introducing DSPy Assertions — to guide LM behavior in DSPy programs. DSPy Assertions Assertion-driven backtracking Self-refine LMs by providing feedback and past erroneous response Example selection and bootstrapping Provide better demonstrations for in-context learning
  • 20. def extract_number(question): """extract numbers from a question""" def has_numbers(rationale, numbers): """whether rationale has all the numbers, if not, return the missing number""" class SimpleMathSolverWithSuggest(dspy.Module): def __init__(self): self.prog = dspy.ChainOfThought("question -> answer") def forward(self, question): pred = self.prog(question=question) rationale_has_numbers, missing_number = has_numbers(pred.rationale, extract_number(question)) dspy.Suggest(rationale_has_numbers, f"Your Reasoning should contain {missing_number}.") dspy.Suggest(len(pred.answer) < 10, "Your Answer should be a number.") return pred simple_math_solver_suggest = SimpleMathSolverWithSuggest().activate_assertions() DSPy program with Assertions Assertion-driven backtracking Self-refine LMs by providing feedback and past erroneous response
  • 21. Given the fields `question`, produce the fields `answer`. --- Follow the following format. Question: ${question} Reasoning: Let's think step by step in order to ${produce the answer}. We ... Answer: ${answer} --- Question: 20 birds migrate on a seasonal basis from one lake to another, searching for food. If they fly from lake Jim to lake Disney in one season, which is 50 miles apart, then the next season they fly from lake Disney to lake London, 60 miles apart, calculate the combined distance all of the birds have traveled in the two seasons. Reasoning: Let's think step by step in order to produce the answer. We know that the birds fly 50 miles from lake Jim to lake Disney and then 60 miles from lake Disney to lake London. To find the combined distance, we simply add the two distances together. Answer: The combined distance all of the birds have traveled in the two seasons is 50 miles + 60 miles = 110 miles.
  • 22. def extract_number(question): """extract numbers from a question""" def has_numbers(rationale, numbers): """whether rationale has all the numbers, if not, return the missing number""" class SimpleMathSolverWithSuggest(dspy.Module): def __init__(self): self.prog = dspy.ChainOfThought("question -> answer") def forward(self, question): pred = self.prog(question=question) rationale_has_numbers, missing_number = has_numbers(pred.rationale, extract_number(question)) dspy.Suggest(rationale_has_numbers, f"Your Reasoning should contain {missing_number}.") dspy.Suggest(len(pred.answer) < 10, "Your Answer should be a number.") return pred simple_math_solver_suggest = SimpleMathSolverWithSuggest().activate_assertions()
  • 23. Given the fields `question`, produce the fields `answer`. --- Follow the following format. Question: ${question} Previous Reasoning: past Reasoning with errors Instructions: Some instructions you must satisfy Reasoning: Let's think step by step in order to ${produce the answer}. We ... Answer: ${answer} --- Question: 20 birds migrate on a seasonal basis from one lake to another, searching for food. If they fly from lake Jim to lake Disney in one season, which is 50 miles apart, then the next season they fly from lake Disney to lake London, 60 miles apart, calculate the combined distance all of the birds have traveled in the two seasons. Previous Reasoning: produce the answer. We know that the birds fly 50 miles from lake Jim to lake Disney and then 60 miles from lake Disney to lake London. To find the combined distance, we simply add the two distances together. Instructions: Your Reasoning should contain 20. Reasoning: Let's think step by step in order to calculate the combined distance all of the birds have traveled in the two seasons. In the first season, 20 birds fly 50 miles from lake Jim to lake Disney, which totals to 20 birds * 50 miles = 1000 miles. In the second season, they fly 60 miles from lake Disney to lake London, which totals to 20 birds * 60 miles = 1200 miles. Therefore, the combined distance all of the birds have traveled in the two seasons is 1000 miles + 1200 miles = 2200 miles. Answer: The combined distance all of the birds have traveled in the two seasons is 2200 miles.
  • 24. def extract_number(question): """extract numbers from a question""" def has_numbers(rationale, numbers): """whether rationale has all the numbers, if not, return the missing number""" class SimpleMathSolverWithSuggest(dspy.Module): def __init__(self): self.prog = dspy.ChainOfThought("question -> answer") def forward(self, question): pred = self.prog(question=question) rationale_has_numbers, missing_number = has_numbers(pred.rationale, extract_number(question)) dspy.Suggest(rationale_has_numbers, f"Your Reasoning should contain {missing_number}.") dspy.Suggest(len(pred.answer) < 10, "Your Answer should be a number.") return pred simple_math_solver_suggest = SimpleMathSolverWithSuggest().activate_assertions()
  • 25. Given the fields `question`, produce the fields `answer`. --- Follow the following format. Question: ${question} Previous Reasoning: past Reasoning with errors Instructions: Some instructions you must satisfy Reasoning: Let's think step by step in order to ${produce the answer}. We ... Answer: ${answer} --- Question: 20 birds migrate on a seasonal basis from one lake to another, searching for food. If they fly from lake Jim to lake Disney in one season, which is 50 miles apart, then the next season they fly from lake Disney to lake London, 60 miles apart, calculate the combined distance all of the birds have traveled in the two seasons. Previous Answer: The combined distance all of the birds have traveled in the two seasons is 2200 miles. Instructions: Your Answer should be a number. Reasoning: Let's think step by step in order to produce the answer. We know that in the first season, 20 birds fly 50 miles from lake Jim to lake Disney, totaling 20 birds * 50 miles = 1000 miles. In the second season, they fly 60 miles from lake Disney to lake London, totaling 20 birds * 60 miles = 1200 miles. Therefore, the combined distance all of the birds have traveled in the two seasons is 1000 miles + 1200 miles = 2200 miles. Answer: 2200 miles
  • 26. optimizer = BootstrapFewShotWithRandomSearch( gsm8k_metric, max_bootstrapped_demos=3, max_labeled_demos=6, num_candidate_programs=6) compiled_prog = optimizer.compile(student=simple_math_solver) compiled_prog_suggest = optimizer.compile(student=simple_math_solver_suggest) Example selection and bootstrapping Provide better demonstrations for in-context learning 1. Demonstrations need to pass DSPy assertions, too. 2. Demonstrations can also contain traces with errors and fixes. 3. Optimizer is "assertion-aware" - it also calculates how many assertion failure in addition to "metric"
  • 27. Results Simple CoT CoT w Assertions Compiled CoT Compiled w Assertion 61.7 74.3 83.7 84.0 *Compiled w Assertion does not outperform Compiled on this task. Our observations are: the harder assertions are, the better they are at improving in-context learning demonstrations.
  • 28. How could we use LM Assertions here? HumanEval - code generation
  • 29. class NaiveCodeGenerator(dspy.Module): def __init__(self): self.prog = dspy.ChainOfThought("prompt -> code") def forward(self, prompt): pred = self.prog(prompt=prompt) return pred First DSPy program
  • 30. 🤖Code Generator Prompt 🤖Test Generator suggest(check_tests(code, test), f"The generated code failed the test {test}") Code Code Generation Pipeline w. Suggestions Test Code 🤖Code Generator Prompt Code Code Generation Pipeline Code UPDATED PROMPT WITH FEEDBACK Prompt: . . . Past Code: <previous attempt w/ errors> . . . Instruction: The generated code failed the test <test>, please fix it. . .
  • 31. class NaiveCodeGenerator(dspy.Module): def __init__(self): self.prog = dspy.ChainOfThought("prompt -> code") self.generate_test = dspy.ChainOfThought("prompt -> test") def forward(self, prompt): pred = self.prog(prompt=prompt) tests = self.generate_test(prompt=prompt) result, test, error = check_tests(pred, tests) dspy.Suggest( result == "passed", f"The generated code failed the test {test}, please fix {error}.", backtrack_module = self.prog ) return pred DSPy program with Assertions
  • 32. Results Naive Program Generator Program Generator w Assertions 70.7 75.6 *Issues with self-consistency: 1. Both tests and code went wrong in the same way 2. Generated tests are wrong, thus not giving useful feedback
  • 34. Multi-Hop Question Answering with HotPotQA with Suggestions class MultiHop(dspy.Module): def __init__(self, passages_per_hop=3): self.generate_query = dspy.ChainOfThought("context, question -> query") self.retrieve = dspy.Retrieve(k=passages_per_hop) self.generate_answer = dspy.ChainOfThought("context, question -> answer") def forward(self, question): context = [] queries = [question] for _ in range(2): query = self.generate_query(context=context, question=question).query dspy.Suggest(len(query) < 100, "Query should be less than 100 characters") dspy.Suggest(is_query_distinct(query, queries), f"Query should be distinct from {queries}") queries += query context += self.retrieve(query).passages return self.generate_answer(context=context, question=question) The queries should be different from previous ones The query should be concise
  • 35. Multi-Hop Question Answering with HotPotQA with Suggestions class MultiHop(dspy.Module): def __init__(self, passages_per_hop=3): self.generate_query = dspy.ChainOfThought("context, question -> query") self.retrieve = dspy.Retrieve(k=passages_per_hop) self.generate_answer = dspy.ChainOfThought("context, question -> answer") def forward(self, question): context = [] queries = [question] for _ in range(2): query = self.generate_query(context=context, question=question).query dspy.Suggest(len(query) < 100, "Query should be less than 100 characters") dspy.Suggest(is_query_distinct(query, queries), f"Query should be distinct from {queries}") queries += query context += self.retrieve(query).passages return self.generate_answer(context=context, question=question) Context: … Question: … Past_Query: {previous attempt w/ errors} Instructions: Query should be distinct from … backtrack and regenerate query with new prompt update prompt with feedback Fail ❌
  • 36. Context 🤖Query Generator Retriever Question 🤖Answer Generator Answer Query Context + Previous Queries ✓ suggest(len(query) < 100, "Query should be less than 100 chars") ✗ suggest(is_query_distinct(query, prev_queries), f"Query should be distinct from {prev_queries}") UPDATED PROMPT WITH FEEDBACK Context: . . . Question: . . . Past Query: <previous attempt w/ errors> . . . Instruction: Query should be distinct from . . . Multi-hop QA Pipeline