DeepSeek和ERP集成,自动生成单据

前段时间,DeepSeek作为国产人工智能大型语言模型,在全世界大火了一把。它的出现,很大程度上解决卡脖子的问题,追平美国OpenAI的ChatGPT大模型,甚至某些领域超越了ChatGPT。

如果体验过DeepSeek的同学,应该感受到了它的强大和便捷,但是有没有发现,如果只是单单使用DeepSeek的对话功能,它的作用好像也就只能和我们聊聊天,帮写写文案,分析下表格文件。

这些事情没有办法和我们使用的系统和工具集成起来,真正意义上帮我们减轻工作,更不用说帮我们自动完成工作。

那有没有办法呢?答案是有的!

我们可以引入AI Agent(智能工作体),AI Agent 可被定义为一种目标驱动的自主智能体,它通过 “感知→决策→行动” 的闭环流程来运作。在这个过程中,Agent 首先借助各类传感器对所处环境进行感知,收集相关信息;接着,依据内置的算法和策略对感知到的信息进行分析与决策,确定下一步行动方案;最后,通过执行器将决策转化为实际行动,作用于环境。

那这个AI Agent和LLM(大模型语言,如ChatGPT,DeepSeek)它们两是什么关系呢,下面我们来看一张图。

简单说,LLM是一个无所不知的大脑,而AI Agent就是躯体,LLM负责构想和理论指导,AI Agent就将这些构想和理论落实到具体智能场景中,替代人工,提升效率。

下面,我将使用微软提供的AI Agent框架SemanticKernel,使用DeepSeek大模型语言,来模拟实现在ERP系统中自动创建一个采购订单。

1.创建一个控制台项目

SemanticKernel只支持.net 6及以上版本,这里需要注意以下,然后我们在项目中引入Nuget包:

项目结果如下:

2.替换Program文件

DeepSeek通过api调用是收费的,我们需要在deepseek官网: https://www.deepseek.com上进行注册,获取我们的apikey

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Mozhi.Agent.DeepSeek.Plugins;
// Create a kernel with deepseek
var builder = Kernel.CreateBuilder();
Kernel kernel = builder.Build();
var chatCompletionService = new OpenAIChatCompletionService("deepseek-chat", new Uri("https://api.deepseek.com"), "deepseek给你的apikey");
// Add a plugin 
kernel.Plugins.AddFromType<ProductPlugin>("Products");
kernel.Plugins.AddFromType<SupplierPlugin>("Suppliers");
kernel.Plugins.AddFromType<PurchaseOrderPlugin>("PurchaseOrders");
// Enable planning
OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
{
    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};
// Create a history store the conversation
var history = new ChatHistory();
// Initiate a back-and-forth chat
string? userInput;
do
{
    // Collect user input
    Console.Write("User > ");
    userInput = Console.ReadLine();
    // Add user input
    history.AddUserMessage(userInput);
    // Get the response from the AI
    var result = await chatCompletionService.GetChatMessageContentAsync(
        history,
        executionSettings: openAIPromptExecutionSettings,
        kernel: kernel);
    // Print the results
    Console.WriteLine("Assistant > " + result);
    // Add the message from the agent to the chat history
    history.AddMessage(result.Role, result.Content ?? string.Empty);
} while (userInput is not null);

3. 编写Plugin

一个标准的采购订单,最基本的应该包括单据号,采购日期,供应商和若干条分录。

分录应该包含产品,价格,数量,金额的等字段。

所以我们定义三个Plugin

ProductPlugin:

定义了我们需要传给deepseek的产品数据,包括id,名称,价格信息,以及DeepSeek可以调用的函数方法。

using Microsoft.SemanticKernel;
using System.ComponentModel;
using System.Text.Json.Serialization;
namespace Mozhi.Agent.DeepSeek.Plugins
{
    public class ProductPlugin
    {
        private readonly List<Product> products = new()
        {
            new Product { Id = Guid.Parse("482565ec-8e27-fa3b-04a9-a0295e898a88"), Name = "哇哈哈", Price = 5.69M },
            new Product { Id = Guid.Parse("edf50f80-e02c-ab4d-1628-c88f5f2bbfab"), Name = "旺仔牛奶", Price = 3.50M },
        };
        [KernelFunction("get_products")]
        [Description("Gets a list of products and their price")]
        public async Task<List<Product>> GetProductsAsync()
        {
            return products;
        }
    }
    public class Product
    {
        [JsonPropertyName("id")]
        public Guid Id { get; set; }
        [JsonPropertyName("name")]
        public string Name { get; set; }
        [JsonPropertyName("price")]
        public decimal Price { get; set; }
    }
}

SupplierPlugin

定义供应商的数据以及DeepSeek可以调用的方法。

using Microsoft.SemanticKernel;
using System.ComponentModel;
using System.Text.Json.Serialization;
namespace Mozhi.Agent.DeepSeek.Plugins
{
    public class SupplierPlugin
    {
        private readonly List<Supplier> suppliers = new()
        {
            new Supplier { Id = Guid.Parse("00b7187e-fd0f-32e8-5568-bf86ffc9016a"), Name = "杭州哇哈哈饮料有限公司" },
            new Supplier { Id = Guid.Parse("fecb4326-f426-8fce-1e6a-7b166b3570a1"), Name = "长沙旺旺食品有限公司" },
        };
        [KernelFunction("get_suppliers")]
        [Description("Gets a list of supplier")]
        public async Task<List<Supplier>> GetSuppliersAsync()
        {
            return suppliers;
        }
    }
    public class Supplier
    {
        [JsonPropertyName("id")]
        public Guid Id { get; set; }
        [JsonPropertyName("name")]
        public string Name { get; set; }
    }
}

PurchaseOrderPlugin

定义了采购订单的基本模型和生成的方法,DeepSeek会根据我们的模型,生成一个Json的结果返回给我们。

从代码中可以看出,大模型生成的结果,其实并不能完全满足系统的需求,我们还是需要对数据进行一些加工处理。

using Microsoft.SemanticKernel;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Text.Json.Serialization;
namespace Mozhi.Agent.DeepSeek.Plugins
{
    public class PurchaseOrderPlugin
    {
        [KernelFunction("create_purchase_order")]
        [Description("create purchase order")]
        public async Task<PuchaseOrder> CreatePurchaseOrder(string result)
        {
            var purchaseOrder = JsonConvert.DeserializeObject<PuchaseOrder>(result);
            if (purchaseOrder.Items.Count == 0)
                throw new Exception("item is required");
            if(string.IsNullOrEmpty(purchaseOrder.Supplier) && !purchaseOrder.SupplierId.HasValue)
                throw new Exception("supplier is required");
            if(!string.IsNullOrEmpty(purchaseOrder.Supplier) && !purchaseOrder.SupplierId.HasValue)
                purchaseOrder.SupplierId = (await new SupplierPlugin().GetSuppliersAsync()).FirstOrDefault(x=>x.Name == purchaseOrder.Supplier)?.Id;
            foreach(var item in purchaseOrder.Items)
            {
                if (string.IsNullOrEmpty(item.Product) && !item.ProductId.HasValue)
                    throw new Exception("productId is required");
                if(item.Price<=0)
                    throw new Exception("item price is required");
                if (!string.IsNullOrEmpty(item.Product) && !item.ProductId.HasValue)
                    item.ProductId = (await new ProductPlugin().GetProductsAsync()).FirstOrDefault(x => x.Name == item.Product)?.Id;
                if(item.Quantity<=0)
                    throw new Exception("item quantity is required");
                item.Amount = item.Amount == 0 ? item.Price * item.Quantity : item.Amount;
                item.Id = item.Id ?? Guid.NewGuid();
            }
            purchaseOrder.Number = "PO-" + DateTime.Now.ToString("yyyyMMddHHmmss");
            purchaseOrder.Id = purchaseOrder.Id ?? Guid.NewGuid();
            return purchaseOrder;
        }
    }
    public class PuchaseOrder
    {
        [JsonPropertyName("id")]
        public Guid? Id { get; set; }
        [JsonPropertyName("number")]
        public string Number { get; set; }
        [JsonPropertyName("date")]
        public DateTime Date { get; set; }
        [JsonPropertyName("supplierId")]
        public Guid? SupplierId { get; set; }
        [JsonPropertyName("supplier")]
        public string Supplier { get; set; }
        [JsonPropertyName("items")]
        public List<PurchaseOrderItem> Items { get; set; }
    }
    public class PurchaseOrderItem
    {
        [JsonPropertyName("id")]
        public Guid? Id { get; set; }
        [JsonPropertyName("productId")]
        public Guid? ProductId { get; set; }
        [JsonPropertyName("product")]
        public string Product {  get; set; }
        [JsonPropertyName("price")]
        public decimal Price { get; set; }
        [JsonPropertyName("quantity")]
        public decimal Quantity { get; set; }
        [JsonPropertyName("amount")]
        public decimal Amount { get; set; }
    }
}

