Skip to content

Instantly share code, notes, and snippets.

@jrknox1977
Last active April 14, 2024 08:43
Show Gist options
  • Save jrknox1977/607e8c164fcd5385bf62877413e40d10 to your computer and use it in GitHub Desktop.
Save jrknox1977/607e8c164fcd5385bf62877413e40d10 to your computer and use it in GitHub Desktop.
Simple DSPY example of BasicQA
# 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 system was final fantasy 1 made for?",
"answer": "NES",
}
# 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.
generate_answer = dspy.Predict(BasicQA)
# Call the predictor on a particular input.
pred = generate_answer(question=my_example['question'])
# Print the answer...profit :)
print(pred.answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment