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.
87 lines
2.5 KiB
C#
87 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using OllamaSharp;
|
|
|
|
using OllamaStudy.Core;
|
|
|
|
namespace OllamaStudy.UseOllamaSharp
|
|
{
|
|
/// <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);
|
|
|
|
});
|
|
}
|
|
#endregion
|
|
}
|
|
}
|