Skip to content

Query

This example assumes using Vectara; is in scope and apiKey contains your Vectara 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
using var client = new VectaraClient(apiKey);

// First, list available corpora to get a corpus key.
ListCorporaResponse corporaResponse = await client.Corpora.ListAsync();

if (corporaResponse.Corpora is not { Count: > 0 })
{
    return;
}

var corpusKey = corporaResponse.Corpora[0].Key!;

// Execute a query with generation (RAG) against the corpus.
QueryFullResponse response = await client.Queries.Query2Async(
    query: "What is this about?",
    search: new SearchCorporaParameters
    {
        Value1 = new SearchCorporaParametersVariant1
        {
            Corpora =
            [
                new KeyedSearchCorpus
                {
                    Value2 = new KeyedSearchCorpusVariant2
                    {
                        CorpusKey = corpusKey,
                    },
                },
            ],
        },
    },
    generation: new GenerationParameters
    {
        MaxUsedSearchResults = 5,
    });

Console.WriteLine($"Summary: {response.Summary}");

foreach (var result in response.SearchResults ?? [])
{
    Console.WriteLine($"  Score: {result.Score}, Text: {result.Text?[..Math.Min(80, result.Text.Length)]}...");
}