hereâs a simple RAG sample
- for testing, i made a text file with this content:
mango.txt
Mango
A mango is an edible stone fruit produced by the tropical tree Mangifera indica. It is believed to have originated between northwestern Myanmar, Bangladesh, and northeastern India.[1] M. indica has been cultivated in South and Southeast Asia since ancient times resulting in two types of modern mango cultivars: the âIndian typeâ and the âSoutheast Asian typeâ.[2][3] Other species in the genus Mangifera also produce edible fruits that are also called âmangoesâ, the majority of which are found in the Malesian ecoregion.[4]
Worldwide, there are several hundred cultivars of mango. Depending on the cultivar, mango fruit varies in size, shape, sweetness, skin color, and flesh color, which may be pale yellow, gold, green, or orange.[1] Mango is the national fruit of India, Pakistan and the Philippines,[5][6] while the mango tree is the national tree of Bangladesh.[7]
Etymology
The English word mango (plural âmangoesâ or âmangosâ) originated in the 16th century from the Portuguese word, manga, from the Malay mangga, and ultimately from the Tamil man (âmango treeâ) + kay (âfruitâ).[8][9] The scientific name, Mangifera indica, refers to a plant bearing mangoes in India.[9]
- i then chunked the text data in 3 parts, then using text-embedding-3-small model, i called the embedding api to get the vector data for each chunks
text-embeddings [
{
embedding: [
0.04287149, 0.01839651, 0.025731485, 0.02845928,
... 1436 more items
],
text: 'Mango A mango is an edible stone fruit produced by the tropical tree Mangifera indica. It is believed to have originated between northwestern Myanmar, Bangladesh, and northeastern India. [1] M. indica has been cultivated in South and Southeast Asia since ancient times resulting in two types of modern mango cultivars: the "Indian type" and the "Southeast Asian type".'
},
{
embedding: [
0.02501605, 0.027974246, 0.05487668, 0.064951696, -0.023451207, ... 1436 more items
],
text: '[4] Worldwide, there are several hundred cultivars of mango. Depending on the cultivar, mango fruit varies in size, shape, sweetness, skin color, and flesh color, which may be pale yellow, gold, green, or orange. [1] Mango is the national fruit of India, Pakistan and the Philippines,[5][6] while the mango tree is the national tree of Bangladesh.'
},
{
embedding: [
0.042156275, 0.010176958, 0.008787963, 0.037421804, -0.024364166,
-0.005610028, 0.01934865, 0.0076097483, -0.008755534, -0.03653544,
... 1436 more items
],
text: '. [8][9] The scientific name, Mangifera indica, refers to a plant bearing mangoes in India. [9]'
}
]
- now, using chat completions, i sent a query
where does mango originated from?
-
i called the embeddings api to get its vector data
-
using semantic search, i used the queryâs vector data against the previously saved embeddings result. got hit and sent it back to the chat completion api
{
id: âchatcmpl-9bkU4pR1GPUwVJboUfXIjwudNmIqmâ,
object: âchat.completionâ,
created: 1718783848,
model: âgpt-3.5-turbo-0125â,
choices: [
{
index: 0,
message: [Object],
logprobs: null,
finish_reason: âstopâ
}
],
usage: { prompt_tokens: 385, completion_tokens: 69, total_tokens: 454 },
system_fingerprint: null
}
{
role: âassistantâ,
content: âThe mango is believed to have originated between northwestern Myanmar, Bangladesh, and northeastern India.\nâ +
â\nâ +
âI found this information in the file âmango.txtâ where it states: âA mango is an edible stone fruit produced by the tropical tree Mangifera indica. It is believed to have originated between northwestern Myanmar, Bangladesh, and northeastern India.ââ
}