0

Add a document to a corpus

by
Published Nov 5, 2024

Add a document to a corpus. You can add documents that are either in a typical structured format, or in a format that explicitly specifies each document part. Each part becomes a separate search result.

Script vectara Verified

The script

Submitted by hugo697 Bun
Verified 581 days ago
1
//native
2
type Vectara = {
3
	apiKey: string
4
}
5
/**
6
 * Add a document to a corpus
7
 * Add a document to a corpus. You can add documents that are either in a typical structured format,
8
or in a format that explicitly specifies each document part.  Each part becomes a separate search result.
9

10
 */
11
export async function main(
12
	auth: Vectara,
13
	corpus_key: string,
14
	body:
15
		| {
16
				id: string
17
				type: string
18
				metadata?: {}
19
				document_parts: {
20
					text: string
21
					metadata?: {}
22
					context?: string
23
					custom_dimensions?: {}
24
				}[]
25
		  }
26
		| {
27
				id: string
28
				type: string
29
				title?: string
30
				description?: string
31
				metadata?: {}
32
				custom_dimensions?: {}
33
				sections: {
34
					id?: number
35
					title?: string
36
					text: string
37
					metadata?: {}
38
					sections?: {}[]
39
				}[]
40
		  }
41
) {
42
	const url = new URL(`https://api.vectara.io/v2/corpora/${corpus_key}/documents`)
43

44
	const response = await fetch(url, {
45
		method: 'POST',
46
		headers: {
47
			'Content-Type': 'application/json',
48
			'x-api-key': auth.apiKey
49
		},
50
		body: JSON.stringify(body)
51
	})
52
	if (!response.ok) {
53
		const text = await response.text()
54
		throw new Error(`${response.status} ${text}`)
55
	}
56
	return await response.json()
57
}
58