使用PocketFlowSharp创建一个Human_Evaluation示例

效果

image-20250516142423902
image-20250516142423902
image-20250516142438960
image-20250516142438960

实践

有时候AI生成的结果我们并不满意在进入下一步之前,我们需要对AI生成的结果进行人工审核,同意了才能进入下一个流程。

Human_Evaluation就是人工判断的一个简单示例。

internal classProgram
 {
     static async Task Main(string[] args)
     {
         // Load .env file
         DotEnv.Load();

         // Get environment variables from .env file
         var envVars = DotEnv.Read();

         string ModelName = envVars["ModelName"];
         string EndPoint = envVars["EndPoint"];
         string ApiKey = envVars["ApiKey"];

         Utils.ModelName = ModelName;
         Utils.EndPoint = EndPoint;
         Utils.ApiKey = ApiKey;

         // 创建共享数据字典
         var shared = new Dictionary<string, object>();

         // 创建并运行流程
         var humanEvalFlow = CreateFlow();
         Console.WriteLine("\n欢迎使用人工判断示例!");
         Console.WriteLine("------------------------");
         await humanEvalFlow.RunAsync(shared);
         Console.WriteLine("\n感谢使用人工判断示例!");
     }

     static AsyncFlow CreateFlow()
     {
         // 创建节点实例
         var inputNode = new TaskInputNode();
         var aiResponseNode = new AIResponseNode();
         var humanApprovalNode = new HumanApprovalNode();
         var endNode = new NoOpNode();

         // 创建从输入节点开始的流程
         var flow = new AsyncFlow(inputNode);

         // 连接节点
         _ = inputNode - "generate" - aiResponseNode;
         _ = aiResponseNode - "approve" - humanApprovalNode;
         _ = humanApprovalNode - "retry" - aiResponseNode;     // 不接受时重新生成
         _ = humanApprovalNode - "accept" - endNode;          // 接受时结束流程

         return flow;
     }
 }

看一下整体的流程图:

graph TD
    A[输入节点] -->|"generate"| B[AI回复节点]
    B -->|"approve"| C[人工审核节点]
    C -->|"approve"| D[结束节点]
    C -->|"retry"| B
image-20250516143406016
image-20250516143406016

输入节点:

public classTaskInputNode : AsyncNode
 {
     protected override async Task<object> PrepAsync(Dictionary<string, object> shared)
     {
         Console.WriteLine("\n请输入需要AI处理的任务:");
         string task = Console.ReadLine();
         return task;
     }

     protected override async Task<object> ExecAsync(object prepResult)
     {
         string task = (string)prepResult;
         Console.WriteLine($"\n已收到任务:{task}");
         return task;
     }

     protected override async Task<object> PostAsync(Dictionary<string, object> shared, object prepResult, object execResult)
     {
         string task = (string)execResult;
         shared["task"] = task;
         return"generate";
     }
 }

AI回复节点:

public classAIResponseNode : AsyncNode
{
    privatestaticint attemptCount = 0;

    protected override async Task<object> PrepAsync(Dictionary<string, object> shared)
    {
        return shared["task"];
    }

    protected override async Task<object> ExecAsync(object prepResult)
    {
        string task = (string)prepResult;
        attemptCount++;
        
        Console.WriteLine("AI正在生成回复...\n");
        Console.WriteLine($"任务:{task}\n");
        Console.WriteLine($"这是第 {attemptCount} 次生成的AI回复:\n");
        var result = await Utils.CallLLMStreamingAsync(task);

        string response="";
        Console.ForegroundColor = ConsoleColor.Green;
        awaitforeach (StreamingChatCompletionUpdate completionUpdate in result)
        {
            if (completionUpdate.ContentUpdate.Count > 0)
            {
                Console.Write(completionUpdate.ContentUpdate[0].Text);
                response += completionUpdate.ContentUpdate[0].Text.ToString();
            }
        }
        Console.ForegroundColor = ConsoleColor.White;

        return response;
    }

    protected override async Task<object> PostAsync(Dictionary<string, object> shared, object prepResult, object execResult)
    {
        string response = (string)execResult;
        shared["response"] = response;
        return"approve";
    }
}

人工审核节点:

public classHumanApprovalNode : AsyncNode
 {
     protected override async Task<object> PrepAsync(Dictionary<string, object> shared)
     {
         return shared["response"];
     }

     protected override async Task<object> ExecAsync(object prepResult)
     {
         Console.Write("\n您接受这个AI回复吗?(y/n): ");
         string answer = Console.ReadLine()?.ToLower() ?? "n";
         return answer;
     }

     protected override async Task<object> PostAsync(Dictionary<string, object> shared, object prepResult, object execResult)
     {
         string answer = (string)execResult;

         if (answer == "y")
         {
             Console.WriteLine($"已接受的回复:\n{shared["response"]}");
             return"accept";
         }
         else
         {
             Console.WriteLine("\n好的,让AI重新生成回复...");
             return"retry";
         }
     }
 }

