Skip to content

Instantly share code, notes, and snippets.

@adrianlzt
Created June 24, 2025 10:02
Show Gist options
  • Save adrianlzt/5623709cd64c3a2e2b2d2ee2688ced8e to your computer and use it in GitHub Desktop.
Save adrianlzt/5623709cd64c3a2e2b2d2ee2688ced8e to your computer and use it in GitHub Desktop.

DSPy LabeledFewShot Optimizer Example

This repository contains a Python script, dspy_LabeledFewShot.py, that demonstrates how to use the dspy.teleprompt.LabeledFewShot optimizer for sentiment analysis.

The script defines a simple DSPy program to classify the sentiment of a sentence as "Positive", "Negative", or "Neutral". It then uses LabeledFewShot to "compile" this program by creating few-shot prompts from a small training set. Finally, it runs the compiled program on a new sentence and inspects the prompt sent to the Language Model (LLM) to show how the few-shot examples were included.

Requirements

  • Python 3.13+
  • uv

The script uses uv to manage and run a virtual environment with the required dependencies.

Setup

  1. Set Environment Variables:

    You need to provide your OpenAI API key. The script will read it from the OPENAI_API_KEY environment variable.

    export OPENAI_API_KEY="your-openai-api-key"

    If you are using a custom OpenAI-compatible API endpoint (like a local LLM server or a proxy), you can specify its URL via the OPENAI_API_BASE environment variable.

    export OPENAI_API_BASE="http://your-api-base-url"

Usage

To run the script, make it executable and run it directly:

chmod +x dspy_LabeledFewShot.py
./dspy_LabeledFewShot.py

The script will:

  1. Configure the gpt-4.1-nano model via the OpenAI API.
  2. Compile a sentiment classifier using 2 few-shot examples from the training data.
  3. Classify the sentiment of the sentence "This is a great day.".
  4. Print the predicted sentiment.
  5. Print the full prompt that was sent to the LLM, including the few-shot examples.

Example Output

The output will look something like this (the exact few-shot examples may vary but will be drawn from the training set):

Sentence: This is a great day.
Predicted Sentiment: Positive

--- LLM Call History (last call) ---
Classify sentiment of a sentence.

---

Follow the following format.

Sentence: a sentence for sentiment analysis
Sentiment: The sentiment of the sentence (e.g., Positive, Negative, Neutral).

---

Sentence: I'm not sure how I feel about this.
Sentiment: Neutral

---

Sentence: The movie was fantastic!
Sentiment: Positive

---

Sentence: This is a great day.
Sentiment:
#!/usr/bin/env -S uv run
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "dspy-ai[openai]",
# ]
# [tool.uv]
# exclude-newer = "2025-06-24T00:00:00Z"
# ///
import os
import dspy
from dspy.teleprompt import LabeledFewShot
# Configure the language model to use gpt-4.1-nano from OpenAI
# Make sure to set the OPENAI_API_KEY environment variable.
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
print("Error: OPENAI_API_KEY environment variable not set.")
exit(1)
# To use a custom API base, set the OPENAI_API_BASE environment variable.
api_base = os.getenv("OPENAI_API_BASE")
try:
# The model name 'openai/gpt-4.1-nano' is based on user request.
# DSPy uses LiteLLM to connect to various providers.
llm_kwargs = {'api_key': api_key}
if api_base:
llm_kwargs['api_base'] = api_base
llm = dspy.LM('openai/gpt-4.1-nano', **llm_kwargs)
dspy.settings.configure(lm=llm)
except Exception as e:
print(f"Error configuring language model: {e}")
exit(1)
class SentimentSignature(dspy.Signature):
"""Classify sentiment of a sentence."""
sentence = dspy.InputField(desc="a sentence for sentiment analysis")
sentiment = dspy.OutputField(desc="The sentiment of the sentence (e.g., Positive, Negative, Neutral).")
class SentimentClassifier(dspy.Module):
def __init__(self):
super().__init__()
self.classify = dspy.Predict(SentimentSignature)
def forward(self, sentence):
return self.classify(sentence=sentence)
# Create a small training set
train_data = [
("The movie was fantastic!", "Positive"),
("I really enjoyed the book.", "Positive"),
("The weather is terrible today.", "Negative"),
("I'm not sure how I feel about this.", "Neutral"),
]
trainset = [dspy.Example(sentence=s, sentiment=c).with_inputs("sentence") for s, c in train_data]
# Initialize our DSPy program
your_dspy_program = SentimentClassifier()
# Initialize the LabeledFewShot optimizer
# k=2 means it will use 2 examples from the trainset for the few-shot prompt
labeled_fewshot_optimizer = LabeledFewShot(k=2)
# Compile the program
# The `compile` method will add demonstrations (few-shot examples) to the predictor
your_dspy_program_compiled = labeled_fewshot_optimizer.compile(student=your_dspy_program, trainset=trainset)
# Run the compiled program on a new example
test_sentence = "This is a great day."
prediction = your_dspy_program_compiled(sentence=test_sentence)
print(f"Sentence: {test_sentence}")
print(f"Predicted Sentiment: {prediction.sentiment}")
# Inspect the last LLM call to see the few-shot prompt
print("\n--- LLM Call History (last call) ---")
dspy.inspect_history(n=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment