Learn to Program Not Write Prompts with DSPy

Databricks · Intermediate ·✍️ Prompt Engineering ·1y ago

Key Takeaways

The video demonstrates how to use DSPI, a declarative framework for programming GenAI applications, to develop large language model abstractions with typing and reusable modules, and how to integrate it with tools like PyTorch, OpenAI, and Databricks.

Full Transcript

All right, for those of you who have their headphones on, can I get a thumbs up? Can you hear me? Fantastic. All right, we're going to get started. Uh, if you have any technical issues, please uh wave down someone um up front or just kind of put your hand up if you have any technical issues. Um, with that, so let's get started. So, we have only 20 minutes to get through this, but today's session is about learning to program, not write problems with DSPI. We're going to use a restaurant example to kind of articulate how you're going to use DSPI to um declaratively program your Genai applications. And we'll go through an example of that as well. Um, but first, our forward-looking statement. You're going to see this a lot, so I'm not going to read through this. And a little bit about myself. I'm a delivery solutions architect from Data Bricks. Uh, I work in startups and emerging enterprises from Los Angeles and I have a huge interest in my cats and video games. So, fun facts. So, all that to say, um, as we all know and as you've probably interfaced with like chat, GBT, claude, and so forth, writing prompts is extremely tedious and very unsustainable, right? You don't want to be managing an entire block of text, which I'm sure some of you have already tried to do, um, to get your large language model to do exactly what you want it to do. So, it's also hard to identify where in that prompt something is going wrong, right? It's it could be this portion of your text, it could be this portion of your text, who knows? Um, but all that to say, you're ultimately just doing this by trial and error, right? If you were writing a system prompt for like your other framework, I'm not going to name drop, but um and you're trying to figure out which system prompt works the best. You're doing no shot, few shots, self-consistency, all these different prompt strategies, and you're just trying to figure out if it works. That's not also good practice. Um and it's also incredibly difficult to modularize, right? um you have to maintain essentially a library of prompts that you're going to send to your different applications, but at the end of the day, it's still text, right? Like what are you going to do with text? So, all that to say, that's why we're here to talk about DSP. DSPI 3.0 will be announced there's a three DSpai 3.0 session tomorrow from the creator of DSP himself. So if you want to learn more about DSPI 3, please register for that session. But this will kind of but I'm not going to cover that. We're just going to walk you through on how to use DSPI to then program your Genai application. So what is DSPI? Omar, sorry if I can't pronounce his last name still, but Omar Katab, he's the one who created it. Came out of Stanford. um he was under mate uh our CTO and a lot of a lot of his inspiration comes from PyTorch and neural network abstractions. So as you look through the DSPI examples, you'll actually start to see some familiar patterns from those frameworks within DSPI. It's a declarative uh framework. A lot of people first used to use DSP before structured output came out for from OpenAI. But ultimately it uses typing. It uses reusable modules to develop and code large language model abstractions that you understand instead of manual strings and you can iterate much more quickly with that structured code instead of again blocks of text that we've all probably have worked with at some point uh in your development. It's lightweight and futurep proof and by what I mean by that is that it's pure Python. You're not here waiting for an integration to come out for your particular framework. It's designed to accept really any Python library that you want to use. It's you don't have to wait till that library is compatible with other with whatever framework specific typing that comes out. Um it's lightweight. There's not a lot of different um dependencies which is probably music to some Python developers ears and it's very reproducible. Okay, so all that to say, let's look at some code and stop looking at slides, but it's really as simple as uh defining a class. So the first thing here, if you can see the laser, no. Okay, I'm going to try and point to it. But this first line right here is you actually setting the large language model itself, right? You don't have to change anything else if you want to use any other provider. you just change it to like bedrock forward slash whatever your model provider is and the and the rest of your uh DSPI code will work because in the back end it uses light LLM that uh handles all that uh conversion for you. Um so again if you don't want to use data bricks you can use open AI you can use anthropic you can use whatever and you you don't have to change or install any other packages you just need to change that line and you'll be able to use that new model. This right here this class is essentially what would be your prompt but you're defining a signature that's describing how you want the model to accomplish the task. So for this one, we're trying to classify emotions. And I'm expecting a specific input called sentence, which I want it to be a string. And the outputs that I want is a literal with these uh strings. So DSPI knows to take this and then force the input to be a string and the output to be one of these emotions. So and then if I need to run it, there's these last three. the actual input, the sentence itself. The um this is us assigning it to a pre-built module DSpi.predict. Uh DSpi.predict is one of their is one of the pre-built ones to actually just interact with the large language model. And then finally, you run classify sentence equal sentence. Um the output is I didn't put it up here, but the output will then come out to be your sadness, joy, love, whatever, whatever the case might be. And you can use the output like classify sentiment to access the value that that came out. So all this to say to help you better like visualize and understand how to develop something with the Spie, we're going to um think of this as a restaurant. It's like taking a recipe and then putting it giving it to your cooks to uh create a brand new dish. So you're trying to define your ingredients like you saw with that DSPI signature. You're trying to uh define what your inputs are and what the final dish is supposed to be, right? What's the final output? And then you got to define your goal so that the so that the uh class so that you know what the class is supposed to do. Try them out together, figure out what you want to do with it, and then and then discover uh your new dish. So you'll be using DSPI signatures as you saw a preview of in that first uh code snippet. uh that's your recipe on how you want the large language model to handle that use case. And then the module itself, there's pre-built one like I said, but you're you are also allowed or DSP lets you build your own DSPI module um which you'll see in my later session at 4M if you want to join that one. Um where it's a much more um detailed version on how to do that. But you can actually just create it by initializing and then having a predict method to put not only different modules together, but your own Python logic. You don't have to use only DSPI code. You can use mostly Python code and then call a DSp.predict to u complete everything, right? So you like do some ETL first or something like that. Um so these are just some example modules out there, but there's still new ones coming out. Uh the PI 3.0 has a few more coming out as well, but these are the main ones that uh you can use. And for those of you in the back who can't see, there's predict, chain of thought, react, and best of end. React is what you would use for agents. So if you are familiar with function calling, there's the reasoning and the action part. And that's kind of what that enables for your language models. Okay. So let's go through the step-by-step process that we talked about uh earlier. Your first step is to define the ingredients. So your ingredient is what model are you going to use? what are you going to use for this classific what do you want to use for this classific uhation task so like I said earlier you can define any multiple configurations from any provider and then you can also interchange them depending on the model module right if you have a uh very specific use ca uh sorry very specific signature that's only doing like text extraction maybe use llama 8 billion or if you have a like image classifier and you need to use a multimodal model then you should maybe configure your anthropic cloud 4 and then use that for that part and you can use that all in one uh DSPI program. Okay, so I kind of went over this step two is defining your goal and your end dish. So let's go back to this example. What were what were we trying to do? We're trying to classify a emotion within some kind of string string text, right? So we need to use the DSPI signature to actually declare the inputs and outputs of these of it so that we know what we're looking for and we know what the inputs are and what the outputs are. So you have to make sure any kind of typing that is in the typing library essentially you can use it could be a boolean it can be an integer it can be a float but just keep that in mind and if you want to use custom uh typing you can always use pideantic um base type also came out from NDS PI 3.0 know where if you're familiar with uh paidantic base model it's pretty much the exact same thing but you can create your own uh own like that and pass that in as a type so that way you can also do some data validation before it uh you put in an input or get an output so one thing that I haven't really mentioned is the iteration step of this right so we especially when developing generative AI applications you want to go through you want to iterate quickly you want to make sure what you had created and the model that you're using is performing up to the task, right? So you it can be it's really all you need to do is pass it into something as simple as this. And but if you don't if you feel like the predict is actually not working too great, but you want to use this large language model, maybe consider using chain of thought. Um if you don't know what chain of thought is, um please Google it. I don't have time to talk about that but you probably know what it is. Uh but chain of thought does help gives it some extra steps extra reasoning to help with that particular use case. So you can go through um some other modules very quickly to in order to uh accomplish that uh use case. Okay. So kind of covered all this and we'll keep iterating. So here's another examp so this here's another example where you maybe you've decided okay I don't actually don't like that large language models performance I'm actually going to switch it to openai's GPT 4.1 I'll just reconfigure it to that mo that model and I don't have to change anything else you see the class is pretty much the same thing um and you can just keep reiterating iterating it over and over again with minimal code change to make to really understand what's impacting your gen AI application the most So once you're satisfied, you can use datab bricks's mlflow integration, dspi.autolog to do mflow tracing. You can also do uh and then you can uh register it, log it, deploy it all on data bricks. Of course, that's going to be the pitch. But um you would use mlflow to um kind of track different versions of your agent. um make sure that you can do governance on it through Unity catalog and use the infrastructure data bricks provides on the model serving infrastructure to actually deploy it. So this is simple I don't have the actual deployment code in here but this is just to get it into uh Unity catalog. So altogether this is what the entire code looks like. Um so with the first step again let's just to kind of like walk it back. The first one is we define what model we want to use and then we define the signature itself right here to understand how we want the large language model to uh accomplish the task that we're defining. Then we do a quick test, maybe do another test. Maybe we want a stronger large language model or maybe you know if I think what there was like a new Quinn model that came out like a few days ago. Um you want to test that one just change this and then you can change it and then it'll work against this same signature. So and then when you really like it you can use it down way down below here. So that's really it. Now you know how to use DSPI. That's all you need to do in order to start using this framework. You don't need to know any other classes. You don't need to know any other uh functions. That's really the core of how you would use uh DSPI. And if you need if you want to see a more advanced or other features of DSPI, there's a 3.0 session tomorrow and then there's a my session, the multi- aent session at 4:00 later today. So, please join the community. Omar is very active. I've never seen a um a creator so active on his Discord, but he's literally replying to every single person. He's um so uh feel free to join him, join us on Discord, contribute to the repo, and this is the official documentation on the left right here. So that's it. Um we got six minutes left. So if there's any questions, I will have to come up to you and repeat your questions. So if you have any questions, raise your hand. But otherwise, that concludes the uh that concludes the session. Thanks all.

