Adding new messages to assistant threads

Hello folks

I’m new to working with all things openAI APIs (so apologies, I’m a newb here). Most of the other forum posts that are similar to this questions are out of date or don’t provide enough insight for me to get traction.

I’m simply trying to allow an assistant to have a conversation using the C# sdk. I can get the initial set up working with the first message but all my attempts to add to the thread just end up with the assistant going back to whatever initial message was passed in. It seems like this should be easier to do. Here’s what I’ve got for the code, just no clue how to add a message to the thread for the next run in the else statement below. Any help is greatly appreciated.

ThreadRun run;
if (string.IsNullOrEmpty(_threadId))
{
var threadOptions = new ThreadCreationOptions
{
InitialMessages =
{
new ThreadInitializationMessage(MessageRole.User, new { MessageContent.FromText(userMessage) })
}
};
run = _assistantClient.CreateThreadAndRun(assistantId, threadOptions);

 _threadId = run.ThreadId; // Store the thread ID for later use

}
else
{
//???
}

The documentation related to using assistants with the official c# sdk are terrible. I’ve tried throwing what is available at chatgpt and it can’t make it work. I came across this post searching Google for documentation for OpenAI.Assistants.MessageContent. I’m trying to do nearly the same as you to let the assistant handle context without reinjecting previous messages using ChatCompletion, which is better documented.

I ended up ditching the sdk and using the APIs for the submission and getting the completion but kept the thread run piece (gross I know) and it started working. I’m not 100% on what was wrong with the SDK but I noticed as soon as I updated the completion checks to the api method it started keeping track of the thread properly.

It seems we need to create a new run for this thread and assistant with the next message. It will look like that:

var threadRun = _assistantClient.CreateRun(_threadId, _assistantId,
                        new RunCreationOptions {
                                    AdditionalMessages = { "Some next message" }
                        });
do
{
    Thread.Sleep(TimeSpan.FromSeconds(1));
    threadRun = _assistantClient.GetRun(threadRun.ThreadId, threadRun.Id);
} while (!threadRun.Status.IsTerminal);
....
1 Like