Skip to main content

Install the Library

To get started, install the together Python library:
pip install --upgrade together
Then, configure your API key by setting the TOGETHER_API_KEY environment variable:
export TOGETHER_API_KEY=xxxxx

Upload your Data

See the data preparation instruction to understand the requirements and its readiness. To upload your data, run the following code:
import os
from together import Together

client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))

resp = client.files.upload(file="joke_explanations.jsonl")  # uploads a file
print(resp.dict())
Here is the output:
{
  "id": "file-f6d02dc8-c9f9-4e38-ae63-7899fa603a86",
  "object": "file",
  "created_at": 1713481731,
  "type": null,
  "purpose": "fine-tune",
  "filename": "joke_explanations.jsonl",
  "bytes": 0,
  "line_count": 0,
  "processed": false
}
You will get back the file id of the file you just uploaded, but if you forget it, you can get the id’s of all the files you have uploaded using files.list(). You’ll need these id’s that start with file-960be810-4d.... in order to start a fine-tuning job.
import os
from together import Together

client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))

filesUploaded = client.files.list()  # lists all uploaded files
print(filesUploaded)
[{'filename': 'joke_explanations.jsonl',
  'bytes': 40805,
  'created_at': 1691710036,
  'id': 'file-960be810-4d33-449a-885a-9f69bd8fd0e2',
  'purpose': 'fine-tune',
  'object': 'file',
  'LineCount': 0,
  'Processed': True},
 {'filename': 'sample_jsonl.jsonl',
  'bytes': 1235,
  'created_at': 1692190883,
  'id': 'file-d0d318cb-b7d9-493a-bd70-1cfe089d3815',
  'purpose': 'fine-tune',
  'object': 'file',
  'LineCount': 0,
  'Processed': True}]

Start Fine-Tuning

Once you’ve uploaded your dataset, copy your file id from the output above and select a base model to fine-tune. Check out the full models list available for fine-tuning. Run the following command to start your fine-tuning job using fine_tuning.create:
import os
from together import Together

client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))

resp = client.fine_tuning.create(
    training_file="file-d0d318cb-b7d9-493a-bd70-1cfe089d3815",
    model="meta-llama/Meta-Llama-3-8B",
    n_epochs=3,
    n_checkpoints=1,
    batch_size=4,
    learning_rate=1e-5,
    wandb_api_key="1a2b3c4d5e.......",
)

fine_tune_id = resp["id"]
print(resp)
Here is an example of part of the resp response to highlight some of the useful information about your finetune job.
{
    "id": "ft-3b883474-f39c-40d9-9d5a-7f97ba9eeb9f",
    "training_file": "file-2490a204-16e2-481e-a3d5-5636a6f3a4ea",
    "model": "meta-llama/Meta-Llama-3-8B",
    "output_name": "[email protected]/Meta-Llama-3-8B-2024-04-18-19-37-52",
    "n_epochs": 1,
    "n_checkpoints": 1,
    "batch_size": 32,
    "learning_rate": 3e-05,
    "created_at": "2024-04-18T19:37:52.611Z",
    "updated_at": "2024-04-18T19:37:52.611Z",
    "status": "pending",
    "events": [
        {
            "object":