结束节点:

public class NoOpNode : AsyncNode
 {
     protected override async Task<object> PrepAsync(Dictionary<string, object> shared) => null;
     protected override async Task<object> ExecAsync(object prepResult) => null;
     protected override async Task<object> PostAsync(Dictionary<string, object> shared, object prepResult, object execResult) => null;
 }

帮助类:

public staticclassUtils
 {
     publicstaticstring ModelName { get; set; }
     publicstaticstring EndPoint { get; set; }
     publicstaticstring ApiKey { get; set; }

     public static async Task<string> CallLLMAsync(string prompt)
     {
         ApiKeyCredential apiKeyCredential = new ApiKeyCredential(ApiKey);

         OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
         openAIClientOptions.Endpoint = new Uri(EndPoint);

         ChatClient client = new(model: ModelName, apiKeyCredential, openAIClientOptions);

         ChatCompletion completion = await client.CompleteChatAsync(prompt);

         return completion.Content[0].Text;
     }

     publicstaticasync Task<AsyncCollectionResult<StreamingChatCompletionUpdate>> CallLLMStreamingAsync(string prompt)
     {
         ApiKeyCredential apiKeyCredential = new ApiKeyCredential(ApiKey);

         OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
         openAIClientOptions.Endpoint = new Uri(EndPoint);

         ChatClient client = new(model: ModelName, apiKeyCredential, openAIClientOptions);

         var completion = client.CompleteChatStreamingAsync(prompt);
        
         return completion;
     }
 }
### 回答1: human_segmentation_pphumanseg是一种人体分割技术,它是基于PaddlePaddle深度学习框架开发的。 人体分割是计算机视觉领域的重要任务,它旨在将图像中的人体和背景分离开来。human_segmentation_pphumanseg通过训练深度神经网络,能够准确地识别图像中的人体轮廓,并生成一个遮罩。这个遮罩可以用来抠出人体部分,从而实现人体与背景的分离。 human_segmentation_pphumanseg具有以下优点: 1. 高准确性:human_segmentation_pphumanseg经过大量数据的训练和优化,能够高精度地识别人体轮廓,准确地分离人体和背景。 2. 高效性:human_segmentation_pphumanseg利用深度学习算法,可以实时地对图像进行人体分割,快速生成人体遮罩。 3. 可扩展性:human_segmentation_pphumanseg基于PaddlePaddle框架开发,具有良好的可扩展性,可以应用于各种计算机视觉任务,如图像编辑、虚拟现实等。 4. 简单易用:human_segmentation_pphumanseg提供了简洁易懂的API接口,使用者只需输入图像,即可获得人体遮罩,无需了解复杂的深度学习知识。 human_segmentation_pphumanseg在许多场景中有广泛的应用,例如人像摄影、视频编辑、虚拟试衣等。未来,随着技术的不断发展,相信human_segmentation_pphumanseg会在计算机视觉领域发挥越来越重要的作用,为人体与背景的分离提供更加精确和高效的解决方案。 ### 回答2: human_segmentation_pphumanseg是一种人体分割模型,能够对图像或视频中的人体进行准确的分割。它基于深度学习技术,使用了先进的神经网络架构和训练方法。 通过human_segmentation_pphumanseg,我们可以将图像中的人体从背景中分离出来,得到一个带有透明背景的人体图像。这对于许多应用来说非常实用,比如虚拟背景替换、人像照片编辑、AR/VR应用等。 human_segmentation_pphumanseg的核心原理是基于全卷积网络(FCN),它能够对输入图像的每个像素进行分类。训练时,这个模型使用大量的带有标签的人体图像进行学习,使其能够准确地判断一个像素点是否属于人体。在测试时,我们只需要将待分割的图像输入模型,它会自动为我们完成人体分割任务。 该模型的优势在于准确性和鲁棒性。它能够处理各种场景下的人体分割,包括不同姿势、不同光照条件、复杂的背景等。此外,human_segmentation_pphumanseg的运行速度也相对较快,可以实时地对视频流进行人体分割。 总的来说,human_segmentation_pphumanseg是一种非常实用的人体分割模型。它的功能强大,准确度高,对于许多应用领域都有广泛的应用前景。无论是对于个人用户还是商业用户,都能够通过该模型实现精确的人体分割,提高工作效率和用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值