提交代码
This commit is contained in:
parent
1f6407c515
commit
ef1c3c00b7
|
|
@ -19,6 +19,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "9-test", "9-test", "{8D39E8
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuanMeng.DotNetCore", "src\0-core\HuanMeng.DotNetCore\HuanMeng.DotNetCore.csproj", "{B9014C9B-B47D-4900-9EA7-4FE8998C30BE}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "4-abstractions", "4-abstractions", "{6C2243B0-58DA-4D59-A163-7DDDA7DB01C4}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuanMeng.StableDiffusion.Abstractions", "src\4-abstractions\HuanMeng.StableDiffusion.Abstractions\HuanMeng.StableDiffusion.Abstractions.csproj", "{2A7E25DE-A3B8-4F47-9282-64B8EB2D9953}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuanMeng.StableDiffusion.WebSocket", "src\0-core\HuanMeng.StableDiffusion.WebSocket\HuanMeng.StableDiffusion.WebSocket.csproj", "{1B659309-ACDB-49FD-8AE0-2097A1BC6D17}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuanMeng.StableDiffusion.TextGeneration", "src\0-core\HuanMeng.StableDiffusion.TextGeneration\HuanMeng.StableDiffusion.TextGeneration.csproj", "{CFE784F8-3889-4698-8719-55BAB1724486}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuanMeng.StableDiffusion.TextGenerationTests", "src\9-test\HuanMeng.StableDiffusion.TextGenerationTests\HuanMeng.StableDiffusion.TextGenerationTests.csproj", "{054AFFF5-D25E-4EC7-A135-050EEE7F7FA0}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TextGenerationApi", "src\2-api\TextGenerationApi\TextGenerationApi.csproj", "{A9C5939E-D239-4AB8-9380-7E76166326D9}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -29,6 +41,26 @@ Global
|
|||
{B9014C9B-B47D-4900-9EA7-4FE8998C30BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B9014C9B-B47D-4900-9EA7-4FE8998C30BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B9014C9B-B47D-4900-9EA7-4FE8998C30BE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2A7E25DE-A3B8-4F47-9282-64B8EB2D9953}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2A7E25DE-A3B8-4F47-9282-64B8EB2D9953}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2A7E25DE-A3B8-4F47-9282-64B8EB2D9953}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2A7E25DE-A3B8-4F47-9282-64B8EB2D9953}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{1B659309-ACDB-49FD-8AE0-2097A1BC6D17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{1B659309-ACDB-49FD-8AE0-2097A1BC6D17}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1B659309-ACDB-49FD-8AE0-2097A1BC6D17}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1B659309-ACDB-49FD-8AE0-2097A1BC6D17}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{CFE784F8-3889-4698-8719-55BAB1724486}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CFE784F8-3889-4698-8719-55BAB1724486}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CFE784F8-3889-4698-8719-55BAB1724486}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CFE784F8-3889-4698-8719-55BAB1724486}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{054AFFF5-D25E-4EC7-A135-050EEE7F7FA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{054AFFF5-D25E-4EC7-A135-050EEE7F7FA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{054AFFF5-D25E-4EC7-A135-050EEE7F7FA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{054AFFF5-D25E-4EC7-A135-050EEE7F7FA0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{A9C5939E-D239-4AB8-9380-7E76166326D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A9C5939E-D239-4AB8-9380-7E76166326D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A9C5939E-D239-4AB8-9380-7E76166326D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A9C5939E-D239-4AB8-9380-7E76166326D9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -41,6 +73,12 @@ Global
|
|||
{F34C5BC4-8810-4D6A-B003-AFE12D7ED4BA} = {443B77DF-BB15-4DF7-8F45-C54FE6F51AB9}
|
||||
{8D39E84B-2810-41D7-AFE6-0A58E09E34C3} = {443B77DF-BB15-4DF7-8F45-C54FE6F51AB9}
|
||||
{B9014C9B-B47D-4900-9EA7-4FE8998C30BE} = {DD14191F-22CE-48D8-A944-B8A41C97ACD4}
|
||||
{6C2243B0-58DA-4D59-A163-7DDDA7DB01C4} = {443B77DF-BB15-4DF7-8F45-C54FE6F51AB9}
|
||||
{2A7E25DE-A3B8-4F47-9282-64B8EB2D9953} = {6C2243B0-58DA-4D59-A163-7DDDA7DB01C4}
|
||||
{1B659309-ACDB-49FD-8AE0-2097A1BC6D17} = {DD14191F-22CE-48D8-A944-B8A41C97ACD4}
|
||||
{CFE784F8-3889-4698-8719-55BAB1724486} = {DD14191F-22CE-48D8-A944-B8A41C97ACD4}
|
||||
{054AFFF5-D25E-4EC7-A135-050EEE7F7FA0} = {8D39E84B-2810-41D7-AFE6-0A58E09E34C3}
|
||||
{A9C5939E-D239-4AB8-9380-7E76166326D9} = {0C0B6EB5-E41D-46D9-9F60-90D320A2EEF3}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {4A1DC406-AFAA-4884-859C-51B9B26E37FC}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ namespace HuanMeng.DotNetCore.Base
|
|||
/// 数据
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
public T Data { get; set; }
|
||||
public T? Data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
|
|
@ -60,7 +60,7 @@ namespace HuanMeng.DotNetCore.Base
|
|||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public BaseResponse(int code, string message, T data)
|
||||
public BaseResponse(int code, string message, T? data)
|
||||
{
|
||||
Code = code;
|
||||
Message = message;
|
||||
|
|
@ -70,7 +70,7 @@ namespace HuanMeng.DotNetCore.Base
|
|||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public BaseResponse(ResonseCode code, string message, T data)
|
||||
public BaseResponse(ResonseCode code, string message, T? data)
|
||||
{
|
||||
Code = (int)code;
|
||||
Message = message;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.StableDiffusion.TextGeneration.Abstractions
|
||||
{
|
||||
/// <summary>
|
||||
/// 文本聊天请求类
|
||||
/// </summary>
|
||||
public abstract class TextGenerationRequestAbstract
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 发送数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public abstract IAsyncEnumerable<string> SendMessageAsync();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,94 @@
|
|||
using HuanMeng.StableDiffusion.TextGeneration.api;
|
||||
using HuanMeng.StableDiffusion.TextGeneration.Models;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.StableDiffusion.TextGeneration.BLL
|
||||
{
|
||||
/// <summary>
|
||||
/// ai聊天
|
||||
/// </summary>
|
||||
/// <param name="textGenerationTestContext"></param>
|
||||
public class TextGenerationBLL(TextGenerationTestContext textGenerationTestContext)
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="sessionId"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public async IAsyncEnumerable<string> Chat(string sessionId, string message)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sessionId))
|
||||
{
|
||||
sessionId = Guid.NewGuid().ToString();
|
||||
}
|
||||
var x = textGenerationTestContext.TextGenerationSessionDetails.Where(it => it.SessionId == sessionId).OrderBy(it => it.TimeStamp).ToList();
|
||||
List<TextGenerationRequestHttpApiModel> textGenerationRequestHttpApiModels = new List<TextGenerationRequestHttpApiModel>();
|
||||
if (textGenerationRequestHttpApiModels != null)
|
||||
{
|
||||
foreach (var item in x)
|
||||
{
|
||||
textGenerationRequestHttpApiModels.Add(new TextGenerationRequestHttpApiModel
|
||||
{
|
||||
Role = item.Role,
|
||||
Content = item.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
textGenerationRequestHttpApiModels.Add(new TextGenerationRequestHttpApiModel
|
||||
{
|
||||
Role = "user",
|
||||
Content = message
|
||||
});
|
||||
var messageStr = JsonConvert.SerializeObject(textGenerationRequestHttpApiModels, Formatting.None, new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
Formatting = Formatting.Indented // 这个选项是用于美化输出,便于阅读
|
||||
});
|
||||
TextGenerationSessionDetail d = new TextGenerationSessionDetail()
|
||||
{
|
||||
CreateDateTime = DateTime.Now,
|
||||
Message = message,
|
||||
Role = "user",
|
||||
Model = "",
|
||||
SessionId = sessionId,
|
||||
TimeStamp = ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds(),
|
||||
TextGenerationSessionId = 0
|
||||
};
|
||||
textGenerationTestContext.TextGenerationSessionDetails.Add(d);
|
||||
string json = $@"
|
||||
{{
|
||||
""messages"": {messageStr},
|
||||
""stream"": true
|
||||
}}
|
||||
";
|
||||
TextGenerationRequestHttpApi textGenerationRequestHttpApi = new TextGenerationRequestHttpApi(json);
|
||||
await foreach (var item in textGenerationRequestHttpApi.SendMessageAsync())
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
TextGenerationSessionDetail d1 = new TextGenerationSessionDetail()
|
||||
{
|
||||
CreateDateTime = DateTime.Now,
|
||||
Message = textGenerationRequestHttpApi?.textGenerationRequestHttpModel?.Choices?[0]?.Delta?.Content ?? "",
|
||||
Role = textGenerationRequestHttpApi?.textGenerationRequestHttpModel?.Choices?[0]?.Delta?.Role ?? "",
|
||||
Model = textGenerationRequestHttpApi?.textGenerationRequestHttpModel?.Model ?? "",
|
||||
SessionId = sessionId,
|
||||
TimeStamp = ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds(),
|
||||
TextGenerationSessionId = 0
|
||||
};
|
||||
textGenerationTestContext.TextGenerationSessionDetails.Add(d1);
|
||||
await textGenerationTestContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,355 @@
|
|||
<#@ template hostSpecific="true" #>
|
||||
<#@ assembly name="Microsoft.EntityFrameworkCore" #>
|
||||
<#@ assembly name="Microsoft.EntityFrameworkCore.Design" #>
|
||||
<#@ assembly name="Microsoft.EntityFrameworkCore.Relational" #>
|
||||
<#@ assembly name="Microsoft.Extensions.DependencyInjection.Abstractions" #>
|
||||
<#@ parameter name="Model" type="Microsoft.EntityFrameworkCore.Metadata.IModel" #>
|
||||
<#@ parameter name="Options" type="Microsoft.EntityFrameworkCore.Scaffolding.ModelCodeGenerationOptions" #>
|
||||
<#@ parameter name="NamespaceHint" type="System.String" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ import namespace="System.Linq" #>
|
||||
<#@ import namespace="System.Text" #>
|
||||
<#@ import namespace="Microsoft.EntityFrameworkCore" #>
|
||||
<#@ import namespace="Microsoft.EntityFrameworkCore.Design" #>
|
||||
<#@ import namespace="Microsoft.EntityFrameworkCore.Infrastructure" #>
|
||||
<#@ import namespace="Microsoft.EntityFrameworkCore.Scaffolding" #>
|
||||
<#@ import namespace="Microsoft.Extensions.DependencyInjection" #>
|
||||
<#
|
||||
if (!ProductInfo.GetVersion().StartsWith("8.0"))
|
||||
{
|
||||
Warning("Your templates were created using an older version of Entity Framework. Additional features and bug fixes may be available. See https://aka.ms/efcore-docs-updating-templates for more information.");
|
||||
}
|
||||
|
||||
var services = (IServiceProvider)Host;
|
||||
var providerCode = services.GetRequiredService<IProviderConfigurationCodeGenerator>();
|
||||
var annotationCodeGenerator = services.GetRequiredService<IAnnotationCodeGenerator>();
|
||||
var code = services.GetRequiredService<ICSharpHelper>();
|
||||
|
||||
var usings = new List<string>
|
||||
{
|
||||
"System",
|
||||
"System.Collections.Generic",
|
||||
"Microsoft.EntityFrameworkCore"
|
||||
};
|
||||
|
||||
if (NamespaceHint != Options.ModelNamespace
|
||||
&& !string.IsNullOrEmpty(Options.ModelNamespace))
|
||||
{
|
||||
usings.Add(Options.ModelNamespace);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(NamespaceHint))
|
||||
{
|
||||
#>
|
||||
namespace <#= NamespaceHint #>;
|
||||
|
||||
<#
|
||||
}
|
||||
#>
|
||||
public partial class <#= Options.ContextName #> : DbContext
|
||||
{
|
||||
<#
|
||||
if (!Options.SuppressOnConfiguring)
|
||||
{
|
||||
#>
|
||||
public <#= Options.ContextName #>()
|
||||
{
|
||||
}
|
||||
|
||||
<#
|
||||
}
|
||||
#>
|
||||
public <#= Options.ContextName #>(DbContextOptions<<#= Options.ContextName #>> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
<#
|
||||
foreach (var entityType in Model.GetEntityTypes().Where(e => !e.IsSimpleManyToManyJoinEntityType()))
|
||||
{
|
||||
#>
|
||||
public virtual DbSet<<#= entityType.Name #>> <#= entityType.GetDbSetName() #> { get; set; }
|
||||
|
||||
<#
|
||||
}
|
||||
|
||||
if (!Options.SuppressOnConfiguring)
|
||||
{
|
||||
#>
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
<#
|
||||
if (!Options.SuppressConnectionStringWarning)
|
||||
{
|
||||
#>
|
||||
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
|
||||
<#
|
||||
}
|
||||
|
||||
var useProviderCall = providerCode.GenerateUseProvider(Options.ConnectionString);
|
||||
usings.AddRange(useProviderCall.GetRequiredUsings());
|
||||
#>
|
||||
=> optionsBuilder<#= code.Fragment(useProviderCall, indent: 3) #>;
|
||||
|
||||
<#
|
||||
}
|
||||
|
||||
#>
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
<#
|
||||
var anyConfiguration = false;
|
||||
|
||||
var modelFluentApiCalls = Model.GetFluentApiCalls(annotationCodeGenerator);
|
||||
if (modelFluentApiCalls != null)
|
||||
{
|
||||
usings.AddRange(modelFluentApiCalls.GetRequiredUsings());
|
||||
#>
|
||||
modelBuilder<#= code.Fragment(modelFluentApiCalls, indent: 3) #>;
|
||||
<#
|
||||
anyConfiguration = true;
|
||||
}
|
||||
|
||||
StringBuilder mainEnvironment;
|
||||
foreach (var entityType in Model.GetEntityTypes().Where(e => !e.IsSimpleManyToManyJoinEntityType()))
|
||||
{
|
||||
// Save all previously generated code, and start generating into a new temporary environment
|
||||
mainEnvironment = GenerationEnvironment;
|
||||
GenerationEnvironment = new StringBuilder();
|
||||
|
||||
if (anyConfiguration)
|
||||
{
|
||||
WriteLine("");
|
||||
}
|
||||
|
||||
var anyEntityTypeConfiguration = false;
|
||||
#>
|
||||
modelBuilder.Entity<<#= entityType.Name #>>(entity =>
|
||||
{
|
||||
<#
|
||||
var key = entityType.FindPrimaryKey();
|
||||
if (key != null)
|
||||
{
|
||||
var keyFluentApiCalls = key.GetFluentApiCalls(annotationCodeGenerator);
|
||||
if (keyFluentApiCalls != null
|
||||
|| (!key.IsHandledByConvention() && !Options.UseDataAnnotations))
|
||||
{
|
||||
if (keyFluentApiCalls != null)
|
||||
{
|
||||
usings.AddRange(keyFluentApiCalls.GetRequiredUsings());
|
||||
}
|
||||
#>
|
||||
entity.HasKey(<#= code.Lambda(key.Properties, "e") #>)<#= code.Fragment(keyFluentApiCalls, indent: 4) #>;
|
||||
<#
|
||||
anyEntityTypeConfiguration = true;
|
||||
}
|
||||
}
|
||||
|
||||
var entityTypeFluentApiCalls = entityType.GetFluentApiCalls(annotationCodeGenerator)
|
||||
?.FilterChain(c => !(Options.UseDataAnnotations && c.IsHandledByDataAnnotations));
|
||||
if (entityTypeFluentApiCalls != null)
|
||||
{
|
||||
usings.AddRange(entityTypeFluentApiCalls.GetRequiredUsings());
|
||||
|
||||
if (anyEntityTypeConfiguration)
|
||||
{
|
||||
WriteLine("");
|
||||
}
|
||||
#>
|
||||
entity<#= code.Fragment(entityTypeFluentApiCalls, indent: 4) #>;
|
||||
<#
|
||||
anyEntityTypeConfiguration = true;
|
||||
}
|
||||
|
||||
foreach (var index in entityType.GetIndexes()
|
||||
.Where(i => !(Options.UseDataAnnotations && i.IsHandledByDataAnnotations(annotationCodeGenerator))))
|
||||
{
|
||||
if (anyEntityTypeConfiguration)
|
||||
{
|
||||
WriteLine("");
|
||||
}
|
||||
|
||||
var indexFluentApiCalls = index.GetFluentApiCalls(annotationCodeGenerator);
|
||||
if (indexFluentApiCalls != null)
|
||||
{
|
||||
usings.AddRange(indexFluentApiCalls.GetRequiredUsings());
|
||||
}
|
||||
#>
|
||||
entity.HasIndex(<#= code.Lambda(index.Properties, "e") #>, <#= code.Literal(index.GetDatabaseName()) #>)<#= code.Fragment(indexFluentApiCalls, indent: 4) #>;
|
||||
<#
|
||||
anyEntityTypeConfiguration = true;
|
||||
}
|
||||
|
||||
var firstProperty = true;
|
||||
foreach (var property in entityType.GetProperties())
|
||||
{
|
||||
var propertyFluentApiCalls = property.GetFluentApiCalls(annotationCodeGenerator)
|
||||
?.FilterChain(c => !(Options.UseDataAnnotations && c.IsHandledByDataAnnotations)
|
||||
&& !(c.Method == "IsRequired" && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType));
|
||||
if (propertyFluentApiCalls == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
usings.AddRange(propertyFluentApiCalls.GetRequiredUsings());
|
||||
|
||||
if (anyEntityTypeConfiguration && firstProperty)
|
||||
{
|
||||
WriteLine("");
|
||||
}
|
||||
#>
|
||||
entity.Property(e => e.<#= property.Name #>)<#= code.Fragment(propertyFluentApiCalls, indent: 4) #>;
|
||||
<#
|
||||
anyEntityTypeConfiguration = true;
|
||||
firstProperty = false;
|
||||
}
|
||||
|
||||
foreach (var foreignKey in entityType.GetForeignKeys())
|
||||
{
|
||||
var foreignKeyFluentApiCalls = foreignKey.GetFluentApiCalls(annotationCodeGenerator)
|
||||
?.FilterChain(c => !(Options.UseDataAnnotations && c.IsHandledByDataAnnotations));
|
||||
if (foreignKeyFluentApiCalls == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
usings.AddRange(foreignKeyFluentApiCalls.GetRequiredUsings());
|
||||
|
||||
if (anyEntityTypeConfiguration)
|
||||
{
|
||||
WriteLine("");
|
||||
}
|
||||
#>
|
||||
entity.HasOne(d => d.<#= foreignKey.DependentToPrincipal.Name #>).<#= foreignKey.IsUnique ? "WithOne" : "WithMany" #>(<#= foreignKey.PrincipalToDependent != null ? $"p => p.{foreignKey.PrincipalToDependent.Name}" : "" #>)<#= code.Fragment(foreignKeyFluentApiCalls, indent: 4) #>;
|
||||
<#
|
||||
anyEntityTypeConfiguration = true;
|
||||
}
|
||||
|
||||
foreach (var skipNavigation in entityType.GetSkipNavigations().Where(n => n.IsLeftNavigation()))
|
||||
{
|
||||
if (anyEntityTypeConfiguration)
|
||||
{
|
||||
WriteLine("");
|
||||
}
|
||||
|
||||
var left = skipNavigation.ForeignKey;
|
||||
var leftFluentApiCalls = left.GetFluentApiCalls(annotationCodeGenerator, useStrings: true);
|
||||
var right = skipNavigation.Inverse.ForeignKey;
|
||||
var rightFluentApiCalls = right.GetFluentApiCalls(annotationCodeGenerator, useStrings: true);
|
||||
var joinEntityType = skipNavigation.JoinEntityType;
|
||||
|
||||
if (leftFluentApiCalls != null)
|
||||
{
|
||||
usings.AddRange(leftFluentApiCalls.GetRequiredUsings());
|
||||
}
|
||||
|
||||
if (rightFluentApiCalls != null)
|
||||
{
|
||||
usings.AddRange(rightFluentApiCalls.GetRequiredUsings());
|
||||
}
|
||||
#>
|
||||
entity.HasMany(d => d.<#= skipNavigation.Name #>).WithMany(p => p.<#= skipNavigation.Inverse.Name #>)
|
||||
.UsingEntity<Dictionary<string, object>>(
|
||||
<#= code.Literal(joinEntityType.Name) #>,
|
||||
r => r.HasOne<<#= right.PrincipalEntityType.Name #>>().WithMany()<#= code.Fragment(rightFluentApiCalls, indent: 6) #>,
|
||||
l => l.HasOne<<#= left.PrincipalEntityType.Name #>>().WithMany()<#= code.Fragment(leftFluentApiCalls, indent: 6) #>,
|
||||
j =>
|
||||
{
|
||||
<#
|
||||
var joinKey = joinEntityType.FindPrimaryKey();
|
||||
var joinKeyFluentApiCalls = joinKey.GetFluentApiCalls(annotationCodeGenerator);
|
||||
|
||||
if (joinKeyFluentApiCalls != null)
|
||||
{
|
||||
usings.AddRange(joinKeyFluentApiCalls.GetRequiredUsings());
|
||||
}
|
||||
#>
|
||||
j.HasKey(<#= code.Arguments(joinKey.Properties.Select(e => e.Name)) #>)<#= code.Fragment(joinKeyFluentApiCalls, indent: 7) #>;
|
||||
<#
|
||||
var joinEntityTypeFluentApiCalls = joinEntityType.GetFluentApiCalls(annotationCodeGenerator);
|
||||
if (joinEntityTypeFluentApiCalls != null)
|
||||
{
|
||||
usings.AddRange(joinEntityTypeFluentApiCalls.GetRequiredUsings());
|
||||
#>
|
||||
j<#= code.Fragment(joinEntityTypeFluentApiCalls, indent: 7) #>;
|
||||
<#
|
||||
}
|
||||
|
||||
foreach (var index in joinEntityType.GetIndexes())
|
||||
{
|
||||
var indexFluentApiCalls = index.GetFluentApiCalls(annotationCodeGenerator);
|
||||
if (indexFluentApiCalls != null)
|
||||
{
|
||||
usings.AddRange(indexFluentApiCalls.GetRequiredUsings());
|
||||
}
|
||||
#>
|
||||
j.HasIndex(<#= code.Literal(index.Properties.Select(e => e.Name).ToArray()) #>, <#= code.Literal(index.GetDatabaseName()) #>)<#= code.Fragment(indexFluentApiCalls, indent: 7) #>;
|
||||
<#
|
||||
}
|
||||
|
||||
foreach (var property in joinEntityType.GetProperties())
|
||||
{
|
||||
var propertyFluentApiCalls = property.GetFluentApiCalls(annotationCodeGenerator);
|
||||
if (propertyFluentApiCalls == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
usings.AddRange(propertyFluentApiCalls.GetRequiredUsings());
|
||||
#>
|
||||
j.IndexerProperty<<#= code.Reference(property.ClrType) #>>(<#= code.Literal(property.Name) #>)<#= code.Fragment(propertyFluentApiCalls, indent: 7) #>;
|
||||
<#
|
||||
}
|
||||
#>
|
||||
});
|
||||
<#
|
||||
anyEntityTypeConfiguration = true;
|
||||
}
|
||||
#>
|
||||
});
|
||||
<#
|
||||
// If any signicant code was generated, append it to the main environment
|
||||
if (anyEntityTypeConfiguration)
|
||||
{
|
||||
mainEnvironment.Append(GenerationEnvironment);
|
||||
anyConfiguration = true;
|
||||
}
|
||||
|
||||
// Resume generating code into the main environment
|
||||
GenerationEnvironment = mainEnvironment;
|
||||
}
|
||||
|
||||
foreach (var sequence in Model.GetSequences())
|
||||
{
|
||||
var needsType = sequence.Type != typeof(long);
|
||||
var needsSchema = !string.IsNullOrEmpty(sequence.Schema) && sequence.Schema != sequence.Model.GetDefaultSchema();
|
||||
var sequenceFluentApiCalls = sequence.GetFluentApiCalls(annotationCodeGenerator);
|
||||
#>
|
||||
modelBuilder.HasSequence<#= needsType ? $"<{code.Reference(sequence.Type)}>" : "" #>(<#= code.Literal(sequence.Name) #><#= needsSchema ? $", {code.Literal(sequence.Schema)}" : "" #>)<#= code.Fragment(sequenceFluentApiCalls, indent: 3) #>;
|
||||
<#
|
||||
}
|
||||
|
||||
if (anyConfiguration)
|
||||
{
|
||||
WriteLine("");
|
||||
}
|
||||
#>
|
||||
OnModelCreatingPartial(modelBuilder);
|
||||
}
|
||||
|
||||
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
||||
}
|
||||
<#
|
||||
mainEnvironment = GenerationEnvironment;
|
||||
GenerationEnvironment = new StringBuilder();
|
||||
|
||||
foreach (var ns in usings.Distinct().OrderBy(x => x, new NamespaceComparer()))
|
||||
{
|
||||
#>
|
||||
using <#= ns #>;
|
||||
<#
|
||||
}
|
||||
|
||||
WriteLine("");
|
||||
|
||||
GenerationEnvironment.Append(mainEnvironment);
|
||||
#>
|
||||
|
|
@ -0,0 +1,173 @@
|
|||
<#@ template hostSpecific="true" #>
|
||||
<#@ assembly name="Microsoft.EntityFrameworkCore" #>
|
||||
<#@ assembly name="Microsoft.EntityFrameworkCore.Design" #>
|
||||
<#@ assembly name="Microsoft.EntityFrameworkCore.Relational" #>
|
||||
<#@ assembly name="Microsoft.Extensions.DependencyInjection.Abstractions" #>
|
||||
<#@ parameter name="EntityType" type="Microsoft.EntityFrameworkCore.Metadata.IEntityType" #>
|
||||
<#@ parameter name="Options" type="Microsoft.EntityFrameworkCore.Scaffolding.ModelCodeGenerationOptions" #>
|
||||
<#@ parameter name="NamespaceHint" type="System.String" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ import namespace="System.ComponentModel.DataAnnotations" #>
|
||||
<#@ import namespace="System.Linq" #>
|
||||
<#@ import namespace="System.Text" #>
|
||||
<#@ import namespace="Microsoft.EntityFrameworkCore" #>
|
||||
<#@ import namespace="Microsoft.EntityFrameworkCore.Design" #>
|
||||
<#@ import namespace="Microsoft.Extensions.DependencyInjection" #>
|
||||
<#
|
||||
if (EntityType.IsSimpleManyToManyJoinEntityType())
|
||||
{
|
||||
// Don't scaffold these
|
||||
return "";
|
||||
}
|
||||
|
||||
var services = (IServiceProvider)Host;
|
||||
var annotationCodeGenerator = services.GetRequiredService<IAnnotationCodeGenerator>();
|
||||
var code = services.GetRequiredService<ICSharpHelper>();
|
||||
|
||||
var usings = new List<string>
|
||||
{
|
||||
"System",
|
||||
"System.Collections.Generic"
|
||||
};
|
||||
|
||||
if (Options.UseDataAnnotations)
|
||||
{
|
||||
usings.Add("System.ComponentModel.DataAnnotations");
|
||||
usings.Add("System.ComponentModel.DataAnnotations.Schema");
|
||||
usings.Add("Microsoft.EntityFrameworkCore");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(NamespaceHint))
|
||||
{
|
||||
#>
|
||||
namespace <#= NamespaceHint #>;
|
||||
|
||||
<#
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(EntityType.GetComment()))
|
||||
{
|
||||
#>
|
||||
/// <summary>
|
||||
/// <#= code.XmlComment(EntityType.GetComment()) #>
|
||||
/// </summary>
|
||||
<#
|
||||
}
|
||||
|
||||
if (Options.UseDataAnnotations)
|
||||
{
|
||||
foreach (var dataAnnotation in EntityType.GetDataAnnotations(annotationCodeGenerator))
|
||||
{
|
||||
#>
|
||||
<#= code.Fragment(dataAnnotation) #>
|
||||
<#
|
||||
}
|
||||
}
|
||||
#>
|
||||
public partial class <#= EntityType.Name #>
|
||||
{
|
||||
<#
|
||||
var firstProperty = true;
|
||||
foreach (var property in EntityType.GetProperties().OrderBy(p => p.GetColumnOrder() ?? -1))
|
||||
{
|
||||
if (!firstProperty)
|
||||
{
|
||||
WriteLine("");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(property.GetComment()))
|
||||
{
|
||||
#>
|
||||
/// <summary>
|
||||
/// <#= code.XmlComment(property.GetComment(), indent: 1) #>
|
||||
/// </summary>
|
||||
<#
|
||||
}
|
||||
|
||||
if (Options.UseDataAnnotations)
|
||||
{
|
||||
var dataAnnotations = property.GetDataAnnotations(annotationCodeGenerator)
|
||||
.Where(a => !(a.Type == typeof(RequiredAttribute) && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType));
|
||||
foreach (var dataAnnotation in dataAnnotations)
|
||||
{
|
||||
#>
|
||||
<#= code.Fragment(dataAnnotation) #>
|
||||
<#
|
||||
}
|
||||
}
|
||||
|
||||
usings.AddRange(code.GetRequiredUsings(property.ClrType));
|
||||
|
||||
var needsNullable = Options.UseNullableReferenceTypes && property.IsNullable && !property.ClrType.IsValueType;
|
||||
var needsInitializer = Options.UseNullableReferenceTypes && !property.IsNullable && !property.ClrType.IsValueType;
|
||||
#>
|
||||
public <#= code.Reference(property.ClrType) #><#= needsNullable ? "?" : "" #> <#= property.Name #> { get; set; }<#= needsInitializer ? " = null!;" : "" #>
|
||||
<#
|
||||
firstProperty = false;
|
||||
}
|
||||
|
||||
foreach (var navigation in EntityType.GetNavigations())
|
||||
{
|
||||
WriteLine("");
|
||||
|
||||
if (Options.UseDataAnnotations)
|
||||
{
|
||||
foreach (var dataAnnotation in navigation.GetDataAnnotations(annotationCodeGenerator))
|
||||
{
|
||||
#>
|
||||
<#= code.Fragment(dataAnnotation) #>
|
||||
<#
|
||||
}
|
||||
}
|
||||
|
||||
var targetType = navigation.TargetEntityType.Name;
|
||||
if (navigation.IsCollection)
|
||||
{
|
||||
#>
|
||||
public virtual ICollection<<#= targetType #>> <#= navigation.Name #> { get; set; } = new List<<#= targetType #>>();
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
var needsNullable = Options.UseNullableReferenceTypes && !(navigation.ForeignKey.IsRequired && navigation.IsOnDependent);
|
||||
var needsInitializer = Options.UseNullableReferenceTypes && navigation.ForeignKey.IsRequired && navigation.IsOnDependent;
|
||||
#>
|
||||
public virtual <#= targetType #><#= needsNullable ? "?" : "" #> <#= navigation.Name #> { get; set; }<#= needsInitializer ? " = null!;" : "" #>
|
||||
<#
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var skipNavigation in EntityType.GetSkipNavigations())
|
||||
{
|
||||
WriteLine("");
|
||||
|
||||
if (Options.UseDataAnnotations)
|
||||
{
|
||||
foreach (var dataAnnotation in skipNavigation.GetDataAnnotations(annotationCodeGenerator))
|
||||
{
|
||||
#>
|
||||
<#= code.Fragment(dataAnnotation) #>
|
||||
<#
|
||||
}
|
||||
}
|
||||
#>
|
||||
public virtual ICollection<<#= skipNavigation.TargetEntityType.Name #>> <#= skipNavigation.Name #> { get; set; } = new List<<#= skipNavigation.TargetEntityType.Name #>>();
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
<#
|
||||
var previousOutput = GenerationEnvironment;
|
||||
GenerationEnvironment = new StringBuilder();
|
||||
|
||||
foreach (var ns in usings.Distinct().OrderBy(x => x, new NamespaceComparer()))
|
||||
{
|
||||
#>
|
||||
using <#= ns #>;
|
||||
<#
|
||||
}
|
||||
|
||||
WriteLine("");
|
||||
|
||||
GenerationEnvironment.Append(previousOutput);
|
||||
#>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace HuanMeng.StableDiffusion.TextGeneration.Models;
|
||||
|
||||
public partial class TextGenerationSession
|
||||
{
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户Id
|
||||
/// </summary>
|
||||
public string UserId { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 绘画Id
|
||||
/// </summary>
|
||||
public string SessionId { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 绘画名称
|
||||
/// </summary>
|
||||
public string SessionName { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
public DateTime CreateDateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 最后一次请求时间
|
||||
/// </summary>
|
||||
public DateTime LastDateTime { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace HuanMeng.StableDiffusion.TextGeneration.Models;
|
||||
|
||||
public partial class TextGenerationSessionDetail
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 会话id
|
||||
/// </summary>
|
||||
public int TextGenerationSessionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 会话Id
|
||||
/// </summary>
|
||||
public string SessionId { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 角色
|
||||
/// </summary>
|
||||
public string Role { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 消息内容
|
||||
/// </summary>
|
||||
public string? Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模型
|
||||
/// </summary>
|
||||
public string? Model { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 时间戳
|
||||
/// </summary>
|
||||
public long TimeStamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
public DateTime CreateDateTime { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HuanMeng.StableDiffusion.TextGeneration.Models;
|
||||
|
||||
public partial class TextGenerationTestContext : DbContext
|
||||
{
|
||||
public TextGenerationTestContext()
|
||||
{
|
||||
}
|
||||
|
||||
public TextGenerationTestContext(DbContextOptions<TextGenerationTestContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual DbSet<TextGenerationSession> TextGenerationSessions { get; set; }
|
||||
|
||||
public virtual DbSet<TextGenerationSessionDetail> TextGenerationSessionDetails { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
|
||||
=> optionsBuilder.UseSqlServer("Server=172.27.27.12;Database=TextGenerationTest;User Id=zpc;Password=zpc;TrustServerCertificate=true;");
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<TextGenerationSession>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.Id).HasName("PK_TextGenerationSession'");
|
||||
|
||||
entity.ToTable("TextGenerationSession");
|
||||
|
||||
entity.Property(e => e.Id).HasComment("主键");
|
||||
entity.Property(e => e.CreateDateTime)
|
||||
.HasComment("创建时间")
|
||||
.HasColumnType("datetime");
|
||||
entity.Property(e => e.LastDateTime)
|
||||
.HasComment("最后一次请求时间")
|
||||
.HasColumnType("datetime");
|
||||
entity.Property(e => e.SessionId)
|
||||
.HasMaxLength(32)
|
||||
.IsUnicode(false)
|
||||
.HasComment("绘画Id");
|
||||
entity.Property(e => e.SessionName)
|
||||
.HasMaxLength(100)
|
||||
.HasComment("绘画名称");
|
||||
entity.Property(e => e.UserId)
|
||||
.HasMaxLength(32)
|
||||
.IsUnicode(false)
|
||||
.HasComment("用户Id");
|
||||
});
|
||||
|
||||
modelBuilder.Entity<TextGenerationSessionDetail>(entity =>
|
||||
{
|
||||
entity.Property(e => e.CreateDateTime)
|
||||
.HasComment("创建时间")
|
||||
.HasColumnType("datetime");
|
||||
entity.Property(e => e.Message)
|
||||
.HasMaxLength(1000)
|
||||
.HasComment("消息内容");
|
||||
entity.Property(e => e.Model)
|
||||
.HasMaxLength(100)
|
||||
.HasComment("模型");
|
||||
entity.Property(e => e.Role)
|
||||
.HasMaxLength(50)
|
||||
.HasComment("角色");
|
||||
entity.Property(e => e.SessionId)
|
||||
.HasMaxLength(32)
|
||||
.IsUnicode(false)
|
||||
.HasComment("会话Id");
|
||||
entity.Property(e => e.TextGenerationSessionId).HasComment("会话id");
|
||||
entity.Property(e => e.TimeStamp).HasComment("时间戳");
|
||||
});
|
||||
|
||||
OnModelCreatingPartial(modelBuilder);
|
||||
}
|
||||
|
||||
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
||||
}
|
||||
|
|
@ -0,0 +1,104 @@
|
|||
using HuanMeng.StableDiffusion.TextGeneration.Abstractions;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using System;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
|
||||
namespace HuanMeng.StableDiffusion.TextGeneration.api
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class TextGenerationRequestHttpApi : TextGenerationRequestAbstract
|
||||
{
|
||||
public string Url = "http://117.50.182.144:5000/v1/chat/completions";
|
||||
string jsonContent = "{\"messages\":[{\"role\":\"user\",\"content\":\"你好?\"}],\"stream\":true}";
|
||||
string analysisStr = "data:";
|
||||
/// <summary>
|
||||
/// 请求内容
|
||||
/// </summary>
|
||||
public TextGenerationRequestHttpModel textGenerationRequestHttpModel { get; set; }
|
||||
public TextGenerationRequestHttpApi(string jsonContent)
|
||||
{
|
||||
this.jsonContent = jsonContent;
|
||||
}
|
||||
public override async IAsyncEnumerable<string> SendMessageAsync()
|
||||
{
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
// 设置请求头
|
||||
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
//httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");
|
||||
|
||||
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
||||
// 发送请求
|
||||
using (var response = await httpClient.PostAsync(Url, content))
|
||||
{
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 获取响应流
|
||||
using (var stream = await response.Content.ReadAsStreamAsync())
|
||||
using (var reader = new System.IO.StreamReader(stream))
|
||||
{
|
||||
string textContent = "";
|
||||
// 持续读取流中的事件
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
var line = await reader.ReadLineAsync();
|
||||
if (!string.IsNullOrEmpty(line))
|
||||
{
|
||||
var index = line.IndexOf(analysisStr);
|
||||
if (index > -1)
|
||||
{
|
||||
line = line.Substring(index + analysisStr.Length).Trim();
|
||||
}
|
||||
if (!string.IsNullOrEmpty(line))
|
||||
{
|
||||
var linex = JsonConvert.DeserializeObject<TextGenerationRequestHttpModel>(line);
|
||||
if (linex != null)
|
||||
{
|
||||
textGenerationRequestHttpModel = linex;
|
||||
textContent += linex?.Choices?[0].Delta?.Content ?? "";
|
||||
yield return linex?.Choices?[0].Delta?.Content ?? "";
|
||||
}
|
||||
}
|
||||
//yield return line;
|
||||
}
|
||||
}
|
||||
if (textGenerationRequestHttpModel != null)
|
||||
{
|
||||
if (textGenerationRequestHttpModel?.Choices?.Length == 0)
|
||||
{
|
||||
textGenerationRequestHttpModel.Choices = new Choice[1] { new Choice() { Delta = new Delta() } };
|
||||
}
|
||||
textGenerationRequestHttpModel.Choices[0].Delta.Content = textContent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public string DecodeUnicodeString(string input)
|
||||
{
|
||||
// 使用正则表达式匹配 \u 后面跟随的 4 个十六进制数字
|
||||
return Regex.Replace(input, @"\\u([0-9A-Fa-f]{4})", m =>
|
||||
{
|
||||
return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.StableDiffusion.TextGeneration.api
|
||||
{
|
||||
/// <summary>
|
||||
/// 消息内容
|
||||
/// </summary>
|
||||
public class TextGenerationRequestHttpApiModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 角色
|
||||
/// </summary>
|
||||
public string Role { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 控制器
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.StableDiffusion.TextGeneration.api
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 返回内容
|
||||
/// </summary>
|
||||
public class TextGenerationRequestHttpModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 文本生成请求的唯一标识符。
|
||||
/// </summary>
|
||||
[JsonPropertyName("id")]
|
||||
public string? Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 返回的对象类型(例如,"text_completion")。
|
||||
/// </summary>
|
||||
[JsonPropertyName("object")]
|
||||
public string? Object { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 请求创建的时间戳。
|
||||
/// </summary>
|
||||
[JsonPropertyName("created")]
|
||||
public long? Created { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用于文本生成的模型。
|
||||
/// </summary>
|
||||
[JsonPropertyName("model")]
|
||||
public string? Model { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 包含生成文本和相关信息的选项数组。
|
||||
/// </summary>
|
||||
[JsonPropertyName("choices")]
|
||||
public Choice[]? Choices { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 请求的使用统计信息,包括令牌计数。
|
||||
/// </summary>
|
||||
[JsonPropertyName("usage")]
|
||||
public Usage? Usage { get; set; }
|
||||
}
|
||||
|
||||
public class Usage
|
||||
{
|
||||
/// <summary>
|
||||
/// 提示中使用的令牌数。
|
||||
/// </summary>
|
||||
[JsonPropertyName("prompt_tokens")]
|
||||
public int? PromptTokens { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 生成的完成内容中的令牌数。
|
||||
/// </summary>
|
||||
[JsonPropertyName("completion_tokens")]
|
||||
public int? CompletionTokens { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 使用的令牌总数(提示 + 完成)。
|
||||
/// </summary>
|
||||
[JsonPropertyName("total_tokens")]
|
||||
public int? TotalTokens { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 消息内容
|
||||
/// </summary>
|
||||
public class Choice
|
||||
{
|
||||
/// <summary>
|
||||
/// 返回列表中选项的索引。
|
||||
/// </summary>
|
||||
[JsonPropertyName("index")]
|
||||
public int? Index { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 完成的原因(例如,"length"、"stop")。
|
||||
/// </summary>
|
||||
[JsonPropertyName("finish_reason")]
|
||||
public string? FinishReason { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 包含生成文本的角色和内容的 delta 对象。
|
||||
/// </summary>
|
||||
[JsonPropertyName("delta")]
|
||||
public Delta? Delta { get; set; }
|
||||
}
|
||||
|
||||
public class Delta
|
||||
{
|
||||
/// <summary>
|
||||
/// 消息发送者的角色(例如,"user"、"system")。
|
||||
/// </summary>
|
||||
[JsonPropertyName("role")]
|
||||
public string? Role { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 生成的内容。
|
||||
/// </summary>
|
||||
[JsonPropertyName("content")]
|
||||
public string? Content { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
7
src/0-core/HuanMeng.StableDiffusion.WebSocket/Class1.cs
Normal file
7
src/0-core/HuanMeng.StableDiffusion.WebSocket/Class1.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace HuanMeng.StableDiffusion.WebSocket
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.5" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
@HuanMeng.StableDiffusion.WebSocket_HostAddress = http://localhost:5077
|
||||
|
||||
GET {{HuanMeng.StableDiffusion.WebSocket_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
39
src/1-service/HuanMeng.StableDiffusion.WebSocket/Program.cs
Normal file
39
src/1-service/HuanMeng.StableDiffusion.WebSocket/Program.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
|
||||
namespace HuanMeng.StableDiffusion.WebSocket
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
app.MapGet("/", (HttpContext httpContext) =>
|
||||
{
|
||||
return "请求成功";
|
||||
})
|
||||
.WithName("GetWeatherForecast")
|
||||
.WithOpenApi();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:19623",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5077",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
namespace HuanMeng.StableDiffusion.WebSocket
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
using HuanMeng.StableDiffusion.TextGeneration.BLL;
|
||||
using HuanMeng.StableDiffusion.TextGeneration.Models;
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace TextGenerationApi.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class TextGenerationApiController(TextGenerationTestContext textGenerationTestContext) : ControllerBase
|
||||
{
|
||||
[HttpPost]
|
||||
/// <summary>
|
||||
/// 聊天接口
|
||||
/// </summary>
|
||||
/// <param name="sessionId"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Chat(string sessionId, string message)
|
||||
{
|
||||
var outputStream = this.Response.Body;
|
||||
TextGenerationBLL textGenerationBLL = new TextGenerationBLL(textGenerationTestContext);
|
||||
await foreach (var item in textGenerationBLL.Chat(sessionId, message))
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
await outputStream.WriteAsync(Encoding.UTF8.GetBytes(item));
|
||||
await outputStream.FlushAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace TextGenerationApi.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class WeatherForecastController : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet(Name = "GetWeatherForecast")]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
60
src/2-api/TextGenerationApi/Program.cs
Normal file
60
src/2-api/TextGenerationApi/Program.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
using HuanMeng.StableDiffusion.TextGeneration.Models;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddHttpContextAccessor(); //添加httpContext注入访问
|
||||
builder.Services.AddControllers();
|
||||
|
||||
//注入数据库
|
||||
builder.Services.AddDbContext<TextGenerationTestContext>((options) =>
|
||||
{
|
||||
options.UseSqlServer(builder.Configuration.GetConnectionString("TextGenerationTest"));
|
||||
});
|
||||
|
||||
//添加跨域设置
|
||||
var _myAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
options.AddPolicy(_myAllowSpecificOrigins,
|
||||
builder =>
|
||||
{
|
||||
|
||||
builder.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyOrigin();// 许来自任意源的跨域请求
|
||||
}));
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c =>
|
||||
{
|
||||
|
||||
c.EnableDeepLinking();
|
||||
c.DefaultModelsExpandDepth(3);
|
||||
c.DefaultModelExpandDepth(3);
|
||||
c.EnableFilter("true");
|
||||
//c.RoutePrefix = "swagger";
|
||||
//c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API V1");
|
||||
});
|
||||
//}
|
||||
}
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
//使用跨域
|
||||
app.UseCors(_myAllowSpecificOrigins);
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
41
src/2-api/TextGenerationApi/Properties/launchSettings.json
Normal file
41
src/2-api/TextGenerationApi/Properties/launchSettings.json
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:54212",
|
||||
"sslPort": 44383
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5210",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7095;http://localhost:5210",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
src/2-api/TextGenerationApi/TextGenerationApi.csproj
Normal file
32
src/2-api/TextGenerationApi/TextGenerationApi.csproj
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\0-core\HuanMeng.DotNetCore\HuanMeng.DotNetCore.csproj" />
|
||||
<ProjectReference Include="..\..\0-core\HuanMeng.StableDiffusion.TextGeneration\HuanMeng.StableDiffusion.TextGeneration.csproj" />
|
||||
<ProjectReference Include="..\..\4-abstractions\HuanMeng.StableDiffusion.Abstractions\HuanMeng.StableDiffusion.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
6
src/2-api/TextGenerationApi/TextGenerationApi.http
Normal file
6
src/2-api/TextGenerationApi/TextGenerationApi.http
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
@TextGenerationApi_HostAddress = http://localhost:5210
|
||||
|
||||
GET {{TextGenerationApi_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
13
src/2-api/TextGenerationApi/WeatherForecast.cs
Normal file
13
src/2-api/TextGenerationApi/WeatherForecast.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
namespace TextGenerationApi
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
||||
8
src/2-api/TextGenerationApi/appsettings.Development.json
Normal file
8
src/2-api/TextGenerationApi/appsettings.Development.json
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
32
src/2-api/TextGenerationApi/appsettings.json
Normal file
32
src/2-api/TextGenerationApi/appsettings.json
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"ConnectionStrings": {
|
||||
"TextGenerationTest": "Server=172.27.27.12;Database=TextGenerationTest;User Id=zpc;Password=zpc;TrustServerCertificate=true;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
"Url": "http://*:800"
|
||||
}
|
||||
//"HttpsInlineCertFile": {
|
||||
// "Url": "https://*:443",
|
||||
// "Certificate": {
|
||||
// "Path": "", //证书地址
|
||||
// "Password": "" //证书密码
|
||||
// }
|
||||
//}
|
||||
}
|
||||
//"Certificates": {
|
||||
// "Default": {
|
||||
// "Path": "", //证书地址
|
||||
// "Password": "" //证书密码
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.StableDiffusion.Abstractions.PlugIn
|
||||
{
|
||||
public interface IStableDiffusionGenerateImage
|
||||
{
|
||||
/// <summary>
|
||||
/// 文生图
|
||||
/// </summary>
|
||||
public Task TextToImage();
|
||||
|
||||
/// <summary>
|
||||
/// 图生图
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task ImageToImage();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.StableDiffusion.Abstractions
|
||||
{
|
||||
/// <summary>
|
||||
/// StableDiffusion请求类
|
||||
/// </summary>
|
||||
public abstract class StableDiffusionRequestAbstract
|
||||
{
|
||||
/// <summary>
|
||||
/// 会话标识
|
||||
/// </summary>
|
||||
protected string SessionHash { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public StableDiffusionRequestAbstract()
|
||||
{
|
||||
this.SessionHash = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// StableDiffusion请求类
|
||||
/// </summary>
|
||||
/// <param name="sessionHash"></param>
|
||||
public StableDiffusionRequestAbstract(string sessionHash)
|
||||
{
|
||||
this.SessionHash = sessionHash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="requestUrl">请求地址</param>
|
||||
/// <param name="fn_index">编号</param>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="session_hash">session编号</param>
|
||||
/// <returns></returns>
|
||||
public abstract Task<string> SendMessageAsync(string requestUrl, int fn_index, List<object> data);
|
||||
|
||||
/// <summary>
|
||||
/// 发送自定义消息
|
||||
/// </summary>
|
||||
/// <param name="requestUrl"></param>
|
||||
/// <param name="fromdata"></param>
|
||||
/// <returns></returns>
|
||||
public abstract Task<string> SendMessageAsync(string requestUrl, string jsonData);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\0-core\HuanMeng.StableDiffusion.TextGeneration\HuanMeng.StableDiffusion.TextGeneration.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.StableDiffusion.TextGeneration.api.Tests
|
||||
{
|
||||
[TestClass()]
|
||||
public class TextGenerationRequestHttpApiTests
|
||||
{
|
||||
[TestMethod()]
|
||||
public async Task SendMessageAsyncTest()
|
||||
{
|
||||
TextGenerationRequestHttpApi textGenerationRequestHttpApi = new TextGenerationRequestHttpApi();
|
||||
await foreach (var item in textGenerationRequestHttpApi.SendMessageAsync())
|
||||
{
|
||||
Console.WriteLine(item);
|
||||
}
|
||||
Console.WriteLine(textGenerationRequestHttpApi.textGenerationRequestHttpModel.Choices[0].Delta.Content);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user