Created
June 25, 2025 02:10
-
-
Save brenorb/21a4df4875db84beb6c0ff084e5c6688 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
commented
Jul 1, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment