Skip to content

Instantly share code, notes, and snippets.

@jrknox1977
Created February 5, 2024 18:56
Show Gist options
  • Save jrknox1977/847c869fe9ee3b0723a9007427c38ef6 to your computer and use it in GitHub Desktop.
Save jrknox1977/847c869fe9ee3b0723a9007427c38ef6 to your computer and use it in GitHub Desktop.
DSPy example with chain of thought.
# install DSPy: pip install dspy
import dspy
# This sets up the language model for DSPy in this case we are using GPT-3.5-turbo
turbo = dspy.OpenAI(model='gpt-3.5-turbo')
# This sets the language model for DSPy. This must be set or you get an error that is not helpful:
# --> temperature = lm.kwargs['temperature'] if temperature is None else temperature
# --> AttributeError: 'NoneType' object has no attribute 'kwargs'
dspy.settings.configure(lm=turbo)
# This is not required but it helps to understand what is happening
my_example = {
"question": "What was the first commercially successful video game released by Atari in 1972?",
"answer": "Pong",
}
# This is the signature for the predictor. It is a simple question and answer model.
class BasicQA(dspy.Signature):
"""Answer questions with short factoid answers."""
question = dspy.InputField()
answer = dspy.OutputField(desc="often between 1 and 5 words")
# Define the predictor. This time it is Chain of Thought.
generate_answer = dspy.ChainOfThought(BasicQA)
# Call the predictor on a particular input.
pred = generate_answer(question=my_example['question'])
# Print the whole answer to see the 'Thought'...profit :)
print(pred)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment