Hi everyone,
I’m working on a bot using OpenAI’s Assistant API within the Bot Framework SDK. I’ve integrated it to handle conversations, but I’m struggling with adding chat history. I’m using Cosmos DB to store previous conversations. When a user re-engages, I want to load the previous chat into the assistant thread.
Here’s a simplified part of my code:
// Fetch previous messages
var recentMessages = await _cosmosDbService.GetFormattedRecentMessagesAsync(conversationData.ThreadID);
var messages = new List<ThreadInitializationMessage>();
foreach (var msg in recentMessages)
{
if (msg.who == "User")
{
messages.Add(new ThreadInitializationMessage(msg.Message, MessageRole.User));
}
else
{
messages.Add(new ThreadInitializationMessage(msg.Message, MessageRole.Assistant));
}
}
// Initialize thread with messages
var threadOptions = new ThreadCreationOptions(messages);
var run = await client.CreateThreadAndRunAsync(assistantId, threadOptions);
However, I’m running into issues with message types and correctly loading chat history. Any suggestions on improving this?
Thanks!