Original Description

Writing prompts for our GenAI applications is long, tedious, and unmaintainable. A proper software development lifecycle requires proper testing and maintenance, something incredibly difficult to do on a block of text. Our current prompt engineering best practices have largely been manual trial and error, testing which of our prompts work well in certain situations. This process worsens as our prompts become more complex, adding multiple tasks and functionality within one long singular prompt. Enter DSPy, your PROGRAMATIC way of building GenAI Applications. Learn how DSPy allows you to modularize your prompt into modules and enforce typing through signatures. Then, utilize state of the art algorithms to optimize the prompts and weights against your evaluation datasets, just like machine learning! We will compare DSPy to a restaurant to help illustrate and demo DSPy’s capabilities. It's time to start programming, rather than prompting, again! Talk By: Austin Choi, Delivery Solutions Architect, Databricks Databricks Named a Leader in the 2025 Gartner® Magic Quadrant™ for Data Science and Machine Learning Platforms: https://www.databricks.com/blog/databricks-named-leader-2025-gartner-magic-quadrant-data-science-and-machine-learning Build and deploy quality AI agent systems: https://www.databricks.com/product/artificial-intelligence See all the product announcements from Data + AI Summit: https://www.databricks.com/events/dataaisummit-2025-announcements Connect with us: Website: https://databricks.com Twitter: https://twitter.com/databricks LinkedIn: https://www.linkedin.com/company/databricks Instagram: https://www.instagram.com/databricksinc Facebook: https://www.facebook.com/databricksinc
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Databricks · Databricks · 0 of 60

