Quickstart Data
This example assumes using Instill; is in scope and apiKey contains your Instill API key.
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 | using var client = new InstillClient(apiKey);
using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(5));
var cancellationToken = cancellationTokenSource.Token;
GetAuthenticatedUserResponse getAuthenticatedUserResponse = await client.Namespace.MgmtPublicServiceGetAuthenticatedUserAsync(
cancellationToken: cancellationToken);
var authenticatedUser = getAuthenticatedUserResponse.User;
var namespaceName = $"namespaces/{authenticatedUser.Id}";
string? knowledgeBaseName = null;
string? fileName = null;
try
{
CreateKnowledgeBaseResponse createKnowledgeBaseResponse = await client.Artifact.ArtifactPublicServiceCreateKnowledgeBaseAsync(
parent: namespaceName,
displayName: $"shakespeare-{Random.Shared.Next(1_000_000)}",
description: "Works of Shakespeare",
cancellationToken: cancellationToken);
var knowledgeBase = createKnowledgeBaseResponse.KnowledgeBase;
knowledgeBaseName = knowledgeBase.Name!;
Console.WriteLine($"KnowledgeBaseName: {knowledgeBaseName}");
Console.WriteLine($"Id: {knowledgeBase.Id}");
Console.WriteLine($"DisplayName: {knowledgeBase.DisplayName}");
Console.WriteLine($"Description: {knowledgeBase.Description}");
Console.WriteLine($"TotalFiles: {knowledgeBase.TotalFiles}");
Console.WriteLine($"TotalTokens: {knowledgeBase.TotalTokens}");
CreateFileResponse createFileResponse = await client.Artifact.ArtifactPublicServiceCreateFileAsync(
parent: knowledgeBaseName,
displayName: "midsummer-nights-dream.pdf",
type: FileType.TypePdf,
content: Convert.ToBase64String(H.Resources.midsummer_nights_dream_pdf.AsBytes()),
cancellationToken: cancellationToken);
var file = createFileResponse.File;
fileName = file.Name!;
Console.WriteLine($"FileName: {fileName}");
Console.WriteLine($"Id: {file.Id}");
Console.WriteLine($"DisplayName: {file.DisplayName}");
Console.WriteLine($"Type: {file.Type}");
Console.WriteLine($"ProcessStatus: {file.ProcessStatus}");
Console.WriteLine($"Size: {file.Size}");
Console.WriteLine($"TotalTokens: {file.TotalTokens}");
Console.WriteLine($"TotalChunks: {file.TotalChunks}");
while (!cancellationToken.IsCancellationRequested)
{
GetFileResponse getFileResponse = await client.Artifact.ArtifactPublicServiceGetFileAsync(
fileName,
cancellationToken: cancellationToken);
file = getFileResponse.File;
Console.WriteLine($"PolledProcessStatus: {file.ProcessStatus}");
Console.WriteLine($"PolledProcessOutcome: {file.ProcessOutcome}");
if (file.ProcessStatus is FileProcessStatus.Completed or FileProcessStatus.Failed)
{
break;
}
await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
}
if (file.ProcessStatus == FileProcessStatus.Failed)
{
if (file.ProcessOutcome?.Contains("insufficient credit", StringComparison.OrdinalIgnoreCase) == true)
{
throw new AssertInconclusiveException($"Artifact processing requires credits: {file.ProcessOutcome}");
}
}
const string question = "Who are the main characters involved in the love triangle in Act I?";
Console.WriteLine($"Question: {question}");
SearchChunksResponse searchChunksResponse = await client.Artifact.ArtifactPublicServiceSearchChunksAsync(
parent: namespaceName,
knowledgeBase: knowledgeBaseName,
textPrompt: question,
topK: 5,
cancellationToken: cancellationToken);
Console.WriteLine("SimilarChunks:");
foreach (var chunk in searchChunksResponse.SimilarChunks ?? [])
{
Console.WriteLine($" Chunk: {chunk.Chunk}");
Console.WriteLine($" SimilarityScore: {chunk.SimilarityScore}");
Console.WriteLine($" TextContent: {chunk.TextContent}");
Console.WriteLine($" File: {chunk.File}");
Console.WriteLine("----------------------------------------");
}
}
finally
{
if (fileName is not null)
{
try
{
DeleteFileResponse deleteFileResponse = await client.Artifact.ArtifactPublicServiceDeleteFileAsync(
fileName,
cancellationToken: CancellationToken.None);
}
catch (Exception exception)
{
Console.WriteLine($"DeleteFile cleanup failed: {exception.Message}");
}
}
if (knowledgeBaseName is not null)
{
try
{
DeleteKnowledgeBaseResponse deleteKnowledgeBaseResponse = await client.Artifact.ArtifactPublicServiceDeleteKnowledgeBaseAsync(
knowledgeBaseName,
cancellationToken: CancellationToken.None);
}
catch (Exception exception)
{
Console.WriteLine($"DeleteKnowledgeBase cleanup failed: {exception.Message}");
}
}
}
|