54 lines
2.0 KiB
C#
54 lines
2.0 KiB
C#
using Flurl;
|
||
using Flurl.Http;
|
||
using ModelContextProtocol.Server;
|
||
using System.ComponentModel;
|
||
|
||
namespace lazy52API.Tool
|
||
{
|
||
[McpServerToolType]
|
||
public static class Painting
|
||
{
|
||
/// <summary>
|
||
/// 调用豆包绘图接口
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[McpServerTool(Name = "DouBaoPainting"), Description("调用豆包绘图接口;输出image_url数组(一般是四个)")]
|
||
public static string DouBao(
|
||
[Description("要生成图片的描述")] string description,
|
||
[Description("绘画风格")] string type,
|
||
[Description("绘画比例,可选9:16,2:3,3:4,4:3,1:1,3:2,16:9。不填默认16:9")] string ratio = "16:9"
|
||
)
|
||
{
|
||
Console.WriteLine($"接收到绘图任务:{description}");
|
||
var Painting = new
|
||
{
|
||
description,
|
||
type,
|
||
ratio
|
||
};
|
||
var filteredPainting = new System.Collections.Generic.Dictionary<string, object>();
|
||
foreach (var prop in Painting.GetType().GetProperties())
|
||
{
|
||
var value = prop.GetValue(Painting);
|
||
if (value != null)
|
||
{
|
||
filteredPainting[prop.Name] = value;
|
||
}
|
||
}
|
||
var request = new FlurlRequest("https://npi.lazy52.com/api/doubao")
|
||
.WithTimeout(Timeout.InfiniteTimeSpan);
|
||
var response = request
|
||
.SetQueryParams(filteredPainting).GetAsync().GetAwaiter().GetResult();
|
||
if (response != null)
|
||
{
|
||
var responseString = response.GetStringAsync().GetAwaiter().GetResult();
|
||
//JToken rootToken = JToken.Parse(responseString);
|
||
//var resultPath = "$.image_url[*]";
|
||
//IEnumerable<JToken> resultTokens = rootToken.SelectTokens(resultPath);
|
||
return responseString;
|
||
}
|
||
return "";
|
||
}
|
||
}
|
||
}
|