This is the SDK for TrainLoop. It allows you to send data to TrainLoop to be used for finetuning.
pip install trainloop-sdk
go get -u github.com/TrainLoop/[email protected]
npm install @trainloop/sdk
You can get an API key from the TrainLoop dashboard.
import os
from trainloop import Client, SampleFeedbackType
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
def main():
# Initialize the TrainLoop client with your api key
client = Client(api_key=os.getenv("TRAINLOOP_API_KEY"))
# Some example message thread - this should be generated by your system
messages = [
{"role": "system", "content": "System message here"},
{"role": "user", "content": "Hello from the user!"},
]
# Send data to trainloop and let us know if this was a success case or a failure case
success = client.send_data(
messages, sample_feedback=SampleFeedbackType.GOOD, dataset_id="test-dataset"
)
if success:
print("Data sent successfully!")
else:
print("Data was not sent. Check logs for details.")
if __name__ == "__main__":
main()
package main
// 1. Import the SDK
import (
trainloop "github.com/TrainLoop/sdk/go"
)
// 2. Get the api key from the environment
apiKey := os.Getenv("TRAINLOOP_API_KEY")
if apiKey == "" {
log.Fatal("TRAINLOOP_API_KEY environment variable is not set")
}
// 3. Initialize the SDK
client := trainloop.NewClient(apiKey)
// 4. Do some inference and generate some message thread
messages := []trainloop.Message{
{Role: "user", Content: "Hello, from the user"},
{Role: "assistant", Content: "Hello, from the assistant"},
}
// 3. Send data to TrainLoop (good or bad)
success, err := client.SendData(messages, trainloop.GoodFeedback, "example-dataset-id")
if err != nil {
log.Printf("Failed to send data to TrainLoop: %v", err)
return
}
if success {
log.Printf("Data sent to TrainLoop successfully")
}
import { Client, SampleFeedbackType } from '@trainloop/sdk';
import * as dotenv from 'dotenv';
async function main() {
dotenv.config();
// Check if API key was loaded
const apiKey = process.env.TRAINLOOP_API_KEY;
if (!apiKey) {
console.error('API key not found in environment variables');
return;
}
// Initialize the TrainLoop client with your api key
const client = new Client(apiKey);
// Example messages
const messages = [
{ role: 'system', content: 'System message here' },
{ role: 'user', content: 'Hello from the user!' },
];
// Send data
const success = await client.sendData(
messages,
SampleFeedbackType.GOOD,
'test-dataset'
);
if (success) {
console.log('Data sent successfully!');
} else {
console.log('Data was not sent. Check logs for details.');
}
}
main().catch(error => {
console.error('Unexpected error:', error);
});
For all SDKs, it's recommended to use environment variables to manage your API key. Create a .env
file in your project:
TRAINLOOP_API_KEY=your_api_key_here
Each example shows how you can load from the environment variables.
- Python: Use python-dotenv with
load_dotenv()
- TypeScript/JavaScript: Use dotenv with
dotenv.config()
- Go: Environment variables are loaded automatically by the operating system or your deployment platform