找回密码
 立即注册
首页 业界区 业界 使用PocketFlowSharp创建一个Human_Evaluation示例 ...

使用PocketFlowSharp创建一个Human_Evaluation示例

毋峻舷 2025-6-2 23:44:16
效果

1.gif

2.png

3.png

实践

有时候AI生成的结果我们并不满意在进入下一步之前,我们需要对AI生成的结果进行人工审核,同意了才能进入下一个流程。
Human_Evaluation就是人工判断的一个简单示例。
  1. internal class Program
  2. {
  3.      static async Task Main(string[] args)
  4.      {
  5.          // Load .env file
  6.          DotEnv.Load();
  7.          // Get environment variables from .env file
  8.          var envVars = DotEnv.Read();
  9.          string ModelName = envVars["ModelName"];
  10.          string EndPoint = envVars["EndPoint"];
  11.          string ApiKey = envVars["ApiKey"];
  12.          Utils.ModelName = ModelName;
  13.          Utils.EndPoint = EndPoint;
  14.          Utils.ApiKey = ApiKey;
  15.          // 创建共享数据字典
  16.          var shared = new Dictionary<string, object>();
  17.          // 创建并运行流程
  18.          var humanEvalFlow = CreateFlow();
  19.          Console.WriteLine("\n欢迎使用人工判断示例!");
  20.          Console.WriteLine("------------------------");
  21.          await humanEvalFlow.RunAsync(shared);
  22.          Console.WriteLine("\n感谢使用人工判断示例!");
  23.      }
  24.      static AsyncFlow CreateFlow()
  25.      {
  26.          // 创建节点实例
  27.          var inputNode = new TaskInputNode();
  28.          var aiResponseNode = new AIResponseNode();
  29.          var humanApprovalNode = new HumanApprovalNode();
  30.          var endNode = new NoOpNode();
  31.          // 创建从输入节点开始的流程
  32.          var flow = new AsyncFlow(inputNode);
  33.          // 连接节点
  34.          _ = inputNode - "generate" - aiResponseNode;
  35.          _ = aiResponseNode - "approve" - humanApprovalNode;
  36.          _ = humanApprovalNode - "retry" - aiResponseNode;     // 不接受时重新生成
  37.          _ = humanApprovalNode - "accept" - endNode;          // 接受时结束流程
  38.          return flow;
  39.      }
  40. }
复制代码
看一下整体的流程图:
graph TD    A[输入节点] -->|"generate"| B[AI回复节点]    B -->|"approve"| C[人工审核节点]    C -->|"approve"| D[结束节点]    C -->|"retry"| B输入节点:
  1. public class TaskInputNode : AsyncNode
  2. {
  3.      protected override async Task<object> PrepAsync(Dictionary<string, object> shared)
  4.      {
  5.          Console.WriteLine("\n请输入需要AI处理的任务:");
  6.          string task = Console.ReadLine();
  7.          return task;
  8.      }
  9.      protected override async Task<object> ExecAsync(object prepResult)
  10.      {
  11.          string task = (string)prepResult;
  12.          Console.WriteLine($"\n已收到任务:{task}");
  13.          return task;
  14.      }
  15.      protected override async Task<object> PostAsync(Dictionary<string, object> shared, object prepResult, object execResult)
  16.      {
  17.          string task = (string)execResult;
  18.          shared["task"] = task;
  19.          return "generate";
  20.      }
  21. }
