Getting Started with Python
Installation
Selecting files using JSON metadata
A storage consists of images of cats and dogs
(dog.1048.jpg, cat.1009.jpg), annotated with
ground truth and model inferences in the json-pairs format, where
each image has a matching JSON file like cat.1009.json:
Example of downloading only high-confidence cat inferred images
using JSON metadata:
Data curation with a local AI model
Batch inference with a simple sentiment model using the
transformers library:
Note, transformers works only if torch, tensorflow >= 2.0, or flax are installed.
The code below downloads files from the cloud, and applies a user-defined function to each one of them. All files with a positive sentiment detected are then copied to the local directory.
13 files were exported
LLM judging chatbots
LLMs can work as universal classifiers. In the example below, we employ a free API from Mistral to judge the publicly available chatbot dialogs. Please get a free Mistral API key at https://console.mistral.ai
DataChain can parallelize API calls; the free Mistral tier supports up to 4 requests at the same time.
With the instruction above, the Mistral model considers 31/50 files to hold the successful dialogues:
Serializing Python objects
LLM responses may contain valuable information for analytics, such as the number of tokens used or the model performance parameters.
Instead of extracting this information from the Mistral response data
structure (class ChatCompletionResponse), DataChain can
serialize the entire LLM response to the internal DB:
Output:
Iterating over Python data structures
In the previous examples, datasets were saved in the embedded database
(SQLite in folder .datachain of the working directory). These datasets were automatically versioned, and
can be accessed using dc.read_dataset("dataset_name").
Here is how to retrieve a saved dataset and iterate over the objects:
Output:
Vectorized analytics over Python objects
Some operations can run inside the DB without deserialization. For instance, let's calculate the total cost of using the LLM APIs, assuming the Mixtral call costs $2 per 1M input tokens and $6 per 1M output tokens:
Output:
PyTorch data loader
Chain results can be exported or passed directly to PyTorch dataloader. For example, if we are interested in passing image and a label based on file name suffix, the following code will do it: