Usage Overview¶
This Usage section provides comprehensive guides on how to effectively use the key features of ClientAI. Each topic focuses on a specific aspect of usage, ensuring you have all the information needed to leverage the full potential of ClientAI in your projects.
Key Topics¶
1. Initializing ClientAI¶
This guide covers the process of initializing ClientAI with different AI providers. It provides a step-by-step approach to setting up ClientAI for use with OpenAI, Replicate, and Ollama.
2. Text Generation with ClientAI¶
Learn how to use ClientAI for text generation tasks. This guide explores the various options and parameters available for generating text across different AI providers.
3. Chat Functionality in ClientAI¶
Discover how to leverage ClientAI's chat functionality. This guide covers creating chat conversations, managing context, and handling chat-specific features across supported providers.
4. Working with Multiple Providers¶
Explore techniques for effectively using multiple AI providers within a single project. This guide demonstrates how to switch between providers and leverage their unique strengths.
5. Using Ollama Manager¶
Learn how to use Ollama Manager for streamlined prototyping and development. This guide covers server lifecycle management, resource configuration, and best practices for different use cases.
6. Handling Responses and Errors¶
Learn best practices for handling responses from AI providers and managing potential errors. This guide covers response parsing, error handling, and retry strategies.
- Soon
Getting Started¶
To make the most of these guides, we recommend familiarizing yourself with basic Python programming and asynchronous programming concepts, as ClientAI leverages these extensively.
Quick Start Example¶
Here's a simple example to get you started with ClientAI:
from clientai import ClientAI
# Initialize the client
client = ClientAI('openai', api_key="your-openai-api-key")
# Generate text
response = client.generate_text(
"Explain the concept of machine learning in simple terms.",
model="gpt-3.5-turbo"
)
print(response)
For more detailed examples and explanations, refer to the specific guides linked above.
Advanced Usage¶
Streaming Responses¶
ClientAI supports streaming responses for compatible providers. Here's a basic example:
for chunk in client.generate_text(
"Tell me a long story about space exploration",
model="gpt-3.5-turbo",
stream=True
):
print(chunk, end="", flush=True)
Using Different Models¶
ClientAI allows you to specify different models for each provider. For example:
# Using GPT-4 with OpenAI
openai_response = openai_client.generate_text(
"Explain quantum computing",
model="gpt-4"
)
# Using Llama 2 with Replicate
replicate_response = replicate_client.generate_text(
"Describe the process of photosynthesis",
model="meta/llama-2-70b-chat:latest"
)
Best Practices¶
- API Key Management: Always store your API keys securely, preferably as environment variables.
- Error Handling: Implement proper error handling to manage potential API failures or rate limiting issues.
- Model Selection: Choose appropriate models based on your task requirements and budget considerations.
- Context Management: For chat applications, manage conversation context efficiently to get the best results.
Contribution¶
If you have suggestions or contributions to these guides, please refer to our Contributing Guidelines. We appreciate your input in improving our documentation and making ClientAI more accessible to all users.