Parallel inference calls to LLM APIs using Polars dataframes with Pydantic-based structured outputs
Project description
Polar Llama
Overview
Polar Llama is a Python library designed to enhance the efficiency of making parallel inference calls to the ChatGPT API using the Polars dataframe tool. This library enables users to manage multiple API requests simultaneously, significantly speeding up the process compared to serial request handling.
Key Features
- Parallel Inference: Send multiple inference requests in parallel to the ChatGPT API without waiting for each individual request to complete.
- Integration with Polars: Utilizes the Polars dataframe for organizing and handling requests, leveraging its efficient data processing capabilities.
- Easy to Use: Simplifies the process of sending queries and retrieving responses from the ChatGPT API through a clean and straightforward interface.
- Multi-Message Support: Create and process conversations with multiple messages in context, supporting complex multi-turn interactions.
- Multiple Provider Support: Works with OpenAI, Anthropic, Gemini, Groq, and AWS Bedrock models, giving you flexibility in your AI infrastructure.
- Structured Outputs: Define response schemas using Pydantic models for type-safe, validated LLM outputs returned as Polars Structs with direct field access.
Installation
To install Polar Llama, you can use pip:
pip install polar-llama
Alternatively, for development purposes, you can install from source:
maturin develop
Example Usage
Here's how you can use Polar Llama to send multiple inference requests in parallel:
import polars as pl
from polar_llama import string_to_message, inference_async, Provider
import dotenv
dotenv.load_dotenv()
# Example questions
questions = [
'What is the capital of France?',
'What is the difference between polars and pandas?'
]
# Creating a dataframe with questions
df = pl.DataFrame({'Questions': questions})
# Adding prompts to the dataframe
df = df.with_columns(
prompt=string_to_message("Questions", message_type='user')
)
# Sending parallel inference requests
df = df.with_columns(
answer=inference_async('prompt', provider = Provider.OPENAI, model = 'gpt-4o-mini')
)
Multi-Message Conversations
Polar Llama now supports multi-message conversations, allowing you to maintain context across multiple turns:
import polars as pl
from polar_llama import string_to_message, combine_messages, inference_messages
import dotenv
dotenv.load_dotenv()
# Create a dataframe with system prompts and user questions
df = pl.DataFrame({
"system_prompt": [
"You are a helpful assistant.",
"You are a math expert."
],
"user_question": [
"What's the weather like today?",
"Solve x^2 + 5x + 6 = 0"
]
})
# Convert to structured messages
df = df.with_columns([
pl.col("system_prompt").invoke("string_to_message", message_type="system").alias("system_message"),
pl.col("user_question").invoke("string_to_message", message_type="user").alias("user_message")
])
# Combine into conversations
df = df.with_columns(
pl.invoke("combine_messages", pl.col("system_message"), pl.col("user_message")).alias("conversation")
)
# Send to model and get responses
df = df.with_columns(
pl.col("conversation").invoke("inference_messages", provider="openai", model="gpt-4").alias("response")
)
AWS Bedrock Support
Polar Llama now supports AWS Bedrock models. To use Bedrock, ensure you have AWS credentials configured (via AWS CLI, environment variables, or IAM roles):
import polars as pl
from polar_llama import string_to_message, inference_async
import dotenv
dotenv.load_dotenv()
# Example questions
questions = [
'What is the capital of France?',
'Explain quantum computing in simple terms.'
]
# Creating a dataframe with questions
df = pl.DataFrame({'Questions': questions})
# Adding prompts to the dataframe
df = df.with_columns(
prompt=string_to_message("Questions", message_type='user')
)
# Using AWS Bedrock with Claude model
df = df.with_columns(
answer=inference_async('prompt', provider='bedrock', model='anthropic.claude-3-haiku-20240307-v1:0')
)
Structured Outputs with Pydantic
Polar Llama supports structured outputs using Pydantic models. Define your response schema as a Pydantic BaseModel, and the LLM will return validated, type-safe data as a Polars Struct:
import polars as pl
from polar_llama import inference_async, Provider
from pydantic import BaseModel
# Define your response schema
class MovieRecommendation(BaseModel):
title: str
genre: str
year: int
reason: str
# Create a dataframe
df = pl.DataFrame({
'prompt': ['Recommend a great sci-fi movie from the 2010s']
})
# Get structured output
df = df.with_columns(
recommendation=inference_async(
pl.col('prompt'),
provider=Provider.OPENAI,
model='gpt-4o-mini',
response_model=MovieRecommendation
)
)
# Access struct fields directly!
print(df['recommendation'].struct.field('title')[0]) # "Interstellar"
print(df['recommendation'].struct.field('year')[0]) # 2014
Key Features:
- Type Safety: Responses are validated against your Pydantic schema
- Direct Field Access: Use
.struct.field('field_name')to access individual fields - Error Handling: Built-in
_error,_details, and_rawfields for graceful error handling - Works Everywhere: Compatible with
inference_async(),inference(), andinference_messages() - Multi-Provider: Works with OpenAI, Anthropic, Groq, Gemini, and Bedrock
Error Handling:
# Check for errors in responses
error = df['recommendation'].struct.field('_error')[0]
if error:
print(f"Error: {error}")
print(f"Details: {df['recommendation'].struct.field('_details')[0]}")
print(f"Raw response: {df['recommendation'].struct.field('_raw')[0]}")
Benefits
- Speed: Processes multiple queries in parallel, drastically reducing the time required for bulk query handling.
- Scalability: Scales efficiently with the increase in number of queries, ideal for high-demand applications.
- Ease of Integration: Integrates seamlessly into existing Python projects that utilize Polars, making it easy to add parallel processing capabilities.
- Context Preservation: Maintain conversation context with multi-message support for more natural interactions.
- Provider Flexibility: Choose from multiple LLM providers based on your needs and access.
- Type Safety: Get validated, structured outputs using Pydantic schemas for reliable data extraction.
Testing
Polar Llama includes a comprehensive test suite that validates parallel execution, provider support, and core functionality.
Setup:
-
Copy
.env.exampleto.envand add your API keys:cp .env.example .env # Edit .env and add your provider API keys
-
Install test dependencies:
pip install -r tests/requirements.txt
Run Python tests:
pytest tests/ -v
Run Rust tests:
cargo test --test model_client_tests -- --nocapture
Tests automatically detect configured providers and only run tests for those with valid API keys. See tests/README.md for detailed testing documentation.
Contributing
We welcome contributions to Polar Llama! If you're interested in improving the library or adding new features, please feel free to fork the repository and submit a pull request.
License
Polar Llama is released under the MIT license. For more details, see the LICENSE file in the repository.
Roadmap
- Multi-Message Support: Support for multi-message conversations to maintain context.
- Multiple Provider Support: Support for different LLM providers (OpenAI, Anthropic, Gemini, Groq, AWS Bedrock).
- Structured Data Outputs: Add support for structured data outputs using Pydantic models with type validation and Polars Struct returns.
- Streaming Responses: Support for streaming responses from LLM providers.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polar_llama-0.2.0.tar.gz.
File metadata
- Download URL: polar_llama-0.2.0.tar.gz
- Upload date:
- Size: 196.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc09b253c1089481028cde27a19de7951b4aea16c8eb28db044752c844a5c01b
|
|
| MD5 |
28b5c1a70c5dc17ee7e9438311f33e10
|
|
| BLAKE2b-256 |
6ebb4d24530cc02cf649fae31812cf0c47dfe56c28d913d6b0e31b3aeb6732a1
|
File details
Details for the file polar_llama-0.2.0-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: polar_llama-0.2.0-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 10.3 MB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1406108aeb0a15bce552ea59e2f28413bcad699d2b2ddbd26ba120e59d5a697
|
|
| MD5 |
5b1e185a58b322bb41c4a4fe91f80b9e
|
|
| BLAKE2b-256 |
cce5e24c61c90105c450409d056ef1611b9405f0d3ab5256aa9b9716a2c83de5
|
File details
Details for the file polar_llama-0.2.0-cp38-abi3-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: polar_llama-0.2.0-cp38-abi3-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 12.5 MB
- Tags: CPython 3.8+, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1403b8ad74140df75dd7962fa6c552775fc1efcdeb421a185d42f314b9a3a57f
|
|
| MD5 |
20bb8d67ea260cc061ba6dc6e6dab1be
|
|
| BLAKE2b-256 |
110d54d7e827d7b2a22014bf2dc86eea13c5175173bd359f39414d3d46fdaff8
|
File details
Details for the file polar_llama-0.2.0-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: polar_llama-0.2.0-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 10.9 MB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f29e3f8ee31cd85446971c806d8881008c0e6311989aa68ed6154c4978220fb
|
|
| MD5 |
a8f105519f8d2dfd5f3cf756524353cb
|
|
| BLAKE2b-256 |
3a63cbc706bb1f7e1ef604c9ad0308c0b537419336efc9b787518967d016657c
|
File details
Details for the file polar_llama-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: polar_llama-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 11.3 MB
- Tags: CPython 3.8+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a199ad1909dba42f97d57c91fc423e494c7b70c4342ae28f42437466da0daf3
|
|
| MD5 |
97513ed8e3c932418f7e4eeb684f0a59
|
|
| BLAKE2b-256 |
e7b5cf062c0b8dc3e4a96d21909b32b6f9ee2e439ed6d651e8ce766cf6bd647c
|