Skip to content

Instantly share code, notes, and snippets.

@brenorb
Created June 25, 2025 02:10
Show Gist options
  • Save brenorb/21a4df4875db84beb6c0ff084e5c6688 to your computer and use it in GitHub Desktop.
Save brenorb/21a4df4875db84beb6c0ff084e5c6688 to your computer and use it in GitHub Desktop.
import os
import dspy
from dotenv import load_dotenv
load_dotenv()
# Configure OpenAI API key
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
lm = dspy.LM(model="openai/gpt-4o-mini", model_type="chat")
dspy.configure(lm=lm, adapter=dspy.ChatAdapter())
class QASignature(dspy.Signature):
"""Answer questions using conversation history for context."""
question: str = dspy.InputField(desc="The current question from the user")
history: dspy.History = dspy.InputField(desc="Previous conversation messages")
answer: str = dspy.OutputField(desc="Answer that takes conversation history into account")
qa = dspy.Predict(QASignature)
# Initialize History with empty messages list
chat_history = dspy.History(messages=[])
while True:
question = input("Ask a question (q to quit): ")
if question.lower() == "q":
print("Goodbye!")
break
print(f"User: {question}")
result = qa(question=question, history=chat_history)
print(f"Assistant: {result.answer}")
# Add messages to history AFTER getting the response
chat_history.messages.append({"role": "user", "content": question})
chat_history.messages.append({"role": "assistant", "content": result.answer})
# Debug: print current history length
# print(f"[History has {len(chat_history.messages)} messages]")
@OmGuptaIND
Copy link

lm = dspy.LM(model="gemini/gemini-2.0-flash", api_key=settings.gemini_api_key)
dspy.configure(lm=lm)

class QASignature(dspy.Signature):
    """Answer questions using conversation history for context."""
    question: str = dspy.InputField(desc="The current question from the user")
    history: dspy.History = dspy.InputField(desc="Previous conversation messages")

    answer: str = dspy.OutputField(desc="Answer that takes conversation history into account")

qa = dspy.Predict(QASignature)

# Initialize History with empty messages list
chat_history = dspy.History(messages=[])

while True:
    question = input("Ask a question (q to quit): ")
    if question.lower() == "q":
        print("Goodbye!")
        break
    
    print(f"User: {question}")

    result = qa(question=question, history=chat_history)
    print(f"Assistant: {result.answer}")
    
    chat_history.messages.append({"questions": f"User: {question}", "answer": f"Assistant: {result.answer}"})`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment