The Friendli Python SDK provides a powerful and flexible way to interact with FriendliAI services, including Serverless Endpoints, Dedicated Endpoints, and Container. This allows developers to easily integrate their Python applications with FriendliAI.
The SDK supports streaming responses using server-sent events, which can be consumed using a simple for loop:
Copy
Ask AI
import osfrom friendli import SyncFriendliwith SyncFriendli( token=os.environ["FRIENDLI_TOKEN"],) as friendli: res = friendli.serverless.chat.stream( messages=[ { "content": "You are a helpful assistant.", "role": "system", }, { "content": "Hello!", "role": "user", }, ], model="meta-llama-3.1-8b-instruct", max_tokens=200, ) with res as event_stream: for event in event_stream: # Process each chunk as it arrives print(event, flush=True)
You can pass your own logger to the client class to help troubleshoot and diagnose issues during API interactions. This is especially useful when you encounter unexpected behavior or errors.
Copy
Ask AI
import loggingimport osfrom friendli import SyncFriendli# Configure your custom logger, for example:logger = logging.getLogger(__name__)logging.basicConfig( format="[%(filename)s:%(lineno)s - %(funcName)s()] %(message)s", level=logging.INFO, handlers=[logging.StreamHandler()],)with SyncFriendli( server_url=SERVER_URL, token=TOKEN, debug_logger=logger, # Pass your logger here) as friendli: # Your code here pass
Our SDK provides a straightforward way to create, retrieve, and update datasets within your projects. Datasets can contain samples across various modalities—such as text, images, and more—allowing flexible and comprehensive dataset construction for your fine-tuning and validation workflows.
Copy
Ask AI
import osfrom friendli.friendli import SyncFriendlifrom friendli.models import SampleTEAM_ID = os.environ["FRIENDLI_TEAM_ID"]PROJECT_ID = os.environ["FRIENDLI_PROJECT_ID"]TOKEN = os.environ["FRIENDLI_TOKEN"]with SyncFriendli( token=TOKEN, x_friendli_team=TEAM_ID,) as friendli: # Create dataset with friendli.dataset.create( modality=["TEXT", "IMAGE"], name="test-create-dataset-sync", project_id=PROJECT_ID, ) as dataset: # Read dataset with open("dataset.jsonl", "rb") as f: data = [Sample.model_validate_json(line) for line in f] # Add samples to dataset dataset.upload_samples( samples=data, split="train", )
You can download and upload files to and from our database. This feature is primarily designed for storing sample files related to datasets, with additional use cases planned for the future.
Copy
Ask AI
import ioimport osfrom hashlib import sha256import httpxfrom friendli import SyncFriendliTEAM_ID = os.environ["FRIENDLI_TEAM_ID"]PROJECT_ID = os.environ["FRIENDLI_PROJECT_ID"]TOKEN = os.environ["FRIENDLI_TOKEN"]with SyncFriendli( token=TOKEN,) as friendli: # Read data from file with open("lorem.txt", "rb") as f: data = f.read() # Inititate upload init_upload_res = friendli.file.init_upload( digest=f"sha256:{sha256(data).hexdigest()}", name="lorem.txt", project_id=PROJECT_ID, size=len(data), x_friendli_team=TEAM_ID, ) # Upload to S3 if init_upload_res.upload_url is not None: httpx.post( url=init_upload_res.upload_url, data=init_upload_res.aws, files={"file": io.BytesIO(data)}, timeout=60, ).raise_for_status() # Complete upload friendli.file.complete_upload( file_id=init_upload_res.file_id, x_friendli_team=TEAM_ID, ) # Get download URL get_download_url_res = friendli.file.get_download_url( file_id=init_upload_res.file_id, x_friendli_team=TEAM_ID, ) print(get_download_url_res.download_url)