-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
165 lines (143 loc) · 4.58 KB
/
Program.cs
File metadata and controls
165 lines (143 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.Text.Json;
using System.Text.Json.Serialization;
using DifySharp;
using DifySharp.Chat.ChatMessages;
using DifySharp.Extensions;
using DifySharp.KnowledgeBase;
using DifySharp.KnowledgeBase.Document;
using WebApiClientCore.Parameters;
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 you api key here
var app = builder.Build();
var group = app.MapGroup("example");
var knowledgeGroup = group.MapGroup("knowledge");
knowledgeGroup.MapGet("create_file_by_text", async (IServiceProvider sp) =>
{
var api = sp
.GetRequiredKeyedService<KnowledgeBaseClient>("knowledge"); // get client instance by name in configuration
var uuid = Guid.NewGuid().ToString("N")[..6];
var response = await api.PostCreateDocumentByTextAsync(
"<your-dataset-id>", // add a dataset id here
new CreateByText.RequestBody(
$"Test Document {uuid}",
"Test Content",
IndexingTechnique.Economy,
DocForm.TextModel,
"",
new ProcessRule(
"automatic",
new Rules(
[
new PreProcessingRule(
"remove_extra_spaces",
true
),
new PreProcessingRule(
"remove_urls_emails",
true
)
],
new Segmentation(
"\n\n",
1000
),
"paragraph",
new SubChunkSegmentation(
"\n\n",
1000,
200
)
)
),
new CreateByText.RetrievalModel(
CreateByText.SearchMethod.HybridSearch,
false,
new CreateByText.RerankingModel(
"",
""
),
4,
false,
0.9f
),
"",
""
));
var document = response.Document;
return new
{
document
};
});
knowledgeGroup.MapGet("create_file_by_file", async (IServiceProvider sp) =>
{
var api = sp
.GetRequiredKeyedService<KnowledgeBaseClient>("knowledge"); // get client instance by name in configuration
var uuid = Guid.NewGuid().ToString("N")[..6];
var tmpFile = Path.GetTempFileName();
await File.WriteAllTextAsync(tmpFile, "Test Content");
var _defaultProcessRule = new ProcessRule(
"automatic",
new Rules(
[
new PreProcessingRule(
"remove_extra_spaces",
true
),
new PreProcessingRule(
"remove_urls_emails",
true
)
],
new Segmentation(
"\n\n",
1000
),
"paragraph",
new SubChunkSegmentation(
"\n\n",
1000,
200
)
)
);
// var dataJsonStr =
// "{\"indexing_technique\":\"high_quality\",\"process_rule\":{\"rules\":{\"pre_processing_rules\":[{\"id\":\"remove_extra_spaces\",\"enabled\":true},{\"id\":\"remove_urls_emails\",\"enabled\":true}],\"segmentation\":{\"separator\":\"###\",\"max_tokens\":500}},\"mode\":\"custom\"}}";
var data = new CreateByFile.Data(
null,
IndexingTechnique.Economy,
DocForm.TextModel,
"",
_defaultProcessRule
);
var response = await api.PostCreateDocumentByFileAsync( // add a dataset id here
"<your-dataset-id>", data, new FormDataFile("<your-file-path>"));
var document = response.Document;
return new
{
document
};
});
app.MapGet("/ChatApiDemo/ChatMessagesBlocking", async (IServiceProvider sp) =>
{
// get chat client instance by name in configuration
var client = sp.GetRequiredKeyedService<ChatClient>("chat");
// send chat message in blocking mode
var response = await client.PostChatMessageBlocking(new ChatMessage.RequestBody
{
Query = "ping",
User = "test-user"
});
return new
{
response.Answer,
};
});
app.Run();