I’m working with the OpenAI .NET SDK and trying to implement conversation memory using the Assistant API. The goal is to have the assistant remember what we talked about in previous messages.
My approach: When a user logs in, I create an assistant and thread, then save the thread ID in the session. For follow-up questions, I pull the thread ID from session storage and use it for the next API call.
Current implementation
ExecutionSettings executionSettings = new ExecutionSettings()
{
MessageLimit = RunTruncationStrategy.CreateLastMessagesStrategy(10)
};
if (maintainChatHistory)
{
if (_sessionAccessor.HttpContext.Session.GetString("ChatThreadId") != null)
{
currentThreadId = _sessionAccessor.HttpContext.Session.GetString("ChatThreadId");
var existingThread = chatClient.GetThread(currentThreadId);
responseStream = chatClient.CreateRunStreaming(existingThread.Value.Id, chatAssistant.Id, executionSettings);
}
else
{
result = "";
var newThread = chatClient.CreateThread(threadConfiguration);
responseStream = chatClient.CreateRunStreaming(newThread.Value.Id, chatAssistant.Id, executionSettings);
_sessionAccessor.HttpContext.Session.SetString("ChatThreadId", newThread.Value.Id);
currentThreadId = newThread.Value.Id;
}
}
else
{
result = "";
var freshThread = chatClient.CreateThread(threadConfiguration);
responseStream = chatClient.CreateRunStreaming(freshThread.Value.Id, chatAssistant.Id, executionSettings);
currentThreadId = freshThread.Value.Id;
}
The problem is that the assistant keeps acting like each message is the first one and asks “how can I help you” instead of continuing our conversation. What am I missing here?