Skip to content

Instantly share code, notes, and snippets.

@joelgrus
Last active September 12, 2025 15:37
Show Gist options
  • Save joelgrus/eb144fda2d9b94429ba1ed1ca48e2861 to your computer and use it in GitHub Desktop.
Save joelgrus/eb144fda2d9b94429ba1ed1ca48e2861 to your computer and use it in GitHub Desktop.
toy example: qa with history running in a fastapi app
import dspy
import fastapi
import uuid
import pydantic
dspy.configure(lm=dspy.LM('gemini/gemini-2.5-flash-lite', max_tokens=20_000))
class QA(dspy.Signature):
question: str = dspy.InputField()
history: dspy.History = dspy.InputField()
answer: str = dspy.OutputField()
qa = dspy.Predict(QA)
app = fastapi.FastAPI()
# pretend this is a database
histories: dict[str, dspy.History] = {}
class QuestionRequest(pydantic.BaseModel):
question: str
conversation_id: str | None = None
@app.post("/ask")
def ask_question(request: QuestionRequest):
conversation_id = request.conversation_id or str(uuid.uuid4())
if conversation_id not in histories:
histories[conversation_id] = dspy.History(messages=[])
response = qa(question=request.question, history=histories[conversation_id])
histories[conversation_id].messages.append({"question": request.question, "answer": response.answer})
return {"answer": response.answer, "conversation_id": conversation_id}
curl -X POST \
-H "Content-Type: application/json" \
-d '{"question": "who was Elvis?"}' \
http://localhost:8000/ask
{"answer":"Elvis Presley was an American singer and actor. Nicknamed the \"King of Rock and Roll\", or simply \"the King\", he is regarded as one of the most significant cultural icons of the 20th century and is often referred to as the \"King of Rock and Roll\". His energetic performances and distinctive voice helped popularize rock and roll music, making him one of the most celebrated musicians of all time.","conversation_id":"f4dd00c6-dd1c-428e-9bc0-f8be0ae54b4e"}
curl -X POST \
-H "Content-Type: application/json" \
-d '{"question": "who was Elvis Costello?"}' \
http://localhost:8000/ask
{"answer":"Elvis Costello is an English singer-songwriter who rose to prominence in the late 1970s as part of the new wave and punk rock movements. He is known for his distinctive vocal style, witty and often acerbic lyrics, and his ability to blend various musical genres, including rock, pop, soul, and country. Throughout his career, he has released numerous critically acclaimed albums and singles, and has been recognized with multiple awards, including Grammy Awards.","conversation_id":"d0c40287-30bb-488d-abbd-ce4fde1608ab"}
curl -X POST \
-H "Content-Type: application/json" \
-d '{"question": "where was he born?", "conversation_id": "f4dd00c6-dd1c-428e-9bc0-f8be0ae54b4e"}' \
http://localhost:8000/ask
{"answer":"Elvis Presley was born in Tupelo, Mississippi.", "conversation_id":"f4dd00c6-dd1c-428e-9bc0-f8be0ae54b4e"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment