OptimAI is a powerful Python module designed to optimize your code by analyzing its performance and providing actionable suggestions. It leverages a large language model (LLM) to give you detailed insights and recommendations based on the profiling data collected during the execution of your code.
Project description
OptimAI
OptimAI is a powerful Python module designed to optimize your code by analyzing its performance and providing actionable suggestions. It leverages a large language model (LLM) to give you detailed insights and recommendations based on the profiling data collected during the execution of your code. This module supports various kinds of profilers from the perfwatch package.
Features
- Custom decorators to optimize functions with ease.
- Integration with perfwatch for performance profiling.
- Capture and analyze stdout, function execution time, network usage, function calls, CPU/GPU usage, etc using perfwatch.
- Seamless integration with various LLMs for code optimization suggestions.
- Support for OpenAI, Google Gemini, and Hugging Face inference.
Installation
You can install OptimAI using pip:
pip install optimizeai
Setup
To use OptimAI, you need to configure it with your preferred LLM provider and API key. Supported LLM providers include Google (Gemini models), OpenAI, and Hugging Face.
-
Select the LLM Provider:
- For Google Gemini models:
llm = "google"
- For OpenAI models:
llm = "openai"
- For Hugging Face inference:
llm = "huggingface"
- For Google Gemini models:
-
Choose the Model:
- Example:
model = "gpt-4"
,model = "gemini-1.5-flash"
, or any other model specific to the chosen LLM provider.
- Example:
-
Set the API Key:
- Use the corresponding API key for the selected LLM provider.
Sample Code
Here's a basic example demonstrating how to use OptimAI to optimize a function:
from optimizeai.decorators.optimize import optimize
from optimizeai.config import Config
from dotenv import load_dotenv
import time
import os
# Load environment variables
load_dotenv()
llm = os.getenv("LLM")
key = os.getenv("API_KEY")
model = os.getenv("MODEL")
# Configure LLM
llm_config = Config(llm=llm, model=model, key=key, mode="online")
perfwatch_params = ["line", "cpu", "time"]
# Define a test function to be optimized
@optimize(config=llm_config, profiler_types=perfwatch_params)
def test():
for _ in range(10):
time.sleep(0.1)
print("Hello World!")
pass
if __name__ == "__main__":
test()
Setting Environment Variables
You can set the environment variables (LLM
, API_KEY
, MODEL
) in a .env
file for ease of use:
LLM=google
API_KEY=your_google_api_key
MODEL=gemini-1.5-flash
Upcoming Features
- Ollama Support: Integrate with the Ollama platform for enhanced LLM capabilities.
- Optimized Prompts: Use domain-specific prompts (dspy) to provide better optimization suggestions.
- Improved Context for Code Optimization: Enhance the context provided to the LLM for more accurate and relevant optimization recommendations.
Contributing
We welcome contributions to OptimAI! If you have an idea for a new feature or have found a bug, please open an issue on GitHub. If you'd like to contribute code, please fork the repository and submit a pull request.
Steps to Contribute
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -m 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Open a pull request.
License
OptimAI is licensed under the MIT License. See the LICENSE file for more details.
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 Distribution
Hashes for optimizeai-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95b643c537ed8999a6a79f865d46e3bec860906983bfc88a0682bcb2a008584d |
|
MD5 | da46a28b06a1cfd47f0a1f5d1090a0c8 |
|
BLAKE2b-256 | cebb8efd0f4bcfcb3c67030649cdfb5ccd0c4965978da505205ecc843c9011f9 |