using Microsoft.Extensions.Hosting; namespace OllamaStudy.UseExtensionsAI { /// /// /// public class Startup { //支持两种配置样式,并且两种 Configure 方法都受支持 #region HostApplicationBuilder 风格, 此风格优先级高于Startup(只执行一种风格) //public void ConfigureHostApplicationBuilder(IHostApplicationBuilder hostApplicationBuilder) //{ //} //public IHost BuildHostApplicationBuilder(HostApplicationBuilder hostApplicationBuilder) //{ // return hostApplicationBuilder.Build(); //} #endregion #region Startup 风格 /// /// (可选) 创建IHostBuilder /// /// public IHostBuilder CreateHostBuilder() { return Host.CreateDefaultBuilder(); } public void ConfigureHost(IHostBuilder hostBuilder) { hostBuilder.ConfigureAppConfiguration((hostBuilder,configBuilder) => { configBuilder .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("OllamaServerConfig.json", false, true); }); } /// /// (可选) 构建Host /// /// /// public IHost BuildHost(IHostBuilder hostBuilder) { return hostBuilder.Build(); } /// /// 配置服务 /// /// /// public void ConfigureServices(IServiceCollection services, HostBuilderContext context) { services .AddOptions() .Configure(context.Configuration.GetRequiredSection("OllamaServer")); services .AddScoped(provider => { var options = provider.GetRequiredService>().CurrentValue; return new OllamaApiClient(options.OllamaServerUrl,options.Model); }) .AddScoped(provider => { var options = provider.GetRequiredService>().CurrentValue; var openAIClientOptions = new OpenAIClientOptions() { Endpoint = new Uri(new Uri(options.OllamaServerUrl),"v1") }; return new OpenAIClient(new ApiKeyCredential("nokey"),openAIClientOptions); }) .AddScoped(provider => { var options = provider.GetRequiredService>().CurrentValue; var openAIClientOptions = new OpenAIClientOptions() { Endpoint = new Uri(new Uri(options.OllamaServerUrl),"v1") }; return new ChatClient(options.Model,new ApiKeyCredential("nokey"),openAIClientOptions); }) //OpenAI 客户端是线程安全的,可安全的注册为单例 .AddKeyedSingleton("OpenAIChatClient",(provider,obj) => { var options = provider.GetRequiredService>().CurrentValue; var openAIClientOptions = new OpenAIClientOptions() { Endpoint = new Uri(new Uri(options.OllamaServerUrl), "v1") }; return new ChatClient(options.Model, new ApiKeyCredential("nokey"), openAIClientOptions); }) //UiUiAPI OpenAI 兼容API .AddKeyedSingleton("UiUiAPIClient", (provider, obj) => { var openAIClientOptions = new OpenAIClientOptions() { Endpoint = new Uri("https://sg.uiuiapi.com/v1") }; return new OpenAIClient(new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"), openAIClientOptions); }) //阿里百炼 OpenAI兼容API .AddKeyedSingleton("BailianAPIClient", (provider, obj) => { var openAIClientOptions = new OpenAIClientOptions() { Endpoint = new Uri("https://dashscope.aliyuncs.com/compatible-mode/v1") }; return new OpenAIClient(new ApiKeyCredential(""), openAIClientOptions); }) //智谱 OpenAI兼容API .AddKeyedSingleton("ZipuAPIClient", (provider, obj) => { var openAIClientOptions = new OpenAIClientOptions() { Endpoint = new Uri("https://open.bigmodel.cn/api/paas/v4/") }; return new OpenAIClient(new ApiKeyCredential("397a799102a6453282da8abb2a1b2581.8fTMHZGRkPHJya4R"), openAIClientOptions); }); } #endregion } }