diff --git a/dotnet/AutoGen.sln b/dotnet/AutoGen.sln index 5ecfe1938873..34a1c235ee4b 100644 --- a/dotnet/AutoGen.sln +++ b/dotnet/AutoGen.sln @@ -61,6 +61,8 @@ 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}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -171,6 +173,10 @@ Global {6B82F26D-5040-4453-B21B-C8D1F913CE4C}.Debug|Any CPU.Build.0 = Debug|Any CPU {6B82F26D-5040-4453-B21B-C8D1F913CE4C}.Release|Any CPU.ActiveCfg = Release|Any CPU {6B82F26D-5040-4453-B21B-C8D1F913CE4C}.Release|Any CPU.Build.0 = Release|Any CPU + {0E635268-351C-4A6B-A28D-593D868C2CA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -202,6 +208,7 @@ Global {8EA16BAB-465A-4C07-ABC4-1070D40067E9} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64} {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} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {93384647-528D-46C8-922C-8DB36A382F0B} diff --git a/dotnet/sample/AutoGen.OpenAI.Sample/AutoGen.OpenAI.Sample.csproj b/dotnet/sample/AutoGen.OpenAI.Sample/AutoGen.OpenAI.Sample.csproj new file mode 100644 index 000000000000..ffe18f8a616a --- /dev/null +++ b/dotnet/sample/AutoGen.OpenAI.Sample/AutoGen.OpenAI.Sample.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + enable + enable + True + $(NoWarn);CS8981;CS8600;CS8602;CS8604;CS8618;CS0219;SKEXP0054;SKEXP0050;SKEXP0110 + true + + + + + + + + + + + diff --git a/dotnet/sample/AutoGen.OpenAI.Sample/Connect_To_Ollama.cs b/dotnet/sample/AutoGen.OpenAI.Sample/Connect_To_Ollama.cs new file mode 100644 index 000000000000..b4206b4b6c22 --- /dev/null +++ b/dotnet/sample/AutoGen.OpenAI.Sample/Connect_To_Ollama.cs @@ -0,0 +1,61 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Example16_OpenAIChatAgent_ConnectToThirdPartyBackend.cs +#region using_statement +using AutoGen.Core; +using AutoGen.OpenAI.Extension; +using Azure.AI.OpenAI; +using Azure.Core.Pipeline; +#endregion using_statement + +namespace AutoGen.OpenAI.Sample; + +#region CustomHttpClientHandler +public sealed class CustomHttpClientHandler : HttpClientHandler +{ + private string _modelServiceUrl; + + public CustomHttpClientHandler(string modelServiceUrl) + { + _modelServiceUrl = modelServiceUrl; + } + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + request.RequestUri = new Uri($"{_modelServiceUrl}{request.RequestUri.PathAndQuery}"); + + return base.SendAsync(request, cancellationToken); + } +} +#endregion CustomHttpClientHandler + +public class Connect_To_Ollama +{ + public static async Task RunAsync() + { + #region create_agent + using var client = new HttpClient(new CustomHttpClientHandler("http://localhost:11434")); + var option = new OpenAIClientOptions(OpenAIClientOptions.ServiceVersion.V2024_04_01_Preview) + { + Transport = new HttpClientTransport(client), + }; + + // api-key is not required for local server + // so you can use any string here + var openAIClient = new OpenAIClient("api-key", option); + var model = "llama3"; + + var agent = new OpenAIChatAgent( + openAIClient: openAIClient, + name: "assistant", + modelName: model, + systemMessage: "You are a helpful assistant designed to output JSON.", + seed: 0) + .RegisterMessageConnector() + .RegisterPrintMessage(); + #endregion create_agent + + #region send_message + await agent.SendAsync("Can you write a piece of C# code to calculate 100th of fibonacci?"); + #endregion send_message + } +} diff --git a/dotnet/sample/AutoGen.OpenAI.Sample/Program.cs b/dotnet/sample/AutoGen.OpenAI.Sample/Program.cs new file mode 100644 index 000000000000..5a38a3ff03b9 --- /dev/null +++ b/dotnet/sample/AutoGen.OpenAI.Sample/Program.cs @@ -0,0 +1,6 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Program.cs + +using AutoGen.OpenAI.Sample; + +Tool_Call_With_Ollama_And_LiteLLM.RunAsync().Wait(); diff --git a/dotnet/sample/AutoGen.OpenAI.Sample/Tool_Call_With_Ollama_And_LiteLLM.cs b/dotnet/sample/AutoGen.OpenAI.Sample/Tool_Call_With_Ollama_And_LiteLLM.cs new file mode 100644 index 000000000000..f4fabe3c9e83 --- /dev/null +++ b/dotnet/sample/AutoGen.OpenAI.Sample/Tool_Call_With_Ollama_And_LiteLLM.cs @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Tool_Call_With_Ollama_And_LiteLLM.cs + +using AutoGen.Core; +using AutoGen.OpenAI.Extension; +using Azure.AI.OpenAI; +using Azure.Core.Pipeline; + +namespace AutoGen.OpenAI.Sample; + +public partial class Function +{ + [Function] + public async Task GetWeatherAsync(string city) + { + return await Task.FromResult("The weather in " + city + " is 72 degrees and sunny."); + } +} +public class Tool_Call_With_Ollama_And_LiteLLM +{ + public static async Task RunAsync() + { + #region Create_Agent + var liteLLMUrl = "http://localhost:4000"; + using var httpClient = new HttpClient(new CustomHttpClientHandler(liteLLMUrl)); + var option = new OpenAIClientOptions(OpenAIClientOptions.ServiceVersion.V2024_04_01_Preview) + { + Transport = new HttpClientTransport(httpClient), + }; + + var functions = new Function(); + var functionMiddleware = new FunctionCallMiddleware( + functions: [functions.GetWeatherAsyncFunctionContract], + functionMap: new Dictionary>> + { + { functions.GetWeatherAsyncFunctionContract.Name!, functions.GetWeatherAsyncWrapper }, + }); + + // api-key is not required for local server + // so you can use any string here + var openAIClient = new OpenAIClient("api-key", option); + + var agent = new OpenAIChatAgent( + openAIClient: openAIClient, + name: "assistant", + modelName: "placeholder", + systemMessage: "You are a helpful AI assistant") + .RegisterMessageConnector() + .RegisterMiddleware(functionMiddleware) + .RegisterPrintMessage(); + + var reply = await agent.SendAsync("what's the weather in new york"); + #endregion Create_Agent + } +}