You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please reference code below. I'm trying to figure out how to only receive the response to the last request only within a given thread. As the thread get's longer, I am seeing longer delays with the amount of time it takes the "assistantClient.CreateRunAsync" to run, because it responds with not only the most recent request and response, but also all previous requests and responses in that same thread. Is there a way of only receiving the content from the last request to prevent that ever increasing delay?
` public static async Task GetAssistantReplyNew(string chatMessage, AssistantSetting assistantSetting)
{
// Initialize the OpenAI + Assistant clients
#pragma warning disable OPENAI001
var openAi = new OpenAIClient(apiKey);
var assistantClient = openAi.GetAssistantClient();
Assistant assistant;
ThreadRun threadRun;
if (assistantSetting.UseExistingThread
&& !string.IsNullOrEmpty(assistantSetting.AssistantId)
&& !string.IsNullOrEmpty(assistantSetting.ThreadId))
{
// 1) Reuse: retrieve the assistant (so we can use its ID)
assistant = await assistantClient.GetAssistantAsync(assistantSetting.AssistantId);
// 2) Append the new user message to the existing thread
await assistantClient.CreateMessageAsync(
assistantSetting.ThreadId,
MessageRole.User,
new[] { MessageContent.FromText(chatMessage) }
);
// (This call simply adds your text to the thread) :contentReference[oaicite:0]{index=0}
// 3) Kick off a new run on that same thread
var runOptions = new RunCreationOptions();
threadRun = await assistantClient.CreateRunAsync(assistantSetting.ThreadId,
assistantSetting.AssistantId,
runOptions
);
// (This tells the assistant to process *all* messages on that thread, including the one you just added) :contentReference[oaicite:1]{index=1}
}
else
{
// 1) Create a brand‑new assistant
var assistantOpts = new AssistantCreationOptions
{
Name = assistantSetting.AssistantName,
Instructions = assistantSetting.AssistantInstructions,
};
assistant = await assistantClient.CreateAssistantAsync("gpt-4o", assistantOpts);
// 2) Create a brand‑new thread *and* run it immediately
var threadOpts = new ThreadCreationOptions();
threadOpts.InitialMessages.Add(chatMessage);
threadRun = await assistantClient.CreateThreadAndRunAsync(
assistant.Id,
threadOpts
);
}
// Poll until complete
while (!threadRun.Status.IsTerminal)
{
await Task.Delay(TimeSpan.FromSeconds(1));
threadRun = assistantClient.GetRun(threadRun.ThreadId, threadRun.Id);
}
var opts = new MessageCollectionOptions
{
Order = MessageCollectionOrder.Descending,
PageSizeLimit = 1
};
// Stream all messages back in order
AsyncCollectionResult<ThreadMessage> messages = assistantClient.GetMessagesAsync(
threadRun.ThreadId, opts);
var messageList = new List<ThreadMessage>();
var enumerator = messages.GetAsyncEnumerator();
try
{
//get all of the responses and put them in a list we can easily work with
while (await enumerator.MoveNextAsync())
{
messageList.Add(enumerator.Current);
}
}
finally
{
var disposable = enumerator as IAsyncDisposable;
if (disposable != null)
{
await disposable.DisposeAsync();
}
}
// Finally, we'll print out the returned messages
foreach (ThreadMessage message in messageList)
{
Debug.Write($"[{message.Role.ToString().ToUpper()}]: ");
foreach (MessageContent contentItem in message.Content)
{
if (!string.IsNullOrEmpty(contentItem.Text))
{
Debug.WriteLine($"{contentItem.Text}");
if (contentItem.TextAnnotations.Count > 0)
{
Debug.WriteLine("");
}
}
}
Debug.WriteLine("");
}
// Clean up
if (assistantSetting.DeleteThread)
{
await assistantClient.DeleteThreadAsync(threadRun.ThreadId);
}
if (assistantSetting.DeleteAssistant)
{
await assistantClient.DeleteAssistantAsync(assistant.Id);
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Please reference code below. I'm trying to figure out how to only receive the response to the last request only within a given thread. As the thread get's longer, I am seeing longer delays with the amount of time it takes the "assistantClient.CreateRunAsync" to run, because it responds with not only the most recent request and response, but also all previous requests and responses in that same thread. Is there a way of only receiving the content from the last request to prevent that ever increasing delay?
` public static async Task GetAssistantReplyNew(string chatMessage, AssistantSetting assistantSetting)
{
// Initialize the OpenAI + Assistant clients
#pragma warning disable OPENAI001
var openAi = new OpenAIClient(apiKey);
var assistantClient = openAi.GetAssistantClient();
`
Beta Was this translation helpful? Give feedback.
All reactions