Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions DifySharp.Demo.AspNet/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using DifySharp;
using DifySharp.Apis;
using DifySharp.Chat.ChatMessages;
using DifySharp.Extensions;
using DifySharp.KnowledgeBase;
using DifySharp.KnowledgeBase.Document;
using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -91,7 +87,6 @@
var response = await client.PostChatMessageBlocking(new ChatMessage.RequestBody
{
Query = "ping",
ResponseMode = ChatMessage.ResponseMode.Blocking,
User = "test-user"
});

Expand Down
8 changes: 1 addition & 7 deletions DifySharp.Demo.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// See https://aka.ms/new-console-template for more information

using System.Text.Encodings.Web;
using System.Text.Json;
using DifySharp;
using DifySharp.Chat.ChatMessages;
using DifySharp.Extensions;
using DifySharp.KnowledgeBase.Dataset;
using Microsoft.Extensions.Logging;


// knowledge base api
Expand All @@ -28,7 +24,6 @@
var response = await chatClient.PostChatMessageBlocking(new ChatMessage.RequestBody
{
Query = "ping",
ResponseMode = ChatMessage.ResponseMode.Blocking,
User = "test-user"
});

Expand All @@ -38,13 +33,12 @@
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)
if (chunk is MessageEvent messageEvent)
Console.Write(messageEvent.Answer);
}

Expand Down
2 changes: 1 addition & 1 deletion DifySharp.Test/Apis/KnowledgeBaseApiTest/ChunkApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public KnowledgeBaseClient Client { get; set; }

