Initializing ClientAI¶
This guide covers the process of initializing ClientAI with different AI providers. You'll learn how to set up ClientAI for use with OpenAI, Replicate, and Ollama.
Table of Contents¶
- Prerequisites
- OpenAI Initialization
- Replicate Initialization
- Ollama Initialization
- Multiple Provider Initialization
- Best Practices
Prerequisites¶
Before initializing ClientAI, ensure you have:
- Installed ClientAI:
pip install clientai[all]
- Obtained necessary API keys for the providers you plan to use
- Basic understanding of Python and asynchronous programming
OpenAI Initialization¶
To initialize ClientAI with OpenAI:
Replace "your-openai-api-key"
with your actual OpenAI API key.
Replicate Initialization¶
To initialize ClientAI with Replicate:
from clientai import ClientAI
replicate_client = ClientAI('replicate', api_key="your-replicate-api-key")
Replace "your-replicate-api-key"
with your actual Replicate API key.
Ollama Initialization¶
To initialize ClientAI with Ollama:
Ensure that you have Ollama running locally on the specified host.
Groq Initialization¶
To initialize ClientAI with Groq:
Multiple Provider Initialization¶
You can initialize multiple providers in the same script:
from clientai import ClientAI
openai_client = ClientAI('openai', api_key="your-openai-api-key")
replicate_client = ClientAI('replicate', api_key="your-replicate-api-key")
groq_client = ClientAI('groq', api_key="your-groq-api-key")
ollama_client = ClientAI('ollama', host="http://localhost:11434")
Best Practices¶
- Environment Variables: Store API keys in environment variables instead of hardcoding them in your script:
import os
from clientai import ClientAI
openai_client = ClientAI('openai', api_key=os.getenv('OPENAI_API_KEY'))
- Error Handling: Wrap initialization in a try-except block to handle potential errors:
try:
client = ClientAI('openai', api_key="your-openai-api-key")
except ValueError as e:
print(f"Error initializing ClientAI: {e}")
- Configuration Files: For projects with multiple providers, consider using a configuration file:
import json
from clientai import ClientAI
with open('config.json') as f:
config = json.load(f)
openai_client = ClientAI('openai', **config['openai'])
replicate_client = ClientAI('replicate', **config['replicate'])
- Lazy Initialization: If you're not sure which provider you'll use, initialize clients only when needed:
def get_client(provider):
if provider == 'openai':
return ClientAI('openai', api_key="your-openai-api-key")
elif provider == 'replicate':
return ClientAI('replicate', api_key="your-replicate-api-key")
# ... other providers ...
# Use the client when needed
client = get_client('openai')
By following these initialization guidelines, you'll be well-prepared to start using ClientAI with various AI providers in your projects.