4.测试

我们启动项目,在console对话框中输入以下内容:帮我生成一张采购订单,日期为今天,供应商为旺旺,商品明细为旺仔牛奶,数量100,哇哈哈,数量20

我们在PurchaseOrderPlugin打个断点,看下DeepSeek给我们返回的消息:

从结果来说,deepseek很智能的帮我们补充了供应商的名称,返回的结果基本正确。但是在items里面,返回的产品名称字段是name,而我们需要的是Prodct或者ProductId,我们抛出了一个异常。

这个时候,AI Agent并不会使整个程序退出,而是再次帮我们发送请求给了DeepSeek,直到DeepSeek返回的结果信息符合我们的预期,能正常生成采购订单。

这是最终的结果

从上面的案例中,我们只是定义了输入数据和函数方法,AI Agent如何和deepseek进行沟通,以及在DeepSeek分析有偏差时,如何进行纠正,我们实际上并没有进行代码编码,这一切都是在智能的处理下完成。非常的nice!

当然,上面的案例,我们还不能真正的使用到生产中,但是我们只需要把plugin方法和我们ERP系统进行对接,再加上语音识别,就能真正的获得一个AI助手,应用到我们实际生产中。

### 将 DeepSeek 集成ERP 系统的技术路径 #### 技术背景概述 DeepSeek 是一种基于先进人工智能技术的平台,它可以通过自然语言处理 (NLP) 机器学习 (ML)[^2] 提供高效的决策支持。将其集成ERP 系统中可以显著增强企业的数据分析能力、自动化水平整体效率。 --- #### 一、集成的关键步骤技术要点 1. **API 接口对接** 使用 RESTful API 或 GraphQL API 实现 DeepSeek 平台与 ERP 系统之间的通信。大多数现代 ERP 系统(如 SAP、Oracle NetSuite、金蝶云等)均提供了开放接口来支持第三方服务的集成[^4]。 ```python import requests def call_deepseek_api(data): url = "https://api.deepseek.com/v1/analyze" headers = {"Authorization": "Bearer YOUR_API_KEY", "Content-Type": "application/json"} response = requests.post(url, json=data, headers=headers) return response.json() ``` 2. **数据流设计** 数据从 ERP 系统流向 DeepSeek 的过程需要经过清洗、转换标准化。这一步骤通常由 ETL 工具完成,或者通过编写脚本实现自动化的数据预处理[^5]。 3. **智能模块嵌入** 在 ERP 中创建新的功能模块或扩展现有模块的功能,利用 DeepSeek 的 NLP 能力自动生成单据、优化供应链管理流程或提供实时预测分析报告[^3]。 4. **错误校正机制** 当 DeepSeek 返回的结果存在偏差时,应设置反馈循环以便及时调整算法参数并重新训练模型。此部分可通过监控日志文件或引入人工审核环节达成目标。 --- #### 二、具体实施案例参考 - **SAP 场景下的实践** SAP 正在探索将 DeepSeek AI 平台融入其核心业务逻辑之中,从而提高财务报表生成速度及准确性[^3]。开发者可借鉴此类项目经验,在本地部署环境中测试类似的集成功能。 - **金蝶云实例说明** 根据官方公告显示,金蝶已经完成了对 DeepSeek 大规模语言模型的支持,并允许用户借助苍穹 PaaS 平台灵活配置个性化场景应用。 --- #### 三、注意事项 为了确保整个项目的顺利推进,请务必关注以下几个方面: - 明确双方团队职责划分; - 制定详尽的时间表与里程碑节点; - 对最终成果进行全面验收测试; 以上措施有助于降低潜在风险并保障预期收益最大化。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨汁软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值