Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save karthikscale3/45800fc88123566ea11c8e079ac5799f to your computer and use it in GitHub Desktop.
Save karthikscale3/45800fc88123566ea11c8e079ac5799f to your computer and use it in GitHub Desktop.
DSPy Chain of Thought - Query transformation
import dspy
from dspy.teleprompt import BootstrapFewShot
class CoT(dspy.Module):
def __init__(self):
super().__init__()
self.prog = dspy.ChainOfThought("question -> answer")
def forward(self, question):
return self.prog(question=question)
def run_cot():
# Set up the LM
turbo = dspy.OpenAI(model='gpt-3.5-turbo', max_tokens=250)
dspy.settings.configure(lm=turbo)
trainset = [
dspy.Example({
"question": "How did revenue change between Q4 2023 and year before that?",
"gold_reasoning": "Revenue is a financial metric hence metric is revenue. The question is asking about the change in revenue between Q4 2023 and the year before that hence the years are 2023 and 2022 and the quarters are 4 and 4. Hence the answer is {'years': [2023, 2022], 'quarters': [4, 4]}",
"answer": "{'years': [2023, 2022], 'quarters': [4, 4]}"
}),
dspy.Example({
"question": "What was the gross profit in Q3 2023 and Q4 2023?",
"gold_reasoning": "Gross profit is a financial metric hence metric is gross profit. The question is asking about the gross profit in Q3 2023 and Q4 2023 hence the year is 2023 and the quarters are 3 and 4. Hence the answer is {'years': [2023], 'quarters': [3, 4]}",
"answer": "{'years': [2023], 'quarters': [3, 4]}"
})
]
devset = [
dspy.Example({
"question": "How did revenue change between Q4 2023 and year before that?",
"gold_reasoning": "Revenue is a financial metric hence metric is revenue. The question is asking about the change in revenue between Q4 2023 and the year before that hence the years are 2023 and 2022 and the quarters are 4 and 4. Hence the answer is {'years': [2023, 2022], 'quarters': [4, 4]}",
"answer": "{'years': [2023, 2022], 'quarters': [4, 4]}"
}),
dspy.Example({
"question": "What was the gross profit in Q3 2023 and Q4 2023?",
"gold_reasoning": "Gross profit is a financial metric hence metric is gross profit. The question is asking about the gross profit in Q3 2023 and Q4 2023 hence the year is 2023 and the quarters are 3 and 4. Hence the answer is {'years': [2023], 'quarters': [3, 4]}",
"answer": "{'years': [2023], 'quarters': [3, 4]}"
})
]
# Set up the optimizer: we want to "bootstrap" (i.e., self-generate) 2-shot examples of our CoT program.
config = dict(max_bootstrapped_demos=2, max_labeled_demos=2)
# Optimize! Use the `gsm8k_metric` here. In general, the metric is going to tell the optimizer how well it's doing.
teleprompter = BootstrapFewShot(**config)
cot = CoT()
optimized_cot = teleprompter.compile(
cot, trainset=trainset, valset=devset)
answer = optimized_cot(
question='How did revenue change between Q2 2019 and year after that?')
answer = optimized_cot(
question='What were the stock price changes between Q3 2018 and 7 quarters before that across 3 years?')
print(answer)
turbo.inspect_history(n=1)
run_cot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment