C# 上传票据文件 调用Dify的API完成OCR识别

为了验证Dify对票据识别的正确率,博主开发了一个批量调用Dify API 完成OCR识别工具,在RPA项目上测试样本数据识别的正确率。只需要点一下按钮,程序就放出10次请求,然后把AI智能体OCR识别的结果全部返回。感谢zoujiawei提供的DifyWebClient类库,我们只需要直接调用就行,不过还是有一些地方需要博主说明一下:

1、Dify 调用API上传文件,还是老问题,如果设置了多个文件,你需要给数组,如果单个文件,你不能给数组,否则接口调用会报错。DifyWebClient类库提供的是多个文件功能,所以Dify中也需要设置成多个文件。

2、DifyWebClient类库,需要在线程中调用,比如winform中使用,你会一直卡在那儿,但如果放线程中,使用就正常了。

网上C# 调用Dify API的代码居然很少,我来提供一篇吧。WinForm .net8

cs 复制代码
using DifyWebClient.Net;
using DifyWebClient.Net.ApiClients;
using DifyWebClient.Net.Enum;
using DifyWebClient.Net.Models.Base;
using DifyWebClient.Net.Models.Knowledge;
using DifyWebClient.Net.Models.WorkflowApp;

namespace DifyUpload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < 10; i++)
            {
                Task task = Task.Run(() => Thread1());
            }

        }

        void Thread1()
        {

            //上传需要识别的图片
            WorkflowAppApiClient workflowAppApiClient = new WorkflowAppApiClient("https://agent.cloud-uat.XXXXX.cn/v1", "app-65i3syVDIQ6U3op3mVdKEXXX");
            FileUploadResponse fileUploadResponse = workflowAppApiClient.FileUpload(FileToBinaryConverter.ConvertFileToBinary("d:\\527800.png"), "527800.png");
            // ps(listBox1, fileUploadResponse.id);

            //准备调用工作流的参数,注意:智能体的文件输入必须是多个文件,设置成单文件会报错!
            Dictionary<string, object> inputkeyValuePairs = new Dictionary<string, object>();
            List<FileListElement> fileListElements = new List<FileListElement>();
            fileListElements.Add(new FileListElement(upload_file_id: fileUploadResponse.id, "image", "local_file", null));
            inputkeyValuePairs.Add("input_file", fileListElements); // input_file 是自己在dify中的命名参数

            //调用工作流
            ExecuteWorkflowRequest executeWorkflowRequest = new ExecuteWorkflowRequest(inputkeyValuePairs, user: "abc-123", ResponseMode.Blocking);
            CompletionResponse completionResponse = workflowAppApiClient.ExecuteWorkflow(executeWorkflowRequest);

            //  ps(listBox1, completionResponse.RealJsonstring);
            //   textBox1.Text = (completionResponse.data.outputs["text"].ToString());
            // Console.WriteLine(completionResponse.RealJsonstring);
            // ps(listBox1, completionResponse.data.outputs["text"].ToString());
            ps(listBox1, completionResponse.data.outputs["text"].ToString());
        }

        public void ps(ListBox box, string s)
        {
            String line = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + s;
            box.Items.Add(line);
        }
    }
}

----------2025.10.23--------------

为了批量调用Dify 完成OCR识别后,校对识别的内容,然后去不断优化修改提示词,工具升级了自动校对全部返回文本的功能。

相关推荐
黑咩狗夜.cm19 小时前
Aspose.word实现表格每页固定表头、最后一行填满整个页面
开发语言·c#·word
code bean20 小时前
【C#笔记】Newtonsoft.Json 中 `[JsonIgnore]` 的作用详解
笔记·c#·json
ccut 第一混20 小时前
用c# 制作一个扑克牌小游戏
开发语言·c#
IT老大哥21 小时前
局域网扫码枪/局域网二维码接收工具
c#·net
自由的好好干活21 小时前
C#桌面框架与Qt对比及选型(国产操作系统开发视角)
开发语言·qt·c#
The Sheep 20231 天前
MicroService(Redis)
数据库·redis·c#
CodeCraft Studio1 天前
Excel处理控件Aspose.Cells教程:如何使用C#在Excel中添加、编辑和更新切片器
ui·c#·excel·aspose·excel切片器·创建表格切片器
.NET修仙日记1 天前
第四章:C# 面向对象编程详解:从类与对象到完整项目实践
开发语言·c#·.net·源码·教程·.net core
AI人工智能+1 天前
复杂版式下的关键信息抽取:机动车登记证的视觉识别与结构化理解
人工智能·ocr·机动车登记证识别
旗讯数字1 天前
传统生产制造企业手写单据数字化落地:旗讯 OCR 的技术实现与系统对接方案
ocr·制造