← Previous Next →
1 Building AI Agent Systems with Databricks
Building AI Agent Systems with Databricks
Databricks
2 Databricks Workflows
Databricks Workflows
Databricks
3 Automate Unity Catalog Upgrade with UCX Part 1: Overview
Automate Unity Catalog Upgrade with UCX Part 1: Overview
Databricks
4 Automate Unity Catalog Upgrade with UCX Part 2: Installation
Automate Unity Catalog Upgrade with UCX Part 2: Installation
Databricks
5 Automate Unity Catalog Upgrade with UCX Part 3 - Assessment
Automate Unity Catalog Upgrade with UCX Part 3 - Assessment
Databricks
6 Automate Unity Catalog Upgrade with UCX  Part 4 - Group Migration
Automate Unity Catalog Upgrade with UCX Part 4 - Group Migration
Databricks
7 Table Migration and Catalog Design with UCX | Part 5
Table Migration and Catalog Design with UCX | Part 5
Databricks
8 Setting Up Azure Access for UCX Table Migration | Part 6
Setting Up Azure Access for UCX Table Migration | Part 6
Databricks
9 UCX Table Migration: Creating Catalogs and Schemas | Part 7
UCX Table Migration: Creating Catalogs and Schemas | Part 7
Databricks
10 Automate Unity Catalog Upgrade with UCX  Part 8: Code Migration
Automate Unity Catalog Upgrade with UCX Part 8: Code Migration
Databricks
11 Streaming to Kafka Just Got Easier with DLT Pipelines
Streaming to Kafka Just Got Easier with DLT Pipelines
Databricks
12 Data Engineering From Data to Dashboards with DABs: Crunching the Cookies Dataset
Data Engineering From Data to Dashboards with DABs: Crunching the Cookies Dataset
Databricks
13 Epsilon helps businesses connect with their consumers using Databricks Data Intelligence Platform
Epsilon helps businesses connect with their consumers using Databricks Data Intelligence Platform
Databricks
14 Unilever transforms operations with GenAI using the Databricks Data Intelligence Platform
Unilever transforms operations with GenAI using the Databricks Data Intelligence Platform
Databricks
15 ActionIQ enables businesses to unlock customer data with the Databricks Data Intelligence Platform
ActionIQ enables businesses to unlock customer data with the Databricks Data Intelligence Platform
Databricks
16 Mixed Attention & LLM Context | Data Brew | Episode 35
Mixed Attention & LLM Context | Data Brew | Episode 35
Databricks
17 Inside Databricks SQL: Engineering innovation with Hans
Inside Databricks SQL: Engineering innovation with Hans
Databricks
18 Inside Databricks: Engineering innovation with Michael Armbrust
Inside Databricks: Engineering innovation with Michael Armbrust
Databricks
19 The Money Team at Databricks: driving revenue and customer growth
The Money Team at Databricks: driving revenue and customer growth
Databricks
20 Unity Catalog unveiled: engineering data governance at scale
Unity Catalog unveiled: engineering data governance at scale
Databricks
21 Create a view in Databricks and share it with Power BI using Delta Sharing
Create a view in Databricks and share it with Power BI using Delta Sharing
Databricks
22 NDUS leverages Databricks Data Intelligence Platform to revolutionize higher education management
NDUS leverages Databricks Data Intelligence Platform to revolutionize higher education management
Databricks
23 Démo Databricks de AI/BI
Démo Databricks de AI/BI
Databricks
24 EMEA Data + AI World Tour 2024
EMEA Data + AI World Tour 2024
Databricks
25 GenAI: The Shift to Data Intelligence - Customer Panel on Industry Use Cases
GenAI: The Shift to Data Intelligence - Customer Panel on Industry Use Cases
Databricks
26 GenAI: The Shift to Data Intelligence - Ft. Ash Jhaveri, VP of Reality Labs Partnerships at Meta
GenAI: The Shift to Data Intelligence - Ft. Ash Jhaveri, VP of Reality Labs Partnerships at Meta
Databricks
27 Virtue Foundation leverages the Databricks Data Intelligence Platform to advance global health
Virtue Foundation leverages the Databricks Data Intelligence Platform to advance global health
Databricks
28 Announcing Synthetic Data Generation in Mosaic AI Agent Evaluation
Announcing Synthetic Data Generation in Mosaic AI Agent Evaluation
Databricks
29 AI/BI Dashboards Embedding - A tutorial
AI/BI Dashboards Embedding - A tutorial
Databricks
30 Bayer transforms global data management with the Databricks Data Intelligence Platform
Bayer transforms global data management with the Databricks Data Intelligence Platform
Databricks
31 Databricks at AWS re:Invent 2024
Databricks at AWS re:Invent 2024
Databricks
32 Hive Metastore and AWS Glue Federation in Unity Catalog
Hive Metastore and AWS Glue Federation in Unity Catalog
Databricks
33 Data + AI World Tour Paris 2024
Data + AI World Tour Paris 2024
Databricks
34 Retail reimagined: Currys data-first strategy to driving growth and improving operations
Retail reimagined: Currys data-first strategy to driving growth and improving operations
Databricks
35 Mixture of Memory Experts (MoME) | Data Brew | Episode 36
Mixture of Memory Experts (MoME) | Data Brew | Episode 36
Databricks
36 Verana Health Data Curation and Innovation with Databricks and AWS
Verana Health Data Curation and Innovation with Databricks and AWS
Databricks
37 Securing SaaS Applications: Obsidian Security on Their Journey with Databricks and AWS
Securing SaaS Applications: Obsidian Security on Their Journey with Databricks and AWS
Databricks
38 Twilio Eng VP on Data Intelligence & AI at AWS re:Invent 2024
Twilio Eng VP on Data Intelligence & AI at AWS re:Invent 2024
Databricks
39 Chegg Eng SVP on Data-Driven Approach to Student Success with Databricks and AWS
Chegg Eng SVP on Data-Driven Approach to Student Success with Databricks and AWS
Databricks
40 Ibotta Personalized Rewards Innovation with Databricks and AWS
Ibotta Personalized Rewards Innovation with Databricks and AWS
Databricks
41 Simplify AI governance with #databricks AI Gateway
Simplify AI governance with #databricks AI Gateway
Databricks
42 Databricks SQL and Power BI Integration
Databricks SQL and Power BI Integration
Databricks
43 Databricks Serverless SQL Warehouses
Databricks Serverless SQL Warehouses
Databricks
44 7 West powers audience growth with the Databricks Data Intelligence Platform
7 West powers audience growth with the Databricks Data Intelligence Platform
Databricks
45 Secret to Production AI: Tools & Infrastructure | Data Brew | Episode 37
Secret to Production AI: Tools & Infrastructure | Data Brew | Episode 37
Databricks
46 Skyflow CEO on Data Privacy with Databricks at AWS re:Invent
Skyflow CEO on Data Privacy with Databricks at AWS re:Invent
Databricks
47 Databricks Clean Rooms Product Demo
Databricks Clean Rooms Product Demo
Databricks
48 Dun & Bradstreet Enrichment & Monitoring, powered by Delta Sharing & Databricks Marketplace
Dun & Bradstreet Enrichment & Monitoring, powered by Delta Sharing & Databricks Marketplace
Databricks
49 Unpacking Libraries in Databricks
Unpacking Libraries in Databricks
Databricks
50 Providence uses an AI agent system from Databricks to help doctors improve their communication
Providence uses an AI agent system from Databricks to help doctors improve their communication
Databricks
51 How State Street Uses AI to Transform Millions of Trades Daily
How State Street Uses AI to Transform Millions of Trades Daily
Databricks
52 Vevo Therapeutics CEO on Curing Disease with Data at AWS re:Invent
Vevo Therapeutics CEO on Curing Disease with Data at AWS re:Invent
Databricks
53 Over Architected with Nick & Holly: Databricks updates for Feb 2025
Over Architected with Nick & Holly: Databricks updates for Feb 2025
Databricks
54 The Power of Synthetic Data | Data Brew | Episode 38
The Power of Synthetic Data | Data Brew | Episode 38
Databricks
55 Use Databricks Lakehouse Federation to break down data silos
Use Databricks Lakehouse Federation to break down data silos
Databricks
56 AI's rugby score: National Rugby League rallies fans with analytics and unified data
AI's rugby score: National Rugby League rallies fans with analytics and unified data
Databricks
57 Open Variant Data Type in Delta Lake and Apache Spark
Open Variant Data Type in Delta Lake and Apache Spark
Databricks
58 How would you sort Ætheldred in the alphabet using Databricks?
How would you sort Ætheldred in the alphabet using Databricks?
Databricks
59 A guide on how to operationalize the Databricks AI Security Framework (DASF)
A guide on how to operationalize the Databricks AI Security Framework (DASF)
Databricks
60 Future-Proof Your Asset Performance Management with Generative AI - Field Assistant Live Demo
Future-Proof Your Asset Performance Management with Generative AI - Field Assistant Live Demo
Databricks

The video teaches how to use DSPI to develop large language model abstractions and integrate them with GenAI applications, allowing for more efficient and maintainable prompt engineering. It covers the use of DSPI signatures, custom modules, and integration with tools like Databricks and OpenAI.

Key Takeaways
  1. Define a class to set the large language model and its provider
  2. Use DSPI signatures to describe how the model should accomplish a task
  3. Run the task using DSPI.predict
  4. Build custom DSPI modules by initializing and having a predict method
  5. Define ingredients including model and configuration
  6. Define goal and end dish with DSPI signature
  7. Use typing library for data validation
  8. Iterate quickly to ensure model performance
  9. Deploy models on Databricks' model serving infrastructure through Unity catalog
💡 DSPI provides a declarative framework for programming GenAI applications, allowing for more efficient and maintainable prompt engineering by using typing and reusable modules.

Related Reads

Up next
The 8 Levels of Claude Code (Beginner to Architect)
Preporato | AI for Engineers
Watch →