public ChunkApiTestFixture() : base()
public ChunkApiTestFixture()
{
Client = ServiceProvider.GetRequiredKeyedService<KnowledgeBaseClient>("knowledge");

Expand Down Expand Up @@ -100,7 +100,7 @@
[TestSubject(typeof(IChunkApi))]
public class ChunkApiTest(
ChunkApiTestFixture fixture,
ILogger<DocumentApiTest> logger

Check warning on line 103 in DifySharp.Test/Apis/KnowledgeBaseApiTest/ChunkApiTest.cs

View workflow job for this annotation

GitHub Actions / test

Parameter 'logger' is unread.

Check warning on line 103 in DifySharp.Test/Apis/KnowledgeBaseApiTest/ChunkApiTest.cs

View workflow job for this annotation

GitHub Actions / publish

Parameter 'logger' is unread.
) : IClassFixture<ChunkApiTestFixture>
{
public Dataset Dataset => fixture.Dataset;
Expand All @@ -110,7 +110,7 @@


[Fact]
public async Task TestGetChunk()

Check warning on line 113 in DifySharp.Test/Apis/KnowledgeBaseApiTest/ChunkApiTest.cs

View workflow job for this annotation

GitHub Actions / publish

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
Assert.NotNull(Dataset);
Assert.NotNull(Document);
Expand Down
1 change: 0 additions & 1 deletion DifySharp.Test/Apis/KnowledgeBaseApiTest/DatasetApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;


namespace DifySharp.Test.Apis.KnowledgeBaseApiTest;

public class DatasetApiTestFixture : KnowledgeBaseApiTestFixture
Expand Down
3 changes: 1 addition & 2 deletions DifySharp.Test/Apis/KnowledgeBaseApiTest/DocumentApiTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using DifySharp.Apis;
using DifySharp.KnowledgeBase;
using DifySharp.KnowledgeBase.Dataset;
using DifySharp.KnowledgeBase.Document;
Expand All @@ -13,7 +12,7 @@
public Dataset Dataset { get; private set; }
public KnowledgeBaseClient Client { get; }

public DocumentApiTestFixture() : base()
public DocumentApiTestFixture()
{
Client = ServiceProvider.GetRequiredKeyedService<KnowledgeBaseClient>("knowledge");

Expand All @@ -36,7 +35,7 @@
[TestSubject(typeof(IDocumentApi))]
public class DocumentApiTest(
DocumentApiTestFixture fixture,
ILogger<DocumentApiTest> logger

Check warning on line 38 in DifySharp.Test/Apis/KnowledgeBaseApiTest/DocumentApiTest.cs

View workflow job for this annotation

GitHub Actions / test

Parameter 'logger' is unread.

Check warning on line 38 in DifySharp.Test/Apis/KnowledgeBaseApiTest/DocumentApiTest.cs

View workflow job for this annotation

GitHub Actions / publish

Parameter 'logger' is unread.
) : IClassFixture<DocumentApiTestFixture>
{
private static Document? Document { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit.DependencyInjection.Logging;

namespace DifySharp.Test.Apis.KnowledgeBaseApiTest;

Expand Down
1 change: 0 additions & 1 deletion DifySharp.Test/DifyClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using DifySharp;
using JetBrains.Annotations;

namespace DifySharp.Test;
Expand All @@ -7,7 +6,7 @@
public class DifyClientTest
{
[Fact]
public async Task TestDifyClient()

Check warning on line 9 in DifySharp.Test/DifyClientTest.cs

View workflow job for this annotation

GitHub Actions / test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 9 in DifySharp.Test/DifyClientTest.cs

View workflow job for this annotation

GitHub Actions / publish

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
// var client = new KnowledgeBaseClient(new ApiKeyOptions("apikey", "test-name", DifyApiType.KNOWLEDGE_BASE));
//
Expand Down
4 changes: 4 additions & 0 deletions DifySharp.Test/DifySharp.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@
</Content>
</ItemGroup>

<ItemGroup>
<Folder Include="Apis\WorkflowApiTest\" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion DifySharp.Test/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class Startup
{
public static IServiceProvider ServiceProvider { get; set; }

Check warning on line 11 in DifySharp.Test/Startup.cs

View workflow job for this annotation

GitHub Actions / test

Non-nullable property 'ServiceProvider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public void ConfigureServices(IServiceCollection services)
{
Expand All @@ -34,7 +34,7 @@
if (!File.Exists(settingFilePath)) return;

using var file = File.Open(settingFilePath, FileMode.Open);
var document = JsonDocument.Parse(file, new JsonDocumentOptions()
var document = JsonDocument.Parse(file, new JsonDocumentOptions
{
CommentHandling = JsonCommentHandling.Skip
});
Expand Down
33 changes: 0 additions & 33 deletions DifySharp/Apis/ApplicationApi.cs

This file was deleted.

2 changes: 1 addition & 1 deletion DifySharp/Apis/ChatApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Delete.RequestBody requestBody
/// <param name="requestBody"></param>
/// <returns></returns>
[HttpPost("/v1/conversations/{conversationId}/name")]
public Task<Conversations.Conversation> PostRenameConversation(
public Task<Conversation> PostRenameConversation(
string conversationId,
[JsonContent] Rename.RequestBody requestBody
);
Expand Down
126 changes: 100 additions & 26 deletions DifySharp/Apis/CompletionApi.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,106 @@
using WebApiClientCore.Attributes;
using DifySharp.Completion.Application;
using DifySharp.Completion.CompletionMessages;
using DifySharp.Completion.Messages;
using WebApiClientCore.Attributes;

namespace DifySharp.Apis;

/// <summary>
/// Completion App API
/// The text generation application offers non-session support and is ideal for translation, article writing, summarization AI, and more.
/// </summary>
public interface ICompletionApi
namespace DifySharp.Apis
{
/// <summary>
/// # Create Completion Message
/// Send a request to the text generation application.
/// Completion App API
/// The text generation application offers non-session support and is ideal for translation, article writing, summarization AI, and more.
/// </summary>
/// <param name="requestBody"></param>
/// <returns></returns>
[HttpPost("/completion-messages")]
public Task<HttpResponseMessage> PostCompletionMessages(
[JsonContent] object requestBody
);
public interface ICompletionApi : IApplicationApi, ICompletionMessagesApi, IMessagesApi
{
/// <summary>
/// Upload a file (currently only images are supported) for use when sending messages,
/// enabling multimodal understanding of images and text. Supports png, jpg, jpeg, webp,
/// gif formats. Uploaded files are for use by the current end-user only.
/// </summary>
/// <param name="user"></param>
/// <param name="file"></param>
/// <returns></returns>
[HttpPost("files/upload")]
public Task<HttpResponseMessage> PostFilesUpload(
[FormDataContent] string user,
FileInfo file
);

/// <summary>
/// # Stop Generate
/// Only supported in streaming mode.
/// </summary>
/// <param name="taskId"></param>
/// <returns></returns>
[HttpPost("/completion-messages/taskId/stop")]
public Task<HttpResponseMessage> PostCompletionsMessagesStop(
string taskId
);
/// <summary>
/// Text to audio
/// </summary>
/// <param name="requestBody"></param>
/// <returns></returns>
[HttpPost("/v1/text-to-audio")]
public Task<HttpResponseMessage> PostTextToAudio(
[JsonContent] object requestBody
);
}
}

namespace DifySharp.Completion.CompletionMessages
{
public interface ICompletionMessagesApi
{
/// <summary>
/// # Create Completion Message
/// Send a request to the text generation application.
/// </summary>
/// <param name="requestBody"></param>
/// <returns></returns>
[HttpPost("/completion-messages")]
public Task<HttpResponseMessage> PostCompletionMessages(
[JsonContent] CompletionMessages.RequestBody requestBody
);

/// <summary>
/// # Stop Generate
/// Only supported in streaming mode.
/// </summary>
/// <param name="taskId"></param>
/// <returns></returns>
[HttpPost("/completion-messages/taskId/stop")]
public Task<Stop.ResponseBody> PostCompletionsMessagesStop(string taskId);
}
}

namespace DifySharp.Completion.Messages
{
public interface IMessagesApi
{
/// <summary>
/// # Message Feedback
/// End-users can provide feedback messages, facilitating application developers to optimize expected outputs.
/// </summary>
/// <param name="messageId"></param>
/// <param name="requestBody"></param>
/// <returns></returns>
[HttpPost("/v1/messages/{messageId}/feedbacks")]
public Task<PostFeedback.ResponseBody> PostMessagesFeedbacks(
string messageId,
[JsonContent] PostFeedback.RequestBody requestBody
);
}
}


namespace DifySharp.Completion.Application
{
public interface IApplicationApi
{
/// <summary>
/// # Get application basic information
/// <para>Used to get basic information about this application</para>
/// </summary>
/// <returns></returns>
[HttpGet("/v1/info")]
public Task<Basic.ResponseBody> GetInfo();

/// <summary>
/// # Get Application Parameters Information
/// <para>Used at the start of entering the page to obtain information such as features, input parameter names, types, and default values.</para>
/// </summary>
/// <returns></returns>
[HttpGet("/v1/parameters")]
public Task<Parameters.ResponseBody> GetParameters();
}
}
1 change: 0 additions & 1 deletion DifySharp/Apis/KnowledgeBaseApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using DifySharp.KnowledgeBase.Dataset;
using DifySharp.KnowledgeBase.Document;
using WebApiClientCore.Attributes;
using WebApiClientCore;

namespace DifySharp.Apis
{
Expand Down
Loading
Loading