复制代码
AI回复节点:
  1. public class AIResponseNode : AsyncNode
  2. {
  3.     private static int attemptCount = 0;
  4.     protected override async Task<object> PrepAsync(Dictionary<string, object> shared)
  5.     {
  6.         return shared["task"];
  7.     }
  8.     protected override async Task<object> ExecAsync(object prepResult)
  9.     {
  10.         string task = (string)prepResult;
  11.         attemptCount++;
  12.         
  13.         Console.WriteLine("AI正在生成回复...\n");
  14.         Console.WriteLine($"任务:{task}\n");
  15.         Console.WriteLine($"这是第 {attemptCount} 次生成的AI回复:\n");
  16.         var result = await Utils.CallLLMStreamingAsync(task);
  17.         string response="";
  18.         Console.ForegroundColor = ConsoleColor.Green;
  19.         await foreach (StreamingChatCompletionUpdate completionUpdate in result)
  20.         {
  21.             if (completionUpdate.ContentUpdate.Count > 0)
  22.             {
  23.                 Console.Write(completionUpdate.ContentUpdate[0].Text);
  24.                 response += completionUpdate.ContentUpdate[0].Text.ToString();
  25.             }
  26.         }
  27.         Console.ForegroundColor = ConsoleColor.White;
  28.         return response;
  29.     }
  30.     protected override async Task<object> PostAsync(Dictionary<string, object> shared, object prepResult, object execResult)
  31.     {
  32.         string response = (string)execResult;
  33.         shared["response"] = response;
  34.         return "approve";
  35.     }
  36. }
复制代码
人工审核节点:
  1. public class HumanApprovalNode : AsyncNode
  2. {
  3.      protected override async Task<object> PrepAsync(Dictionary<string, object> shared)
  4.      {
  5.          return shared["response"];
  6.      }
  7.      protected override async Task<object> ExecAsync(object prepResult)
  8.      {
  9.          Console.Write("\n您接受这个AI回复吗?(y/n): ");
  10.          string answer = Console.ReadLine()?.ToLower() ?? "n";
  11.          return answer;
  12.      }
  13.      protected override async Task<object> PostAsync(Dictionary<string, object> shared, object prepResult, object execResult)
  14.      {
  15.          string answer = (string)execResult;
  16.          if (answer == "y")
  17.          {
  18.              Console.WriteLine($"已接受的回复:\n{shared["response"]}");
  19.              return "accept";
  20.          }
  21.          else
  22.          {
  23.              Console.WriteLine("\n好的,让AI重新生成回复...");
  24.              return "retry";
  25.          }
  26.      }
  27. }
复制代码
结束节点:
  1. public class NoOpNode : AsyncNode
  2. {
  3.      protected override async Task<object> PrepAsync(Dictionary<string, object> shared) => null;
  4.      protected override async Task<object> ExecAsync(object prepResult) => null;
  5.      protected override async Task<object> PostAsync(Dictionary<string, object> shared, object prepResult, object execResult) => null;
  6. }
复制代码
帮助类:
  1. public static class Utils
  2. {
  3.      public static string ModelName { get; set; }
  4.      public static string EndPoint { get; set; }
  5.      public static string ApiKey { get; set; }
  6.      public static async Task<string> CallLLMAsync(string prompt)
  7.      {
  8.          ApiKeyCredential apiKeyCredential = new ApiKeyCredential(ApiKey);
  9.          OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
  10.          openAIClientOptions.Endpoint = new Uri(EndPoint);
  11.          ChatClient client = new(model: ModelName, apiKeyCredential, openAIClientOptions);
  12.          ChatCompletion completion = await client.CompleteChatAsync(prompt);
  13.          return completion.Content[0].Text;
  14.      }
  15.      public static async Task> CallLLMStreamingAsync(string prompt)
  16.      {
  17.          ApiKeyCredential apiKeyCredential = new ApiKeyCredential(ApiKey);
  18.          OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
  19.          openAIClientOptions.Endpoint = new Uri(EndPoint);
  20.          ChatClient client = new(model: ModelName, apiKeyCredential, openAIClientOptions);
  21.          var completion = client.CompleteChatStreamingAsync(prompt);
  22.         
  23.          return completion;
  24.      }
  25. }
复制代码
全部代码在:https://github.com/Ming-jiayou/PocketFlowSharp/tree/main/PocketFlowSharpSamples.Console/Human_Evaluation

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册