Skip to content

IcedMango/DifyAi-csharp-sdk

Repository files navigation

Dify C# SDK

Github Nuget Version Nuget Download GitHub License Action Status Commit Status Language .NET-6.0 .NET-7.0 .NET-8.0

Introduction

简体中文 English

A Dify C# SDK for interacting with Dify's APIs.

Supports Chat/Knowledge Base APIs.

If you encounter any issues, feel free to raise an Issue or PR.

Quick Start

Installation

Install-Package IcedMango.DifyAi

Service Registration:

Startup.cs

// Startup.cs

using DifyAi.ServiceExtension;

public void ConfigureServices(IServiceCollection services)
{
    ...other code

    // Register services
    services.AddDifyAiServices();
}

Configuration File (appsettings.json)

The following configurations must be filled in

Here is a sample configuration that you need to modify according to your actual situation.

Note:

  • BaseUrl: [Required] The URL of the Dify API instance. Must end with a /.
  • BotApiKey: [Required] Your bot API key.
  • DatasetApiKey: Your knowledge base API key.
  • Proxy: Proxy settings, supports http, https, socks5. If not needed, leave it blank.
{
  "DifyAi": {
    "BaseUrl": "https://example.com/v1/",
    "BotApiKey": "app-your-bot-key",
    "DatasetApiKey": "dataset-your-dataset-key",
    "Proxy": "socks5://127.0.0.1:8889"
  }
}

Usage

using DifyAi.Dto.ParamDto;
using DifyAi.Services;

namespace TestDifyAi;
public class TestClass
{
    // Chat bot public API
    private readonly IDifyAiChatServices _difyAiChatServices;
    
    // Knowledge base public API
    private readonly IDifyAiDatasetServices _difyAiDatasetServices;
    
    public TestClass(IDifyAiChatServices difyAiChatServices, IDifyAiDatasetServices difyAiDatasetServices)
    {
        _difyAiChatServices = difyAiChatServices;
        _difyAiDatasetServices = difyAiDatasetServices;
    }

    // Chat bot
    public async Task<string> TestCompletion()
    {
        var res = await _difyAiChatServices.CreateChatCompletionBlockModeAsync(new Dify_CreateChatCompletionParamDto()
        {
            Query = "Who are you?",
            User = "IcedMango",
            ConversationId = string.Empty
        });
        
        if (res.Success)
        {
            return res.Data.Answer;
        }

        return "Error";
    }
    
    // Add knowledge base document
    public async Task<bool> AddDatasetDocAsync()
    {
        var difyAiDto = new Dify_CreateDocumentByTextParamDto() 
        {
            DatasetId = "your-dataset-id",
            Text = "who are you? Why are you here? I am a bot.",
            Name = "About me",
            IsAutomaticProcess = true,
            EnableHighQualityIndex = true
        };
        
        var res = await _difyAiDatasetServices.CreateDocumentByTextAsync(difyAiDto);
        if (res.Success)
        {
            var docInfo = res.Data.Document;
            return true;
        }
        return false;
    }
}

API Documentation

Click to view

TODO

  • Unit Tests
  • Message Completion Streaming Mode

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributor License Agreement (CLA)

By submitting a PR, you agree to license your contributions to the project owner under the MIT License.

About

a simple Dify Ai CSharp SDK

Resources

License

Stars

Watchers

Forks

Languages