Article·npmjs.com
llmapi-designautomationinfrastructurepythonopen-sourceai-agentspi-ai
@mariozechner/pi-ai
Pi-AI provides a unified Python API to interact with multiple Large Language Models, automating model discovery and provider configuration. It simplifies LLM integration, allowing developers to seamlessly switch between different AI models and providers with minimal code changes, enhancing development speed and application resilience.
beginner10 min4 steps
The play
- Install Pi-AIAdd the `pi-ai` library to your Python project.
- Initialize LLMClient with Provider CredentialsCreate an `LLMClient` instance, providing API keys for your desired LLM providers (e.g., OpenAI, Anthropic). The client will automatically discover available models.
- Make a Chat Completion RequestUse the client to make a chat completion request to a specific model, such as OpenAI's `gpt-4o`. The API structure is consistent across providers.
- Switch to a Different LLMLeverage Pi-AI's unified API to easily switch to another model, like Anthropic's `claude-3-opus-20240229`, using the exact same `client.chat.completions.create` method.
Starter code
pip install pi-ai
from pi_ai import LLMClient
# Replace with your actual API keys
client = LLMClient(
provider_config={
"openai": {"api_key": "YOUR_OPENAI_KEY"},
"anthropic": {"api_key": "YOUR_ANTHROPIC_KEY"}
}
)
response = client.chat.completions.create(
model="gpt-4o", # Or "claude-3-opus-20240229"
messages=[
{"role": "system", "content": "You are a helpful assistant that responds concisely."},
{"role": "user", "content": "Explain the benefits of a unified LLM API."}
]
)
print(response.choices[0].message.content)Source