🤖 AI Module
The AI module provides a unified interface for working with Artificial Intelligence providers. It supports chat completion, embeddings, image generation, vision analysis, speech generation, moderation, and streaming responses.
- ⚙️ AI Provider Configuration
- 💬 Chat Completion
- 🧠 Text Embeddings
- 👁️ Vision Analysis
- 🎨 Image Generation
- 🔊 Speech Generation
- 🛡️ Content Moderation
- 🌊 Streaming Responses
- 🔌 Provider Registry
- 🔑 API Configuration
🚀 Quick Example
Configure an AI provider and send a chat request.
💻 Code
import allkitpy
allkitpy.ai.configure(
provider="openai",
api_key="YOUR_API_KEY"
)
response = allkitpy.ai.chat(
"Explain Python"
)
print(response)
📤 Output
Python is a high-level
programming language...
📚 Functions
⚙️ configure()
Configure the global AI provider settings.
💻 Code
import allkitpy
allkitpy.ai.configure(
provider="openai",
api_key="YOUR_API_KEY",
model="gpt-4"
)
📤 Output
Provider configured
💬 chat()
Generate a response from an AI provider.
💻 Code
import allkitpy
answer = allkitpy.ai.chat(
"What is AI?"
)
print(answer)
📤 Output
AI is the simulation
of human intelligence
by machines.
🧠 embedding()
Generate numerical vector representations of text using an AI embedding model.
💻 Code
import allkitpy
vector = allkitpy.ai.embedding(
"Artificial intelligence"
)
print(vector)
📤 Output
[
0.023,
0.154,
0.762,
...
]
Using a specific embedding model
💻 Code
import allkitpy
result = allkitpy.ai.embedding(
"Machine learning",
model="embedding-model"
)
print(result)
📤 Output
Embedding vector
👁️ vision()
Analyze images using an AI vision model. The function accepts an image and optional instructions.
💻 Code
import allkitpy
result = allkitpy.ai.vision(
"photo.png",
"Describe this image"
)
print(result)
📤 Output
The image contains
a mountain landscape.
Ask questions about images
💻 Code
answer = allkitpy.ai.vision(
"car.jpg",
"What color is the car?"
)
print(answer)
📤 Output
The car is red.
🎨 image()
Generate images from text descriptions.
💻 Code
import allkitpy
result = allkitpy.ai.image(
"A futuristic city at night"
)
print(result)
📤 Output
Generated image
Image generation options
💻 Code
image = allkitpy.ai.image(
"Robot assistant",
size="1024x1024",
quality="high"
)
print(image)
📤 Output
Generated image URL
🔊 speech()
Convert text into AI-generated speech.
💻 Code
import allkitpy
audio = allkitpy.ai.speech(
"Welcome to AllkitPy"
)
print(audio)
📤 Output
Generated audio file
Using voice options
💻 Code
audio = allkitpy.ai.speech(
"Hello world",
voice="default",
format="mp3"
)
print(audio)
📤 Output
hello.mp3
🛡️ moderation()
Analyze text content for safety, policy, or moderation requirements using an AI provider.
💻 Code
import allkitpy
result = allkitpy.ai.moderation(
"Hello world"
)
print(result)
📤 Output
{
"safe": true
}
Using moderation settings
💻 Code
result = allkitpy.ai.moderation(
"Text to check",
model="moderation-model"
)
print(result)
📤 Output
Moderation result
🌊 stream()
Receive AI responses progressively as they are generated.
💻 Code
import allkitpy
response = allkitpy.ai.stream(
"Write a story"
)
for chunk in response:
print(chunk)
📤 Output
Once
upon
a
time
...
🔌 Providers
The AI module supports multiple providers through a registry system. Providers can be registered and loaded dynamically.
- Register custom providers
- Switch providers easily
- Use a common AI interface
- Support multiple AI services
➕ register()
Add an AI provider to the registry.
💻 Code
from allkitpy.ai.registry import register
class MyProvider:
pass
register(
"custom",
MyProvider
)
📤 Output
Provider registered
🔎 get()
Retrieve a registered provider class.
💻 Code
from allkitpy.ai.registry import get
provider = get(
"custom"
)
print(provider)
📤 Output
<class MyProvider>
📋 names()
Return all registered provider names.
💻 Code
from allkitpy.ai.registry import names
print(
names()
)
📤 Output
[
"openai",
"custom"
]
🤖 AIClient
The AIClient manages the connection between AllkitPy and the configured AI provider. It loads the provider from the registry and creates a provider instance automatically.
💻 Code
from allkitpy.ai.client import AIClient
client = AIClient()
provider = client.provider()
print(provider)
📤 Output
Configured AI Provider
⚠️ Exceptions
The AI module provides custom exceptions for easier error handling.
-
AIError
Base AI exception. -
ProviderNotConfigured
Raised when no provider is configured. -
ProviderNotSupported
Raised when a provider is not registered. -
APIKeyMissing
Raised when an API key is required.
⚠️ AIError
Base exception class for all AI-related errors.
💻 Code
from allkitpy.ai.exceptions import AIError
try:
raise AIError(
"AI error"
)
except AIError as error:
print(error)
📤 Output
AI error
🛠️ Utilities
The AI module includes helper utilities for validating inputs and estimating tokens.
- validate_prompt()
- validate_model()
- token_estimate()
✅ validate_prompt()
Check whether a prompt contains valid text.
💻 Code
from allkitpy.ai import AI
AI.validate_prompt(
"Hello AI"
)
📤 Output
Valid prompt
🧩 validate_model()
Validate that a model name is provided.
💻 Code
from allkitpy.ai import AI
AI.validate_model(
"gpt-model"
)
📤 Output
Valid model
🔢 token_estimate()
Estimate approximate token usage from text length.
💻 Code
from allkitpy.ai import AI
tokens = AI.token_estimate(
"Hello world"
)
print(tokens)
📤 Output
2
🚀 Complete Workflow
💻 Code
import allkitpy
allkitpy.ai.configure(
provider="openai",
api_key="KEY"
)
response = allkitpy.ai.chat(
"Create a Python example"
)
print(response)
📤 Output
AI generated response
📘 AI Module Summary
The AllkitPy AI module provides a simple and unified interface for modern AI workflows. It supports conversations, embeddings, vision, image generation, speech, moderation, streaming, and custom provider integration.
- 💬 Chat
- 🧠 Embeddings
- 👁️ Vision
- 🎨 Image Generation
- 🔊 Speech
- 🛡️ Moderation
- 🌊 Streaming
- 🔌 Custom Providers