forked from microsoft/autogen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[.Net] Agent as service: Run an
IAgent
as openai chat completion en…
…dpoint (microsoft#2633) * update * add test * clean up * update * Delete dotnet/src/AutoGen.Server/AutoGen.Service.csproj.user * implement streaming * add sample project * rename AutoGen.Service to AutoGen.WebAPI * rename AutoGen.Service to AutoGen.WebAPI
- Loading branch information
1 parent
4e95630
commit b021e44
Showing
29 changed files
with
889 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
dotnet/sample/AutoGen.WebAPI.Sample/AutoGen.WebAPI.Sample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
dotnet/src/AutoGen.WebAPI/OpenAI/Converter/OpenAIMessageConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIAssistantMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} |
30 changes: 30 additions & 0 deletions
30
dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} |
21 changes: 21 additions & 0 deletions
21
dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionChoice.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} |
15 changes: 15 additions & 0 deletions
15
dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} |
33 changes: 33 additions & 0 deletions
33
dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionOption.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} |
18 changes: 18 additions & 0 deletions
18
dotnet/src/AutoGen.WebAPI/OpenAI/DTO/OpenAIChatCompletionUsage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} |
Oops, something went wrong.