Skip to content

fengb3/DifySharp

Repository files navigation

DifySharp

GitHub Actions Workflow Status NuGet Version

Dify SDK for csharp

Install

dotnet add package DifySharp

Usage

Console

var chatClient = new ChatClient("<your-api-key>");

// for blocking mode
var response = await chatClient.PostChatMessageBlocking(new ChatMessage.RequestBody
{
    Query        = "ping!",
    ResponseMode = ChatMessage.ResponseMode.Blocking,
    User         = "test-user"
});

Console.WriteLine(response.Answer); // pong!

// for streaming mode
var chunks = chatClient.PostChatMessageStreaming(new ChatMessage.RequestBody
{
    Query        = "What dose the fox say?",
    ResponseMode = ChatMessage.ResponseMode.Streaming,
    User         = "test-user"
});

await foreach (var chunk in chunks)
{
    if (chunk is ChatMessage.MessageEvent messageEvent)
        Console.Write(messageEvent.Answer);
}
// Ring-ding-ding-ding-dingeringeding! Gering-ding-ding-ding-dingeringeding! Wa-pa-pa-pa-pa-pa-pow! Jacha-chacha-chacha-chow! Fraka-kaka-kaka-kaka-kow! A-hee-ahee ha-hee! A-oo-oo-oo-ooo!

Dependency Injection - ASP.NET Core

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDifySharp(o =>
{
    o.Secrets =
    [
        new DifyApiSecret("<your-knowledge-base-secret>", "knowledge", DifyApiType.KNOWLEDGE_BASE),
        new DifyApiSecret("<your-chat-application-secret>", "chat", DifyApiType.CHAT)
        .. // add more secrets
    ];
});

var app = builder.Build();

// get ChatClient from DI container with client name
app.MapGet("/", async ([FromKeyedServices("chat")] ChatClient chatClient) =>
    {
        var response = await chatClient.PostChatMessageBlocking(new ChatMessage.RequestBody
        {
            Query        = "ping!",
            ResponseMode = ChatMessage.ResponseMode.Blocking,
            User         = "test-user"
        });

        return Results.Text(response.Answer); // pong!
    }
);

app.Run();