mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
ff3e13c2aa
* Add thread storage and serialization POC * Switch to using JsonElement and add unit tests * Add additional unit tests. * Exclude private debugger properties from CodeCoverage. * Rename IChatMessagesStorable to IChatMessageStore * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Improve xml doc. * Update the message storing thread to always use external store for both local and remote storage. * Remove threadid from the IChatMessageStore interface, since the store should own the thread id itself, if it requires one. * Switch GetMessages to IEnumerable * Address pr comments. * Make jsonserializer options default consistent on DeserializeThreadAsync * Move message storing thread functionality into AgentThread and simplify AgentThread behavior. * Remove embedding generation from VectorStore chat history sample. * Remove unecessary code and fix formatting. * Make GetNewThread and DeserializeThread virtual with default implementations. Remove unsued json utilities. * Fix formatting * Remove problem test. * Add more unit tests * Remove unused using clause. * Address pr feedback. * Address PR comments. * Make InMemory store internal * Switch InMemoryChatMessageStore to implement IList instead of inheriting from List. * Rename store deserialize param. * Update serialization based on PR comments. * Remove confusing comment. * Address Deserialization PR comments in the same way as Serialization * Add State to IChatMessageStore Serialize and Deserialize names. Make Thread Deserialize internal. Make AgentThread type switching fobidden. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
102 lines
3.3 KiB
C#
102 lines
3.3 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using AgentConformance.IntegrationTests;
|
|
using AgentConformance.IntegrationTests.Support;
|
|
using Microsoft.Extensions.AI;
|
|
using Microsoft.Extensions.AI.Agents;
|
|
using OpenAI;
|
|
using OpenAI.Assistants;
|
|
using Shared.IntegrationTests;
|
|
|
|
namespace OpenAIAssistant.IntegrationTests;
|
|
|
|
public class OpenAIAssistantFixture : IChatClientAgentFixture
|
|
{
|
|
private static readonly OpenAIConfiguration s_config = TestConfiguration.LoadSection<OpenAIConfiguration>();
|
|
|
|
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
|
|
private AssistantClient? _assistantClient;
|
|
private ChatClientAgent _agent;
|
|
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
|
|
|
|
public AIAgent Agent => this._agent;
|
|
|
|
public IChatClient ChatClient => this._agent.ChatClient;
|
|
|
|
public async Task<List<ChatMessage>> GetChatHistoryAsync(AgentThread thread)
|
|
{
|
|
List<ChatMessage> messages = [];
|
|
await foreach (var agentMessage in this._assistantClient!.GetMessagesAsync(thread.ConversationId, new() { Order = MessageCollectionOrder.Ascending }))
|
|
{
|
|
messages.Add(new()
|
|
{
|
|
Role = agentMessage.Role == MessageRole.User ? ChatRole.User : ChatRole.Assistant,
|
|
Contents =
|
|
[
|
|
new TextContent(agentMessage.Content[0].Text ?? string.Empty)
|
|
],
|
|
});
|
|
}
|
|
|
|
return messages;
|
|
}
|
|
|
|
public async Task<ChatClientAgent> CreateChatClientAgentAsync(
|
|
string name = "HelpfulAssistant",
|
|
string instructions = "You are a helpful assistant.",
|
|
IList<AITool>? aiTools = null)
|
|
{
|
|
var assistant =
|
|
await this._assistantClient!.CreateAssistantAsync(
|
|
s_config.ChatModelId!,
|
|
new AssistantCreationOptions()
|
|
{
|
|
Name = name,
|
|
Instructions = instructions
|
|
});
|
|
|
|
return new ChatClientAgent(
|
|
this._assistantClient.AsIChatClient(assistant.Value.Id),
|
|
options: new()
|
|
{
|
|
Id = assistant.Value.Id,
|
|
ChatOptions = new() { Tools = aiTools }
|
|
});
|
|
}
|
|
|
|
public Task DeleteAgentAsync(ChatClientAgent agent)
|
|
{
|
|
return this._assistantClient!.DeleteAssistantAsync(agent.Id);
|
|
}
|
|
|
|
public Task DeleteThreadAsync(AgentThread thread)
|
|
{
|
|
if (thread?.ConversationId is not null)
|
|
{
|
|
return this._assistantClient!.DeleteThreadAsync(thread.ConversationId);
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
var client = new OpenAIClient(s_config.ApiKey);
|
|
this._assistantClient = client.GetAssistantClient();
|
|
|
|
this._agent = await this.CreateChatClientAgentAsync();
|
|
}
|
|
|
|
public Task DisposeAsync()
|
|
{
|
|
if (this._assistantClient is not null && this._agent is not null)
|
|
{
|
|
return this._assistantClient.DeleteAssistantAsync(this._agent.Id);
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|