Chatlas: A Unified LLM Interface for Python

1 Introduction to Chatlas

chatlas is a Python package that provides a simple and unified interface for interacting with various large language model (LLM) providers. This allows you to switch between different models and providers with minimal code changes, streamlining your workflow.

This document demonstrates how to use chatlas to connect to Google’s Gemini models, perform text generation, and analyze images.

2 Setup

First, you need to install the necessary Python packages.

Code
# Ensure required Python packages are available to reticulate
reticulate::py_require(c('chatlas', 'keyring', 'google-genai', 'Pillow'))
Code
# Install the chatlas package and its dependencies
pip3 install -U chatlas
pip3 install -q -U google-genai
pip3 install shiny
pip3 install keyring
Code
# Import necessary classes and functions
from chatlas import ChatGoogle, token_usage, content_image_file
import keyring

3 Using Google Gemini

Here’s how to set up and use a Google Gemini model with chatlas.

Code
# Import the ChatGoogle class
from chatlas import ChatGoogle

# Initialize the chat model
chat_google_model = ChatGoogle(
  model="gemini-1.5-flash",
  api_key=keyring.get_password("system", "google_ai_api_key")
  # You can also set a system prompt to guide the model's behavior
  #, system_prompt="You are an IT expert"
)

chat_google_model

3.1 Token Counting

You can count the number of tokens in a given text before sending it to the model. This is useful for managing costs and staying within model limits.

Code
# Count the tokens in a prompt
chat_google_model.token_count("What preceding languages most influenced Python?")

3.2 Text Generation

Now, let’s generate a response from the model.

Code
# Send a prompt to the model and get a response
result = chat_google_model.chat("What preceding languages most influenced Python?")
result

4 Interactive Modes

chatlas offers interactive modes for a more conversational experience.

4.1 Web Application

You can launch a web-based chat application.

Code
# Launch the interactive web app
chat_google_model.app()

4.2 Console Mode

Alternatively, you can chat with the model directly in your console.

Code
# Start an interactive console session
chat_google_model.console()

5 Vision Capabilities

chatlas also supports multi-modal models that can analyze images.

Code
# Analyze an image and describe what you see
chat_google_model.chat(
    "What do you see in this image?",
    content_image_file("images/IMG_0220.jpg")
)

6 Advanced Features

chatlas also supports more advanced features like structured output and tool calling, which allow for more complex and powerful applications.

7 Conclusion

chatlas simplifies the process of working with large language models in Python by providing a consistent and easy-to-use interface. Whether you’re performing simple text generation, analyzing images, or building complex applications with structured data, chatlas is a valuable tool for any data scientist or developer working with LLMs.

8 Reference