-
Notifications
You must be signed in to change notification settings - Fork 392
docs: Add daft.File usage throughout modalities #6074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
everettVT
wants to merge
17
commits into
main
Choose a base branch
from
everettVT/daft_file_docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
655de41
Enhance error messages for missing audio dependencies and improve doc…
everettVT 1131743
Merge branch 'main' into everettVT/daft_file_docs
everettVT 697dc6b
merge origin/main into daft_file_docs
everettVT 241935b
fix style and doctests
everettVT b0f2932
ignore doctest.
everettVT 9fcff6a
Remove Documents and Code from SUMMARY.md
everettVT 5326194
Reorganize API documentation structure
everettVT c85835c
Update SUMMARY.md to reflect API changes
everettVT b5abad7
Update SUMMARY.md to include Window and Schema sections
everettVT 5d51d25
Add Series section to SUMMARY.md
everettVT 4e50a94
Remove duplicate 'Functions' entry from SUMMARY.md
everettVT 4de55f4
Clean up comments in test_audio.py
everettVT 7754a15
Clean up soundfile import checks in tests
everettVT 5b03786
Enhance audio tests with soundfile imports
everettVT 435fb1e
Merge remote-tracking branch 'origin/main' into everettVT/daft_file_docs
everettVT c3e89ed
docs: Enhance documentation for file handling and modalities
everettVT 2b10e24
Update files.md
everettVT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| The `File` DataType provides first-class support for handling file data across local and remote storage, enabling seamless file operations in distributed environments. | ||
|
|
||
| ::: daft.file.File | ||
| options: | ||
| filters: ["!^_"] | ||
|
|
||
| ::: daft.file.AudioFile | ||
| options: | ||
| filters: ["!^_"] | ||
|
|
||
| ::: daft.file.VideoFile | ||
| options: | ||
| filters: ["!^_"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| # Working with Embeddings | ||
|
|
||
| Embeddings transform text, images, and other data into dense vector representations that capture semantic meaning—enabling similarity search, retrieval-augmented generation (RAG), and AI-powered discovery. Daft makes it easy to generate, store, and query embeddings at scale. | ||
|
|
||
| With the native [`daft.DataType.embedding`](../api/datatypes/embedding.md) type and [`embed_text`](../api/functions/embed_text.md) function, you can: | ||
|
|
||
| - **Generate embeddings** from any text column using providers like OpenAI, Cohere, or local models | ||
| - **Compute similarity** with built-in distance functions like `cosine_distance` | ||
| - **Build search pipelines** that scale from local development to distributed clusters | ||
| - **Write to vector databases** like Turbopuffer, Pinecone, or LanceDB | ||
|
|
||
| ## Semantic Search Example | ||
|
|
||
| The following example creates a simple semantic search pipeline—embedding documents, comparing them to a query, and ranking by similarity: | ||
|
|
||
| ```python | ||
| import daft | ||
| from daft.functions import embed_text, cosine_distance | ||
|
|
||
| # Create a knowledge base with documents | ||
| documents = daft.from_pydict( | ||
| { | ||
| "text": [ | ||
| "Python is a high-level programming language", | ||
| "Machine learning models require training data", | ||
| "Daft is a distributed dataframe library", | ||
| "Embeddings capture semantic meaning of text", | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| # Embed all documents | ||
| documents = documents.with_column( | ||
| "embedding", | ||
| embed_text( | ||
| daft.col("text"), | ||
| provider="openai", | ||
| model="text-embedding-3-small", | ||
| ), | ||
| ) | ||
|
|
||
| # Create a query | ||
| query = daft.from_pydict({"query_text": ["What is Daft?"]}) | ||
|
|
||
| # Embed the query | ||
| query = query.with_column( | ||
| "query_embedding", | ||
| embed_text( | ||
| daft.col("query_text"), | ||
| provider="openai", | ||
| model="text-embedding-3-small", | ||
| ), | ||
| ) | ||
|
|
||
| # Cross join to compare query against all documents | ||
| results = query.join(documents, how="cross") | ||
|
|
||
| # Calculate cosine distance (lower is more similar) | ||
| results = results.with_column( | ||
| "distance", cosine_distance(daft.col("query_embedding"), daft.col("embedding")) | ||
| ) | ||
|
|
||
| # Sort by distance and show top results | ||
| results = results.sort("distance").select("query_text", "text", "distance", "embedding") | ||
| results.show() | ||
| ``` | ||
|
|
||
| ```{title="Output"} | ||
| ╭───────────────┬────────────────────────────────┬────────────────────┬──────────────────────────╮ | ||
| │ query_text ┆ text ┆ distance ┆ embedding │ | ||
| │ --- ┆ --- ┆ --- ┆ --- │ | ||
| │ String ┆ String ┆ Float64 ┆ Embedding[Float32; 1536] │ | ||
| ╞═══════════════╪════════════════════════════════╪════════════════════╪══════════════════════════╡ | ||
| │ What is Daft? ┆ Daft is a distributed datafra… ┆ 0.3621492191359764 ┆ ▄▇▆▅▄▄█▆▄▄▃▂▄▃▃▃▁▄▃▃▄▄▃▂ │ | ||
| ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ | ||
| │ What is Daft? ┆ Python is a high-level progra… ┆ 0.9163975397319742 ┆ ▇▆▅▇▅▆█▇▃▄▆▄▄▁▅▄▅▃▁▃▃▂▅▃ │ | ||
| ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ | ||
| │ What is Daft? ┆ Embeddings capture semantic m… ┆ 0.9374004015203741 ┆ ▄█▅▄▅▅▅▇▄▃▂▁▃▄▄▁▃▃▂▂▂▂▁▃ │ | ||
| ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ | ||
| │ What is Daft? ┆ Machine learning models requi… ┆ 0.9696998373223874 ┆ ▇▇▆▃▄▆▅█▆▂▄▃▄▄▂▄▂▁▂▂▁▃▂▁ │ | ||
| ╰───────────────┴────────────────────────────────┴────────────────────┴──────────────────────────╯ | ||
|
|
||
| (Showing first 4 of 4 rows) | ||
| ``` | ||
|
|
||
| ## Building a Document Search Pipeline | ||
|
|
||
| For production use cases, you'll typically combine embeddings with LLM-powered metadata extraction and write the results to a vector database. | ||
|
|
||
| This example shows an end-to-end pipeline that: | ||
|
|
||
| 1. Loads PDF documents from cloud storage | ||
| 2. Extracts structured metadata using an LLM | ||
| 3. Generates vector embeddings from the abstracts | ||
| 4. Writes everything to Turbopuffer for semantic search | ||
|
|
||
| ```python | ||
| # /// script | ||
| # description = "This example shows how using LLMs and embedding models, Daft chunks documents, extracts metadata, generates vectors, and writes them to any vector database..." | ||
| # dependencies = ["daft[openai, turbopuffer]", "pymupdf"] | ||
| # /// | ||
| import os | ||
| import daft | ||
| from daft import col, lit | ||
| from daft.functions import embed_text, prompt, file, unnest, monotonically_increasing_id | ||
| from pydantic import BaseModel | ||
|
|
||
| class Classifier(BaseModel): | ||
| title: str | ||
| author: str | ||
| year: int | ||
| keywords: list[str] | ||
| abstract: str | ||
|
|
||
| daft.set_execution_config(enable_dynamic_batching=True) | ||
| daft.set_provider("openai", api_key=os.environ.get("OPENAI_API_KEY")) | ||
|
|
||
| # Load documents and generate vector embeddings | ||
| df = ( | ||
| daft.from_glob_path("hf://datasets/Eventual-Inc/sample-files/papers/*.pdf").limit(10) | ||
| .with_column( | ||
| "metadata", | ||
| prompt( | ||
| messages=file(col("path")), | ||
| system_message="Read the paper and extract the classifier metadata.", | ||
| return_format=Classifier, | ||
| model="gpt-5-mini", | ||
| ) | ||
| ) | ||
| .with_column( | ||
| "abstract_embedding", | ||
| embed_text( | ||
| daft.col("metadata")["abstract"], | ||
| model="text-embedding-3-large" | ||
| ) | ||
| ) | ||
| .with_column("id", monotonically_increasing_id()) | ||
| .select("id", "path", unnest(col("metadata")), "abstract_embedding") | ||
| ) | ||
|
|
||
| # Write to Turbopuffer | ||
| df.write_turbopuffer( | ||
| namespace="ai_papers", | ||
| api_key=os.environ.get("TURBOPUFFER_API_KEY"), | ||
| distance_metric="cosine_distance", | ||
| region='us-west-2', | ||
| schema={ | ||
| "id": "int64", | ||
| "path": "string", | ||
| "title": "string", | ||
| "author": "string", | ||
| "year": "int", | ||
| "keywords": "list[string]", | ||
| "abstract": "string", | ||
| "abstract_embedding": "vector", | ||
| } | ||
| ) | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The model name "gpt-5-mini" does not exist. This should likely be "gpt-4o-mini" or another valid OpenAI model. GPT-5 has not been released as of the knowledge cutoff.