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.
Install-Package IcedMango.DifyAi
// Startup.cs
using DifyAi.ServiceExtension;
public void ConfigureServices(IServiceCollection services)
{
...other code
// Register services
services.AddDifyAiServices();
}
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"
}
}
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;
}
}
- Unit Tests
- Message Completion Streaming Mode
This project is licensed under the MIT License - see the LICENSE file for details.
By submitting a PR, you agree to license your contributions to the project owner under the MIT License.