0

Embed documents

by
Published Apr 8, 2025

Perform embedding on a Telnyx Storage Bucket using the a embedding model.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Embed documents
7
 * Perform embedding on a Telnyx Storage Bucket using the a embedding model.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	body: {
12
		bucket_name: string
13
		document_chunk_size?: number
14
		document_chunk_overlap_size?: number
15
		embedding_model?:
16
			| 'thenlper/gte-large'
17
			| 'intfloat/multilingual-e5-large'
18
			| 'sentence-transformers/all-mpnet-base-v2'
19
		loader?: 'default' | 'intercom'
20
	}
21
) {
22
	const url = new URL(`https://api.telnyx.com/v2/ai/embeddings`)
23

24
	const response = await fetch(url, {
25
		method: 'POST',
26
		headers: {
27
			'Content-Type': 'application/json',
28
			Authorization: 'Bearer ' + auth.apiKey
29
		},
30
		body: JSON.stringify(body)
31
	})
32
	if (!response.ok) {
33
		const text = await response.text()
34
		throw new Error(`${response.status} ${text}`)
35
	}
36
	return await response.json()
37
}
38