Skip to content

Commit

Permalink
[.Net] fix #2609 (#2618)
Browse files Browse the repository at this point in the history
* add lmstudio agent to assistant agent

* fix #2609

* update updatelog

* Update Directory.Build.props
  • Loading branch information
LittleLittleCloud committed May 7, 2024
1 parent ecc4113 commit f75103f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 10 deletions.
3 changes: 2 additions & 1 deletion dotnet/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Nullable>enable</Nullable>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)eng/opensource.snk</AssemblyOriginatorKeyFile>
<PublicKey>0024000004800000940000000602000000240000525341310004000001000100f1d038d0b85ae392ad72011df91e9343b0b5df1bb8080aa21b9424362d696919e0e9ac3a8bca24e283e10f7a569c6f443e1d4e3ebc84377c87ca5caa562e80f9932bf5ea91b7862b538e13b8ba91c7565cf0e8dfeccfea9c805ae3bda044170ecc7fc6f147aeeac422dd96aeb9eb1f5a5882aa650efe2958f2f8107d2038f2ab</PublicKey>
<CSNoWarn>CS1998;CS1591</CSNoWarn>
<NoWarn>$(NoWarn);$(CSNoWarn);NU5104</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand All @@ -20,4 +21,4 @@
<PropertyGroup>
<RepoRoot>$(MSBuildThisFileDirectory)</RepoRoot>
</PropertyGroup>
</Project>
</Project>
11 changes: 10 additions & 1 deletion dotnet/src/AutoGen.LMStudio/LMStudioConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ public LMStudioConfig(string host, int port, int version = 1)
this.Host = host;
this.Port = port;
this.Version = version;
this.Uri = new Uri($"http://{host}:{port}/v{version}");
}

public LMStudioConfig(Uri uri)
{
this.Uri = uri;
this.Host = uri.Host;
this.Port = uri.Port;
this.Version = int.Parse(uri.Segments[1].TrimStart('v'));
}

public string Host { get; }
Expand All @@ -21,5 +30,5 @@ public LMStudioConfig(string host, int port, int version = 1)

public int Version { get; }

public Uri Uri => new Uri($"http://{Host}:{Port}/v{Version}");
public Uri Uri { get; }
}
13 changes: 12 additions & 1 deletion dotnet/src/AutoGen/Agent/ConversableAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoGen.LMStudio;
using AutoGen.OpenAI;

namespace AutoGen;
Expand Down Expand Up @@ -74,15 +75,25 @@ public class ConversableAgent : IAgent
this.functions = llmConfig?.FunctionContracts;
}

/// <summary>
/// For test purpose only.
/// </summary>
internal IAgent? InnerAgent => this.innerAgent;

private IAgent? CreateInnerAgentFromConfigList(ConversableAgentConfig config)
{
IAgent? agent = null;
foreach (var llmConfig in config.ConfigList ?? Enumerable.Empty<ILLMConfig>())
{
var nextAgent = llmConfig switch
IAgent nextAgent = llmConfig switch
{
AzureOpenAIConfig azureConfig => new GPTAgent(this.Name!, this.systemMessage, azureConfig, temperature: config.Temperature ?? 0),
OpenAIConfig openAIConfig => new GPTAgent(this.Name!, this.systemMessage, openAIConfig, temperature: config.Temperature ?? 0),
LMStudioConfig lmStudioConfig => new LMStudioAgent(
name: this.Name,
config: lmStudioConfig,
systemMessage: this.systemMessage,
temperature: config.Temperature ?? 0),
_ => throw new ArgumentException($"Unsupported config type {llmConfig.GetType()}"),
};

Expand Down
4 changes: 4 additions & 0 deletions dotnet/src/AutoGen/AutoGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@
<ProjectReference Include="..\AutoGen.Core\AutoGen.Core.csproj" />
<ProjectReference Include="..\AutoGen.OpenAI\AutoGen.OpenAI.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="AutoGen.Tests" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task MistralAgentChatCompletionTestAsync()
model: "open-mistral-7b")
.RegisterMessageConnector();
var singleAgentTest = new SingleAgentTest(_output);
await singleAgentTest.UpperCaseTest(agent);
await singleAgentTest.UpperCaseTestAsync(agent);
await singleAgentTest.UpperCaseStreamingTestAsync(agent);
}

Expand Down
48 changes: 42 additions & 6 deletions dotnet/test/AutoGen.Tests/SingleAgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AutoGen.LMStudio;
using AutoGen.OpenAI;
using Azure.AI.OpenAI;
using FluentAssertions;
Expand Down Expand Up @@ -42,7 +43,7 @@ public async Task GPTAgentTestAsync()

