-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Allow specifying chat history for LMs #1435
Comments
Update: I quickly realised that it's more complicated than I thought. After some investigation, I managed to make it work for my case. Please see the MWE: Not sure I did it in an ideal way, but the solution looked quite hacky to me. Any suggestions on a better solution are much appreciated! |
Hey @ShaojieJiang ! What does specifying chat history mean? Do you mean passing multiple user/assistant turns to chat LMs? |
Hi @okhat, I have a similar scenario. What would be the best way to emulate these multiple user/assistant turns within input so that the end user can ask follow-up questions in a conversational flow? One obvious way is to convert |
Hi @okhat , apologies for the late response. I've found a solution for TextGrad and didn't pay much attention to the GitHub notifications. Basically, I want to be able to specify the chat history in their original format ( Here you can find a MWE of my solution for TextGrad. I tried the same for DSPy, but it's much more hacky as you might have seen from my attachments above. |
I am in a similar boat where I want to be able to do something like this: messages=[]
resp=dspy.Predict(MyQA)("What is the capital of France...")
messages.append(resp)
resp=dspy.Predict(MyQA)("What is the native language name for the country", messages)
messages.append(resp)
resp=dspy.Predict(MyQA)("What is the main district of the city", messages)
.... Being able to simulate a conversation would be useful. Is this currently possible with the current dspy implementation? |
You can achieve properly passing the chat history by overriding the default Here is an example: import dspy
class ChatModule(dspy.Module):
def __init__(self):
super().__init__()
self.predict = dspy.Predict("question -> answer")
self.conversation_history = []
def forward(self, question: str):
prediction = self.predict(question=question, history_messages=self.conversation_history)
self.conversation_history.append(
{
"inputs": dict(question=question),
"outputs": prediction
}
)
return prediction
class MultiTurnChatAdapter(dspy.ChatAdapter):
def format(self, signature, demos, inputs) -> list[dict[str, Any]]:
history_messages = inputs.pop("history_messages", [])
inputs_ = super().format(signature, demos, inputs) # system prompt, demos, user prompt
formatted_history = []
for turn in history_messages:
formatted_history.append(format_turn(signature, turn["inputs"], role="user"))
formatted_history.append(format_turn(signature, turn["outputs"], role="assistant"))
return inputs_[:-1] + formatted_history + [inputs_[-1]] # concat system and demos with past turns and finally the user prompt usage: # assuming you already have a LM configured
dspy.settings.configure(adapter=MultiTurnChatAdapter())
chat_module = ChatModule()
chat_module("My name is Itay")
chat_module("What is my name?") Prediction(
answer='Your name is Itay.'
) |
Is there no plan for implementing multi-turn conversation natively in DSPy? For any kind of chatbot, being able to pass chat history is necessary. Of course you can pass it in as an input field, but ultimately it's not representing that chat history as the model's native syntax, so it seems like it almost certainly wouldn't be as performant simply embedding it into a single user message like DSPy currently does. I could be wrong about my assumptions and maybe LLMs handle this equally well, but chat models are trained to handle chat history this way, so by doing it the way it's currently implemented (sys message, entire chat history embedded in a single user message) is taking it more out of distribution. If my assumptions are true (def worth testing first), I think it makes a lot of sense for multi-turn conversation to be a first-class citizen of DSPy. |
I think you can do sth like this
Some usages of it
|
Hi DSPy developers,
First of all, thanks a lot for this great work!
Recently I've been trying to integrate DSPy into my work, but I stumbled upon the chat history specification. My task is to design a professional interviewer chatbot, therefore, the optimisation target for me is to optimise the conversation flow in diverse cases (such as with both cooperative and uncooperative interviewees). This is different from the existing DSPy modules which mostly focus on answer generation.
Although my task is also roughly answer generation, being able to specify the chat history is important. So I'm wondering is it already planned to add support for history specification? If not, do you think this aligns well with your development agenda?
For the moment I'll add the support in my inherited LM class. Looking forward to your response!
Best regards,
Shaojie Jiang
The text was updated successfully, but these errors were encountered: