Skip to main content
This guide provides a simple introduction to using the langchain_bytez package to interact with the Bytez API. It covers text generation, chat models (including multimodal), image-text-to-text, video-text-to-text, audio-text-to-text, streaming, async operations, and provides examples to get you started.

Installation

First, install the package:

Authentication

You’ll need your Bytez API key to use the package. Set it as an environment variable:
Replace "YOUR_BYTEZ_API_KEY" with your actual API key.

Text Generation (LLM)

The BytezLLM class allows you to use Bytez for text generation.
python

Chat Models

The BytezChatModel class provides a convenient way to interact with chat models.
python

Multimodal Models

BytezChatModel also supports multimodal models by accepting a list of messages, where the content of each message can be text or image data.
python

Image-to-Text, Video-to-Text, and Audio-to-Text

Bytez supports different kinds of multimodal models for extracting information from media. The input format is similar to the image example, but you’ll use the appropriate content type for each media type. These are also supported with both synchronous and asynchronous invocations, as well as streaming and batch. Make sure you replace the model_id with a model that supports the type of input you are giving it.

Image-to-Text

python

Video-to-Text

python

Audio-to-Text

python

Streaming

To enable streaming, set streaming=True in the constructor. This allows you to receive responses in real-time. The provided StreamingStdOutCallbackHandler is a simple way to see the streamed output.
python

Extending Callback Handlers (Observability)

You can extend the behavior of the model runs by creating your own callback handlers. BytezStdOutCallbackHandler is provided as a utility, but you’re free to create your own for enhanced logging, metrics, or other custom behavior.
python

Async Operations

The langchain_bytez package also fully supports asynchronous operations using asyncio.
python

Configuration Options (kwargs)

Both BytezChatModel and BytezLLM accept the following keyword arguments:
  • model_id (str): The Bytez model ID (required). Check the Bytez documentation for available models.
  • api_key (str): Your Bytez API key (required).
  • capacity (dict): Controls cluster scaling. Supports min, max, and desired keys.
  • timeout (int): Timeout in minutes for cluster shutdown after the last inference (optional).
  • streaming (bool): Enable streaming responses (default: False).
  • params (dict): Parameters to pass to the Bytez API (optional), such as max_new_tokens.
  • headers (dict): Custom headers to send with the API request (optional). Useful for authentication.
  • http_timeout_s (float): Timeout in seconds for the HTTP request (default: 300 seconds).

Resources

Feedback: Join our Discord or open an issue on GitHub

Important Notes

  • Replace placeholder model_id values with actual Bytez model IDs. Ensure that the model ID you select supports the media type that you provide.
  • Ensure your API_KEY environment variable is correctly set.
  • Check the Bytez documentation for the latest model availability and API parameters.
  • For more complex use cases, consider creating your own custom callback handlers to monitor the lifecycle of model runs.