You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.4 KiB
C#

using Microsoft.Extensions.Hosting;
namespace OllamaStudy.UseExtensionsAI
{
/// <summary>
///
/// </summary>
public class Startup
{
//支持两种配置样式,并且两种 Configure 方法都受支持
#region HostApplicationBuilder 风格, 此风格优先级高于Startup(只执行一种风格)
//public void ConfigureHostApplicationBuilder(IHostApplicationBuilder hostApplicationBuilder)
//{
//}
//public IHost BuildHostApplicationBuilder(HostApplicationBuilder hostApplicationBuilder)
//{
// return hostApplicationBuilder.Build();
//}
#endregion
#region Startup 风格
/// <summary>
/// (可选) 创建IHostBuilder
/// </summary>
/// <returns></returns>
public IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder();
}
public void ConfigureHost(IHostBuilder hostBuilder)
{
hostBuilder.ConfigureAppConfiguration((hostBuilder,configBuilder) =>
{
configBuilder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("OllamaServerConfig.json", false, true);
});
}
/// <summary>
/// (可选) 构建Host
/// </summary>
/// <param name="hostBuilder"></param>
/// <returns></returns>
public IHost BuildHost(IHostBuilder hostBuilder)
{
return hostBuilder.Build();
}
/// <summary>
/// 配置服务
/// </summary>
/// <param name="services"></param>
/// <param name="context"></param>
public void ConfigureServices(IServiceCollection services, HostBuilderContext context)
{
services
.AddOptions()
.Configure<OllamaServerOption>(context.Configuration.GetRequiredSection("OllamaServer"));
services
.AddScoped<IOllamaApiClient>(provider =>
{
var options = provider.GetRequiredService<IOptionsMonitor<OllamaServerOption>>().CurrentValue;
return new OllamaApiClient(options.OllamaServerUrl,options.Model);
})
.AddScoped<OpenAI.OpenAIClient>(provider =>
{
var options = provider.GetRequiredService<IOptionsMonitor<OllamaServerOption>>().CurrentValue;
var openAIClientOptions = new OpenAIClientOptions()
{
Endpoint = new Uri(new Uri(options.OllamaServerUrl),"v1")
};
return new OpenAIClient(new ApiKeyCredential("nokey"),openAIClientOptions);
})
.AddScoped<OpenAI.Chat.ChatClient>(provider =>
{
var options = provider.GetRequiredService<IOptionsMonitor<OllamaServerOption>>().CurrentValue;
var openAIClientOptions = new OpenAIClientOptions()
{
Endpoint = new Uri(new Uri(options.OllamaServerUrl),"v1")
};
return new ChatClient(options.Model,new ApiKeyCredential("nokey"),openAIClientOptions);
})
//OpenAI 客户端是线程安全的,可安全的注册为单例
.AddKeyedSingleton<OpenAI.Chat.ChatClient>("OpenAIChatClient",(provider,obj) =>
{
var options = provider.GetRequiredService<IOptionsMonitor<OllamaServerOption>>().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<OpenAIClient>("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<OpenAIClient>("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<OpenAIClient>("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
}
}