Skip to content

A programming framework for agentic AI

[.Net] Agent as service: Run an `IAgent` as openai chat completion endpoint (#2633)

Xiaoyun Zhangb021e4454085
diff --git a/dotnet/AutoGen.sln b/dotnet/AutoGen.sln
index 5fa215f0c..1218cf129 100644
--- a/dotnet/AutoGen.sln
+++ b/dotnet/AutoGen.sln
@@ -1,4 +1,3 @@
-
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34322.80
@@ -33,6 +32,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.Mistral", "src\Auto
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.Mistral.Tests", "test\AutoGen.Mistral.Tests\AutoGen.Mistral.Tests.csproj", "{15441693-3659-4868-B6C1-B106F52FF3BA}"
EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.WebAPI", "src\AutoGen.WebAPI\AutoGen.WebAPI.csproj", "{257FFD71-08E5-40C7-AB04-6A81A78EB410}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.WebAPI.Tests", "test\AutoGen.WebAPI.Tests\AutoGen.WebAPI.Tests.csproj", "{E2EF5E66-683C-4DDC-8ADA-5F676502B9BA}"
+EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.SemanticKernel.Tests", "test\AutoGen.SemanticKernel.Tests\AutoGen.SemanticKernel.Tests.csproj", "{1DFABC4A-8458-4875-8DCB-59F3802DAC65}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.OpenAI.Tests", "test\AutoGen.OpenAI.Tests\AutoGen.OpenAI.Tests.csproj", "{D36A85F9-C172-487D-8192-6BFE5D05B4A7}"
@@ -61,7 +64,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.Gemini.Sample", "sa
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.AotCompatibility.Tests", "test\AutoGen.AotCompatibility.Tests\AutoGen.AotCompatibility.Tests.csproj", "{6B82F26D-5040-4453-B21B-C8D1F913CE4C}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoGen.OpenAI.Sample", "sample\AutoGen.OpenAI.Sample\AutoGen.OpenAI.Sample.csproj", "{0E635268-351C-4A6B-A28D-593D868C2CA4}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.OpenAI.Sample", "sample\AutoGen.OpenAI.Sample\AutoGen.OpenAI.Sample.csproj", "{0E635268-351C-4A6B-A28D-593D868C2CA4}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.WebAPI.Sample", "sample\AutoGen.WebAPI.Sample\AutoGen.WebAPI.Sample.csproj", "{12079C18-A519-403F-BBFD-200A36A0C083}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -117,6 +122,14 @@ Global
{15441693-3659-4868-B6C1-B106F52FF3BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15441693-3659-4868-B6C1-B106F52FF3BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15441693-3659-4868-B6C1-B106F52FF3BA}.Release|Any CPU.Build.0 = Release|Any CPU
+ {257FFD71-08E5-40C7-AB04-6A81A78EB410}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {257FFD71-08E5-40C7-AB04-6A81A78EB410}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {257FFD71-08E5-40C7-AB04-6A81A78EB410}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {257FFD71-08E5-40C7-AB04-6A81A78EB410}.Release|Any CPU.Build.0 = Release|Any CPU
+ {E2EF5E66-683C-4DDC-8ADA-5F676502B9BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {E2EF5E66-683C-4DDC-8ADA-5F676502B9BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {E2EF5E66-683C-4DDC-8ADA-5F676502B9BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {E2EF5E66-683C-4DDC-8ADA-5F676502B9BA}.Release|Any CPU.Build.0 = Release|Any CPU
{1DFABC4A-8458-4875-8DCB-59F3802DAC65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DFABC4A-8458-4875-8DCB-59F3802DAC65}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DFABC4A-8458-4875-8DCB-59F3802DAC65}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -177,6 +190,10 @@ Global
{0E635268-351C-4A6B-A28D-593D868C2CA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E635268-351C-4A6B-A28D-593D868C2CA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E635268-351C-4A6B-A28D-593D868C2CA4}.Release|Any CPU.Build.0 = Release|Any CPU
+ {12079C18-A519-403F-BBFD-200A36A0C083}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {12079C18-A519-403F-BBFD-200A36A0C083}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {12079C18-A519-403F-BBFD-200A36A0C083}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {12079C18-A519-403F-BBFD-200A36A0C083}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -194,6 +211,8 @@ Global
{63445BB7-DBB9-4AEF-9D6F-98BBE75EE1EC} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{6585D1A4-3D97-4D76-A688-1933B61AEB19} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{15441693-3659-4868-B6C1-B106F52FF3BA} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
+ {257FFD71-08E5-40C7-AB04-6A81A78EB410} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
+ {E2EF5E66-683C-4DDC-8ADA-5F676502B9BA} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{1DFABC4A-8458-4875-8DCB-59F3802DAC65} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{D36A85F9-C172-487D-8192-6BFE5D05B4A7} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{B61388CA-DC73-4B7F-A7B2-7B9A86C9229E} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
@@ -209,8 +228,9 @@ Global
{19679B75-CE3A-4DF0-A3F0-CA369D2760A4} = {FBFEAD1F-29EB-4D99-A672-0CD8473E10B9}
{6B82F26D-5040-4453-B21B-C8D1F913CE4C} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{0E635268-351C-4A6B-A28D-593D868C2CA4} = {FBFEAD1F-29EB-4D99-A672-0CD8473E10B9}
+ {12079C18-A519-403F-BBFD-200A36A0C083} = {FBFEAD1F-29EB-4D99-A672-0CD8473E10B9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93384647-528D-46C8-922C-8DB36A382F0B}
EndGlobalSection
-EndGlobal
\ No newline at end of file
+EndGlobal
diff --git a/dotnet/eng/Version.props b/dotnet/eng/Version.props
index 0b8dcaa56..572bb22e4 100644
--- a/dotnet/eng/Version.props
+++ b/dotnet/eng/Version.props
@@ -12,6 +12,7 @@
<MicrosoftNETTestSdkVersion>17.7.0</MicrosoftNETTestSdkVersion>
<MicrosoftDotnetInteractive>1.0.0-beta.24229.4</MicrosoftDotnetInteractive>
<MicrosoftSourceLinkGitHubVersion>8.0.0</MicrosoftSourceLinkGitHubVersion>
+ <MicrosoftASPNETCoreVersion>8.0.4</MicrosoftASPNETCoreVersion>
<GoogleCloudAPIPlatformVersion>3.0.0</GoogleCloudAPIPlatformVersion>
<JsonSchemaVersion>4.3.0.2</JsonSchemaVersion>
</PropertyGroup>
diff --git a/dotnet/sample/AutoGen.WebAPI.Sample/AutoGen.WebAPI.Sample.csproj b/dotnet/sample/AutoGen.WebAPI.Sample/AutoGen.WebAPI.Sample.csproj
new file mode 100644
index 000000000..41f3b7d1d
--- /dev/null
+++ b/dotnet/sample/AutoGen.WebAPI.Sample/AutoGen.WebAPI.Sample.csproj
@@ -0,0 +1,13 @@
+<Project Sdk="Microsoft.NET.Sdk.Web">
+
+ <PropertyGroup>
+ <TargetFramework>net8.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\..\src\AutoGen.WebAPI\AutoGen.WebAPI.csproj" />
+ </ItemGroup>
+
+</Project>
diff --git a/dotnet/sample/AutoGen.WebAPI.Sample/Program.cs b/dotnet/sample/AutoGen.WebAPI.Sample/Program.cs
new file mode 100644
index 000000000..71b8b8921
--- /dev/null
+++ b/dotnet/sample/AutoGen.WebAPI.Sample/Program.cs
@@ -0,0 +1,45 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Program.cs
+
+using System.Runtime.CompilerServices;
+using AutoGen.Core;
+using AutoGen.Service;
+
+var alice = new DummyAgent("alice");
+var bob = new DummyAgent("bob");
+
+var builder = WebApplication.CreateBuilder(args);
+// Add services to the container.
+
+// run endpoint at port 5000
+builder.WebHost.UseUrls("http://localhost:5000");
+var app = builder.Build();
+
+app.UseAgentAsOpenAIChatCompletionEndpoint(alice);
+app.UseAgentAsOpenAIChatCompletionEndpoint(bob);
+
+app.Run();
+
+public class DummyAgent : IStreamingAgent
+{
+ public DummyAgent(string name = "dummy")
+ {
+ Name = name;
+ }
+
+ public string Name { get; }
+
+ public async Task<IMessage> GenerateReplyAsync(IEnumerable<IMessage> messages, GenerateReplyOptions? options = null, CancellationToken cancellationToken = default)
+ {
+ return new TextMessage(Role.Assistant, $"I am dummy {this.Name}", this.Name);
+ }
+
+ public async IAsyncEnumerable<IMessage> GenerateStreamingReplyAsync(IEnumerable<IMessage> messages, GenerateReplyOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
+ {
+ var reply = $"I am dummy {this.Name}";
+ foreach (var c in reply)
+ {
+ yield return new TextMessageUpdate(Role.Assistant, c.ToString(), this.Name);
+ };
+ }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/AutoGen.WebAPI.csproj b/dotnet/src/AutoGen.WebAPI/AutoGen.WebAPI.csproj
new file mode 100644
index 000000000..cd24d5930
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/AutoGen.WebAPI.csproj
@@ -0,0 +1,16 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFrameworks>net6.0;net8.0</TargetFrameworks>
+ <GenerateDocumentationFile>true</GenerateDocumentationFile>
+ <NoWarn>$(NoWarn);CS1591;CS1573</NoWarn>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <FrameworkReference Include="Microsoft.AspNetCore.App" Version="$(MicrosoftASPNETCoreVersion)" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\AutoGen.Core\AutoGen.Core.csproj" />
+ </ItemGroup>
+</Project>
diff --git a/dotnet/src/AutoGen.WebAPI/Extension.cs b/dotnet/src/AutoGen.WebAPI/Extension.cs
new file mode 100644
index 000000000..30fd88431
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/Extension.cs
@@ -0,0 +1,24 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Extension.cs
+
+using AutoGen.Core;
+using Microsoft.AspNetCore.Builder;
+
+namespace AutoGen.Service;
+
+public static class Extension
+{
+ /// <summary>
+ /// Serve the agent as an OpenAI chat completion endpoint using <see cref="OpenAIChatCompletionMiddleware"/>.
+ /// If the request path is /v1/chat/completions and model name is the same as the agent name,
+ /// the request will be handled by the agent.
+ /// otherwise, the request will be passed to the next middleware.
+ /// </summary>
+ /// <param name="app">application builder</param>
+ /// <param name="agent"><see cref="IAgent"/></param>
+ public static IApplicationBuilder UseAgentAsOpenAIChatCompletionEndpoint(this IApplicationBuilder app, IAgent agent)
+ {
+ var middleware = new OpenAIChatCompletionMiddleware(agent);
+ return app.Use(middleware.InvokeAsync);
+ }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/Converter/OpenAIMessageConverter.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/Converter/OpenAIMessageConverter.cs
new file mode 100644
index 000000000..78e7b8a6d
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/Converter/OpenAIMessageConverter.cs
@@ -0,0 +1,56 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIMessageConverter.cs
+
+using System;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIMessageConverter : JsonConverter<OpenAIMessage>
+{
+ public override OpenAIMessage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ {
+ using JsonDocument document = JsonDocument.ParseValue(ref reader);
+ var root = document.RootElement;
+ var role = root.GetProperty("role").GetString();
+ var contentDocument = root.GetProperty("content");
+ var isContentDocumentString = contentDocument.ValueKind == JsonValueKind.String;
+ switch (role)
+ {
+ case "system":
+ return JsonSerializer.Deserialize<OpenAISystemMessage>(root.GetRawText()) ?? throw new JsonException();
+ case "user" when isContentDocumentString:
+ return JsonSerializer.Deserialize<OpenAIUserMessage>(root.GetRawText()) ?? throw new JsonException();
+ case "user" when !isContentDocumentString:
+ return JsonSerializer.Deserialize<OpenAIUserMultiModalMessage>(root.GetRawText()) ?? throw new JsonException();
+ case "assistant":
+ return JsonSerializer.Deserialize<OpenAIAssistantMessage>(root.GetRawText()) ?? throw new JsonException();
+ case "tool":
+ return JsonSerializer.Deserialize<OpenAIToolMessage>(root.GetRawText()) ?? throw new JsonException();
+ default:
+ throw new JsonException();
+ }
+ }
+
+ public override void Write(Utf8JsonWriter writer, OpenAIMessage value, JsonSerializerOptions options)
+ {
+ switch (value)
+ {
+ case OpenAISystemMessage systemMessage:
+ JsonSerializer.Serialize(writer, systemMessage, options);
+ break;
+ case OpenAIUserMessage userMessage:
+ JsonSerializer.Serialize(writer, userMessage, options);
+ break;
+ case OpenAIAssistantMessage assistantMessage:
+ JsonSerializer.Serialize(writer, assistantMessage, options);
+ break;
+ case OpenAIToolMessage toolMessage:
+ JsonSerializer.Serialize(writer, toolMessage, options);
+ break;
+ default:
+ throw new JsonException();
+ }
+ }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIAssistantMessage.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIAssistantMessage.cs
new file mode 100644
index 000000000..62771d58a
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIAssistantMessage.cs
@@ -0,0 +1,21 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIAssistantMessage.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIAssistantMessage : OpenAIMessage
+{
+ [JsonPropertyName("role")]
+ public override string? Role { get; } = "assistant";
+
+ [JsonPropertyName("content")]
+ public string? Content { get; set; }
+
+ [JsonPropertyName("name")]
+ public string? Name { get; set; }
+
+ [JsonPropertyName("tool_calls")]
+ public OpenAIToolCallObject[]? ToolCalls { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletion.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletion.cs
new file mode 100644
index 000000000..c5fa3334c
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletion.cs
@@ -0,0 +1,30 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIChatCompletion.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIChatCompletion
+{
+ [JsonPropertyName("id")]
+ public string? ID { get; set; }
+
+ [JsonPropertyName("created")]
+ public long Created { get; set; }
+
+ [JsonPropertyName("choices")]
+ public OpenAIChatCompletionChoice[]? Choices { get; set; }
+
+ [JsonPropertyName("model")]
+ public string? Model { get; set; }
+
+ [JsonPropertyName("system_fingerprint")]
+ public string? SystemFingerprint { get; set; }
+
+ [JsonPropertyName("object")]
+ public string Object { get; set; } = "chat.completion";
+
+ [JsonPropertyName("usage")]
+ public OpenAIChatCompletionUsage? Usage { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionChoice.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionChoice.cs
new file mode 100644
index 000000000..82ba56a6a
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionChoice.cs
@@ -0,0 +1,21 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIChatCompletionChoice.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIChatCompletionChoice
+{
+ [JsonPropertyName("finish_reason")]
+ public string? FinishReason { get; set; }
+
+ [JsonPropertyName("index")]
+ public int Index { get; set; }
+
+ [JsonPropertyName("message")]
+ public OpenAIChatCompletionMessage? Message { get; set; }
+
+ [JsonPropertyName("delta")]
+ public OpenAIChatCompletionMessage? Delta { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionMessage.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionMessage.cs
new file mode 100644
index 000000000..8699523f7
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionMessage.cs
@@ -0,0 +1,15 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIChatCompletionMessage.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIChatCompletionMessage
+{
+ [JsonPropertyName("role")]
+ public string Role { get; } = "assistant";
+
+ [JsonPropertyName("content")]
+ public string? Content { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionOption.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionOption.cs
new file mode 100644
index 000000000..6e656ded8
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionOption.cs
@@ -0,0 +1,33 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIChatCompletionOption.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIChatCompletionOption
+{
+ [JsonPropertyName("messages")]
+ public OpenAIMessage[]? Messages { get; set; }
+
+ [JsonPropertyName("model")]
+ public string? Model { get; set; }
+
+ [JsonPropertyName("max_tokens")]
+ public int? MaxTokens { get; set; }
+
+ [JsonPropertyName("temperature")]
+ public float Temperature { get; set; } = 1;
+
+ /// <summary>
+ /// If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message
+ /// </summary>
+ [JsonPropertyName("stream")]
+ public bool? Stream { get; set; } = false;
+
+ [JsonPropertyName("stream_options")]
+ public OpenAIStreamOptions? StreamOptions { get; set; }
+
+ [JsonPropertyName("stop")]
+ public string[]? Stop { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionUsage.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionUsage.cs
new file mode 100644
index 000000000..299d62c9d
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionUsage.cs
@@ -0,0 +1,18 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIChatCompletionUsage.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIChatCompletionUsage
+{
+ [JsonPropertyName("completion_tokens")]
+ public int CompletionTokens { get; set; }
+
+ [JsonPropertyName("prompt_tokens")]
+ public int PromptTokens { get; set; }
+
+ [JsonPropertyName("total_tokens")]
+ public int TotalTokens { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIImageUrlObject.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIImageUrlObject.cs
new file mode 100644
index 000000000..081620c41
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIImageUrlObject.cs
@@ -0,0 +1,15 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIImageUrlObject.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIImageUrlObject
+{
+ [JsonPropertyName("url")]
+ public string? Url { get; set; }
+
+ [JsonPropertyName("detail")]
+ public string? Detail { get; set; } = "auto";
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIMessage.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIMessage.cs
new file mode 100644
index 000000000..3cad9ae36
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIMessage.cs
@@ -0,0 +1,13 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIChatCompletionOption.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+[JsonConverter(typeof(OpenAIMessageConverter))]
+internal abstract class OpenAIMessage
+{
+ [JsonPropertyName("role")]
+ public abstract string? Role { get; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIStreamOptions.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIStreamOptions.cs
new file mode 100644
index 000000000..3c188b865
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIStreamOptions.cs
@@ -0,0 +1,12 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+//
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIStreamOptions
+{
+ [JsonPropertyName("include_usage")]
+ public bool? IncludeUsage { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAISystemMessage.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAISystemMessage.cs
new file mode 100644
index 000000000..57a671b8c
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAISystemMessage.cs
@@ -0,0 +1,18 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAISystemMessage.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAISystemMessage : OpenAIMessage
+{
+ [JsonPropertyName("role")]
+ public override string? Role { get; } = "system";
+
+ [JsonPropertyName("content")]
+ public string? Content { get; set; }
+
+ [JsonPropertyName("name")]
+ public string? Name { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIToolCallObject.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIToolCallObject.cs
new file mode 100644
index 000000000..7d003f2d7
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIToolCallObject.cs
@@ -0,0 +1,15 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIToolCallObject.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIToolCallObject
+{
+ [JsonPropertyName("name")]
+ public string? Name { get; set; }
+
+ [JsonPropertyName("arguments")]
+ public string? Arguments { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIToolMessage.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIToolMessage.cs
new file mode 100644
index 000000000..5a0bb968d
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIToolMessage.cs
@@ -0,0 +1,18 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIToolMessage.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIToolMessage : OpenAIMessage
+{
+ [JsonPropertyName("role")]
+ public override string? Role { get; } = "tool";
+
+ [JsonPropertyName("content")]
+ public string? Content { get; set; }
+
+ [JsonPropertyName("tool_call_id")]
+ public string? ToolCallId { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserImageContent.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserImageContent.cs
new file mode 100644
index 000000000..c6ada9d8f
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserImageContent.cs
@@ -0,0 +1,15 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIUserImageContent.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIUserImageContent : OpenAIUserMessageItem
+{
+ [JsonPropertyName("type")]
+ public override string MessageType { get; } = "image";
+
+ [JsonPropertyName("image_url")]
+ public string? Url { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserMessage.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserMessage.cs
new file mode 100644
index 000000000..17cb529da
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserMessage.cs
@@ -0,0 +1,18 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIUserMessage.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIUserMessage : OpenAIMessage
+{
+ [JsonPropertyName("role")]
+ public override string? Role { get; } = "user";
+
+ [JsonPropertyName("content")]
+ public string? Content { get; set; }
+
+ [JsonPropertyName("name")]
+ public string? Name { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserMessageItem.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserMessageItem.cs
new file mode 100644
index 000000000..af363c1c9
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserMessageItem.cs
@@ -0,0 +1,12 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIUserMessageItem.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal abstract class OpenAIUserMessageItem
+{
+ [JsonPropertyName("type")]
+ public abstract string MessageType { get; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserMultiModalMessage.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserMultiModalMessage.cs
new file mode 100644
index 000000000..acb8279e7
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserMultiModalMessage.cs
@@ -0,0 +1,18 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIUserMultiModalMessage.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIUserMultiModalMessage : OpenAIMessage
+{
+ [JsonPropertyName("role")]
+ public override string? Role { get; } = "user";
+
+ [JsonPropertyName("content")]
+ public OpenAIUserMessageItem[]? Content { get; set; }
+
+ [JsonPropertyName("name")]
+ public string? Name { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserTextContent.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserTextContent.cs
new file mode 100644
index 000000000..fdb122253
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIUserTextContent.cs
@@ -0,0 +1,15 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIUserTextContent.cs
+
+using System.Text.Json.Serialization;
+
+namespace AutoGen.Service.OpenAI.DTO;
+
+internal class OpenAIUserTextContent : OpenAIUserMessageItem
+{
+ [JsonPropertyName("type")]
+ public override string MessageType { get; } = "text";
+
+ [JsonPropertyName("text")]
+ public string? Content { get; set; }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAI/Service/OpenAIChatCompletionService.cs b/dotnet/src/AutoGen.WebAPI/OpenAI/Service/OpenAIChatCompletionService.cs
new file mode 100644
index 000000000..b84f62c7c
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAI/Service/OpenAIChatCompletionService.cs
@@ -0,0 +1,157 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIChatCompletionService.cs
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using AutoGen.Core;
+using AutoGen.Service.OpenAI.DTO;
+
+namespace AutoGen.Server;
+
+internal class OpenAIChatCompletionService
+{
+ private readonly IAgent agent;
+
+ public OpenAIChatCompletionService(IAgent agent)
+ {
+ this.agent = agent;
+ }
+
+ public async Task<OpenAIChatCompletion> GetChatCompletionAsync(OpenAIChatCompletionOption request)
+ {
+ var messages = this.ProcessMessages(request.Messages ?? Array.Empty<OpenAIMessage>());
+
+ var generateOption = this.ProcessReplyOptions(request);
+
+ var reply = await this.agent.GenerateReplyAsync(messages, generateOption);
+
+ var openAIChatCompletion = new OpenAIChatCompletion()
+ {
+ Created = DateTimeOffset.UtcNow.Ticks / TimeSpan.TicksPerMillisecond / 1000,
+ Model = this.agent.Name,
+ };
+
+ if (reply.GetContent() is string content)
+ {
+ var message = new OpenAIChatCompletionMessage()
+ {
+ Content = content,
+ };
+
+ var choice = new OpenAIChatCompletionChoice()
+ {
+ Message = message,
+ Index = 0,
+ FinishReason = "completed",
+ };
+
+ openAIChatCompletion.Choices = [choice];
+
+ return openAIChatCompletion;
+ }
+
+ throw new NotImplementedException("Unsupported reply content type");
+ }
+
+ public async IAsyncEnumerable<OpenAIChatCompletion> GetStreamingChatCompletionAsync(OpenAIChatCompletionOption request)
+ {
+ if (this.agent is IStreamingAgent streamingAgent)
+ {
+ var messages = this.ProcessMessages(request.Messages ?? Array.Empty<OpenAIMessage>());
+
+ var generateOption = this.ProcessReplyOptions(request);
+
+ await foreach (var reply in streamingAgent.GenerateStreamingReplyAsync(messages, generateOption))
+ {
+ var openAIChatCompletion = new OpenAIChatCompletion()
+ {
+ Created = DateTimeOffset.UtcNow.Ticks / TimeSpan.TicksPerMillisecond / 1000,
+ Model = this.agent.Name,
+ };
+
+ if (reply.GetContent() is string content)
+ {
+ var message = new OpenAIChatCompletionMessage()
+ {
+ Content = content,
+ };
+
+ var choice = new OpenAIChatCompletionChoice()
+ {
+ Delta = message,
+ Index = 0,
+ };
+
+ openAIChatCompletion.Choices = [choice];
+
+ yield return openAIChatCompletion;
+ }
+ else
+ {
+ throw new NotImplementedException("Unsupported reply content type");
+ }
+ }
+
+ var doneMessage = new OpenAIChatCompletion()
+ {
+ Created = DateTimeOffset.UtcNow.Ticks / TimeSpan.TicksPerMillisecond / 1000,
+ Model = this.agent.Name,
+ };
+
+ var doneChoice = new OpenAIChatCompletionChoice()
+ {
+ FinishReason = "stop",
+ Index = 0,
+ };
+
+ doneMessage.Choices = [doneChoice];
+
+ yield return doneMessage;
+ }
+ else
+ {
+ yield return await this.GetChatCompletionAsync(request);
+ }
+ }
+
+ private IEnumerable<IMessage> ProcessMessages(IEnumerable<OpenAIMessage> messages)
+ {
+ return messages.Select<OpenAIMessage, IMessage>(m => m switch
+ {
+ OpenAISystemMessage systemMessage when systemMessage.Content is string content => new TextMessage(Role.System, content, this.agent.Name),
+ OpenAIUserMessage userMessage when userMessage.Content is string content => new TextMessage(Role.User, content, this.agent.Name),
+ OpenAIAssistantMessage assistantMessage when assistantMessage.Content is string content => new TextMessage(Role.Assistant, content, this.agent.Name),
+ OpenAIUserMultiModalMessage userMultiModalMessage when userMultiModalMessage.Content is { Length: > 0 } => this.CreateMultiModaMessageFromOpenAIUserMultiModalMessage(userMultiModalMessage),
+ _ => throw new ArgumentException($"Unsupported message type {m.GetType()}")
+ });
+ }
+
+ private GenerateReplyOptions ProcessReplyOptions(OpenAIChatCompletionOption request)
+ {
+ return new GenerateReplyOptions()
+ {
+ Temperature = request.Temperature,
+ MaxToken = request.MaxTokens,
+ StopSequence = request.Stop,
+ };
+ }
+
+ private MultiModalMessage CreateMultiModaMessageFromOpenAIUserMultiModalMessage(OpenAIUserMultiModalMessage message)
+ {
+ if (message.Content is null)
+ {
+ throw new ArgumentNullException(nameof(message.Content));
+ }
+
+ IEnumerable<IMessage> items = message.Content.Select<OpenAIUserMessageItem, IMessage>(item => item switch
+ {
+ OpenAIUserImageContent imageContent when imageContent.Url is string url => new ImageMessage(Role.User, url, this.agent.Name),
+ OpenAIUserTextContent textContent when textContent.Content is string content => new TextMessage(Role.User, content, this.agent.Name),
+ _ => throw new ArgumentException($"Unsupported content type {item.GetType()}")
+ });
+
+ return new MultiModalMessage(Role.User, items, this.agent.Name);
+ }
+}
diff --git a/dotnet/src/AutoGen.WebAPI/OpenAIChatCompletionMiddleware.cs b/dotnet/src/AutoGen.WebAPI/OpenAIChatCompletionMiddleware.cs
new file mode 100644
index 000000000..3563a6dc2
--- /dev/null
+++ b/dotnet/src/AutoGen.WebAPI/OpenAIChatCompletionMiddleware.cs
@@ -0,0 +1,92 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIChatCompletionMiddleware.cs
+
+using System.Text.Json;
+using System.Threading.Tasks;
+using AutoGen.Core;
+using AutoGen.Server;
+using AutoGen.Service.OpenAI.DTO;
+using Microsoft.AspNetCore.Http;
+
+namespace AutoGen.Service;
+
+public class OpenAIChatCompletionMiddleware : Microsoft.AspNetCore.Http.IMiddleware
+{
+ private readonly IAgent _agent;
+ private readonly OpenAIChatCompletionService chatCompletionService;
+
+ public OpenAIChatCompletionMiddleware(IAgent agent)
+ {
+ _agent = agent;
+ chatCompletionService = new OpenAIChatCompletionService(_agent);
+ }
+
+ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
+ {
+ // if HttpPost and path is /v1/chat/completions
+ // get the request body
+ // call chatCompletionService.GetChatCompletionAsync(request)
+ // return the response
+
+ // else
+ // call next middleware
+ if (context.Request.Method == HttpMethods.Post && context.Request.Path == "/v1/chat/completions")
+ {
+ context.Request.EnableBuffering();
+ var body = await context.Request.ReadFromJsonAsync<OpenAIChatCompletionOption>();
+ context.Request.Body.Position = 0;
+ if (body is null)
+ {
+ // return 400 Bad Request
+ context.Response.StatusCode = 400;
+ return;
+ }
+
+ if (body.Model != _agent.Name)
+ {
+ await next(context);
+ return;
+ }
+
+ if (body.Stream is true)
+ {
+ // Send as server side events
+ context.Response.Headers.Append("Content-Type", "text/event-stream");
+ context.Response.Headers.Append("Cache-Control", "no-cache");
+ context.Response.Headers.Append("Connection", "keep-alive");
+ await foreach (var chatCompletion in chatCompletionService.GetStreamingChatCompletionAsync(body))
+ {
+ if (chatCompletion?.Choices?[0].FinishReason is "stop")
+ {
+ // the stream is done
+ // send Data: [DONE]\n\n
+ await context.Response.WriteAsync("data: [DONE]\n\n");
+ break;
+ }
+ else
+ {
+ // remove null
+ var option = new JsonSerializerOptions
+ {
+ DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
+ };
+ var data = JsonSerializer.Serialize(chatCompletion, option);
+ await context.Response.WriteAsync($"data: {data}\n\n");
+ }
+ }
+
+ return;
+ }
+ else
+ {
+ var chatCompletion = await chatCompletionService.GetChatCompletionAsync(body);
+ await context.Response.WriteAsJsonAsync(chatCompletion);
+ return;
+ }
+ }
+ else
+ {
+ await next(context);
+ }
+ }
+}
diff --git a/dotnet/test/AutoGen.WebAPI.Tests/AutoGen.WebAPI.Tests.csproj b/dotnet/test/AutoGen.WebAPI.Tests/AutoGen.WebAPI.Tests.csproj
new file mode 100644
index 000000000..3a9caf38f
--- /dev/null
+++ b/dotnet/test/AutoGen.WebAPI.Tests/AutoGen.WebAPI.Tests.csproj
@@ -0,0 +1,28 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>$(TestTargetFramework)</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ <IsPackable>false</IsPackable>
+ <IsTestProject>true</IsTestProject>
+ <GenerateDocumentationFile>True</GenerateDocumentationFile>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="$(MicrosoftASPNETCoreVersion)" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\..\src\AutoGen.Mistral\AutoGen.Mistral.csproj" />
+ <ProjectReference Include="..\..\src\AutoGen.WebAPI\AutoGen.WebAPI.csproj" />
+ <ProjectReference Include="..\..\src\AutoGen.SourceGenerator\AutoGen.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
+ <ProjectReference Include="..\..\src\AutoGen\AutoGen.csproj" />
+ <ProjectReference Include="..\AutoGen.Tests\AutoGen.Tests.csproj" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <Using Include="Xunit" />
+ </ItemGroup>
+
+</Project>
diff --git a/dotnet/test/AutoGen.WebAPI.Tests/EchoAgent.cs b/dotnet/test/AutoGen.WebAPI.Tests/EchoAgent.cs
new file mode 100644
index 000000000..64d48d2a0
--- /dev/null
+++ b/dotnet/test/AutoGen.WebAPI.Tests/EchoAgent.cs
@@ -0,0 +1,45 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIChatCompletionMiddlewareTests.cs
+
+using System.Runtime.CompilerServices;
+using AutoGen.Core;
+
+namespace AutoGen.Service.Tests;
+
+public class EchoAgent : IStreamingAgent
+{
+ public EchoAgent(string name)
+ {
+ Name = name;
+ }
+ public string Name { get; }
+
+ public async Task<IMessage> GenerateReplyAsync(
+ IEnumerable<IMessage> messages,
+ GenerateReplyOptions? options = null,
+ CancellationToken cancellationToken = default)
+ {
+ return messages.Last();
+ }
+
+ public async IAsyncEnumerable<IMessage> GenerateStreamingReplyAsync(
+ IEnumerable<IMessage> messages,
+ GenerateReplyOptions? options = null,
+ [EnumeratorCancellation] CancellationToken cancellationToken = default)
+ {
+ var lastMessage = messages.LastOrDefault();
+ if (lastMessage == null)
+ {
+ yield break;
+ }
+
+ // return each character of the last message as a separate message
+ if (lastMessage.GetContent() is string content)
+ {
+ foreach (var c in content)
+ {
+ yield return new TextMessageUpdate(Role.Assistant, c.ToString(), this.Name);
+ }
+ }
+ }
+}
diff --git a/dotnet/test/AutoGen.WebAPI.Tests/OpenAIChatCompletionMiddlewareTests.cs b/dotnet/test/AutoGen.WebAPI.Tests/OpenAIChatCompletionMiddlewareTests.cs
new file mode 100644
index 000000000..bb0f6048e
--- /dev/null
+++ b/dotnet/test/AutoGen.WebAPI.Tests/OpenAIChatCompletionMiddlewareTests.cs
@@ -0,0 +1,82 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// OpenAIChatCompletionMiddlewareTests.cs
+
+using AutoGen.Core;
+using AutoGen.OpenAI;
+using AutoGen.OpenAI.Extension;
+using Azure.AI.OpenAI;
+using Azure.Core.Pipeline;
+using FluentAssertions;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.TestHost;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+
+namespace AutoGen.Service.Tests;
+
+public class OpenAIChatCompletionMiddlewareTests
+{
+ [Fact]
+ public async Task ItReturnTextMessageWhenSendTextMessage()
+ {
+ var agent = new EchoAgent("test");
+ var hostBuilder = CreateHostBuilder(agent);
+ using var host = await hostBuilder.StartAsync();
+ var client = host.GetTestClient();
+ var openaiClient = CreateOpenAIClient(client);
+ var openAIAgent = new OpenAIChatAgent(openaiClient, "test", "test")
+ .RegisterMessageConnector();
+
+ var response = await openAIAgent.SendAsync("Hey");
+
+ response.GetContent().Should().Be("Hey");
+ response.Should().BeOfType<TextMessage>();
+ response.From.Should().Be("test");
+ }
+
+ [Fact]
+ public async Task ItReturnTextMessageWhenSendTextMessageUseStreaming()
+ {
+ var agent = new EchoAgent("test");
+ var hostBuilder = CreateHostBuilder(agent);
+ using var host = await hostBuilder.StartAsync();
+ var client = host.GetTestClient();
+ var openaiClient = CreateOpenAIClient(client);
+ var openAIAgent = new OpenAIChatAgent(openaiClient, "test", "test")
+ .RegisterMessageConnector();
+
+ var message = new TextMessage(Role.User, "ABCDEFGHIJKLMN");
+ var chunks = new List<IMessage>();
+ await foreach (var chunk in openAIAgent.GenerateStreamingReplyAsync([message]))
+ {
+ chunk.Should().BeOfType<TextMessageUpdate>();
+ chunks.Add(chunk);
+ }
+
+ var mergedChunks = string.Join("", chunks.Select(c => c.GetContent()));
+ mergedChunks.Should().Be("ABCDEFGHIJKLMN");
+ chunks.Count.Should().Be(14);
+ }
+
+ private IHostBuilder CreateHostBuilder(IAgent agent)
+ {
+ return new HostBuilder()
+ .ConfigureWebHost(webHost =>
+ {
+ webHost.UseTestServer();
+ webHost.Configure(app =>
+ {
+ app.UseAgentAsOpenAIChatCompletionEndpoint(agent);
+ });
+ });
+ }
+
+ private OpenAIClient CreateOpenAIClient(HttpClient client)
+ {
+ var clientOption = new OpenAIClientOptions(OpenAIClientOptions.ServiceVersion.V2024_02_15_Preview)
+ {
+ Transport = new HttpClientTransport(client),
+ };
+ return new OpenAIClient("api-key", clientOption);
+ }
+}