var agent = new GPTAgent("gpt", "You are a helpful AI assistant", config);

await UpperCaseTest(agent);
await UpperCaseTestAsync(agent);
await UpperCaseStreamingTestAsync(agent);
}

Expand Down Expand Up @@ -117,7 +118,7 @@ public async Task GPTFunctionCallAgentTestAsync()
var agentWithFunction = new GPTAgent("gpt", "You are a helpful AI assistant", config, 0, functions: new[] { this.EchoAsyncFunction });

await EchoFunctionCallTestAsync(agentWithFunction);
await UpperCaseTest(agentWithFunction);
await UpperCaseTestAsync(agentWithFunction);
}

[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT")]
Expand All @@ -143,7 +144,43 @@ public async Task AssistantAgentFunctionCallTestAsync()
llmConfig: llmConfig);

await EchoFunctionCallTestAsync(assistantAgent);
await UpperCaseTest(assistantAgent);
await UpperCaseTestAsync(assistantAgent);
}

[Fact]
public async Task ItCreateAssistantAgentFromLMStudioConfigAsync()
{
var host = "http://localhost";
var port = 8080;
var lmStudioConfig = new LMStudioConfig(host, port);

var assistantAgent = new AssistantAgent(
name: "assistant",
llmConfig: new ConversableAgentConfig()
{
ConfigList = [lmStudioConfig],
});

assistantAgent.Name.Should().Be("assistant");
assistantAgent.InnerAgent.Should().BeOfType<LMStudioAgent>();
}

[ApiKeyFact("LMStudio_ENDPOINT")]
public async Task ItTestAssistantAgentFromLMStudioConfigAsync()
{
var Uri = Environment.GetEnvironmentVariable("LMStudio_ENDPOINT") ?? throw new ArgumentException("LMStudio_ENDPOINT is not set");
var lmStudioConfig = new LMStudioConfig(new Uri(Uri));

var assistantAgent = new AssistantAgent(
name: "assistant",
llmConfig: new ConversableAgentConfig()
{
ConfigList = [lmStudioConfig],
});

assistantAgent.Name.Should().Be("assistant");
assistantAgent.InnerAgent.Should().BeOfType<LMStudioAgent>();
await this.UpperCaseTestAsync(assistantAgent);
}


Expand Down Expand Up @@ -186,7 +223,6 @@ public async Task AssistantAgentFunctionCallSelfExecutionTestAsync()
});

await EchoFunctionCallExecutionTestAsync(assistantAgent);
await UpperCaseTest(assistantAgent);
}

[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT")]
Expand All @@ -206,7 +242,7 @@ public async Task GPTAgentFunctionCallSelfExecutionTestAsync()

await EchoFunctionCallExecutionStreamingTestAsync(agent);
await EchoFunctionCallExecutionTestAsync(agent);
await UpperCaseTest(agent);
await UpperCaseTestAsync(agent);
}

/// <summary>
Expand Down Expand Up @@ -283,7 +319,7 @@ await foreach (var reply in replyStream)
}
}

public async Task UpperCaseTest(IAgent agent)
public async Task UpperCaseTestAsync(IAgent agent)
{
var message = new TextMessage(Role.System, "You are a helpful AI assistant that convert user message to upper case");
var uppCaseMessage = new TextMessage(Role.User, "abcdefg");
Expand Down
1 change: 1 addition & 0 deletions dotnet/website/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- [API Breaking Change] Update the return type of `IStreamingAgent.GenerateStreamingReplyAsync` from `Task<IAsyncEnumerable<IStreamingMessage>>` to `IAsyncEnumerable<IStreamingMessage>`
- [API Breaking Change] Update the return type of `IStreamingMiddleware.InvokeAsync` from `Task<IAsyncEnumerable<IStreamingMessage>>` to `IAsyncEnumerable<IStreamingMessage>`
- [API Breaking Change] Mark `RegisterReply`, `RegisterPreProcess` and `RegisterPostProcess` as obsolete. You can replace them with `RegisterMiddleware`
- Fix [Issue 2609](https://github.com/microsoft/autogen/issues/2609)
##### Update on 0.0.12 (2024-04-22)
- Add AutoGen.Mistral package to support Mistral.AI models
##### Update on 0.0.11 (2024-04-10)
Expand Down

0 comments on commit f75103f

Please sign in to comment.