Automatic Prompt Generation using DSPy

Automatic Prompt Generation using DSPy

Introduction

In this post, I will show you a simple implementation of "automatic prompt generation" for solving math problems from the GSM8K dataset using the techniques used in MIPROv2 optimizer of DSPy. This program is made up of 3 modules:

  1. Module 1 generates demos for the prompt
  2. Module 2 generates an instruction for the prompt
  3. Module 3 uses the outputs of module 1 & 2 to generate the final prompt.

Module 1

This module takes a labeled training data set and generates 2 (NUM_SETS) sets of 10 demos each:

  • 5 demos are directly sampled from the dataset
  • 5 demos are generated using the model and satisfies the metric i.e. generated output = expected output

Module 2

This module takes the 2 sets of 10 demos generated in Step 1 along with a string representation of the application code i.e. the code of this program and generates 2(NUM_INSTRUCTIONS) different instructions by

  • Identifying the class of problems using the demos
  • Identifying the intent of user using the program semantics

Module 3

In this final step, it takes the outputs from the previous steps as inputs and generates two different final prompts (since we have 2 sets of 10 demos from step 1 and 2 instructions from step 2).

Conclusion

That's how you can generate prompt candidates using DSPy. Note that we started purely with a bunch of labeled datasets and nothing else. If you are curious to dive deep and understand more about this prompt optimization technique, check out the research paper here. If you would like to start using this optimizer, check out the dspy docs here.

Source Code

You can find the full source code for this example here.

Additional Notes

  1. Each one of the 3 modules are built using the ChainOfThought optimizer and Signature hints to guide the program to do what we want to do.
  2. You can use Langtrace to understand what goes in and out of the LLM and trace every step within each module deeply.
  3. The final prompts can be further optimized using a metric and you can technically generate 4 prompts with 2 demos and 2 instructions (2 x 2 permutation). These are left out for the sake of simplicity.
  4. Since module 2 uses the program code to identify the intent, re-structuring your code or adding comments can affect the outputs.

Langtrace x DSPy

Langtrace natively supports the tracing and monitoring of key metrics from DSPy optimizers and pipelines. This is helps you with understanding how a chosen module or an optimizer from DSPy works under the hood and gives you key visibility into better optimizing the performance of your application.

For more information, check out our previous blog post on this integration here. Here are some additional threads that people have found helpful:

Useful Resources

Hrijul Dey

AI Engineer| LLM Specialist| Python Developer|Tech Blogger

4 个月

Transforming local LLMs with DSPy! Boosting question answering efficiency, it's a game-changer for building intelligent React agents. Exploring Mistral NeMo and Ollama integrations now. https://www.artificialintelligenceupdate.com/learning-dspy-optimizing-question-answering-of-local-llms/riju/ #learnmore #AI&U

回复

要查看或添加评论,请登录

Karthik Kalyanaraman的更多文章

  • Evaluate CrewAI Agents using Langtrace

    Evaluate CrewAI Agents using Langtrace

    With the latest update, you can now evaluate both entire agent sessions and individual operations across your entire…

  • Attribute Extraction from Images using DSPy

    Attribute Extraction from Images using DSPy

    Introduction DSPy recently added support for VLMs in beta. A quick thread on attributes extraction from images using…

    1 条评论
  • OpenAI launches Evals, Tracing & Fine tuning

    OpenAI launches Evals, Tracing & Fine tuning

    Introduction ?? OpenAI makes an official entry into application specific Evals and LLM Ops tooling. OpenAI announced…

  • Building Compound AI systems

    Building Compound AI systems

    Introduction In this article, I will explain how I think about building and optimizing compound AI pipelines with…

    1 条评论
  • Why you need OpenTelemetry based Observability for your AI apps

    Why you need OpenTelemetry based Observability for your AI apps

    Introduction With the advent of LLMs, modern software development is going through an important shift - from mostly…

  • Evaluating the AI Oracle approach

    Evaluating the AI Oracle approach

    Recently, I came across this tweet about the AI Oracle approach for improving the accuracy and quality of responses for…

  • The year of Podcasts and Audio as UX vs the rest

    The year of Podcasts and Audio as UX vs the rest

    In 2019, I think the biggest threat to social media is going to be digital audio streaming (Podcasts and digital music…

    2 条评论
  • LinkedIn’s nifty new feature?—?Career Advice

    LinkedIn’s nifty new feature?—?Career Advice

    Recently, I was dabbling with LinkedIn’s features when I came across this new spot on my profile’s “Your Dashboard”…

    5 条评论

社区洞察

其他会员也浏览了