提交代码
This commit is contained in:
parent
9282b910ba
commit
bfe3f95055
|
|
@ -44,7 +44,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuanMeng.MiaoYu.Code", "src
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuanMeng.MiaoYu.WebApi", "src\2-api\HuanMeng.MiaoYu.WebApi\HuanMeng.MiaoYu.WebApi.csproj", "{729950F2-71EE-42C0-8B46-295740DE20BA}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HuanMeng.Utility", "src\0-core\HuanMeng.Utility\HuanMeng.Utility.csproj", "{48E1532F-8B50-477C-BB78-8AEA89A167CE}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuanMeng.Utility", "src\0-core\HuanMeng.Utility\HuanMeng.Utility.csproj", "{48E1532F-8B50-477C-BB78-8AEA89A167CE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
|
||||
namespace HuanMeng.DotNetCore.CustomExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加跨域
|
||||
/// </summary>
|
||||
public static class CorsExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加跨域
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="policyName"></param>
|
||||
public static void AddCustomCors(this IServiceCollection services, string policyName)
|
||||
{
|
||||
services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(policyName,
|
||||
builder =>
|
||||
{
|
||||
builder
|
||||
.AllowAnyOrigin()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,11 +7,13 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
using HuanMeng.MiaoYu.Code.Chat.Contract;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Chat.Claude
|
||||
{
|
||||
/// <summary>
|
||||
/// Claude3 通用参数
|
||||
/// </summary>
|
||||
public class BaseClaudeChatChatParams : BaseChatParams
|
||||
{
|
||||
/// <summary>
|
||||
/// 模型
|
||||
/// </summary>
|
||||
public string Model { get; set; }
|
||||
/// <summary>
|
||||
/// 最大token数
|
||||
/// </summary>
|
||||
[JsonProperty("max_tokens")]
|
||||
public int MaxTokens { get; set; }
|
||||
/// <summary>
|
||||
/// 上下文
|
||||
/// </summary>
|
||||
public string System { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消息内容
|
||||
/// </summary>
|
||||
public ClaudeChatMessage[] Messages { get; set; }
|
||||
}
|
||||
}
|
||||
71
src/0-core/HuanMeng.MiaoYu.Code/Chat/Claude/ClaudeChat.cs
Normal file
71
src/0-core/HuanMeng.MiaoYu.Code/Chat/Claude/ClaudeChat.cs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
using HuanMeng.MiaoYu.Code.Chat.Contract;
|
||||
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Chat.Claude
|
||||
{
|
||||
/// <summary>
|
||||
/// Claude3 https://docs.anthropic.com/en/api/messages ClaudeChatInfo
|
||||
/// </summary>
|
||||
public class ClaudeChat(ClaudeChatConfig claudeChatConfig, IHttpClientFactory factory) : IChat
|
||||
{
|
||||
public async Task<BaseChatInfo> MessagesAsync(BaseChatParams chatParams)
|
||||
{
|
||||
var claudeChatChatParams = chatParams as ClaudeChatChatParams;
|
||||
if (claudeChatChatParams == null)
|
||||
{
|
||||
throw new ArgumentException("参数异常");
|
||||
}
|
||||
//添加默认值
|
||||
if (claudeChatChatParams.MaxTokens == 0)
|
||||
{
|
||||
claudeChatChatParams.MaxTokens = claudeChatConfig.MaxTokens;
|
||||
}
|
||||
//添加默认值
|
||||
if (!string.IsNullOrEmpty(claudeChatChatParams.Model))
|
||||
{
|
||||
claudeChatChatParams.Model = claudeChatChatParams.Model;
|
||||
}
|
||||
//去线程池里拿http线程
|
||||
using (var httpClient = factory.CreateClient())
|
||||
{
|
||||
// 设置请求头
|
||||
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
httpClient.DefaultRequestHeaders.Add("x-api-key", claudeChatConfig.ApiKey);
|
||||
httpClient.DefaultRequestHeaders.Add("anthropic-version", claudeChatConfig.AnthropicVersion);
|
||||
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
string json = JsonConvert.SerializeObject(chatParams, settings);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
//HttpClientHandler httpClientHandler = new HttpClientHandler();
|
||||
//HttpContent httpContent = new JsonContent();
|
||||
var response = await httpClient.PostAsync(claudeChatConfig.RequestUrl, content);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var chatInfo = response.Content.ReadFromJsonAsync<BaseChatInfo?>();
|
||||
}
|
||||
}
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<BaseChatStream> MessagesStreamAsync(BaseChatParams chatParams)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
using HuanMeng.MiaoYu.Code.Chat.Contract;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Chat.Claude
|
||||
{
|
||||
/// <summary>
|
||||
/// Claude3 请求的参数
|
||||
/// </summary>
|
||||
public class ClaudeChatChatParams : BaseClaudeChatChatParams
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Chat.Claude
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置类
|
||||
/// </summary>
|
||||
public class ClaudeChatConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 模型
|
||||
/// </summary>
|
||||
public string Model { get; set; }
|
||||
/// <summary>
|
||||
/// 最大token数
|
||||
/// </summary>
|
||||
public int MaxTokens { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// api key
|
||||
/// </summary>
|
||||
public string ApiKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型版本
|
||||
/// </summary>
|
||||
public string AnthropicVersion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 请求地址 = "api.gptsapi.net";
|
||||
/// </summary>
|
||||
public string RequestUrl { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
using HuanMeng.MiaoYu.Code.Chat.Contract;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Chat.Claude
|
||||
{
|
||||
/// <summary>
|
||||
/// 返回内容
|
||||
/// </summary>
|
||||
public class ClaudeChatInfo : BaseChatInfo
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Chat.Claude
|
||||
{
|
||||
/// <summary>
|
||||
/// Claude发送消息内容
|
||||
/// </summary>
|
||||
public class ClaudeChatMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// 角色
|
||||
/// </summary>
|
||||
public string Role { get; set; }
|
||||
/// <summary>
|
||||
/// 内容
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Chat.Contract
|
||||
{
|
||||
/// <summary>
|
||||
/// 聊天返回的实体类
|
||||
/// </summary>
|
||||
public class BaseChatInfo
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Chat.Contract
|
||||
{
|
||||
/// <summary>
|
||||
/// 聊天请求参数
|
||||
/// </summary>
|
||||
public class BaseChatParams
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Chat.Contract
|
||||
{
|
||||
/// <summary>
|
||||
/// 聊天实时流
|
||||
/// </summary>
|
||||
public class BaseChatStream
|
||||
{
|
||||
}
|
||||
}
|
||||
28
src/0-core/HuanMeng.MiaoYu.Code/Chat/Contract/IChat.cs
Normal file
28
src/0-core/HuanMeng.MiaoYu.Code/Chat/Contract/IChat.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Code.Chat.Contract
|
||||
{
|
||||
/// <summary>
|
||||
/// 聊天接口
|
||||
/// </summary>
|
||||
public interface IChat
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="chatParams"></param>
|
||||
/// <returns></returns>
|
||||
Task<BaseChatInfo> MessagesAsync(BaseChatParams chatParams);
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="chatParams"></param>
|
||||
/// <returns></returns>
|
||||
IAsyncEnumerable<BaseChatStream> MessagesStreamAsync(BaseChatParams chatParams);
|
||||
}
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@
|
|||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="System.Text.Json" Version="8.0.3" />
|
||||
<PackageReference Include="System.Text.Json" Version="8.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
using HuanMeng.StableDiffusion.TextGeneration.api;
|
||||
using HuanMeng.StableDiffusion.TextGeneration.api;
|
||||
using HuanMeng.StableDiffusion.TextGeneration.Models;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
using HuanMeng.StableDiffusion.TextGeneration.Abstractions;
|
||||
using HuanMeng.StableDiffusion.TextGeneration.Abstractions;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
|
@ -30,6 +30,7 @@ namespace HuanMeng.StableDiffusion.TextGeneration.api
|
|||
}
|
||||
public override async IAsyncEnumerable<string> SendMessageAsync()
|
||||
{
|
||||
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
// 设置请求头
|
||||
|
|
|
|||
|
|
@ -23,9 +23,9 @@ namespace HuanMeng.Utility.AssemblyHelper
|
|||
// 创建并填充 AssemblyInfo 对象的相关属性
|
||||
var assemblyInfo = new AssemblyInfo
|
||||
{
|
||||
Version = assembly.GetCustomAttributes<AssemblyVersionAttribute>().FirstOrDefault()?.Version ?? "",
|
||||
Version = assembly.GetName().Version.ToString(),
|
||||
FileVersion = assembly.GetCustomAttributes<AssemblyFileVersionAttribute>().FirstOrDefault()?.Version ?? "",
|
||||
AssemblyVersion = assembly.GetName().Version.ToString(),
|
||||
AssemblyVersion = assembly.GetCustomAttributes<AssemblyVersionAttribute>().FirstOrDefault()?.Version ?? "",
|
||||
InformationalVersion = assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>().FirstOrDefault()?.InformationalVersion ?? "",
|
||||
//Company = assembly.GetCustomAttributes<AssemblyCompanyAttribute>().FirstOrDefault()?.Company ?? "",
|
||||
//Product = assembly.GetCustomAttributes<AssemblyProductAttribute>().FirstOrDefault()?.Product ?? "",
|
||||
|
|
|
|||
|
|
@ -9,28 +9,16 @@ using HuanMeng.MiaoYu.Code.Users.UserAccount.VerificationCodeManager;
|
|||
using HuanMeng.MiaoYu.Code.JwtUtil;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using HuanMeng.Utility.AssemblyHelper;
|
||||
using HuanMeng.DotNetCore.CustomExtension;
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// 检索程序集信息
|
||||
AssemblyInfo assemblyInfo = AssemblyInfoHelper.GetAssemblyInfo();
|
||||
// Add services to the container.
|
||||
// Add services to the container.
|
||||
builder.Services.AddHttpContextAccessor(); //添加httpContext注入访问
|
||||
#region 添加跨域
|
||||
var _myAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
options.AddPolicy(_myAllowSpecificOrigins,
|
||||
builder =>
|
||||
{
|
||||
//builder.AllowAnyHeader().AllowAnyMethod().
|
||||
//AllowAnyOrigin();
|
||||
//builder.AllowAnyHeader()
|
||||
// .AllowAnyMethod()
|
||||
// .SetIsOriginAllowed((host) => true)//限制允许跨域请求的特定源
|
||||
// .AllowCredentials();//允许跨域请求发送凭据,如 Cookies、HTTP 认证信息等。启用该配置可能会增加安全风险,因为浏览器会在请求头中包含凭据信息。因此,在设置 AllowCredentials 时,应该确保服务器端的安全性,并且仅允许受信任的源发送包含凭据的请求。
|
||||
builder.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyOrigin();// 许来自任意源的跨域请求
|
||||
}));
|
||||
builder.Services.AddCustomCors(_myAllowSpecificOrigins);
|
||||
#endregion
|
||||
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies().Where(it => it.FullName.Contains("HuanMeng") || it.FullName.Contains("XLib.")).ToList());
|
||||
builder.Services.AddControllers();
|
||||
|
|
@ -73,6 +61,8 @@ builder.Services.AddSwaggerGen(c =>
|
|||
c.ParameterFilter<LowercaseParameterFilter>();
|
||||
c.RequestBodyFilter<LowercaseRequestFilter>();
|
||||
});
|
||||
//配置路由选项,使URL全部小写
|
||||
builder.Services.AddRouting(options => options.LowercaseUrls = true);
|
||||
//添加多租户
|
||||
builder.AddMultiTenantMiaoYu();
|
||||
//添加腾讯云管理
|
||||
|
|
@ -94,8 +84,8 @@ app.UseSwaggerUI(c =>
|
|||
c.DefaultModelsExpandDepth(3);
|
||||
c.DefaultModelExpandDepth(3);
|
||||
c.EnableFilter("true");
|
||||
//c.RoutePrefix = "swagger";
|
||||
//c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API V1");
|
||||
//c.RoutePrefix = string.Empty;
|
||||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API V1");
|
||||
});
|
||||
//}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user