Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TrainLoop/sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TrainLoop SDK

This is the SDK for TrainLoop. It allows you to send data to TrainLoop to be used for finetuning.

Installation

Python

pip install trainloop-sdk

Go

go get -u github.com/TrainLoop/[email protected]

TypeScript

npm install @trainloop/sdk

Getting an API key

You can get an API key from the TrainLoop dashboard.

Usage

Python

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()

Go

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")
}

TypeScript

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);
});

Environment Setup

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

About

The Trainloop SDK for easy data collection

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •