Skip to content

Commit dfae266

Browse files
desmondcheongzxvenkateshdb
authored andcommitted
docs: Add text guide (Eventual-Inc#5102)
## Changes Made Add guide on embedding and chunking text.
1 parent 976160d commit dfae266

2 files changed

Lines changed: 151 additions & 3 deletions

File tree

docs/connectors/turbopuffer.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ This example does the following:
1313
=== "🐍 Python"
1414

1515
```python
16-
# $ pip install turbopuffer
17-
# $ pip install openai OR pip install sentence_transformers
16+
# $ pip install -U "daft[turbopuffer]"
17+
# $ pip install -U "daft[openai]"
18+
# OR
19+
# $ pip install -U "daft[sentence-transformers]"
1820
import os
1921

2022
import daft

docs/modalities/text.md

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,149 @@
11
# Working with Text
22

3-
User guide coming soon!
3+
This how-to guide shows you how to accomplish common text processing tasks with Daft:
4+
5+
- [Generate text embeddings](#generate-text-embeddings)
6+
- [Chunk text into smaller pieces](#chunk-text-into-smaller-pieces)
7+
8+
## Generate text embeddings
9+
10+
Text embeddings convert text into numerical vectors that capture semantic meaning. Use them for semantic search, similarity calculations, and other NLP tasks.
11+
12+
### How to use the embed_text function
13+
14+
By default, `embed_text` uses the [Sentence Transformers provider](#using-sentence-transformers), which requires the `sentence-transformers` [optional dependency](../install.md).
15+
16+
```bash
17+
pip install -U "daft[sentence-transformers]"
18+
```
19+
20+
Once installed, we can run:
21+
22+
```python
23+
import daft
24+
from daft.functions.ai import embed_text
25+
26+
(
27+
daft.read_huggingface("togethercomputer/RedPajama-Data-1T")
28+
.with_column("embedding", embed_text(daft.col("text")))
29+
.show()
30+
)
31+
```
32+
33+
### How to use different providers
34+
35+
#### Using Sentence Transformers
36+
37+
[Sentence Transformers](https://sbert.net/index.html) is a popular module for computing embeddings.
38+
39+
First install the optional Sentence Transformers dependency for Daft.
40+
41+
```bash
42+
pip install -U "daft[sentence-transformers]"
43+
```
44+
45+
Then use the `sentence_transformers` provider with any desired open model hosted on [Hugging Face](https://huggingface.co/) such as [`BAAI/bge-base-en-v1.5`](https://huggingface.co/BAAI/bge-base-en-v1.5).
46+
47+
```python
48+
import daft
49+
from daft.functions.ai import embed_text
50+
51+
provider = "sentence_transformers"
52+
model = "BAAI/bge-base-en-v1.5"
53+
54+
(
55+
daft.read_huggingface("togethercomputer/RedPajama-Data-1T")
56+
.with_column("embedding", embed_text(daft.col("text"), provider=provider, model=model))
57+
.show()
58+
)
59+
```
60+
61+
#### Using OpenAI
62+
63+
[OpenAI](https://platform.openai.com/docs/guides/embeddings) is a popular choice for generating text embeddings.
64+
65+
First install the optional OpenAI dependency for Daft.
66+
67+
```bash
68+
pip install -U "daft[openai]"
69+
```
70+
71+
You will also need to [set your `OPENAI_API_KEY` environment variable](https://platform.openai.com/settings/organization/api-keys).
72+
73+
Then use the `openai` provider with any desired [OpenAI embedding model](https://platform.openai.com/docs/models) such as [`text-embedding-3-small`](https://platform.openai.com/docs/models/text-embedding-3-small).
74+
75+
```python
76+
import daft
77+
from daft.functions.ai import embed_text
78+
79+
provider = "openai"
80+
model = "text-embedding-3-small"
81+
82+
(
83+
daft.read_huggingface("Open-Orca/OpenOrca")
84+
.with_column("embedding", embed_text(daft.col("response"), provider=provider, model=model))
85+
.show()
86+
)
87+
```
88+
!!! tip "Model Constraints"
89+
90+
Different embedding models have different constraints. For example, OpenAI's `text-embedding-3-small` model has a maximum context length of 8,192 tokens. This means you might encounter error messages like
91+
92+
```
93+
openai.BadRequestError: Error code: 400 - {'error': {'message': "This model's maximum context length is 8192 tokens, however you requested 12839 tokens (12839 in your prompt; 0 for the completion). Please reduce your prompt; or completion length.", 'type': 'invalid_request_error', 'param': None, 'code': None}}
94+
```
95+
96+
In this case you could either use a different model with a larger maximum context length, or could chunk your text into smaller segments before generating embeddings. See our [text embeddings guide](../examples/text-embeddings.md) for examples of text chunking strategies, or refer to the section below on [text chunking](#chunk-text-into-smaller-pieces).
97+
98+
99+
### How to work with embeddings
100+
101+
It's common to use embeddings for various tasks like similarity search or retrieval with a vector database.
102+
103+
Check out our guide on [writing to turbopuffer](../connectors/turbopuffer.md) to work with a popular fast vector database.
104+
105+
106+
## Chunk text into smaller pieces
107+
108+
When working with large text documents, you often need to break them into smaller chunks.
109+
110+
### How to chunk by sentences
111+
112+
A popular library for sentence chunking is [spaCy](https://spacy.io/).
113+
114+
First, install spaCy and a spaCy model such as `en_core_web_sm`.
115+
116+
```bash
117+
pip install -U spacy
118+
python -m spacy download en_core_web_sm
119+
```
120+
121+
Then, create a [User-defined Function](../custom-code/udfs.md) that uses spaCy.
122+
123+
```python
124+
import daft
125+
import typing
126+
127+
nlp_model_name = "en_core_web_sm"
128+
129+
@daft.func
130+
def chunk_by_sentences(text: str) -> typing.Iterator[str]:
131+
import spacy
132+
nlp = spacy.load(nlp_model_name)
133+
for sentence in nlp(text):
134+
yield sentence.text
135+
136+
137+
(
138+
daft.read_huggingface("togethercomputer/RedPajama-Data-1T")
139+
.limit(8)
140+
.with_column("chunks", chunk_by_sentences(daft.col("text")))
141+
.show()
142+
)
143+
```
144+
145+
For a fuller discussion on text chunking strategies, check out the [text chunking section in our tutorial on text embeddings](../examples/text-embeddings.md#step-2-create-text-chunking-udf).
146+
147+
## More examples
148+
149+
Check out our [end-to-end tutorial](../examples/text-embeddings.md) for a complete workflow: chunking text, generating embeddings, and uploading to vector databases like [turbopuffer](../connectors/turbopuffer.md).

0 commit comments

Comments
 (0)