Skip to content

Commit 8b5ede1

Browse files
committed
RAG and 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 8b5ede1

3 files changed

Lines changed: 78 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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.
8+
9+
10+
```mermaid
11+
flowchart LR;
12+
A["Natual language prompt <br> *Can GenAI be used for research?*"]
13+
B["Vector embedding <br> [0.052587852, 0.094195396, 0.24439038, 0.104940414, ...]"]
14+
C["Vector Database <br> embedding1 <br> embedding2 <br> embedding3 <br> ... "]
15+
D["Text with embeddings similar to the prompt"]
16+
E["Original prompt with added context"]
17+
F["Response from LLM using context"]
18+
subgraph Retrieval
19+
direction TB
20+
A-- "Embedding" -->B;
21+
B-- "Look for similar embeddings" -->C;
22+
C-- "Generate context" -->D;
23+
end
24+
D-- "With expanded prompt" -->E;
25+
subgraph Augmented Generation
26+
direction TB
27+
E-- "LLM" -->F;
28+
end
29+
```

0 commit comments

Comments
 (0)