Skip to content

Instantly share code, notes, and snippets.

@thomasahle
Last active October 18, 2024 19:26
Show Gist options
  • Save thomasahle/4f650afe7305601fc3e417dda7aecb3c to your computer and use it in GitHub Desktop.
Save thomasahle/4f650afe7305601fc3e417dda7aecb3c to your computer and use it in GitHub Desktop.
import dspy
from pydantic import BaseModel
from typing import List
class State(BaseModel):
name: str
abbreviation: str
capital: str
class States(BaseModel):
states: List[State]
class ExampleSignature(dspy.Signature):
question = dspy.InputField()
states = dspy.OutputField(desc=f"The answer written as a single JSON object using the schema {States.schema()}")
def main():
lm = dspy.OpenAI(model='gpt-3.5-turbo-instruct', max_tokens=400)
dspy.settings.configure(lm=lm, trace=["Test"])
program = dspy.Predict(ExampleSignature)
res = program(question="What are the three largest states in the US?")
try:
states = States.parse_raw(res.states)
except ValueError as e:
dspy.Assert(False, e)
print(states.states)
lm.inspect_history(n=3)
if __name__ == '__main__':
main()
#[
# State(name='Alaska', abbreviation='AK', capital='Juneau'),
# State(name='Texas', abbreviation='TX', capital='Austin'),
# State(name='California', abbreviation='CA', capital='Sacramento')
#]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment