Skip to content

Commit 29eb173

Browse files
committed
RAG & Embedding
modified: docs/genai/04_how_to_guides/01_temperature.md new file: docs/genai/04_how_to_guides/02_embeddings.mdx new file: docs/genai/04_how_to_guides/03_retrieval_augmented_generation.mdx
1 parent 5f1ef02 commit 29eb173

3 files changed

Lines changed: 125 additions & 1 deletion

File tree

docs/genai/04_how_to_guides/01_temperature.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Temperature
1+
# Effect of Temperature
22

33
Generating text (or images) from LLMs is inherently probabilistic. However, as an end user you have many parameters at your disposal to tweak the behavior of LLMs. Of these, temperature is the most commonly used. Broadly, it controls the randomness of the generated text. A lower temperature produces more deterministic outputs, while a higher temperature produces more random "creative" output. For a more comprehensive explanation on this topic, refer to the following:
44
- [How to generate text: using different decoding methods for language generation with Transformers](https://huggingface.co/blog/how-to-generate)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Embeddings
2+
3+
While Decoder-only LLMs gained massive popularity via their usage in chatbots, Encoder-only LLMs can be used for a wider variety of tasks. Decoder-only LLMs "generate" tokens ("text") one at a time probabalisticsally. Encoder-only LLMs on the other hand take text as their input, tokenize it and generate "embeddings" as their output. Here, we shall walk through a task of generating embeddings from a text document.
4+
5+
```mermaid
6+
flowchart LR;
7+
A["natual language text string <br> *GenAI can be used for research*"]
8+
B["encoder-only LLM"]
9+
C["vector embedding <br> [0.052587852, 0.094195396, 0.24439038, 0.104940414, ...]"]
10+
A-- "Input" -->B;
11+
B-- "Output" -->C;
12+
```
13+
14+
## How to generate embeddings from plain text:
15+
16+
The snippet below uses the `text-embedding-3-small` model to create 32-dimensional floating point vector embeddings for the input string:
17+
18+
```python
19+
from portkey_ai import Portkey
20+
21+
portkey = Portkey(
22+
base_url="https://ai-gateway.apps.cloud.rt.nyu.edu/v1/",
23+
api_key="", # Replace with your Portkey API key
24+
virtual_key="", # Replace with your virtual key
25+
)
26+
27+
response = portkey.embeddings.create(
28+
model="text-embedding-3-small",
29+
input="GenAI can be used for research.",
30+
encoding_format="float",
31+
dimensions=32,
32+
)
33+
34+
print(response["data"][0].embedding)
35+
```
36+
37+
and gives the following response:
38+
```
39+
[0.052587852, 0.094195396, 0.24439038, 0.104940414, -0.028921358, -0.31591928, -0.1846261, 0.221018, 0.033215445, -0.1382735, -0.14776362, -0.15058714, 0.057725072, -0.23435123, 0.07956805, -0.32156628, -0.08454841, 0.04066637, -0.022215525, 0.19090058, -0.11160703, 0.22258662, -0.06843088, -0.22854735, 0.1033718, -0.38085997, 0.2933312, -0.023215517, 0.20768477, -0.039333045, 0.17192031, -0.14180289]
40+
```
41+
42+
## Applications of embeddings
43+
44+
Embeddings have the ability to encode the semantic meaning of the text. Thus, they find applications in:
45+
- retrieval-augmented generation
46+
- search
47+
- classification
48+
among others
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Retrieval-augmented generation
2+
3+
Large Language Models only know about the data they were trained upon and do not have the context needed to be effective at answering questions based on:
4+
- private datasets
5+
- newer knowledge past the cutoff date (i.e. the date at which data collection was frozen)
6+
7+
To get around this issue, one of the most popular techniques is Retrieval-augmented generation, the most basic version of which is outlined below:
8+
9+
```mermaid
10+
flowchart TB;
11+
A["Documents to use in RAG pipeline"]
12+
B@{shape: docs, label: "Document parsed and divided into multiple chunks"}
13+
C["encoder-only LLM"]
14+
D@{shape: procs, label: "text chunk embedding"}
15+
E[("vector database")]
16+
F["natual language prompt"]
17+
G["query embedding"]
18+
I["relevant chunks"]
19+
J["original prompt with added context"]
20+
K["relevant response from LLM using context"]
21+
subgraph Ingestion
22+
A-- "Parse and Chunk" -->B;
23+
end
24+
subgraph Embeddings
25+
B-- "Pass to embedding model" -->C;
26+
C-- "Generate Embeddings" -->D;
27+
D-- "Store Emebddings" -->E;
28+
end
29+
subgraph Retrieval
30+
F-- "Pass to embedding model" -->C;
31+
C-- "Generate Emebddings" -->G;
32+
G-- "Query vector database" -->E;
33+
E-- "Gather text chunks with embeddings similar to prompt" -->I;
34+
end
35+
I-- "With expanded prompt" -->J;
36+
subgraph Augmented Generation
37+
J-- "LLM" -->K;
38+
end
39+
```
40+
41+
It starts with the "Ingestion" phase where a document to be used as context is parsed and broken into chunks. These chunks are then converted to embeddings and stored in a vector database (which specializes in storing and retreiving vectors). This setup allows us now "retreive" the required context for an incoming prompt before it is sent to an LLM. The "retreival" phase consists of converting the prompt to an embedding and looking up embeddings for chunks of the document that are similar to it. The text chunks associated with the embeddings similar to the embedding for the query are then added as additional context to the prompt before passing it to an LLM.
42+
The LLM now has the associated context it needs to generate an relevant response to the prompt.
43+
44+
Here's a script to test this out yourself, once you have API access to an embedding model and an LLM: https://github.com/NYU-RTS/rts-docs-examples/tree/main/genai/rag . You can run it to ask a question about a recent event that occured after the knowledge cutoff for the dataset used to train the LLM:
45+
```sh
46+
ss19980@ITS-JQKQGQQMTX ~/D/p/r/g/rag (main)> uv run rag_basic.py \
47+
https://en.wikipedia.org/wiki/2025_London_Marathon \
48+
"Which athletes won the 2025 London Marathon?"
49+
...
50+
Processing chunks: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 44/44 [00:13<00:00, 3.22it/s]
51+
----------------------------------------------------------------
52+
Query embedding vector (first 5 dims) is: [-0.013783585280179977, 0.022411219775676727, -0.018617955967783928, -0.04355597868561745, -0.009368936531245708]
53+
----------------------------------------------------------------
54+
Retreived chunks and similarity scores:
55+
56+
('The 2025 London Marathon was the 45th running of the London Marathon; it took place on 27 April 2025.[1][2]', 0.8441829085350037)
57+
('1. ^ Martin, Andy (26 April 2025). "London Marathon 2025: route, runners and everything else you need to know". The Guardian. Retrieved 26 April 2025.\n2. ^ Poole, Harry (26 April 2025). "Will \'greatest\' London Marathon line-ups break records?". BBC News. Retrieved 26 April 2025.\n3. ^ a b c d "2025 London Marathon results". NBC Sports. 27 April 2025. Retrieved 27 April 2025.', 0.837587296962738)
58+
('Venue, 45th London Marathon = London, England. Date, 45th London Marathon = 27 April 2025. Champions, 45th London Marathon = Champions. Men, 45th London Marathon = Sabastian Sawe (2:02:27). Women, 45th London Marathon = Tigst Assefa (2:15:50). Wheelchair men, 45th London Marathon = Marcel Hug (1:25:25). Wheelchair women, 45th London Marathon = Catherine Debrunner (1:34:18). ←\xa020242026\xa0→, 45th London Marathon = ←\xa020242026\xa0→', 0.8032743334770203)
59+
----------------------------------------------------------------
60+
Generated response from LLM without additional context is:
61+
62+
The 2025 London Marathon has not happened yet!
63+
64+
The event typically takes place in **April** each year. We will only know the winners after the race takes place in April 2025.
65+
---------------------------------------------------------------
66+
Generated response from LLM with additional context is:
67+
68+
The athletes who won the 2025 London Marathon are:
69+
70+
* **Men:** Sabastian Sawe
71+
* **Women:** Tigst Assefa
72+
* **Wheelchair men:** Marcel Hug
73+
* **Wheelchair women:** Catherine Debrunner
74+
E20250611 14:32:54.595919 362220 server.cpp:47] [SERVER][BlockLock][] Process exit
75+
ss19980@ITS-JQKQGQQMTX ~/D/p/r/g/rag (main)>
76+
```

0 commit comments

Comments
 (0)