according to the following: ""DSPy is a framework for algorithmically optimizing LM prompts and weights, especially when LMs are used one or more times within a pipeline. To use LMs to build a complex system without DSPy, you generally have to: (1) break the problem down into steps, (2) prompt your LM well until each step works well in isolation, (3) tweak the steps to work well together, (4) generate synthetic examples to tune each step, and (5) use these examples to finetune smaller LMs to cut costs. Currently, this is hard and messy: every time you change your pipeline, your LM, or your data, all prompts (or finetuning steps) may need to change.

To make this more systematic and much more powerful, DSPy does two things. First, it separates the flow of your program (modules) from the parameters (LM prompts and weights) of each step. Second, DSPy introduces new optimizers, which are LM-driven algorithms that can tune the prompts and/or the weights of your LM calls, given a metric you want to maximize.

DSPy can routinely teach powerful models like GPT-3.5 or GPT-4 and local models like T5-base or Llama2-13b to be much more reliable at tasks, i.e. having higher quality and/or avoiding specific failure patterns. DSPy optimizers will "compile" the same program into different instructions, few-shot prompts, and/or weight updates (finetunes) for each LM. This is a new paradigm in which LMs and their prompts fade into the background as optimizable pieces of a larger system that can learn from data. tldr; less prompting, higher scores, and a more systematic approach to solving hard tasks with LMs.""

question: is dspy the same as this: ""Loading...
Skip to main content
Metaprompt [MAKE A COPY].ipynb
Metaprompt [MAKE A COPY].ipynb_Notebook unstarred
Changes will not be saved
Using This Notebook
The notebook is designed to be maximally easy to use. You don't have to write any code. Just follow these steps:

Make a copy by clicking File -> Save a copy in Drive
Enter your Anthropic API key in between quotation marks where it says "Put your API key here!"
Enter your task where it says "Replace with your task!"
Optionally, enter an all-caps list of variables in quotes separated by commas where it says "specify the input variables you want Claude to use".
Then, you can simply click "Runtime -> Run all" and your prompt will be displayed at the bottom of the notebook.

To run individual cells in Google Colab, click on them and then press Shift + Enter at the same time.

Do this first: install anthropic.
[ ]
  # @title Do this first: install anthropic.
%%capture
!pip install anthropic
[ ]
import anthropic, re
ANTHROPIC_API_KEY = "" # Put your API key here!
MODEL_NAME = "claude-3-opus-20240229"
CLIENT = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
Welcome to the Metaprompt! This is a prompt engineering tool designed to solve the "blank page problem" and give you a starting point for iteration. All you need to do is enter your task, and optionally the names of the variables you'd like Claude to use in the template. Then you'll be able to run the prompt that comes out on any examples you like.

Caveats

This is designed for single-turn question/response prompts, not multiturn.
The Metaprompt is designed for use with Claude 3 Opus. Generating prompts with other models may lead to worse results.
The prompt you'll get at the end is not guaranteed to be optimal by any means, so don't be afraid to change it!
Table of Contents
The Metaprompt
Quickstart - Enter a task, get a prompt template
Testing your prompt template
0. The Metaprompt
The Metaprompt is a long multi-shot prompt filled with half a dozen examples of good prompts for solving various tasks. These examples help Claude to write a good prompt for your task. The full text is below (warning: it's long!)

Metaprompt Text
[ ]
1. Quickstart
Enter your task in the cell below. Here are some examples for inspiration:

Choose an item from a menu for me given user preferences
Rate a resume according to a rubric
Explain a complex scientific concept in simple terms
Draft an email responding to a customer complaint
Design a marketing strategy for launching a new product
There are two examples of tasks + optional variables below.

[ ]
TASK = "Draft an email responding to a customer complaint" # Replace with your task!
# Optional: specify the input variables you want Claude to use. If you want Claude to choose, you can set `variables` to an empty list!
VARIABLES = []
# VARIABLES = ["CUSTOMER_COMPLAINT", "COMPANY_NAME"]
# If you want Claude to choose the variables, just leave VARIABLES as an empty list.

# TASK = "Choose an item from a menu for me given my preferences"
# VARIABLES = []
# VARIABLES = ["MENU", "PREFERENCES"]
[ ]
variable_string = ""
for variable in VARIABLES:
    variable_string += "\n{" + variable.upper() + "}"
print(variable_string)

Next, we'll insert your task into the metaprompt and see what Claude gives us! Expect this to take 20-30 seconds because the Metaprompt is so long.

[ ]
prompt = metaprompt.replace("{{TASK}}", TASK)
assistant_partial = "<Inputs>"
if variable_string:
    assistant_partial += variable_string + "\n</Inputs><Instructions Structure>"

message = CLIENT.messages.create(
    model=MODEL_NAME,
    max_tokens=4096,
    messages=[
        {
            "role": "user",
            "content":  prompt
        },
        {
            "role": "assistant",
            "content": assistant_partial
        }
    ],
    temperature=0
).content[0].text
If you want to see the full text returned by the Metaprompt to see how it planned things out, uncomment out the "pretty_print(message)" line below.

[ ]
def pretty_print(message):
    print('\n\n'.join('\n'.join(line.strip() for line in re.findall(r'.{1,100}(?:\s+|$)', paragraph.strip('\n'))) for paragraph in re.split(r'\n\n+', message)))
# pretty_print(message)
Now, we'll extract the prompt itself and the variables needed, while also removing empty tags at the end of the prompt template.

[ ]
def extract_between_tags(tag: str, string: str, strip: bool = False) -> list[str]:
    ext_list = re.findall(f"<{tag}>(.+?)</{tag}>", string, re.DOTALL)
    if strip:
        ext_list = [e.strip() for e in ext_list]
    return ext_list

def remove_empty_tags(text):
    return re.sub(r'<(\w+)></\1>$', '', text)

def extract_prompt(metaprompt_response):
    between_tags = extract_between_tags("Instructions", metaprompt_response)[0]
    return remove_empty_tags(remove_empty_tags(between_tags).strip()).strip()

def extract_variables(prompt):
    pattern = r'{([^}]+)}'
    variables = re.findall(pattern, prompt)
    return set(variables)
Below: the variables Claude chose (if you didn't provide any; if you did, these should just be the same ones you provided), and the prompt it wrote.

[ ]
extracted_prompt_template = extract_prompt(message)
variables = extract_variables(message)

print("Variables:\n\n" + str(variables))
print("\n************************\n")
print("Prompt:")
pretty_print(extracted_prompt_template)
2. Testing your prompt template
If you like your prompt, try it out! The cell will prompt you to add values for each variable. Then, it will be sent to Claude and you'll see Claude's final output.

[ ]
variable_values = {}
for variable in variables:
    print("Enter value for variable:", variable)
    variable_values[variable] = input()

prompt_with_variables = extracted_prompt_template
for variable in variable_values:
    prompt_with_variables = prompt_with_variables.replace("{" + variable + "}", variable_values[variable])

message = CLIENT.messages.create(
    model=MODEL_NAME,
    max_tokens=4096,
    messages=[
        {
            "role": "user",
            "content":  prompt_with_variables
        },
    ],
).content[0].text

print("Claude's output on your prompt:\n\n")
pretty_print(message)
[ ]""