QuickstartData

 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
using var client = new InstillClient(apiKey);
using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(5));
var cancellationToken = cancellationTokenSource.Token;

CreateCatalogResponse createCatalogResponse = await client.Artifact.CreateCatalogAsync(
    namespaceId: "havendv",
    name: $"shakespeare_{Random.Shared.Next(1_000_000)}",
    description: "Works of Shakespeare",
    cancellationToken: cancellationToken);

var catalog = createCatalogResponse.Catalog;
Console.WriteLine($"CatalogId: {catalog.CatalogId}");
Console.WriteLine($"Name: {catalog.Name}");
Console.WriteLine($"Description: {catalog.Description}");
Console.WriteLine($"TotalFiles: {catalog.TotalFiles}");
Console.WriteLine($"TotalTokens: {catalog.TotalTokens}");


UploadCatalogFileResponse uploadFileResponse = await client.Artifact.UploadCatalogFileAsync(
    namespaceId: "havendv",
    catalogId: catalog.CatalogId,
    name: "midsummer-nights-dream.pdf",
    type: FileType.PDF,
    content: Convert.ToBase64String(H.Resources.midsummer_nights_dream_pdf.AsBytes()),
    cancellationToken: cancellationToken);

var file = uploadFileResponse.File;
Console.WriteLine($"FileUid: {file.FileUid}");
Console.WriteLine($"Name: {file.Name}");
Console.WriteLine($"Type: {file.Type}");
Console.WriteLine($"Size: {file.Size}");
Console.WriteLine($"TotalTokens: {file.TotalTokens}");
Console.WriteLine($"TotalChunks: {file.TotalChunks}");


ProcessCatalogFilesResponse processFilesResponse = await client.Artifact.ProcessCatalogFilesAsync(
    fileUids: [file.FileUid],
    cancellationToken: cancellationToken);


while (!cancellationToken.IsCancellationRequested)
{
    await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);

    ListCatalogFilesResponse listFilesResponse = await client.Artifact.ListCatalogFilesAsync(
        namespaceId: "havendv",
        catalogId: catalog.CatalogId,
        cancellationToken: cancellationToken);

    if (listFilesResponse.Files[0].ProcessStatus is
            FileProcessStatus.COMPLETED or
            FileProcessStatus.FAILED)
    {
        break;
    }
}

const string question = "Who are the main characters involved in the love triangle in Act I?";
Console.WriteLine($"Question: {question}");

QuestionAnsweringResponse questionAnsweringResponse = await client.Artifact.QuestionAnsweringAsync(
    namespaceId: "havendv",
    catalogId: catalog.CatalogId,
    question: question,
    topK: 5,
    cancellationToken: cancellationToken);

Console.WriteLine($"Answer: {questionAnsweringResponse.Answer}");
// Answer: The main characters involved in the love triangle in Act I are Hermia, Lysander, and Demetrius.

Console.WriteLine("SimilarChunks:");

foreach (var chunk in questionAnsweringResponse.SimilarChunks ?? [])
{
    Console.WriteLine($"  ChunkUid: {chunk.ChunkUid}");
    Console.WriteLine($"  SimilarityScore: {chunk.SimilarityScore}");
    Console.WriteLine($"  TextContent: {chunk.TextContent}");
    Console.WriteLine($"  SourceFile: {chunk.SourceFile}");
    Console.WriteLine("----------------------------------------");
}

DeleteCatalogResponse deleteCatalogResponse = await client.Artifact.DeleteCatalogAsync(
    namespaceId: "havendv",
    catalogId: catalog.CatalogId,
    cancellationToken: cancellationToken);